Browse code

Add gcc-optimizations/avoid-imul.c.

Xavier G authored on 23/12/2016 16:42:48
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,100 @@
1
+/*
2
+ * A few dummy functions to show how gcc avoids integer
3
+ * multiplication (i.e. the "imul" instruction) even with -O0.
4
+ * Of course, the avoidance is more, err, straightforward
5
+ * with -O1 and friends.
6
+ * Tested with x86-64 gcc 6.2 and -O0.
7
+ *
8
+ * Let's be honest, I was procrastinating something here...
9
+ */
10
+#include <stdint.h>
11
+
12
+uint32_t gcc_O0_optim_mul_2() {
13
+	uint32_t multiplize_me = 42;
14
+	// This will actually do multiplize_me + multiplize_me
15
+	return multiplize_me * 2;
16
+}
17
+
18
+uint32_t gcc_O0_optim_mul_3() {
19
+	uint32_t multiplize_me = 42;
20
+	// This will actually do multiplize_me + multiplize_me + multiplize_me
21
+	return multiplize_me * 3;
22
+}
23
+
24
+uint32_t gcc_O0_optim_mul_4() {
25
+	uint32_t multiplize_me = 42;
26
+	// This will actually do multiplize_me << 2
27
+	return multiplize_me * 4;
28
+}
29
+
30
+uint32_t gcc_O0_optim_mul_5() {
31
+	uint32_t multiplize_me = 42;
32
+	// This will actually do (multiplize_me << 2) + multiplize_me
33
+	return multiplize_me * 5;
34
+}
35
+
36
+uint32_t gcc_O0_optim_mul_6() {
37
+	uint32_t multiplize_me = 42;
38
+	// This will do gcc_O0_optim_mul_3 followed by gcc_O0_optim_mul_2
39
+	return multiplize_me * 6;
40
+}
41
+
42
+uint32_t gcc_O0_optim_mul_7() {
43
+	uint32_t multiplize_me = 42;
44
+	// This will actually do (multiplize_me << 3) - multiplize_me
45
+	return multiplize_me * 7;
46
+}
47
+
48
+uint32_t gcc_O0_optim_mul_8() {
49
+	uint32_t multiplize_me = 42;
50
+	// This will actually do multiplize_me << 3
51
+	return multiplize_me * 8;
52
+}
53
+
54
+uint32_t gcc_O0_optim_mul_9() {
55
+	uint32_t multiplize_me = 42;
56
+	// This will actually do (multiplize_me << 3) + multiplize_me
57
+	return multiplize_me * 9;
58
+}
59
+
60
+uint32_t gcc_O0_optim_mul_10() {
61
+	uint32_t multiplize_me = 42;
62
+	// This will do gcc_O0_optim_mul_5 followed by gcc_O0_optim_mul_2
63
+	return multiplize_me * 10;
64
+}
65
+
66
+uint32_t gcc_O0_optim_mul_11() {
67
+	uint32_t multiplize_me = 42;
68
+	// This will do gcc_O0_optim_mul_10 then add multiplize_me
69
+	return multiplize_me * 11;
70
+}
71
+
72
+uint32_t gcc_O0_optim_mul_12() {
73
+	uint32_t multiplize_me = 42;
74
+	// This will do gcc_O0_optim_mul_3 followed by gcc_O0_optim_mul_4
75
+	return multiplize_me * 12;
76
+}
77
+
78
+uint32_t gcc_O0_optim_mul_13() {
79
+	uint32_t multiplize_me = 42;
80
+	// This will do gcc_O0_optim_mul_12 then add multiplize_me
81
+	return multiplize_me * 13;
82
+}
83
+
84
+uint32_t gcc_O0_optim_mul_14() {
85
+	uint32_t multiplize_me = 42;
86
+	// This will, at last, call imul
87
+	return multiplize_me * 14;
88
+}
89
+
90
+uint32_t gcc_O0_optim_mul_15() {
91
+	uint32_t multiplize_me = 42;
92
+	// This will actually do (multiplize_me << 4) - multiplize_me
93
+	return multiplize_me * 15;
94
+}
95
+
96
+uint32_t gcc_O0_optim_mul_16() {
97
+	uint32_t multiplize_me = 42;
98
+	// This will actually do multiplize_me << 4
99
+	return multiplize_me * 16;
100
+}
0 101
\ No newline at end of file