Re: 16-bit int

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10/08/2012 09:15, Göran Steen wrote:
Thank you for your response.

I want to make sure that my code snippets, if they are compiled and
run on a machine with 16-bit int, still works. I don't have access to
such a machine, so I want to compile and run them with 16-bit int on
my machine that defaults to 32-bit int. Especially the intermediate
results' size are interesting. What happens if they are truncated to
16-bit?


The answer here is simple - #include <stdint.h>, and use types like int_fast16_t. If intermediary results might need 32 bits, cast to int_fast32_t as needed. On a target with 32-bit ints, both these types will normally be 32-bit (though int_fast16_t could theoretically be 16-bit), and the cast will do nothing. On a target with 16-bit ints, you will get 16-bit and 32-bit ints respectively.

This will give you optimal code for all sizes of target ints, while also being correct on each target.

mvh.,

David


There will not be any problem with library compatibility.

BR /göran

-----Original Message----- From: David Brown
[mailto:david@xxxxxxxxxxxxxxx] Sent: den 9 augusti 2012 15:37 To:
Göran Steen Cc: gcc-help@xxxxxxxxxxx Subject: Re: 16-bit int

On 09/08/2012 13:26, Göran Steen wrote:
Hi!

I use gcc version 4.3.4, where int is 32-bit variables. Is it
possible to setup compiler to compile int as 16-bit variables?

With best regards / Med vänlig hälsning / Mit freundlichen Grüßen
/ Saudações

Göran Steen


The size of an int depends on the target - gcc supports dozens of
targets.  Most have 32-bit ints, but some have 16-bit ints and at
least one has a compile-time option to support 8-bit ints (though
that goes against C standards, and is deprecated on current builds).
There are probably also targets with 64-bit ints.

So step one in asking for help here is to tell us your target.

Step two is to tell us what you are hoping to achieve.  Almost
certainly, there is no way to change the int size - and even if there
happens to be a command-line switch for the given target, it is
probably not a good idea (you'll get in a horrible mess with library
compatibility, for example).  And even if it is possible, it is
highly unlikely to be advantageous.  Tell us what you really want to
achieve here, and people can give you advice towards that.

mvh.,

David




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux