为什么浮点字面量 3.14f 在 LLVM 中用 64 位值 0x40091EB860000000 表示?
Why float point literal 3.14f is represented by a 64-bit value 0x40091EB860000000 in LLVM?
#include <stdlib.h>
#include <stdio.h>
const float PI = 3.14f;
//const float PI = 3.14;
float getPI() {
return PI;
}
int main() {
printf("%d\n", sizeof(float));
return 0;
}
sizeof(float) 是 4。但是在 LLVM 字节码中,3.14f 由 64 位双精度表示 0x40091EB860000000.
@PI = constant float 0x40091EB860000000, align 4
; Function Attrs: noinline nounwind uwtable
define float @getPI() #0 {
entry:
ret float 0x40091EB860000000
}
Floating point constants Floating point constants use standard decimal
notation (e.g. 123.421), exponential notation (e.g. 1.23421e+2), or a
more precise hexadecimal notation (see below). The assembler requires
the exact decimal value of a floating-point constant. For example, the
assembler accepts 1.25 but rejects 1.3 because 1.3 is a repeating
decimal in binary. Floating point constants must have a floating point
type.
还有
When using the hexadecimal form, constants of types half, float, and
double are represented using the 16-digit form shown above (which
matches the IEEE754 representation for double);
#include <stdlib.h>
#include <stdio.h>
const float PI = 3.14f;
//const float PI = 3.14;
float getPI() {
return PI;
}
int main() {
printf("%d\n", sizeof(float));
return 0;
}
sizeof(float) 是 4。但是在 LLVM 字节码中,3.14f 由 64 位双精度表示 0x40091EB860000000.
@PI = constant float 0x40091EB860000000, align 4
; Function Attrs: noinline nounwind uwtable
define float @getPI() #0 {
entry:
ret float 0x40091EB860000000
}
Floating point constants Floating point constants use standard decimal notation (e.g. 123.421), exponential notation (e.g. 1.23421e+2), or a more precise hexadecimal notation (see below). The assembler requires the exact decimal value of a floating-point constant. For example, the assembler accepts 1.25 but rejects 1.3 because 1.3 is a repeating decimal in binary. Floating point constants must have a floating point type.
还有
When using the hexadecimal form, constants of types half, float, and double are represented using the 16-digit form shown above (which matches the IEEE754 representation for double);