authorgravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-26 01:48:51+02:00
committergravatar for 124872+jedisct1@users.noreply.github.comFrank Denis <124872+jedisct1@users.noreply.github.com> 2020-08-26 10:50:34+02:00
logad18078d53b448fc18a100dcebca88fe05a1e3ac
tree2977e9fa1fcbbc58057684adf86ceb0291e16ef2
parent0bd53dd2033c60d3446abfb83209237c6eb6c9e2

forceEval() -> doNotOptimizeAway()


15 files changed, 41 insertions(+), 41 deletions(-)

lib/std/crypto/benchmark.zig+5-5
...@@ -47,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64...@@ -47,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64
47 while (offset < bytes) : (offset += block.len) {47 while (offset < bytes) : (offset += block.len) {
48 h.update(block[0..]);48 h.update(block[0..]);
49 }49 }
50 mem.forceEval(&h);50 mem.doNotOptimizeAway(&h);
51 const end = timer.read();51 const end = timer.read();
5252
53 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;53 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
...@@ -82,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {...@@ -82,7 +82,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 {
82 const start = timer.lap();82 const start = timer.lap();
83 while (offset < bytes) : (offset += in.len) {83 while (offset < bytes) : (offset += in.len) {
84 Mac.create(mac[0..], in[0..], key[0..]);84 Mac.create(mac[0..], in[0..], key[0..]);
85 mem.forceEval(&mac);85 mem.doNotOptimizeAway(&mac);
86 }86 }
87 const end = timer.read();87 const end = timer.read();
8888
...@@ -109,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c...@@ -109,7 +109,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c
109 var i: usize = 0;109 var i: usize = 0;
110 while (i < exchange_count) : (i += 1) {110 while (i < exchange_count) : (i += 1) {
111 _ = DhKeyExchange.create(out[0..], out[0..], in[0..]);111 _ = DhKeyExchange.create(out[0..], out[0..], in[0..]);
112 mem.forceEval(&out);112 mem.doNotOptimizeAway(&out);
113 }113 }
114 }114 }
115 const end = timer.read();115 const end = timer.read();
...@@ -134,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count...@@ -134,7 +134,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count
134 var i: usize = 0;134 var i: usize = 0;
135 while (i < signatures_count) : (i += 1) {135 while (i < signatures_count) : (i += 1) {
136 const s = try Signature.sign(&msg, key_pair, null);136 const s = try Signature.sign(&msg, key_pair, null);
137 mem.forceEval(&s);137 mem.doNotOptimizeAway(&s);
138 }138 }
139 }139 }
140 const end = timer.read();140 const end = timer.read();
...@@ -170,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64...@@ -170,7 +170,7 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64
170 Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);170 Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key);
171 Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;171 Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key) catch unreachable;
172 }172 }
173 mem.forceEval(&in);173 mem.doNotOptimizeAway(&in);
174 const end = timer.read();174 const end = timer.read();
175175
176 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;176 const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
lib/std/math.zig+2-2
...@@ -109,8 +109,8 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {...@@ -109,8 +109,8 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
109 return fabs(x - y) < epsilon;109 return fabs(x - y) < epsilon;
110}110}
111111
112pub fn forceEval(value: anytype) void {112pub fn doNotOptimizeAway(value: anytype) void {
113 mem.forceEval(value);113 mem.doNotOptimizeAway(value);
114}114}
115115
116pub fn raiseInvalid() void {116pub fn raiseInvalid() void {
lib/std/math/asinh.zig+2-2
...@@ -56,7 +56,7 @@ fn asinh32(x: f32) f32 {...@@ -56,7 +56,7 @@ fn asinh32(x: f32) f32 {
56 }56 }
57 // |x| < 0x1p-12, inexact if x != 057 // |x| < 0x1p-12, inexact if x != 0
58 else {58 else {
59 math.forceEval(x + 0x1.0p120);59 math.doNotOptimizeAway(x + 0x1.0p120);
60 }60 }
6161
62 return if (s != 0) -rx else rx;62 return if (s != 0) -rx else rx;
...@@ -87,7 +87,7 @@ fn asinh64(x: f64) f64 {...@@ -87,7 +87,7 @@ fn asinh64(x: f64) f64 {
87 }87 }
88 // |x| < 0x1p-12, inexact if x != 088 // |x| < 0x1p-12, inexact if x != 0
89 else {89 else {
90 math.forceEval(x + 0x1.0p120);90 math.doNotOptimizeAway(x + 0x1.0p120);
91 }91 }
9292
93 return if (s != 0) -rx else rx;93 return if (s != 0) -rx else rx;
lib/std/math/atan.zig+2-2
...@@ -72,7 +72,7 @@ fn atan32(x_: f32) f32 {...@@ -72,7 +72,7 @@ fn atan32(x_: f32) f32 {
72 // |x| < 2^(-12)72 // |x| < 2^(-12)
73 if (ix < 0x39800000) {73 if (ix < 0x39800000) {
74 if (ix < 0x00800000) {74 if (ix < 0x00800000) {
75 math.forceEval(x * x);75 math.doNotOptimizeAway(x * x);
76 }76 }
77 return x;77 return x;
78 }78 }
...@@ -170,7 +170,7 @@ fn atan64(x_: f64) f64 {...@@ -170,7 +170,7 @@ fn atan64(x_: f64) f64 {
170 // |x| < 2^(-27)170 // |x| < 2^(-27)
171 if (ix < 0x3E400000) {171 if (ix < 0x3E400000) {
172 if (ix < 0x00100000) {172 if (ix < 0x00100000) {
173 math.forceEval(@floatCast(f32, x));173 math.doNotOptimizeAway(@floatCast(f32, x));
174 }174 }
175 return x;175 return x;
176 }176 }
lib/std/math/atanh.zig+2-2
...@@ -45,7 +45,7 @@ fn atanh_32(x: f32) f32 {...@@ -45,7 +45,7 @@ fn atanh_32(x: f32) f32 {
45 if (u < 0x3F800000 - (32 << 23)) {45 if (u < 0x3F800000 - (32 << 23)) {
46 // underflow46 // underflow
47 if (u < (1 << 23)) {47 if (u < (1 << 23)) {
48 math.forceEval(y * y);48 math.doNotOptimizeAway(y * y);
49 }49 }
50 }50 }
51 // |x| < 0.551 // |x| < 0.5
...@@ -74,7 +74,7 @@ fn atanh_64(x: f64) f64 {...@@ -74,7 +74,7 @@ fn atanh_64(x: f64) f64 {
74 if (e < 0x3FF - 32) {74 if (e < 0x3FF - 32) {
75 // underflow75 // underflow
76 if (e == 0) {76 if (e == 0) {
77 math.forceEval(@floatCast(f32, y));77 math.doNotOptimizeAway(@floatCast(f32, y));
78 }78 }
79 }79 }
80 // |x| < 0.580 // |x| < 0.5
lib/std/math/ceil.zig+4-4
...@@ -47,14 +47,14 @@ fn ceil32(x: f32) f32 {...@@ -47,14 +47,14 @@ fn ceil32(x: f32) f32 {
47 if (u & m == 0) {47 if (u & m == 0) {
48 return x;48 return x;
49 }49 }
50 math.forceEval(x + 0x1.0p120);50 math.doNotOptimizeAway(x + 0x1.0p120);
51 if (u >> 31 == 0) {51 if (u >> 31 == 0) {
52 u += m;52 u += m;
53 }53 }
54 u &= ~m;54 u &= ~m;
55 return @bitCast(f32, u);55 return @bitCast(f32, u);
56 } else {56 } else {
57 math.forceEval(x + 0x1.0p120);57 math.doNotOptimizeAway(x + 0x1.0p120);
58 if (u >> 31 != 0) {58 if (u >> 31 != 0) {
59 return -0.0;59 return -0.0;
60 } else {60 } else {
...@@ -79,7 +79,7 @@ fn ceil64(x: f64) f64 {...@@ -79,7 +79,7 @@ fn ceil64(x: f64) f64 {
79 }79 }
8080
81 if (e <= 0x3FF - 1) {81 if (e <= 0x3FF - 1) {
82 math.forceEval(y);82 math.doNotOptimizeAway(y);
83 if (u >> 63 != 0) {83 if (u >> 63 != 0) {
84 return -0.0;84 return -0.0;
85 } else {85 } else {
...@@ -106,7 +106,7 @@ fn ceil128(x: f128) f128 {...@@ -106,7 +106,7 @@ fn ceil128(x: f128) f128 {
106 }106 }
107107
108 if (e <= 0x3FFF - 1) {108 if (e <= 0x3FFF - 1) {
109 math.forceEval(y);109 math.doNotOptimizeAway(y);
110 if (u >> 127 != 0) {110 if (u >> 127 != 0) {
111 return -0.0;111 return -0.0;
112 } else {112 } else {
lib/std/math/exp.zig+4-4
...@@ -56,7 +56,7 @@ fn exp32(x_: f32) f32 {...@@ -56,7 +56,7 @@ fn exp32(x_: f32) f32 {
56 return x * 0x1.0p127;56 return x * 0x1.0p127;
57 }57 }
58 if (sign != 0) {58 if (sign != 0) {
59 math.forceEval(-0x1.0p-149 / x); // overflow59 math.doNotOptimizeAway(-0x1.0p-149 / x); // overflow
60 // x <= -103.97208460 // x <= -103.972084
61 if (hx >= 0x42CFF1B5) {61 if (hx >= 0x42CFF1B5) {
62 return 0;62 return 0;
...@@ -88,7 +88,7 @@ fn exp32(x_: f32) f32 {...@@ -88,7 +88,7 @@ fn exp32(x_: f32) f32 {
88 hi = x;88 hi = x;
89 lo = 0;89 lo = 0;
90 } else {90 } else {
91 math.forceEval(0x1.0p127 + x); // inexact91 math.doNotOptimizeAway(0x1.0p127 + x); // inexact
92 return 1 + x;92 return 1 + x;
93 }93 }
9494
...@@ -139,7 +139,7 @@ fn exp64(x_: f64) f64 {...@@ -139,7 +139,7 @@ fn exp64(x_: f64) f64 {
139 }139 }
140 if (x < -708.39641853226410622) {140 if (x < -708.39641853226410622) {
141 // underflow if x != -inf141 // underflow if x != -inf
142 // math.forceEval(@as(f32, -0x1.0p-149 / x));142 // math.doNotOptimizeAway(@as(f32, -0x1.0p-149 / x));
143 if (x < -745.13321910194110842) {143 if (x < -745.13321910194110842) {
144 return 0;144 return 0;
145 }145 }
...@@ -172,7 +172,7 @@ fn exp64(x_: f64) f64 {...@@ -172,7 +172,7 @@ fn exp64(x_: f64) f64 {
172 lo = 0;172 lo = 0;
173 } else {173 } else {
174 // inexact if x != 0174 // inexact if x != 0
175 // math.forceEval(0x1.0p1023 + x);175 // math.doNotOptimizeAway(0x1.0p1023 + x);
176 return 1 + x;176 return 1 + x;
177 }177 }
178178
lib/std/math/exp2.zig+2-2
...@@ -70,7 +70,7 @@ fn exp2_32(x: f32) f32 {...@@ -70,7 +70,7 @@ fn exp2_32(x: f32) f32 {
70 // x < -12670 // x < -126
71 if (u >= 0x80000000) {71 if (u >= 0x80000000) {
72 if (u >= 0xC3160000 or u & 0x000FFFF != 0) {72 if (u >= 0xC3160000 or u & 0x000FFFF != 0) {
73 math.forceEval(-0x1.0p-149 / x);73 math.doNotOptimizeAway(-0x1.0p-149 / x);
74 }74 }
75 // x <= -15075 // x <= -150
76 if (u >= 0x3160000) {76 if (u >= 0x3160000) {
...@@ -393,7 +393,7 @@ fn exp2_64(x: f64) f64 {...@@ -393,7 +393,7 @@ fn exp2_64(x: f64) f64 {
393 if (ux >> 63 != 0) {393 if (ux >> 63 != 0) {
394 // underflow394 // underflow
395 if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {395 if (x <= -1075 or x - 0x1.0p52 + 0x1.0p52 != x) {
396 math.forceEval(@floatCast(f32, -0x1.0p-149 / x));396 math.doNotOptimizeAway(@floatCast(f32, -0x1.0p-149 / x));
397 }397 }
398 if (x <= -1075) {398 if (x <= -1075) {
399 return 0;399 return 0;
lib/std/math/expm1.zig+2-2
...@@ -106,7 +106,7 @@ fn expm1_32(x_: f32) f32 {...@@ -106,7 +106,7 @@ fn expm1_32(x_: f32) f32 {
106 // |x| < 2^(-25)106 // |x| < 2^(-25)
107 else if (hx < 0x33000000) {107 else if (hx < 0x33000000) {
108 if (hx < 0x00800000) {108 if (hx < 0x00800000) {
109 math.forceEval(x * x);109 math.doNotOptimizeAway(x * x);
110 }110 }
111 return x;111 return x;
112 } else {112 } else {
...@@ -237,7 +237,7 @@ fn expm1_64(x_: f64) f64 {...@@ -237,7 +237,7 @@ fn expm1_64(x_: f64) f64 {
237 // |x| < 2^(-54)237 // |x| < 2^(-54)
238 else if (hx < 0x3C900000) {238 else if (hx < 0x3C900000) {
239 if (hx < 0x00100000) {239 if (hx < 0x00100000) {
240 math.forceEval(@floatCast(f32, x));240 math.doNotOptimizeAway(@floatCast(f32, x));
241 }241 }
242 return x;242 return x;
243 } else {243 } else {
lib/std/math/floor.zig+6-6
...@@ -50,13 +50,13 @@ fn floor16(x: f16) f16 {...@@ -50,13 +50,13 @@ fn floor16(x: f16) f16 {
50 if (u & m == 0) {50 if (u & m == 0) {
51 return x;51 return x;
52 }52 }
53 math.forceEval(x + 0x1.0p120);53 math.doNotOptimizeAway(x + 0x1.0p120);
54 if (u >> 15 != 0) {54 if (u >> 15 != 0) {
55 u += m;55 u += m;
56 }56 }
57 return @bitCast(f16, u & ~m);57 return @bitCast(f16, u & ~m);
58 } else {58 } else {
59 math.forceEval(x + 0x1.0p120);59 math.doNotOptimizeAway(x + 0x1.0p120);
60 if (u >> 15 == 0) {60 if (u >> 15 == 0) {
61 return 0.0;61 return 0.0;
62 } else {62 } else {
...@@ -84,13 +84,13 @@ fn floor32(x: f32) f32 {...@@ -84,13 +84,13 @@ fn floor32(x: f32) f32 {
84 if (u & m == 0) {84 if (u & m == 0) {
85 return x;85 return x;
86 }86 }
87 math.forceEval(x + 0x1.0p120);87 math.doNotOptimizeAway(x + 0x1.0p120);
88 if (u >> 31 != 0) {88 if (u >> 31 != 0) {
89 u += m;89 u += m;
90 }90 }
91 return @bitCast(f32, u & ~m);91 return @bitCast(f32, u & ~m);
92 } else {92 } else {
93 math.forceEval(x + 0x1.0p120);93 math.doNotOptimizeAway(x + 0x1.0p120);
94 if (u >> 31 == 0) {94 if (u >> 31 == 0) {
95 return 0.0;95 return 0.0;
96 } else {96 } else {
...@@ -115,7 +115,7 @@ fn floor64(x: f64) f64 {...@@ -115,7 +115,7 @@ fn floor64(x: f64) f64 {
115 }115 }
116116
117 if (e <= 0x3FF - 1) {117 if (e <= 0x3FF - 1) {
118 math.forceEval(y);118 math.doNotOptimizeAway(y);
119 if (u >> 63 != 0) {119 if (u >> 63 != 0) {
120 return -1.0;120 return -1.0;
121 } else {121 } else {
...@@ -142,7 +142,7 @@ fn floor128(x: f128) f128 {...@@ -142,7 +142,7 @@ fn floor128(x: f128) f128 {
142 }142 }
143143
144 if (e <= 0x3FFF - 1) {144 if (e <= 0x3FFF - 1) {
145 math.forceEval(y);145 math.doNotOptimizeAway(y);
146 if (u >> 127 != 0) {146 if (u >> 127 != 0) {
147 return -1.0;147 return -1.0;
148 } else {148 } else {
lib/std/math/log1p.zig+1-1
...@@ -62,7 +62,7 @@ fn log1p_32(x: f32) f32 {...@@ -62,7 +62,7 @@ fn log1p_32(x: f32) f32 {
62 if ((ix << 1) < (0x33800000 << 1)) {62 if ((ix << 1) < (0x33800000 << 1)) {
63 // underflow if subnormal63 // underflow if subnormal
64 if (ix & 0x7F800000 == 0) {64 if (ix & 0x7F800000 == 0) {
65 math.forceEval(x * x);65 math.doNotOptimizeAway(x * x);
66 }66 }
67 return x;67 return x;
68 }68 }
lib/std/math/round.zig+3-3
...@@ -43,7 +43,7 @@ fn round32(x_: f32) f32 {...@@ -43,7 +43,7 @@ fn round32(x_: f32) f32 {
43 x = -x;43 x = -x;
44 }44 }
45 if (e < 0x7F - 1) {45 if (e < 0x7F - 1) {
46 math.forceEval(x + math.f32_toint);46 math.doNotOptimizeAway(x + math.f32_toint);
47 return 0 * @bitCast(f32, u);47 return 0 * @bitCast(f32, u);
48 }48 }
4949
...@@ -76,7 +76,7 @@ fn round64(x_: f64) f64 {...@@ -76,7 +76,7 @@ fn round64(x_: f64) f64 {
76 x = -x;76 x = -x;
77 }77 }
78 if (e < 0x3ff - 1) {78 if (e < 0x3ff - 1) {
79 math.forceEval(x + math.f64_toint);79 math.doNotOptimizeAway(x + math.f64_toint);
80 return 0 * @bitCast(f64, u);80 return 0 * @bitCast(f64, u);
81 }81 }
8282
...@@ -109,7 +109,7 @@ fn round128(x_: f128) f128 {...@@ -109,7 +109,7 @@ fn round128(x_: f128) f128 {
109 x = -x;109 x = -x;
110 }110 }
111 if (e < 0x3FFF - 1) {111 if (e < 0x3FFF - 1) {
112 math.forceEval(x + math.f64_toint);112 math.doNotOptimizeAway(x + math.f64_toint);
113 return 0 * @bitCast(f128, u);113 return 0 * @bitCast(f128, u);
114 }114 }
115115
lib/std/math/tanh.zig+2-2
...@@ -67,7 +67,7 @@ fn tanh32(x: f32) f32 {...@@ -67,7 +67,7 @@ fn tanh32(x: f32) f32 {
67 }67 }
68 // |x| is subnormal68 // |x| is subnormal
69 else {69 else {
70 math.forceEval(x * x);70 math.doNotOptimizeAway(x * x);
71 t = x;71 t = x;
72 }72 }
7373
...@@ -112,7 +112,7 @@ fn tanh64(x: f64) f64 {...@@ -112,7 +112,7 @@ fn tanh64(x: f64) f64 {
112 }112 }
113 // |x| is subnormal113 // |x| is subnormal
114 else {114 else {
115 math.forceEval(@floatCast(f32, x));115 math.doNotOptimizeAway(@floatCast(f32, x));
116 t = x;116 t = x;
117 }117 }
118118
lib/std/math/trunc.zig+3-3
...@@ -46,7 +46,7 @@ fn trunc32(x: f32) f32 {...@@ -46,7 +46,7 @@ fn trunc32(x: f32) f32 {
46 if (u & m == 0) {46 if (u & m == 0) {
47 return x;47 return x;
48 } else {48 } else {
49 math.forceEval(x + 0x1p120);49 math.doNotOptimizeAway(x + 0x1p120);
50 return @bitCast(f32, u & ~m);50 return @bitCast(f32, u & ~m);
51 }51 }
52}52}
...@@ -67,7 +67,7 @@ fn trunc64(x: f64) f64 {...@@ -67,7 +67,7 @@ fn trunc64(x: f64) f64 {
67 if (u & m == 0) {67 if (u & m == 0) {
68 return x;68 return x;
69 } else {69 } else {
70 math.forceEval(x + 0x1p120);70 math.doNotOptimizeAway(x + 0x1p120);
71 return @bitCast(f64, u & ~m);71 return @bitCast(f64, u & ~m);
72 }72 }
73}73}
...@@ -88,7 +88,7 @@ fn trunc128(x: f128) f128 {...@@ -88,7 +88,7 @@ fn trunc128(x: f128) f128 {
88 if (u & m == 0) {88 if (u & m == 0) {
89 return x;89 return x;
90 } else {90 } else {
91 math.forceEval(x + 0x1p120);91 math.doNotOptimizeAway(x + 0x1p120);
92 return @bitCast(f128, u & ~m);92 return @bitCast(f128, u & ~m);
93 }93 }
94}94}
lib/std/mem.zig+1-1
...@@ -2161,7 +2161,7 @@ pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {...@@ -2161,7 +2161,7 @@ pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T {
2161/// Force an evaluation of the expression; this tries to prevent2161/// Force an evaluation of the expression; this tries to prevent
2162/// the compiler from optimizing the computation away even if the2162/// the compiler from optimizing the computation away even if the
2163/// result eventually gets discarded.2163/// result eventually gets discarded.
2164pub fn forceEval(val: anytype) void {2164pub fn doNotOptimizeAway(val: anytype) void {
2165 asm volatile (""2165 asm volatile (""
2166 :2166 :
2167 : [val] "rm" (val)2167 : [val] "rm" (val)