是否有检测硬件位宽的标准方法?

发布于 2024-09-11 07:04:07 字数 1111 浏览 5 评论 0原文

int 类型的变量据称是“长度为一个机器类型的字” 但在嵌入式系统中,8位微的C编译器使用16位的int!(8位用于无符号字符)然后对于更多位,int表现正常: 在 16 位 micros int 中也是 16 位,在 32 位 micros int 中也是 32 位,等等。

那么,是否有一个标准方法来测试它,例如 BITSIZEOF( int ) ?

就像“sizeof”适用于字节但适用于位。

这是我的第一个想法

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

        bitwidth++;

    }while(c<<=1);

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

,但是它需要 c 作为 int,并且在 8 位编译器中很常见使用 int 作为 16 位,所以它给了我 16 作为结果,似乎没有使用“int”作为“寄存器宽度”的标准,(或者不被尊重)

为什么我想检测它?假设我需要许多需要少于 256 个值的变量,因此它们可以是 8、16、32 位,但使用正确的大小(与内存和寄存器相同)将加快速度并节省内存,如果这不能在代码中决定,我必须为每个架构重写函数

编辑 阅读答案后,我发现这篇好文章

http://embeddedgurus。 com/stack-overflow/category/efficient-cc/page/4/

我将引用结论(加粗)

因此 底线是这样的。如果你想 开始高效、便携的写作 嵌入代码,第一步你 应该采取的是开始使用C99 数据类型“最少”和“快速”。如果你的 那么编译器不兼容 C99 抱怨直到它被改变——或者改变 供应商。如果您进行此更改,我 我想你会感到惊喜 代码大小的改进和 您将达到的速度。

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://embeddedgurus.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦明 2024-09-18 07:04:09
#include <limits.h>

const int bitwidth = sizeof(int) * CHAR_BIT;
#include <limits.h>

const int bitwidth = sizeof(int) * CHAR_BIT;
烟火散人牵绊 2024-09-18 07:04:09

当编译器运行您的代码时,您正在编译的 ISA 已经为编译器所知,所以最好的办法是在编译时检测它。根据您的环境,您可以使用从 autoconf/automake 样式到较低级别的 #ifdef 的所有内容来将代码调整到它将运行的特定体系结构。

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.

如痴如狂 2024-09-18 07:04:09

我不太明白你所说的“没有使用“int”作为“寄存器宽度”的标准。在原始的 C 语言规范(C89/90)中,类型 int 隐含在当未提供显式类型时,您的 register c 相当于 register int c,这在 C89/90 中是完全标准的。另请注意,C 语言规范需要类型。 int 支持至少 -32767...+32767 范围,这意味着在任何平台上 int 都将至少有 16 个值形成位

。 .. sizeof(int) * CHAR_BIT 将为您提供 int 类型的对象表示形式中的位数,

但理论上来说,是 int< 类型的值表示形式。 /code> 不保证使用其对象表示的所有位,如果您需要确定用于值表示的位数,您可以简单地分析 INT_MININT_MAX 。 。

PS 看看你的问题的标题,我怀疑你真正需要的只是 CHAR_BIT

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.

我最亲爱的 2024-09-18 07:04:09

unsigned charunsigned short 是否满足您的需求?为什么不使用它呢?如果没有,您应该使用编译时标志来引入适当的代码。

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.

青柠芒果 2024-09-18 07:04:09

我认为在这种情况下,您不需要知道您的架构有多少位。如果您想优化代码,请使用尽可能小的变量。

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.

遥远的绿洲 2024-09-18 07:04:08

我必须为每个架构重新编写函数

不,你不需要。使用 C99 的 stdint.h,它具有诸如 uint_fast8_t 之类的类型,该类型能够快速保存 256 个值。

然后,无论平台如何,类型都会相应更改,并且您无需更改代码中的任何内容。如果您的平台没有定义这些集合,您可以添加自己的。

比重写每个函数要好得多。

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.

难如初 2024-09-18 07:04:08

为了更直接地回答您更深层次的问题,如果您需要可跨平台移植的非常具体的存储大小,您应该使用类似 types.h stdint .h 定义了用位数指定的存储类型。

例如,uint32_t 始终为无符号 32 位,int8_t 始终为有符号 8 位。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文