zig/lib/compiler_rt/fabs.zig
Andrew Kelley 0556a2ba53 compiler-rt: finish cleanups
Finishes cleanups that I started in other commits in this branch.

 * Use common.linkage for all exports instead of redoing the logic in
   each file.
 * Remove pointless `@setRuntimeSafety` calls.
 * Avoid redundantly exporting multiple versions of functions. For
   example, if PPC wants `ceilf128` then don't also export `ceilq`;
   similarly if ARM wants `__aeabi_ddiv` then don't also export
   `__divdf3`.
 * Use `inline` for helper functions instead of making inline calls at
   callsites.
2022-06-17 18:10:00 -07:00

56 lines
1.6 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const arch = builtin.cpu.arch;
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(__fabsh, .{ .name = "__fabsh", .linkage = common.linkage });
@export(fabsf, .{ .name = "fabsf", .linkage = common.linkage });
@export(fabs, .{ .name = "fabs", .linkage = common.linkage });
@export(__fabsx, .{ .name = "__fabsx", .linkage = common.linkage });
const fabsq_sym_name = if (common.want_ppc_abi) "fabsf128" else "fabsq";
@export(fabsq, .{ .name = fabsq_sym_name, .linkage = common.linkage });
@export(fabsl, .{ .name = "fabsl", .linkage = common.linkage });
}
pub fn __fabsh(a: f16) callconv(.C) f16 {
return generic_fabs(a);
}
pub fn fabsf(a: f32) callconv(.C) f32 {
return generic_fabs(a);
}
pub fn fabs(a: f64) callconv(.C) f64 {
return generic_fabs(a);
}
pub fn __fabsx(a: f80) callconv(.C) f80 {
return generic_fabs(a);
}
pub fn fabsq(a: f128) callconv(.C) f128 {
return generic_fabs(a);
}
pub fn fabsl(x: c_longdouble) callconv(.C) c_longdouble {
switch (@typeInfo(c_longdouble).Float.bits) {
16 => return __fabsh(x),
32 => return fabsf(x),
64 => return fabs(x),
80 => return __fabsx(x),
128 => return fabsq(x),
else => @compileError("unreachable"),
}
}
inline fn generic_fabs(x: anytype) @TypeOf(x) {
const T = @TypeOf(x);
const TBits = std.meta.Int(.unsigned, @typeInfo(T).Float.bits);
const float_bits = @bitCast(TBits, x);
const remove_sign = ~@as(TBits, 0) >> 1;
return @bitCast(T, float_bits & remove_sign);
}