开发者

Is there a standard way to detect bit width of hardware?

开发者 https://www.devze.com 2023-01-09 06:42 出处:网络
Variables of type int are allegedly \"one machine-type word in length\" but in embedded systems, C compilers for 8 bit micro use to have int of 16 bits!, (8 bits for unsigned char) then for more bits,

Variables of type int are allegedly "one machine-type word in length" but in embedded systems, C compilers for 8 bit micro use to have int of 16 bits!, (8 bits for unsigned char) then for more bits, int behave normally: in 16 bit micros int is 16 bits too, and in 32 bit micros int is 32 bits, etc..

So, is there a standar way to test it, something as BITSIZEOF( int ) ?

like "sizeof" is for bytes but for bits.

this was my first idea

    register c=1;                
    int bitwidth=0;
    do
    {

        bitwidth++;

    }while(c<<=1);

    printf("Register bit width is : %d",bitwidth);

But it takes c as int, and it's common in 8 bit compilers to use int as 16 bit, so it gives me 16 as result, It seems there is no standar for use "int" as "register width", (or it's not respected)

Why I want to detect it? suppose I need many variables that need less than 256 values, so they can be 8, 16, 32 bits, but using the right size (same as memory and registers) will speed up things and save memory, and if this can't be decided in code, I have to re-write the function for every architecture

EDIT After read the answers I found this good article

http://embedde开发者_如何学运维dgurus.com/stack-overflow/category/efficient-cc/page/4/

I will quote the conclusion (added bold)

Thus the bottom line is this. If you want to start writing efficient, portable embedded code, the first step you should take is start using the C99 data types ‘least’ and ‘fast’. If your compiler isn’t C99 compliant then complain until it is – or change vendors. If you make this change I think you’ll be pleasantly surprised at the improvements in code size and speed that you’ll achieve.


I have to re-write the function for every architecture

No you don't. Use C99's stdint.h, which has types like uint_fast8_t, which will be a type capable of holding 256 values, and quickly.

Then, no matter the platform, the types will change accordingly and you don't change anything in your code. If your platform has no set of these defined, you can add your own.

Far better than rewriting every function.


To answer your deeper question more directly, if you have a need for very specific storage sizes that are portable across platforms, you should use something like types.h stdint.h which defines storage types specified with number of bits.

For example, uint32_t is always unsigned 32 bits and int8_t is always signed 8 bits.


#include <limits.h>

const int bitwidth = sizeof(int) * CHAR_BIT;


The ISA you're compiling for is already known to the compiler when it runs over your code, so your best bet is to detect it at compile time. Depending on your environment, you could use everything from autoconf/automake style stuff to lower level #ifdef's to tune your code to the specific architecture it'll run on.


I don't exactly understand what you mean by "there is no standar for use "int" as "register width". In the original C language specification (C89/90) the type int is implied in certain contexts when no explicit type is supplied. Your register c is equivalent to register int c and that is perfectly standard in C89/90. Note also that C language specification requires type int to support at least -32767...+32767 range, meaning that on any platform int will have at least 16 value-forming bits.

As for the bit width... sizeof(int) * CHAR_BIT will give you the number of bits in the object representation of type int.

Theoretically though, the value representation of type int is not guaranteed to use all bits of its object representation. If you need to determine the number of bits used for value representation, you can simply analyze the INT_MIN and INT_MAX values.

P.S. Looking at the title of your question, I suspect that what you really need is just the CHAR_BIT value.


Does an unsigned char or unsigned short suit your needs? Why not use that? If not, you should be using compile time flags to bring in the appropriate code.


I think that in this case you don't need to know how many bits has your architecture. Just use variables as small as possible if you want to optimize your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号