Could you please not top-post? It makes it very difficult to follow the
order of the thread. Remember that some people see these emails in
isolation, rather than threaded in an email client. I've re-ordered the
mail below.
On 10/08/2012 10:35, Göran Steen wrote:
-----Original Message----- From: David Brown
[mailto:david@xxxxxxxxxxxxxxx] Sent: den 10 augusti 2012 10:12 To:
Göran Steen Cc: gcc-help@xxxxxxxxxxx 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
> Thank you for your response.
>
> Sorry, this will not help. I want to test the snippets, not change
> them. Besides, I don't know if the compiler that will be used for the
> 16-bit machine will support any of the types you suggest, so I only
> want to use standard types.
>
> BR /göran
If you need to test the snippets on different targets, then you need to
have compilers and targets available. You still haven't said which
target(s) you are using, but unless it is one like the M68k then there
are no compiler switches to help you.
Note that there is no point in testing anything until you have done what
you can to make sure the code is correctly written. Otherwise your
tests may work on some targets by coincidence, but not on others, or may
be sub-optimal on some targets.
Any C99 compiler - and almost all pre-C99 compilers - will have
<stdint.h>. I don't know of any compiler less than 15 years old that
doesn't come with a <stdint.h>, and many people using such compilers
have written their own <stdint.h>.
The int_fast16_t and int_fast32_t types (and the unsigned versions) are
mandatory in <stdint.h>, so you can take it for granted that /all/
compilers support them. This is unlike the fixed-size types (like
int16_t) that will be defined if and only if the target supports types
of exactly that size (some architectures don't support the smaller types).
mvh.,
David