Re: Delay Routine: Fully-portable C89 if possible
- From: David Brown <david@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 11 Oct 2007 12:47:12 +0200
Hans-Bernhard Bröker wrote:
Martin Wells wrote:John:
For example, in C99 I could define a 32 bit hardware register like
this:
#include <stdint.h>
#define PORT (*(volatile uint32_t *)(0x1FFF0000))
This will only work on implementations that actually have an unsigned
integer type that has exactly 32 value representational bits.
I suspect you'll find this hard to believe, but: that's actually a good thing. A platform that has no such type can't run that code as designed, so there's no point for it to compile on that platform. It should fail.
On other implementations, it won't compile. Best to use
uint_least32_t (at least 32-Bits).
Not in this particular case. If the platform doesn't have 32-bit integers, it can't have 32-bit hardware registers, so it shouldn't compile this code.
It's perhaps more accurate to talk about the platform "supporting" 32-bit integers - it's not clear whether you are trying to say that the cpu in question must be at least 32 bits (which is of course not the case - your code would work perfectly well on an 8-bit cpu, which regularly have 16-bit hardware registers and perhaps also 32-bit hardware registers) or just that it can work with 32-bit data (as distinct from, say, a 36-bit machine).
.unsigned long is guaranteed to be atleast 32-Bit, so that's fine. If
you wanted to go easy on space consumption, you could still use C89
and use macros:
#if VALUE_BITS(char unsigned) >= 32
typedef char unsigned uint_least32_t;
It would be rather nice if a VALUE_BITS like that were a C89 standard functionality, wouldn't it? Well, sorry, it's not. And not only that: it can't even be implemented in C89. There's only CHAR_BIT (in <limits.h>), and sizeof() --- but the latter doesn't work in preprocessor instructions.
There are reasons <stdint.h> was made part of the standard. One of them is that it's quite hard to implement its functionality unless you're the compiler implementor.
- Follow-Ups:
- Re: Delay Routine: Fully-portable C89 if possible
- From: Hans-Bernhard Bröker
- Re: Delay Routine: Fully-portable C89 if possible
- References:
- Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: David Brown
- Re: Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: John Devereux
- Re: Delay Routine: Fully-portable C89 if possible
- From: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: Hans-Bernhard Bröker
- Delay Routine: Fully-portable C89 if possible
- Prev by Date: Re: Delay Routine: Fully-portable C89 if possible
- Next by Date: Re: Delay Routine: Fully-portable C89 if possible
- Previous by thread: Re: Delay Routine: Fully-portable C89 if possible
- Next by thread: Re: Delay Routine: Fully-portable C89 if possible
- Index(es):
Relevant Pages
|