Skip to content

Commit 063f2c1

Browse files
committed
Improve floating point
1 parent a0a2d3d commit 063f2c1

25 files changed

+789
-359
lines changed

bibliography.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,9 @@
4848

4949
Lots of info on what C code maps to in assembly, and how to look for what matters to reverse engineer stuff.
5050

51+
- <http://www.agner.org/optimize/>
52+
53+
Highly regarded optimization guidelines for x86 and C++.
54+
5155
[Itanium C++ ABI]: http://mentorembedded.github.io/cxx-abi/abi.html
5256
[System V ABI AMD64]: http://www.x86-64.org/documentation_folder/abi-0.99.pdf

compiler-generated/.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*.s
2-
*.o_readelf
3-
*.out_readelf
2+
*.readelf
3+
*.objdump

compiler-generated/Makefile

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
.POSIX:
22

33
ASM_EXT ?= .s
4-
CCC ?= gcc -pedantic-errors -std=c89 -Wextra
4+
O ?= 0
5+
CCC ?= gcc -pedantic-errors -std=c99 -Wextra -ggdb -O$(O)
56
CCC_CPP ?= g++ -pedantic-errors -std=c++11 -Wextra
67
IN_EXT ?= .c
78
IN_EXT_CPP ?= .cpp
9+
LIBS ?= -lm
810
OBJ_EXT ?= .o
911
OUT_EXT ?= .out
10-
OBJ_READELF_EXT ?= .o_readelf
11-
OUT_READELF_EXT ?= .out_readelf
12+
OBJ_READELF_EXT ?= $(OBJ_EXT).readelf
13+
OUT_READELF_EXT ?= $(OUT_EXT).readelf
14+
OBJ_OBJDUMP_EXT ?= $(OBJ_EXT).objdump
15+
OUT_OBJDUMP_EXT ?= $(OUT_EXT).objdump
1216
RUN ?= hello_world
1317

1418
INS := $(wildcard *$(IN_EXT))
@@ -22,18 +26,22 @@ OUTS_CPP := $(addsuffix $(OUT_EXT), $(basename $(INS_CPP)))
2226
all: $(OUTS) $(OUTS_CPP)
2327

2428
%$(OUT_EXT): %$(IN_EXT)
25-
$(CCC) -S '$<' -o '$(basename $@)$(ASM_EXT)'
26-
$(CCC) -c '$(basename $@)$(ASM_EXT)' -o '$(basename $@)$(OBJ_EXT)'
27-
$(CCC) '$(basename $@)$(OBJ_EXT)' -o '$@'
28-
readelf -a '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_READELF_EXT)'
29-
readelf -a '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_READELF_EXT)'
29+
$(CCC) -S '$<' -o '$(basename $@)$(ASM_EXT)' $(LIBS)
30+
$(CCC) -c '$(basename $@)$(ASM_EXT)' -o '$(basename $@)$(OBJ_EXT)' $(LIBS)
31+
readelf -W -a '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_READELF_EXT)'
32+
objdump -S '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_OBJDUMP_EXT)'
33+
$(CCC) '$(basename $@)$(OBJ_EXT)' -o '$@' $(LIBS)
34+
readelf -W -a '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_READELF_EXT)'
35+
objdump -S '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_OBJDUMP_EXT)'
3036

3137
%$(OUT_EXT): %$(IN_EXT_CPP)
3238
$(CCC_CPP) -S '$<' -o '$(basename $@)$(ASM_EXT)'
3339
$(CCC_CPP) -c '$(basename $@)$(ASM_EXT)' -o '$(basename $@)$(OBJ_EXT)'
40+
readelf -W -a '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_READELF_EXT)'
41+
objdump -S '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_OBJDUMP_EXT)'
3442
$(CCC_CPP) '$(basename $@)$(OBJ_EXT)' -o '$@'
35-
readelf -a '$(basename $@)$(OBJ_EXT)' > '$(basename $@)$(OBJ_READELF_EXT)'
36-
readelf -a '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_READELF_EXT)'
43+
readelf -W -a '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_READELF_EXT)'
44+
objdump -S '$(basename $@)$(OUT_EXT)' > '$(basename $@)$(OUT_OBJDUMP_EXT)'
3745

3846
clean:
3947
rm -f *'$(ASM_EXT)' *'$(OBJ_EXT)' *'$(OUT_EXT)' *'$(OBJ_READELF_EXT)' *'$(OUT_READELF_EXT)'

compiler-generated/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Let's see what our compilers are compiling to.
1010
1. [common.c](common.c)
1111
1. [data.c](data.c)
1212
1. [hello_world.c](hello_world.c)
13-
1. [function.c](function.c)
13+
1. [function_int_int_int.c](function_int_int_int.c)
14+
1. Floating point
15+
[float_sum.c](float_sum.c)
16+
[float2int.c](float2int.c)
17+
[sqrt.c](sqrt.c)
18+
1. Optimization
19+
1. [use_rax_return.c](use_rax_return.c)
1420
1. libc
1521
1. [memcmp.c](memcmp.c)

compiler-generated/a

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1, 3, 4, 5, 13, 2, 3, 4, 22, 1, 2, 3
2+
1
3+
4
4+
6
5+
28
6+
30 1, 3, 4, 5, 13, 2, 3, 4, 22, 1, 2, 3
7+
1
8+
4
9+
6
10+
28
11+
301, 2, 2, 4, 8, 1, 8, 1⁄2, 4, 1, 4, 4
12+
1
13+
4
14+
8
15+
12
16+
16
17+
9, 3, 6, 21, 9, 12, 33, 15, 18 11.6, 2.3, 9.7, 11.3, 2.1, 9.6, 11, 1.9, 9.5
18+
10.4
19+
10.7
20+
10.8
21+
11.3
22+
11.9
23+
5, 6, 11, 2, 9, 3, 12, 6, 6, 3
24+
3
25+
6
26+
9
27+
15
28+
18

compiler-generated/double_sum.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
int main() {
5+
double f;
6+
f = (double)(time(NULL) % 3);
7+
/*
8+
GCC 4.8:
9+
10+
- fpmath=sse: addsd (default)
11+
*/
12+
f += 0.5;
13+
printf("%f\n", f);
14+
return 0;
15+
}

compiler-generated/float2int.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
int main() {
5+
int i;
6+
float f;
7+
f = (float)(time(NULL) % 3) + 0.5;
8+
/*
9+
- sse: cvttss2si
10+
*/
11+
i = (int)f;
12+
printf("%d\n", i);
13+
return 0;
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
http://stackoverflow.com/questions/17220787/c-casting-from-double-to-int
3+
4+
Don't reproduce him.
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdarg.h>
9+
10+
int main() {
11+
int a;
12+
int d;
13+
double b = 0.41;
14+
15+
/* Cast from variable. */
16+
double c = b * 100.0;
17+
a = (int)(c);
18+
19+
/* Cast expression directly. */
20+
d = (int)(b * 100.0);
21+
22+
printf("c = %f \n", c);
23+
printf("a = %d \n", a);
24+
printf("d = %d \n", d);
25+
26+
return 0;
27+
}

compiler-generated/float_sum.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
int main() {
5+
float f;
6+
f = (float)(time(NULL) % 3);
7+
/*
8+
GCC 4.8:
9+
10+
- fpmath=sse: addss (default)
11+
- fpmath=fpu: faddp
12+
*/
13+
f += 0.5f;
14+
printf("%f\n", f);
15+
return 0;
16+
}

compiler-generated/function.c

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)