Re: Delay Routine: Fully-portable C89 if possible
- From: Hans-Bernhard Bröker <HBBroeker@xxxxxxxxxxx>
- Date: Wed, 10 Oct 2007 20:53:52 +0200
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.
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: Martin Wells
- Re: Delay Routine: Fully-portable C89 if possible
- From: David Brown
- 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
- Delay Routine: Fully-portable C89 if possible
- Prev by Date: Maximum Memory Usage?
- 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
|