Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

unsigned long versus unsigned int on ARM7

unsigned long versus unsigned int on ARM7

2005-11-09 by rtstofer

I have noticed that many folks are using 'unsigned long' for 32 bit 
entities and I got to wondering if I am doing things wrong (again!)

Looking at limits.h I come up with:

unsigned char -> 8 bits
unsigned short int -> 16 bits
unsigned int -> 32 bits
unsigned long -> 32 bits or 64 bits depending on architecture

Assuming that I care that my code is portable, what is the 
considered opinion on unsigned int versus unsigned long for 32 bit 
entities?

I followed a thread on the Internet that stated that the latest 
standard required that the rank of each type (where rank wasn't 
clearly defined in the minds of the participants) had to be greater 
than its predecessor, not greater than or equal to.  If that is 
indeed the standard, the rank of unsigned long must be greater than 
unsigned int and thus reserved for 64 bit kinds of things.

Now, I realize my stuff is never going to move to a SPARC but it 
would be nice to get it right.

I have been trying to avoid the use of macros such as ULONG just to 
be pedantic, I suppose.

So, what is everyone else doing?

Richard

RE: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-09 by Gromann, Klaus

The difference between long and int is machine dependend.

int depends from the processor type, that means int is 16 bit on a 16
bit machine and 32 bit on a 32 bit machine.
long is at least 32 bit.
short is allways 16 bit.

The best way to write portable code is to define your own datatypes.

i.e. for a 32 bit architecture

#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int

and so on. 

If you change the architecture you only have to change the
"datatypes.h"-File.


regards
Klaus
Show quoted textHide quoted text
-----Original Message-----
From: lpc2000@yahoogroups.com [mailto:lpc2000@...m] On Behalf
Of rtstofer
Sent: Wednesday, November 09, 2005 5:03 PM
To: lpc2000@yahoogroups.com
Subject: [lpc2000] unsigned long versus unsigned int on ARM7


I have noticed that many folks are using 'unsigned long' for 32 bit 
entities and I got to wondering if I am doing things wrong (again!)

Looking at limits.h I come up with:

unsigned char -> 8 bits
unsigned short int -> 16 bits
unsigned int -> 32 bits
unsigned long -> 32 bits or 64 bits depending on architecture

Assuming that I care that my code is portable, what is the 
considered opinion on unsigned int versus unsigned long for 32 bit 
entities?

I followed a thread on the Internet that stated that the latest 
standard required that the rank of each type (where rank wasn't 
clearly defined in the minds of the participants) had to be greater 
than its predecessor, not greater than or equal to.  If that is 
indeed the standard, the rank of unsigned long must be greater than 
unsigned int and thus reserved for 64 bit kinds of things.

Now, I realize my stuff is never going to move to a SPARC but it 
would be nice to get it right.

I have been trying to avoid the use of macros such as ULONG just to 
be pedantic, I suppose.

So, what is everyone else doing?

Richard









 
Yahoo! Groups Links

Re: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-09 by Jim Parziale

That's why it's a good idea to define your types based on sizes:
S8 -> signed 8-bit
U8 -> unsigned 8-bit
S16 -> signed 16-bit
U16 -> unsigned 16-bit
etc.
This way you can define these in one place, base other types (structs, ...)
and objects off of these, and throughout your code you know what you're
getting.

On 11/9/05, rtstofer <rstofer@...> wrote:
>
>  I have noticed that many folks are using 'unsigned long' for 32 bit
> entities and I got to wondering if I am doing things wrong (again!)
>
> Looking at limits.h I come up with:
>
> unsigned char -> 8 bits
> unsigned short int -> 16 bits
> unsigned int -> 32 bits
> unsigned long -> 32 bits or 64 bits depending on architecture
>
> Assuming that I care that my code is portable, what is the
> considered opinion on unsigned int versus unsigned long for 32 bit
> entities?
>
> I followed a thread on the Internet that stated that the latest
> standard required that the rank of each type (where rank wasn't
> clearly defined in the minds of the participants) had to be greater
> than its predecessor, not greater than or equal to. If that is
> indeed the standard, the rank of unsigned long must be greater than
> unsigned int and thus reserved for 64 bit kinds of things.
>
> Now, I realize my stuff is never going to move to a SPARC but it
> would be nice to get it right.
>
> I have been trying to avoid the use of macros such as ULONG just to
> be pedantic, I suppose.
>
> So, what is everyone else doing?
>
> Richard
>
>

--
----------------------------------------------------------
Jim Parziale
Email: nuncio.bitis@...
Malden, MA
----------------------------------------------------------


[Non-text portions of this message have been removed]

Re: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-09 by Richard

First of all, re: standards, the old C86/C89/ISO90 Standard will go on for 
a quite a while. Most (embedded) compilers have not moved to fully support 
C99 and do not appear to have too much pull to do so. I am not aware of 
this "latest standard" you mention but I have not tracked comp.std.c or 
some such for a while.

Second, if you want to be portable and care about the actual # of bits, you 
should always use your own typdefs such as

uint32, uint64 etc. anyway.

At 08:03 AM 11/9/2005, rtstofer wrote:

>I have noticed that many folks are using 'unsigned long' for 32 bit
>entities and I got to wondering if I am doing things wrong (again!)
>
>Looking at limits.h I come up with:
>
>unsigned char -> 8 bits
>unsigned short int -> 16 bits
>unsigned int -> 32 bits
>unsigned long -> 32 bits or 64 bits depending on architecture
>
>Assuming that I care that my code is portable, what is the
>considered opinion on unsigned int versus unsigned long for 32 bit
>entities?
>
>

// richard (This email is for mailing lists. To reach me directly, please 
use richard at imagecraft.com)

Re: unsigned long versus unsigned int on ARM7

2005-11-09 by rtstofer

--- In lpc2000@yahoogroups.com, Richard <richard-lists@i...> wrote:
>
> First of all, re: standards, the old C86/C89/ISO90 Standard will 
go on for 
> a quite a while. Most (embedded) compilers have not moved to fully 
support 
> C99 and do not appear to have too much pull to do so. I am not 
aware of 
> this "latest standard" you mention but I have not tracked 
comp.std.c or 
> some such for a while.

I believe the discussion was related to C99 as it appeared to change 
the definition from previous versions (or it did in the minds of the 
participants).  But, I hate to mention things like 'standards' 
because I haven't read them and I wouldn't understand them if I did.

> 
> Second, if you want to be portable and care about the actual # of 
bits, you 
> should always use your own typdefs such as
> 
> uint32, uint64 etc. anyway.

OK, I give in.  It is no effort to create a 'datatypes.h' and this 
is the approach I have used before.  There is certainly good reason 
to get this squared away early.

Thanks to all!

Richard

RE: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-09 by Paul Curtis

Hi,

> I have noticed that many folks are using 'unsigned long' for 32 bit 
> entities and I got to wondering if I am doing things wrong (again!)
> 
> Looking at limits.h I come up with:
> 
> unsigned char -> 8 bits
> unsigned short int -> 16 bits
> unsigned int -> 32 bits
> unsigned long -> 32 bits or 64 bits depending on architecture
> 
> Assuming that I care that my code is portable, what is the 
> considered opinion on unsigned int versus unsigned long for 32 bit 
> entities?

No difference on many 32-bit compilers.

> I followed a thread on the Internet that stated that the latest 
> standard required that the rank of each type (where rank wasn't 
> clearly defined in the minds of the participants) had to be greater 
> than its predecessor, not greater than or equal to.  If that is 
> indeed the standard, the rank of unsigned long must be greater than 
> unsigned int and thus reserved for 64 bit kinds of things.

That is wrong.  The C standard requires that the following equality and
inequalities hold:

1 = sizeof(char)

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <=
sizeof(long long)

It is quite possible to produce a standard-conforming compiler where all
types are 64 bits in size.  In fact, the Texas Instruments 320C40
compiler had a 32-bit char, short, int, and long.  Yes, it was not a
byte-oriented machine and much trouble with byte-oriented protocols
ensued.

> Now, I realize my stuff is never going to move to a SPARC but it 
> would be nice to get it right.
> 
> I have been trying to avoid the use of macros such as ULONG just to 
> be pedantic, I suppose.
> 
> So, what is everyone else doing?

Try investigating <stdint.h> if your compiler has it.

-- Paul.

RE: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-09 by Paul Curtis

Klaus,

> The difference between long and int is machine dependend.

Actually, it is compiler defined.

> int depends from the processor type, that means int is 16 bit on a 16
> bit machine and 32 bit on a 32 bit machine.

int is supposed to mirror the natural word width of the machine, yes.

> long is at least 32 bit.

Correct.

> short is allways 16 bit.

No, short is required to cover the closed interval [-32768, 32767].
That does not infer 16 bitness for short--short *can* be modeled with a
32-bit type (cf 320C40 compiler cited in a previous message).

> The best way to write portable code is to define your own datatypes.
> 
> i.e. for a 32 bit architecture
> 
> #define u8 unsigned char
> #define u16 unsigned short
> #define u32 unsigned int

Or try <stdint.h> and use uint_8 and so on...

-- Paul.

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-09 by David Hawkins

>>Second, if you want to be portable and care about the actual # of 
> 
> bits, you 
> 
>>should always use your own typdefs such as
>>
>>uint32, uint64 etc. anyway.
> 
> 
> OK, I give in.  It is no effort to create a 'datatypes.h' and this 
> is the approach I have used before.  There is certainly good reason 
> to get this squared away early.
> 
> Thanks to all!

I use <stdint.h> to get the C99 types; int32_t, uint32_t, etc.
If the compiler doesn't have a stdint.h, then write one.
There are already too many versions of code with s32, S32, INT32, ...
definitions out there, I figure go with the C99 names, even
if your compiler is not C99 compliant.

Dave

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-09 by Ake Hedman, eurosource

David Hawkins wrote:

> I use <stdint.h> to get the C99 types; int32_t, uint32_t, etc.
> If the compiler doesn't have a stdint.h, then write one.
> There are already too many versions of code with s32, S32, INT32, ...
> definitions out there, I figure go with the C99 names, even
> if your compiler is not C99 compliant.
>
> Dave


This is a *very* good suggestion. During the years I have used many 
different constructs but as you write it is probably time now to go for 
the C99 types once for all. Is there a place where they all are listed? 
I have never come across a stdint.h file yet.... ;-)
 
Cheers
/Ake

-- 
 ---
Ake Hedman (YAP - Yet Another Programmer)
eurosource, Brattbergavägen 17, 820 50 LOS, Sweden
Phone: (46) 657 413430 Cellular: (46) 73 84 84 102
Company home: http://www.eurosource.se      
Kryddor/Te/Kaffe: http://www.brattberg.com
Personal homepage: http://www.eurosource.se/akhe
Automated home: http://www.vscp.org

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-09 by David Hawkins

Ake Hedman, eurosource wrote:
> David Hawkins wrote:
> 
> 
>>I use <stdint.h> to get the C99 types; int32_t, uint32_t, etc.
>>If the compiler doesn't have a stdint.h, then write one.
>>There are already too many versions of code with s32, S32, INT32, ...
>>definitions out there, I figure go with the C99 names, even
>>if your compiler is not C99 compliant.
>>
>>Dave
> 
> 
> 
> This is a *very* good suggestion. During the years I have used many 
> different constructs but as you write it is probably time now to go for 
> the C99 types once for all. Is there a place where they all are listed? 
> I have never come across a stdint.h file yet.... ;-)
>  
> Cheers
> /Ake
> 

On my Cygwin install

$ find . -name stdint.h -print
./usr/include/mingw/stdint.h
./usr/include/stdint.h

there's a couple. Do the same on your install.

Dave

Re: unsigned long versus unsigned int on ARM7

2005-11-09 by seangra

From a standards point of view the only thing that can be guaranteed 
for the normal basic ordinal types is this:

In section 6.1.2.5 the Standard states, "There are four signed 
integer types, designated as signed char, short int, int, and long 
int." ... "In the list of signed integer types above, the range of 
values of each type is a subrange of the values of the next type in 
the list."

In other words,

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

The standards make no guarantee of how big any of those actually is, 
only that the sizes adhere to that.  Technically all basic ordinal 
types can be the same size, depending on the native type of the 
processor.  It's only by convention that most compilers limit char 
to 8-bit, short to 16-bit, and "int" is fairly universally used 
as "machine-type word in length".  The alignment is also left up to 
the discretion of the compiler.  It may declare a char to be 8-bit, 
but always align it on 32-bit boundaries, meaning that it could 
actually be used as a 32-bit number (in some cases).  printf is a 
bad example of this.  I see people all over the place always use %i 
to display a number.  Again, works great on 32-bit with 32-bit 
alignment, but when ported to a different platform things start to 
screw up.

Although it's a great idea to use C99 for "standard" size-defined 
ordinals when you need them, keep in mind that they should only be 
used when you must be sure of the size of your variable.

If you decide to write everything with uint32_t or uint16_t, keep in 
mind that it will not execute as efficiently on machines that are 
not native to that type.  I.e. if you want to use a loop counter, 
then just use an int or unsigned int, which "almost" guarantees that 
the compiler will choose the most efficient type for the machine.  
I've seen code where people insist on using uint32_t everywhere, 
which works great on 32-bit machines, but when compiled for our 
lovely 8051s the code runs like a total hog (not to mention using a 
lot more memory that necessary).  That's why I usually have 2 sets 
of defines, one for specific variable lengths, one for "minimum 
lengths".  For example, like this:

#ifdef __8051__

#define uint8  unsigned char
#define uint16 unsigned short
#define uint32 unsigned long

#define mint8  unsigned char
#define mint16 unsigned int
#define mint32 unsigned long

#elif defined (__ARM7__)

#define uint8  unsigned char
#define uint16 unsigned short
#define uint32 unsigned long

#define mint8  unsigned int
#define mint16 unsigned int
#deifne mint32 unsigned int

#elif ...

(etc)

In other words

#define mint8  <at least  8-bit in length>
#define mint16 <at least 16-bit in length>
#define mint32 <at least 32-bit in length>

#define uint8  <exactly  8-bits in length>
#define uint16 <exactly 16-bits in length>
#define uint32 <exactly 32-bits in length>

This way I can be guaranteed that if I need to use a machine-type-
efficient counter that only needs to go between 0 and 50 then I use 
an mint8.  If I need a number that must be 32-bit, I use a uint32.

-- Sean

--- In lpc2000@yahoogroups.com, "Ake Hedman, eurosource" <akhe@b...> 
wrote:
>
> David Hawkins wrote:
> 
> > I use <stdint.h> to get the C99 types; int32_t, uint32_t, etc.
> > If the compiler doesn't have a stdint.h, then write one.
> > There are already too many versions of code with s32, S32, 
INT32, ...
> > definitions out there, I figure go with the C99 names, even
> > if your compiler is not C99 compliant.
> >
> > Dave
> 
> 
> This is a *very* good suggestion. During the years I have used 
many 
> different constructs but as you write it is probably time now to 
go for 
> the C99 types once for all. Is there a place where they all are 
listed? 
Show quoted textHide quoted text
> I have never come across a stdint.h file yet.... ;-)
>  
> Cheers
> /Ake
> 
> -- 
>  ---
> Ake Hedman (YAP - Yet Another Programmer)
> eurosource, Brattbergavägen 17, 820 50 LOS, Sweden
> Phone: (46) 657 413430 Cellular: (46) 73 84 84 102
> Company home: http://www.eurosource.se      
> Kryddor/Te/Kaffe: http://www.brattberg.com
> Personal homepage: http://www.eurosource.se/akhe
> Automated home: http://www.vscp.org
>

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-09 by Ake Hedman, eurosource

David Hawkins wrote:

>
> On my Cygwin install
>
> $ find . -name stdint.h -print
> ./usr/include/mingw/stdint.h
> ./usr/include/stdint.h
>
> there's a couple. Do the same on your install.
>
> Dave

Yes I noticed them after I replied to your mail. Thanks!

Cheers
/Ake


-- 
 ---
Ake Hedman (YAP - Yet Another Programmer)
eurosource, Brattbergavägen 17, 820 50 LOS, Sweden
Phone: (46) 657 413430 Cellular: (46) 73 84 84 102
Company home: http://www.eurosource.se      
Kryddor/Te/Kaffe: http://www.brattberg.com
Personal homepage: http://www.eurosource.se/akhe
Automated home: http://www.vscp.org

Re: unsigned long versus unsigned int on ARM7

2005-11-10 by rtstofer

> That is wrong.  The C standard requires that the following 
equality and
> inequalities hold:
> 
> 1 = sizeof(char)
> 
> sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <=
> sizeof(long long)
> 

As I understood the debate, the C99 spec has changed the inequality 
to strictly <  not <= so there had to be a difference in size 
between the various types.  But this may not be correct...


From the C99 standard section 6.3.1.1:

6.3.1.1 Boolean, characters, and integers
1 Every integer type has an integer conversion rank defined as 
follows:

— No two signed integer types shall have the same rank, even if they 
have the same representation.

— The rank of a signed integer type shall be greater than the rank 
of any signed integer type with less precision.

— The rank of long long int shall be greater than the rank of long 
int, which shall be greater than the rank of int, which shall be 
greater than the rank of short int, which shall be greater than the 
rank of signed char.

— The rank of any unsigned integer type shall equal the rank of the 
corresponding signed integer type, if any.

But, I think the real answer is in section 7.18 where the standard 
deals with <stdint.h> and defines macros that represent specific 
widths.  I think I need to read this a little more carefully.

Then there is the matter of which standard applies; '99 or something 
earlier.  Oh, and whether the compiler in question even claims 
standard compliance.  Some don't...

Richard

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-10 by Richard

At 08:51 AM 11/10/2005, rtstofer wrote:


> > That is wrong.  The C standard requires that the following
>equality and
> > inequalities hold:
> >
> > 1 = sizeof(char)
> >
> > sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <=
> > sizeof(long long)
> >
>
>As I understood the debate, the C99 spec has changed the inequality
>to strictly <  not <= so there had to be a difference in size
>between the various types.  But this may not be correct...
>

Richard, I don't recall whether they use the same language for C86/C89/etc. 
or not, but my reading of your excerpt is that your interpretation is 
incorrect. Looks to me that under C99, it is still

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long 
long)

> >From the C99 standard section 6.3.1.1:
>
>6.3.1.1 Boolean, characters, and integers
>1 Every integer type has an integer conversion rank defined as
>follows:
>
>— No two signed integer types shall have the same rank, even if they
>have the same representation.
>
>— The rank of a signed integer type shall be greater than the rank
>of any signed integer type with less precision.
>
>— The rank of long long int shall be greater than the rank of long
>int, which shall be greater than the rank of int, which shall be
>greater than the rank of short int, which shall be greater than the
>rank of signed char.
>
>— The rank of any unsigned integer type shall equal the rank of the
>corresponding signed integer type, if any.
>
>But, I think the real answer is in section 7.18 where the standard
>deals with <stdint.h> and defines macros that represent specific
>widths.  I think I need to read this a little more carefully.
>
>Then there is the matter of which standard applies; '99 or something
>earlier.  Oh, and whether the compiler in question even claims
>standard compliance.  Some don't...
>
>Richard
>
>
>
>
>
>
>
>
>
>Yahoo! Groups Links
>
>
>
>

// richard (This email is for mailing lists. To reach me directly, please 
use richard at imagecraft.com)

Re: [lpc2000] Re: unsigned long versus unsigned int on ARM7

2005-11-10 by Clyde Stubbs

On Thu, Nov 10, 2005 at 04:51:15PM -0000, rtstofer wrote:
> 6.3.1.1 Boolean, characters, and integers
> 1 Every integer type has an integer conversion rank defined as 
> follows:

> \ufffd No two signed integer types shall have the same rank, even if they 
> have the same representation.
> 
> \ufffd The rank of a signed integer type shall be greater than the rank 
> of any signed integer type with less precision.

Read this carefully - what it says is that IF there are two types with
different precisions, then the one with less precision must have the
lower rank - it does NOT say that types with different rank must have
different precisions.

While the wording is different to that of C89, the meaning is the same.


Clyde

RE: [lpc2000] unsigned long versus unsigned int on ARM7

2005-11-11 by Bruce Paterson

> Texas Instruments 320C40 compiler had a 32-bit char, short, 
> int, and long.  Yes, it was not a byte-oriented machine and 
> much trouble with byte-oriented protocols ensued.

Or even less obvious the 24bit gnu based C compiler for the Motorola
series of DSP's (5600x), char, short, int and long were all 24 bits. To
make things work properly using the same code on a test PC we had to
ensure int-like variables were forced to be 32bits (rather than 16bits),
so it was lucky it could be all done in one file where my generic types
were defined. 
A situation like this does mean you should allow for a type that is the
natural int of the machine as well as the specific types like int32_t.
In most cases you probably don't want a 32bit counter on the PC to be
forced into a double word 48bit on the DSP; for most purposes a 24bit
counter will do. I tend towards some global use types like "COUNTER" and
"INDEX" that I set appropriately in the global types file, as well as
min_no_of_bits types like INT32_T where you really do mean you need
32bits (or more).

Cheers,
Bruce

Re: unsigned long versus unsigned int on ARM7

2005-11-11 by rtstofer

--- In lpc2000@yahoogroups.com, "Bruce Paterson" 
<bruce.paterson@b...> wrote:
>
> > Texas Instruments 320C40 compiler had a 32-bit char, short, 
> > int, and long.  Yes, it was not a byte-oriented machine and 
> > much trouble with byte-oriented protocols ensued.
> 
> Or even less obvious the 24bit gnu based C compiler for the 
Motorola
> series of DSP's (5600x), char, short, int and long were all 24 
bits. To
> make things work properly using the same code on a test PC we had 
to
> ensure int-like variables were forced to be 32bits (rather than 
16bits),
> so it was lucky it could be all done in one file where my generic 
types
> were defined. 
> A situation like this does mean you should allow for a type that 
is the
> natural int of the machine as well as the specific types like 
int32_t.
> In most cases you probably don't want a 32bit counter on the PC to 
be
> forced into a double word 48bit on the DSP; for most purposes a 
24bit
> counter will do. I tend towards some global use types 
like "COUNTER" and
> "INDEX" that I set appropriately in the global types file, as well 
as
> min_no_of_bits types like INT32_T where you really do mean you need
> 32bits (or more).
> 
> Cheers,
> Bruce
>

Yes, as I get further into stdint.h I think is will clear up.  And, 
what about portability?  On these embedded things portability of the 
hardware specific code is not going to happen anyway.

It's just that my experience has been limited to 8 bit micros for 
far too long.

Richard

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.