authorgravatar for xavierb@gmail.comxavier <xavierb@gmail.com> 2020-10-07 20:43:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-07 17:04:48-04:00
loga0a834a2f292eb7f4170d590833b5d56084c0468
treef973342ea36a9e4eda5b40c0f30bdc50ac65b35f
parent95a37373e9f576854956c2909cc128b5b6388ec6

restore ability to do comptime math

until https://github.com/ziglang/zig/issues/6168 is implemented, partially revert 0bd53dd2033c60d3446abfb83209237c6eb6c9e2 in order to restore the ability to use std.math in comptime functions.

1 files changed, 12 insertions(+), 1 deletions(-)

lib/std/math.zig+12-1
...@@ -110,7 +110,12 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {...@@ -110,7 +110,12 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
110}110}
111111
112pub fn doNotOptimizeAway(value: anytype) void {112pub fn doNotOptimizeAway(value: anytype) void {
113 mem.doNotOptimizeAway(value);113 // TODO: use @declareSideEffect() when it is available.
114 // https://github.com/ziglang/zig/issues/6168
115 const T = @TypeOf(value);
116 var x: T = undefined;
117 const p = @ptrCast(*volatile T, &x);
118 p.* = x;
114}119}
115120
116pub fn raiseInvalid() void {121pub fn raiseInvalid() void {
...@@ -1131,3 +1136,9 @@ test "compare between signed and unsigned" {...@@ -1131,3 +1136,9 @@ test "compare between signed and unsigned" {
1131 testing.expect(!compare(@as(u8, 255), .eq, @as(i8, -1)));1136 testing.expect(!compare(@as(u8, 255), .eq, @as(i8, -1)));
1132 testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));1137 testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
1133}1138}
1139
1140test "math.comptime" {
1141 comptime const v = sin(@as(f32, 1)) + ln(@as(f32, 5));
1142 testing.expect(v == sin(@as(f32, 1)) + ln(@as(f32, 5)));
1143}
1144