views:

162

answers:

3

Are integers in C assumed to be handled by a hardware spec or handled in software?

By integer, I am referring to the primitive "int"

The underlying idea being that if integers in C are not hardware dependent would it be a violation of standard to have gcc implement different integer handlers. This way you could have your traditional 32 bit int handler, and then you could also have handlers for 256 bit integers, or maybe even dynamic sized ints.

I do know about long and long long, but I'm not sure if those are independent of that hardware provided, and I'd like to just specify the size/type of "int" handler to use when building a toolchain.

I also understand the dangers of doing this (because building building 256 bit integers with a 32 bit integer compiler would be bad!), but for those bits of code that require something like a gmp library, I think it would make code readability much better. Compatibility would be the same but the dependency would be on the compiler instead of the code itself.

Crazy idea, I know... but back to the original question:

Are integers in C assumed to be handled by a hardware spec or handled in software?

+5  A: 

The standard says

A "plain" int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

And the description of <limits.h> constraints INT_MIN to be at most -32767 and INT_MAX to be at least 32767. So if your hardware has a word of 12 bits, you have to use two words for an int.

AProgrammer
Essentially, in kernel space, the EE is the hardware. In user space, the kernel is the EE. Thus, the int must be the natural size of the hardware do to a "EE chain" of sorts.
Crazy Chenz
I've seen a C compiler for an 8 bit microcontroller use 8 bit ints. Once you're in that space and likely already doing things like disallowing recursion, the "natural size" characteristic of an int is more important than the minimum size limitation.
Captain Segfault
When you come to non conformant implementation, there are lot of things. I've also seen 6 bits char for instance. 8 bits ints in fact seems stranger that 6 bits bytes as there is an already good choice for 8 bits data (char), and having 8 bits ints means either twosted relationship between char, short, int and long, or only long as a type with more than 8 bits.
AProgrammer
+2  A: 

Yes, int is going to be handled in the native size. So sizeof(int) might give you a different value depending on what system you compile and run on. All the math for an int is going to be handled by the CPU's native instructions--much faster than doing it in software. If you need an int256, you will need to write that yourself. And I'm sure there are arbitrary-sized integer libraries out there.

Kip
+1  A: 

A C implementation which implemented 256 bit ints would be perfectly compliant with the C standard (but note that it has to make long int and long long int at least that long too).

However code produced by such a compiler would generally not be link-compatible with code produced by other compilers on the same platform, which is one reason it's not done in practice.

caf
Well, I see it as only being used in a custom toolchain or cross compiler type of environment anyway. Therefore you'd have to have everything linked, built with the same compiler specs anyway.
Crazy Chenz