aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math.zig
diff options
context:
space:
mode:
authorxavier <xavierb@gmail.com>2020-10-07 20:43:42 +0200
committerAndrew Kelley <andrew@ziglang.org>2020-10-07 17:04:48 -0400
commita0a834a2f292eb7f4170d590833b5d56084c0468 (patch)
treef973342ea36a9e4eda5b40c0f30bdc50ac65b35f /lib/std/math.zig
parent95a37373e9f576854956c2909cc128b5b6388ec6 (diff)
downloadzig-a0a834a2f292eb7f4170d590833b5d56084c0468.tar.gz
zig-a0a834a2f292eb7f4170d590833b5d56084c0468.zip
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.
Diffstat (limited to 'lib/std/math.zig')
-rw-r--r--lib/std/math.zig13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig
index f05c967b2d..a1db1309c9 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -110,7 +110,12 @@ pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
}
pub fn doNotOptimizeAway(value: anytype) void {
- mem.doNotOptimizeAway(value);
+ // TODO: use @declareSideEffect() when it is available.
+ // https://github.com/ziglang/zig/issues/6168
+ const T = @TypeOf(value);
+ var x: T = undefined;
+ const p = @ptrCast(*volatile T, &x);
+ p.* = x;
}
pub fn raiseInvalid() void {
@@ -1131,3 +1136,9 @@ test "compare between signed and unsigned" {
testing.expect(!compare(@as(u8, 255), .eq, @as(i8, -1)));
testing.expect(compare(@as(u8, 1), .eq, @as(u8, 1)));
}
+
+test "math.comptime" {
+ comptime const v = sin(@as(f32, 1)) + ln(@as(f32, 5));
+ testing.expect(v == sin(@as(f32, 1)) + ln(@as(f32, 5)));
+}
+