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(&__fmaxh, "__fmaxh");
10 symbol(&fmaxf, "fmaxf");
11 symbol(&fmax, "fmax");
12 symbol(&__fmaxx, "__fmaxx");
13 symbol(&fmaxq, "fmaxf128");
14 symbol(&fmaxl, "fmaxl");
15}
16
17fn __fmaxh(x: compiler_rt.f16.Abi, y: compiler_rt.f16.Abi) callconv(.c) compiler_rt.f16.Abi {
18 return compiler_rt.f16.toAbi(fmax_f16(compiler_rt.f16.fromAbi(x), compiler_rt.f16.fromAbi(y)));
19}
20pub fn fmax_f16(x: f16, y: f16) f16 {
21 return generic_fmax(f16, x, y);
22}
23
24fn fmaxf(x: compiler_rt.f32.Abi, y: compiler_rt.f32.Abi) callconv(.c) compiler_rt.f32.Abi {
25 return compiler_rt.f32.toAbi(fmax_f32(compiler_rt.f32.fromAbi(x), compiler_rt.f32.fromAbi(y)));
26}
27pub fn fmax_f32(x: f32, y: f32) f32 {
28 return generic_fmax(f32, x, y);
29}
30
31fn fmax(x: compiler_rt.f64.Abi, y: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f64.Abi {
32 return compiler_rt.f64.toAbi(fmax_f64(compiler_rt.f64.fromAbi(x), compiler_rt.f64.fromAbi(y)));
33}
34pub fn fmax_f64(x: f64, y: f64) f64 {
35 return generic_fmax(f64, x, y);
36}
37
38fn __fmaxx(x: compiler_rt.f80.Abi, y: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f80.Abi {
39 return compiler_rt.f80.toAbi(fmax_f80(compiler_rt.f80.fromAbi(x), compiler_rt.f80.fromAbi(y)));
40}
41pub fn fmax_f80(x: f80, y: f80) f80 {
42 return generic_fmax(f80, x, y);
43}
44
45fn fmaxq(x: compiler_rt.f128.Abi, y: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f128.Abi {
46 return compiler_rt.f128.toAbi(fmax_f128(compiler_rt.f128.fromAbi(x), compiler_rt.f128.fromAbi(y)));
47}
48pub fn fmax_f128(x: f128, y: f128) f128 {
49 return generic_fmax(f128, x, y);
50}
51
52pub fn fmaxl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
53 switch (@typeInfo(c_longdouble).float.bits) {
54 64 => return fmax_f64(x, y),
55 80 => return fmax_f80(x, y),
56 128 => return fmax_f128(x, y),
57 else => comptime unreachable,
58 }
59}
60
61inline fn generic_fmax(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)) y else x;
68 return if (x < y) y else x;
69}
70
71test "generic_fmax" {
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_fmax(T, nan_val, nan_val)));
77 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, nan_val, 1.0));
78 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, 1.0, nan_val));
79
80 try std.testing.expectEqual(@as(T, 10.0), generic_fmax(T, 1.0, 10.0));
81 try std.testing.expectEqual(@as(T, 1.0), generic_fmax(T, 1.0, -1.0));
82
83 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, 0.0))), @as(Int, @bitCast(generic_fmax(T, 0.0, -0.0))));
84 try std.testing.expectEqual(@as(Int, @bitCast(@as(T, 0.0))), @as(Int, @bitCast(generic_fmax(T, -0.0, 0.0))));
85 }
86}