Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > FAQ: int or long int?

Reply
Thread Tools

FAQ: int or long int?

 
 
pozz
Guest
Posts: n/a
 
      04-01-2011
I'm interested to write portable code on embedded 16- or 32-bits
platform.
So I'm wondering which integer type to use between int and long int
when I need to manage values greater than +32767 (but lower than
+2147483647)

The Question 1.1 of comp.lang.c FAQ gives a precise answer: If you
might need large values (above 32,767 or below -32,767), use long.

I imagine this is because int type could be 16-bit on some platforms,
but long must be at least 32-bit.

At the contrary, on some 32-bit platforms, int could be 32-bit and
long int could be 64-bit. In this situation, I will use 64-bit
variables to store values that could be stored just in int variables.
Apart the greater storage space in memory, also the computation will
be more complex and slower.

So I'm wondering if it could be better to use new C99 types, such as
int32_t. In this case, on 32-bit platform with 64-bit long, I'll use
the simple int for my variable...
 
Reply With Quote
 
 
 
 
pozz
Guest
Posts: n/a
 
      04-01-2011
On 1 Apr, 11:59, China Blue Meanies <(E-Mail Removed)> wrote:
> *pozz <(E-Mail Removed)> wrote:
> > So I'm wondering if it could be better to use new C99 types, such as
> > int32_t. In this case, on 32-bit platform with 64-bit long, I'll use
> > the simple int for my variable...

>
> That's what they're there for--to explicitly state the sizes when you need to
> explicitly set the sizes. And if your platform doesn't include stdint.h, make
> your own.


At the contrary, if I use
int16_t x;
on a 32-bit platform, I'll use a non optimized type...

In both cases (long int or int16_t) I can have some drawbacks.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-01-2011
pozz <(E-Mail Removed)> writes:

> On 1 Apr, 11:59, China Blue Meanies <(E-Mail Removed)> wrote:
>> *pozz <(E-Mail Removed)> wrote:
>> > So I'm wondering if it could be better to use new C99 types, such as
>> > int32_t. In this case, on 32-bit platform with 64-bit long, I'll use
>> > the simple int for my variable...

>>
>> That's what they're there for--to explicitly state the sizes when you need to
>> explicitly set the sizes. And if your platform doesn't include stdint.h, make
>> your own.

>
> At the contrary, if I use
> int16_t x;
> on a 32-bit platform, I'll use a non optimized type...
>
> In both cases (long int or int16_t) I can have some drawbacks.


That is what the int_fastN_t types are for (and unlike the intN_t types
they are required by C99).

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-01-2011
China Blue Meanies <(E-Mail Removed)> writes:
<snip>
> For build tools like make it's pretty easy to create the header file and then
> include it.
>
> program: int.h program.c
> ...
>
> int.h:
> echo >int.c '#include <stdio.h>'
> echo >>int.c '#include <limits.h>'
> echo >>int.c 'int main(int n,char **p) {'
> echo >>int.c 'char *i8 = 0,*i16 = 0,*i32 = 0,*i64 = 0;'
> echo >>int.c '#ifdef SCHAR_MAX'
> echo >>int.c 'if (SCHAR_MAX==0x7F) *i8 = "char";'


I think you mean i8 rather than *i8 here (and in 19 similar cases
below).

<snip>
> echo >>int.c 'printf("#define int_h"; puts("");'


missing )

<snip>
--
Ben.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      04-01-2011
pozz <(E-Mail Removed)> writes:

> I'm interested to write portable code on embedded 16- or 32-bits
> platform.
> So I'm wondering which integer type to use between int and long int
> when I need to manage values greater than +32767 (but lower than
> +2147483647)


int_least32_t if you want to optimize for space.
int_fast32_t if you want to optimize for time.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-01-2011
"christian.bau" <(E-Mail Removed)> writes:

> On Apr 1, 10:28*am, pozz <(E-Mail Removed)> wrote:
>
>> At the contrary, on some 32-bit platforms, int could be 32-bit and
>> long int could be 64-bit. In this situation, I will use 64-bit
>> variables to store values that could be stored just in int variables.
>> Apart the greater storage space in memory, also the computation will
>> be more complex and slower.

>
> Name a platform where long = 64 bit, int = 32 bit, and computations
> using long will be more complex and slower than the same computation
> using int. In my experience it is the opposite.


At first glance, it seems to be the case on my Linux system:

$ cat time.c
#include <stdio.h>
#include <limits.h>

#define STR(x) XSTR(x)
#define XSTR(x) #x

ITYPE f(ITYPE x, ITYPE y)
{
return (x * y) / (x + y);
}

int main(void)
{
printf("%s is %zu bits\n", STR(ITYPE), CHAR_BIT * sizeof(ITYPE));
ITYPE r = 1;
for (ITYPE i = 0; i < 10000; i++)
for (ITYPE j = 1; j < 10000; j++)
r = (r << 3) | f(i, j);
printf("%d", (int)r);
return 0;
}
$ gcc -DITYPE=int -std=c99 -pedantic -O3 -o time time.c
$ time ./time
int is 32 bits
-65
real 0m0.399s
user 0m0.400s
sys 0m0.000s
$ gcc -DITYPE=long -std=c99 -pedantic -O3 -o time time.c
$ time ./time
long is 64 bits
-65
real 0m1.306s
user 0m1.310s
sys 0m0.000s

I am very wary of crude timing like this, but it's a start.

--
Ben.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-02-2011
On 4/1/2011 12:01 PM, christian.bau wrote:
> On Apr 1, 10:28 am, pozz<(E-Mail Removed)> wrote:
>
>> At the contrary, on some 32-bit platforms, int could be 32-bit and
>> long int could be 64-bit. In this situation, I will use 64-bit
>> variables to store values that could be stored just in int variables.
>> Apart the greater storage space in memory, also the computation will
>> be more complex and slower.

>
> Name a platform where long = 64 bit, int = 32 bit, and computations
> using long will be more complex and slower than the same computation
> using int. In my experience it is the opposite.


"All."

Argument: Memory access dominates all other operations in a
system; that is why the system has two or three or maybe even more
levels of very complex, very expensive cache. Those caches can hold
only half as many 64-bit objects as 32-bit objects, roughly speaking,
so if you use a 64-bit datum where a 32- or 16- or 8-bit datum would
have sufficed, you increase the "pressure" on the caches and diminish
their effectiveness. This can slow the system down dramatically: A
2GHz CPU that generates a fresh memory reference every 0.5 ns will
s--t--a--l--l for five hundred to a thousand cycles on each cache
miss. If you reduce the cache hit rate from 99% to 98% you *double*
the likelihood of such stalls; roughly speaking, you cut the rate of
"useful work" by half.

See the highly instructive lecture video by Cliff Click at
<http://www.infoq.com/presentations/click-crash-course-modern-hardware>.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-02-2011
On 04/ 2/11 01:52 PM, Eric Sosman wrote:
> On 4/1/2011 12:01 PM, christian.bau wrote:
>> On Apr 1, 10:28 am, pozz<(E-Mail Removed)> wrote:
>>
>>> At the contrary, on some 32-bit platforms, int could be 32-bit and
>>> long int could be 64-bit. In this situation, I will use 64-bit
>>> variables to store values that could be stored just in int variables.
>>> Apart the greater storage space in memory, also the computation will
>>> be more complex and slower.

>>
>> Name a platform where long = 64 bit, int = 32 bit, and computations
>> using long will be more complex and slower than the same computation
>> using int. In my experience it is the opposite.

>
> "All."


Under some conditions. If the data set is small enough there should be
little difference.

The real question probably should have been "Name a 32 bit platform
where int is 32 bit and long is 64bit".

> Argument: Memory access dominates all other operations in a
> system; that is why the system has two or three or maybe even more
> levels of very complex, very expensive cache. Those caches can hold
> only half as many 64-bit objects as 32-bit objects, roughly speaking,
> so if you use a 64-bit datum where a 32- or 16- or 8-bit datum would
> have sufficed, you increase the "pressure" on the caches and diminish
> their effectiveness. This can slow the system down dramatically: A
> 2GHz CPU that generates a fresh memory reference every 0.5 ns will
> s--t--a--l--l for five hundred to a thousand cycles on each cache
> miss. If you reduce the cache hit rate from 99% to 98% you *double*
> the likelihood of such stalls; roughly speaking, you cut the rate of
> "useful work" by half.


On my Solaris system, the code Ben posted else-thread runs slightly
faster in 64 bit mode with long rather than int.

--
Ian Collins
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-02-2011
On 4/1/2011 9:26 PM, Ian Collins wrote:
> [...]
> The real question probably should have been "Name a 32 bit platform
> where int is 32 bit and long is 64bit".


"Mister Chips."

(In other words, define "32 bit platform" -- without inducing
a tautology.)

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-02-2011
Ian Collins <(E-Mail Removed)> writes:

> On 04/ 2/11 01:52 PM, Eric Sosman wrote:
>> On 4/1/2011 12:01 PM, christian.bau wrote:
>>> On Apr 1, 10:28 am, pozz<(E-Mail Removed)> wrote:
>>>
>>>> At the contrary, on some 32-bit platforms, int could be 32-bit and
>>>> long int could be 64-bit. In this situation, I will use 64-bit
>>>> variables to store values that could be stored just in int variables.
>>>> Apart the greater storage space in memory, also the computation will
>>>> be more complex and slower.
>>>
>>> Name a platform where long = 64 bit, int = 32 bit, and computations
>>> using long will be more complex and slower than the same computation
>>> using int. In my experience it is the opposite.

>>
>> "All."

>
> Under some conditions. If the data set is small enough there should
> be little difference.


I don't understand the meaning of "should" here. If the data set is
small, the memory effects that Eric is talking about go away but I don't
see why that "should" mean there will be little difference. Did you
just mean that there may be little difference?

<snip reasoning about memory/cache speeds>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Promoting unsigned long int to long int pereges C Programming 112 07-28-2008 05:00 AM
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments