Andrew Kelley 41dd2beaac compiler-rt: math functions reorg
* unify the logic for exporting math functions from compiler-rt,
   with the appropriate suffixes and prefixes.
   - add all missing f128 and f80 exports. Functions with missing
     implementations call other functions and have TODO comments.
   - also add f16 functions
 * move math functions from freestanding libc to compiler-rt (#7265)
 * enable all the f128 and f80 code in the stage2 compiler and behavior
   tests (#11161).
 * update std lib to use builtins rather than `std.math`.
2022-04-27 12:20:44 -07:00

44 lines
1.2 KiB
Zig

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));
}
}