this is a test for IEEEtrick, used at Lua5.2.2 and before, and removed from Lua5.3.0-work1,
My CPU is Intel i5-2410M, but IEEEtrick still much faster than direct convert.
this is the test:
---------------------------------
sw@sw-acer:/media/sw/Work/Work/Code$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 42
model name : Intel(R) Core(TM) i5-2410M CPU @ 2.30GHz
stepping : 7
microcode : 0x23
cpu MHz : 800.000
cache size : 3072 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
apicid : 0
initial apicid : 0
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx lahf_lm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid
bogomips : 4589.29
clflush size : 64
cache_alignment : 64
address sizes : 36 bits physical, 48 bits virtual
power management:
sw@sw-acer:/media/sw/Work/Work/Code$ cat test_convert.c
#include <stdio.h>
#include <stdlib.h>
int number_to_integer_fast(double d) {
union { double d; int i[2]; } cast;
cast.d = d + 6755399441055744.0;
return cast.i[0];
}
int number_to_integer_normal(double d) {
return (int)d;
}
#define N 100000000
#include <time.h>
void timed(void (*f)()) {
clock_t t = clock();
f();
printf("time: %.2f\n", (double)(clock() - t) / CLOCKS_PER_SEC);
}
volatile int res;
volatile double d = 123.0;
void test1(void) {
int i;
for (i = 0; i < N; ++i) {
res = number_to_integer_fast(d);
(void)res;
}
}
void test2(void) {
int i;
for (i = 0; i < N; ++i) {
res = number_to_integer_normal(d);
(void)res;
}
}
int main(void)
{
timed(test1);
timed(test2);
return 0;
}
/* cc: flags+='-O2 -Wa,-a' */
sw@sw-acer:/media/sw/Work/Work/Code$ gcc -o test -O2 test_convert.c
sw@sw-acer:/media/sw/Work/Work/Code$ ./test
time: 0.11
time: 0.31
sw@sw-acer:/media/sw/Work/Work/Code$ ./test
time: 0.12
time: 0.30
sw@sw-acer:/media/sw/Work/Work/Code$ ./test
time: 0.11
time: 0.30
sw@sw-acer:/media/sw/Work/Work/Code$