How is type resetting done at the hardware level?

In C, sometimes we use a data type for arithmetic operations, for example:

int a = (int)b + (int)c; 

What does a CPU-like type do? What instructions does the listing in (x86) get? Does type harm CPU pipelines?

+7
c cpu
source share
3 answers

It depends on the specific types, obviously. Some casts are just a matter of interpretation, so no instructions are needed - for example, unsigned int - int .

Others may require an β€œextension” of data to propagate the sign bit to higher order bits, such as signed char to int .

The X86 manual used for this is cbw or cwde . http://www.fermimn.gov.it/linux/quarta/x86/cbw.htm

ex: signed char 0b10000000 should become int 0b1111111110000000 (for a 16-bit int)

+6
source share

If b, say, float; the compiler will generate code to call a library routine of type convert_float_to_int() . This is usually not something that is done directly in the hardware. It can be aligned if the procedure is quite short.

+2
source share

Such an architecture and data type. Static casts like this can be a move between register and register, no-op, they can set or clear CPU flags, logically mask bytes, etc. If, for example, b is a float, then the temporary will have to be filled with what gives the mechanism for converting an integer number of CPUs with a floating point. If it is a char , then it will be two additions (possibly an extended character). If it is unsigned char , then the temporary one will contain the value b in its lowest value and zeros in more significant bytes. In fact, the only way to tell is through the generated code (in gcc this is the -S option). Floating point movement can certainly cause a pipe stop or bubble. These days you have a complication that it can even appear in the GPU.

+2
source share

All Articles