|
int i;
float f;
i = (int) f; // Assign a float representation to an int type variable
f = (float) i; // Assign an int representation to an float type variable
|
The assignment statement must first convert one representation into another representation before doing the assignment !
int i;
float f;
i = (int) f;
|
in the above code fragment must perform the following steps:
(1) convert the float (IEEE 754) representation stored in f into
the int (2's complement) representation for the same value
(2) Store the int (2's complement) representation in the variable i
|
int i;
float f;
i = (int) f;
|
in the above code fragment must perform the following steps:
(1) convert the int (2's complement) representation stored in i into
the float (IEEE 754) representation for the same value
(2) Store the float (IEEE 754) representation in the variable f
|
binary number system <---> 2's complement code
|
there is a conversion procedure to convert between:
2's complement representation <---> IEEE 754 (float) representation
(integer) (float)
|
But the int <--> float conversion procedure is much more complex.
The conversion algorithms use bit manipulation operations that you have not had before.
So I will not give the complete algorithms, but describe the algorithms only at a high level (mainly to sastify students' curiocity)....
|
1. Convert the int representation into a sign and a positive binary number
2. Convert the positive binary number to a fixed point representation
where the integral part = 1.xxxxx
(This step uses shift operations - you shift the decimal point to the left
until you find the most significant 1 bit in the binary number)
Let M be the mantissa with the leading 1 bit omitted
Let E be the exponent of the fixed point representation
3. Express the exponent E in excess 127 code
4. Assemble:
Sign Exponent Mantissa into a IEEE 754 respresentation
|
Example:
Let's convert the value -5 from the int representation to the float representation.
Let me show you the representation of -5 first:
-5 in the int representation is: 11111111111111111111111111111011
-5 in the float representation is: 11000000101000000000000000000000 (See: click here)
The question is: how do you go from
11111111111111111111111111111011 --> 11000000101000000000000000000000
|
Because you have not had shift operation before, I won't discuss the algorithm in code form....
int radius;
double area;
area = 3.14 * radius * radius; // Can you detect the int --> float conversion ?
// int radius needs to be converted to float
// before it can be used in the computation !!!
|
most CPU's provide machine instructions that perform the int <--> float conversions
// file: /home/cs255/cs255/c-asm/int2float.c on host cs255host1
// ssh -X cs255@cs255host1, User: cs255, passwd: abc123
int x;
float f;
int main()
{
x = 4;
f = x; // Assign int var to a float var ---> will convert int to float !
}
|
gcc -S int2float.c
|
This will generate the assembler program file int2float.s.
Look inside the int2float.s file and you will see these instructions:
...
vcvt.f32.s32 s15, s15 // This instr converts an int repr to a float repr !!
....
|
Specifically: the instruction vcvt.f32.s32 is the ARM processor's instruction to convert an integer representation to a float representation
(It must be done using an "s" register)
// Program: /home/cs255/cs255/int2float.s on cs255host1
mov r0, #-5 // Convert int -5
vmov s15, r0 // Move r0 to float processor
vcvt.f32.s32 s15, s15 // Convert int code to float code
vmov r1, s15 // Move float code to r1
|
The vcvt.f32.s32 is the ARM processor's instruction to convert an integer representation to a float representation
When you run the program, you will see this result in the registers:
|
The register R0 contains the 2s complement representation for the value 5
The register R0 contains the IEEE 754 representation for the value 5.0
How to run the program:
|
|
Comment:
|