-----Original Message----- From: David Brown
[mailto:david@westcontrol.com] Sent: den 10 augusti 2012 10:12 To:
Göran Steen Cc: gcc-help@gcc.gnu.org Subject: Re: 16-bit int
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