diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-05-06 19:22:40 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-05-06 22:41:00 -0700 |
| commit | ec95e00e28cb23f37dc097f71afd7090e947a1cd (patch) | |
| tree | a7393f13c3d2c7895eb3687d0ebd7f3205699289 /lib/compiler_rt/fmin.zig | |
| parent | 3b60ab4872355f0b9a9c7d0794ca8b548ab99412 (diff) | |
| download | zig-ec95e00e28cb23f37dc097f71afd7090e947a1cd.tar.gz zig-ec95e00e28cb23f37dc097f71afd7090e947a1cd.zip | |
flatten lib/std/special and improve "pkg inside another" logic
stage2: change logic for detecting whether the main package is inside
the std package. Previously it relied on realpath() which is not portable.
This uses resolve() which is how imports already work.
* stage2: fix cleanup bug when creating Module
* flatten lib/std/special/* to lib/*
- this was motivated by making main_pkg_is_inside_std false for
compiler_rt & friends.
* rename "mini libc" to "universal libc"
Diffstat (limited to 'lib/compiler_rt/fmin.zig')
| -rw-r--r-- | lib/compiler_rt/fmin.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/compiler_rt/fmin.zig b/lib/compiler_rt/fmin.zig new file mode 100644 index 0000000000..cc4dbf082b --- /dev/null +++ b/lib/compiler_rt/fmin.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const math = std.math; + +pub fn __fminh(x: f16, y: f16) callconv(.C) f16 { + return generic_fmin(f16, x, y); +} + +pub fn fminf(x: f32, y: f32) callconv(.C) f32 { + return generic_fmin(f32, x, y); +} + +pub fn fmin(x: f64, y: f64) callconv(.C) f64 { + return generic_fmin(f64, x, y); +} + +pub fn __fminx(x: f80, y: f80) callconv(.C) f80 { + return generic_fmin(f80, x, y); +} + +pub fn fminq(x: f128, y: f128) callconv(.C) f128 { + return generic_fmin(f128, x, y); +} + +inline fn generic_fmin(comptime T: type, x: T, y: T) T { + if (math.isNan(x)) + return y; + if (math.isNan(y)) + return x; + return if (x < y) x else y; +} + +test "generic_fmin" { + inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| { + const nan_val = math.nan(T); + + try std.testing.expect(math.isNan(generic_fmin(T, nan_val, nan_val))); + try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, nan_val, 1.0)); + try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, nan_val)); + + try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, 10.0)); + try std.testing.expectEqual(@as(T, -1.0), generic_fmin(T, 1.0, -1.0)); + } +} |
