1const std = @import("std");
2const builtin = @import("builtin");
3const math = std.math;
4const arch = builtin.cpu.arch;
5const compiler_rt = @import("../compiler_rt.zig");
6const symbol = compiler_rt.symbol;
7
8comptime {
9 symbol(&__fminh, "__fminh");
10 symbol(&fminf, "fminf");
11 symbol(&fmin, "fmin");
12 symbol(&__fminx, "__fminx");
13 symbol(&fminq, "fminf128");
14 symbol(&fminl, "fminl");
15}
16
17fn __fminh(x: compiler_rt.f16.Abi, y: compiler_rt.f16.Abi) callconv(.c) compiler_rt.f16.Abi {
18 return compiler_rt.f16.toAbi(fmin_f16(compiler_rt.f16.fromAbi(x), compiler_rt.f16.fromAbi(y)));
19}
20pub fn fmin_f16(x: f16, y: f16) f16 {
21 return generic_fmin(f16, x, y);
22}
23
24fn fminf(x: compiler_rt.f32.Abi, y: compiler_rt.f32.Abi) callconv(.c) compiler_rt.f32.Abi {
25 return compiler_rt.f32.toAbi(fmin_f32(compiler_rt.f32.fromAbi(x), compiler_rt.f32.fromAbi(y)));
26}
27pub fn fmin_f32(x: f32, y: f32) f32 {
28 return generic_fmin(f32, x, y);
29}
30
31fn fmin(x: compiler_rt.f64.Abi, y: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f64.Abi {
32 return compiler_rt.f64.toAbi(fmin_f64(compiler_rt.f64.fromAbi(x), compiler_rt.f64.fromAbi(y)));
33}
34pub fn fmin_f64(x: f64, y: f64) f64 {
35 return generic_fmin(f64, x, y);
36}
37
38fn __fminx(x: compiler_rt.f80.Abi, y: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f80.Abi {
39 return compiler_rt.f80.toAbi(fmin_f80(compiler_rt.f80.fromAbi(x), compiler_rt.f80.fromAbi(y)));
40}
41pub fn fmin_f80(x: f80, y: f80) f80 {
42 return generic_fmin(f80, x, y);
43}
44
45fn fminq(x: compiler_rt.f128.Abi, y: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f128.Abi {
46 return compiler_rt.f128.toAbi(fmin_f128(compiler_rt.f128.fromAbi(x), compiler_rt.f128.fromAbi(y)));
47}
48pub fn fmin_f128(x: f128, y: f128) f128 {
49 return generic_fmin(f128, x, y);
50}
51
52pub fn fminl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
53 switch (@typeInfo(c_longdouble).float.bits) {
54 64 => return fmin_f64(x, y),
55 80 => return fmin_f80(x, y),
56 128 => return fmin_f128(x, y),
57 else => comptime unreachable,
58 }
59}
60
61inline fn generic_fmin(comptime T: type, x: T, y: T) T {
62 if (math.isNan(x))
63 return y;
64 if (math.isNan(y))
65 return x;
66 if (math.signbit(x) != math.signbit(y))
67 return if (math.signbit(x)) x else y;
68 return if (x < y) x else y;
69}
70
71test "generic_fmin" {
72 inline for ([_]type{ f32, f64, c_longdouble, f80, f128 }) |T| {
73 const nan_val = math.nan(T);
74 const Int = @Int(.unsigned, @bitSizeOf(T));
75
76 try std.testing.expect(math.isNan(generic_fmin(T, nan_val, nan_val)));
77 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, nan_val, 1.0));
78 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, nan_val));
79
80 try std.testing.expectEqual(@as(T, 1.0), generic_fmin(T, 1.0, 10.0));
81 try std.testing.expectEqual(@as(T, -1.0), generic_fmin(T, 1.0, -1.0));
82
83 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, -0.0))), @as(Int, @bitCast(generic_fmin(T, 0.0, -0.0))));
84 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, -0.0))), @as(Int, @bitCast(generic_fmin(T, -0.0, 0.0))));
85 }
86}