by tremon · 2mo ago · 0 comments
This is absolutely not true, unless you mean to say that processors should somehow support composite (aka C struct) data types as an instruction primitive. Processor operations have to be strongly typed, by definition. For example, these are the data types supported by operations in the modern x86 instruction set (ignoring vector extensions):
- signed and unsigned integers of 8, 16, 32 and 64 bits
- floating-point decimals of 32, 64 and 80 bits (and 128 via sse)
- nul-terminated byte strings
> For example: casting a `float` to an `int` has a very specific definition in C, and that definition involves altering the pattern of bits
I don't understand this example. Casting a float to an int also has a very specific definition in IEEE-754 and is pretty much universally implemented as a hardware instruction. It has been in the x86 family since its inception: https://www.felixcloutier.com/x86/fisttp
Individual instructions assume the data they operate on is of a particular type, but it doesn't differentiate data types in memory. Here's an example where I forced the C compiler to treat the bit pattern of two floats as integers, then add those values as integers. The result is, of course, absolutely meaningless.
float dx = 1.0;
float dy = 1.0;
int *pix = &dx;
int *piy = &dy;
int isum = *pix + *piy;
float *pdsum = &isum;
printf("%d\n", isum);
printf("%f\n", *pdsum);
That's just how processors work, right? Apparently it doesn't have to be that way. From my understanding of the iAPX 432, attempts were made to encode object types in hardware.The bits are untyped, the choice of operation decides how the bits are interpreted. Nothing enforces the type of that result, you can always interpret it as something else. It may not be meaningful. Or it may be, like a fast inverse square root.
Strong typing means that each value has an intrinsic type, and there is no reinterpreting it. What CPUs do is not that, or rather the only types are "_n_-bits" (_n_ a power of 2).
btw a bit of nitpick: to be fair basically no one uses x87 anymore, it's https://www.felixcloutier.com/x86/cvttss2si and friends but yes :)
Otherwise, no.