1const std = @import("std");
2const builtin = @import("builtin");
3const arch = builtin.cpu.arch;
4const compiler_rt = @import("../compiler_rt.zig");
5const symbol = compiler_rt.symbol;
6
7comptime {
8 symbol(&__fabsh, "__fabsh");
9 symbol(&fabsf, "fabsf");
10 symbol(&fabs, "fabs");
11 symbol(&__fabsx, "__fabsx");
12 symbol(&fabsq, "fabsf128");
13 symbol(&fabsl, "fabsl");
14}
15
16fn __fabsh(a: compiler_rt.f16.Abi) callconv(.c) compiler_rt.f16.Abi {
17 return compiler_rt.f16.toAbi(fabs_f16(compiler_rt.f16.fromAbi(a)));
18}
19pub fn fabs_f16(a: f16) f16 {
20 return generic_fabs(a);
21}
22
23fn fabsf(a: compiler_rt.f32.Abi) callconv(.c) compiler_rt.f32.Abi {
24 return compiler_rt.f32.toAbi(fabs_f32(compiler_rt.f32.fromAbi(a)));
25}
26pub fn fabs_f32(a: f32) f32 {
27 return generic_fabs(a);
28}
29
30fn fabs(a: compiler_rt.f64.Abi) callconv(.c) compiler_rt.f64.Abi {
31 return compiler_rt.f64.toAbi(fabs_f64(compiler_rt.f64.fromAbi(a)));
32}
33pub fn fabs_f64(a: f64) f64 {
34 return generic_fabs(a);
35}
36
37fn __fabsx(a: compiler_rt.f80.Abi) callconv(.c) compiler_rt.f80.Abi {
38 return compiler_rt.f80.toAbi(fabs_f80(compiler_rt.f80.fromAbi(a)));
39}
40pub fn fabs_f80(a: f80) f80 {
41 return generic_fabs(a);
42}
43
44fn fabsq(a: compiler_rt.f128.Abi) callconv(.c) compiler_rt.f128.Abi {
45 return compiler_rt.f128.toAbi(fabs_f128(compiler_rt.f128.fromAbi(a)));
46}
47pub fn fabs_f128(a: f128) f128 {
48 return generic_fabs(a);
49}
50
51pub fn fabsl(x: c_longdouble) callconv(.c) c_longdouble {
52 switch (@typeInfo(c_longdouble).float.bits) {
53 64 => return fabs_f64(x),
54 80 => return fabs_f80(x),
55 128 => return fabs_f128(x),
56 else => comptime unreachable,
57 }
58}
59
60inline fn generic_fabs(x: anytype) @TypeOf(x) {
61 const T = @TypeOf(x);
62 const TBits = @Int(.unsigned, @typeInfo(T).float.bits);
63 const float_bits: TBits = @bitCast(x);
64 const remove_sign = ~@as(TBits, 0) >> 1;
65 return @bitCast(float_bits & remove_sign);
66}