Skip to content

Commit

Permalink
compiler-rt: math functions reorg
Browse files Browse the repository at this point in the history
 * 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`.
  • Loading branch information
andrewrk committed Apr 27, 2022
1 parent 6f4343b commit 41dd2be
Show file tree
Hide file tree
Showing 66 changed files with 2,622 additions and 2,874 deletions.
34 changes: 33 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/math/big.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/float.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig"
"${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig"
Expand Down Expand Up @@ -482,20 +481,40 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/absv.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/addXf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/addo.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/arm.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/atomics.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/aulldiv.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/aullrem.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/bswap.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/ceil.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/clear_cache.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/cmp.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/compareXf2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/cos.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/count0bits.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divdf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divsf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divtf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divti3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divxf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/emutls.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/exp.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/exp2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/extendXfYf2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/extend_f80.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fabs.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fixXfYi.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/floatXiYf.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/floor.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fma.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmax.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmin.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmod.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/int.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log10.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/modti3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/mulXf3.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/muldi3.zig"
Expand All @@ -507,9 +526,22 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/os_version_check.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/parity.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/popcount.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2_large.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2f.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/round.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/shift.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sin.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sincos.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sparc.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sqrt.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/stack_probe.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/subo.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/tan.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trig.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trunc.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/truncXfYf2.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trunc_f80.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivmod.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivmodti4.zig"
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivti3.zig"
Expand Down
8 changes: 4 additions & 4 deletions lib/std/fmt/errol.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn errolSlow(val: f64, buffer: []u8) FloatDecimal {
// normalize the midpoint

const e = math.frexp(val).exponent;
var exp = @floatToInt(i16, math.floor(307 + @intToFloat(f64, e) * 0.30103));
var exp = @floatToInt(i16, @floor(307 + @intToFloat(f64, e) * 0.30103));
if (exp < 20) {
exp = 20;
} else if (@intCast(usize, exp) >= lookup_table.len) {
Expand Down Expand Up @@ -170,10 +170,10 @@ fn errolSlow(val: f64, buffer: []u8) FloatDecimal {
// digit generation
var buf_index: usize = 0;
while (true) {
var hdig = @floatToInt(u8, math.floor(high.val));
var hdig = @floatToInt(u8, @floor(high.val));
if ((high.val == @intToFloat(f64, hdig)) and (high.off < 0)) hdig -= 1;

var ldig = @floatToInt(u8, math.floor(low.val));
var ldig = @floatToInt(u8, @floor(low.val));
if ((low.val == @intToFloat(f64, ldig)) and (low.off < 0)) ldig -= 1;

if (ldig != hdig) break;
Expand All @@ -187,7 +187,7 @@ fn errolSlow(val: f64, buffer: []u8) FloatDecimal {
}

const tmp = (high.val + low.val) / 2.0;
var mdig = @floatToInt(u8, math.floor(tmp + 0.5));
var mdig = @floatToInt(u8, @floor(tmp + 0.5));
if ((@intToFloat(f64, mdig) - tmp) == 0.5 and (mdig & 0x1) != 0) mdig -= 1;

buffer[buf_index] = mdig + '0';
Expand Down
31 changes: 2 additions & 29 deletions lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool {
if (isNan(x) or isNan(y))
return false;

return fabs(x - y) <= tolerance;
return @fabs(x - y) <= tolerance;
}

/// Performs an approximate comparison of two floating point values `x` and `y`.
Expand Down Expand Up @@ -166,7 +166,7 @@ pub fn approxEqRel(comptime T: type, x: T, y: T, tolerance: T) bool {
if (isNan(x) or isNan(y))
return false;

return fabs(x - y) <= max(fabs(x), fabs(y)) * tolerance;
return @fabs(x - y) <= max(@fabs(x), @fabs(y)) * tolerance;
}

pub fn approxEq(comptime T: type, x: T, y: T, tolerance: T) bool {
Expand Down Expand Up @@ -233,11 +233,6 @@ pub fn raiseDivByZero() void {

pub const isNan = @import("math/isnan.zig").isNan;
pub const isSignalNan = @import("math/isnan.zig").isSignalNan;
pub const fabs = @import("math/fabs.zig").fabs;
pub const ceil = @import("math/ceil.zig").ceil;
pub const floor = @import("math/floor.zig").floor;
pub const trunc = @import("math/trunc.zig").trunc;
pub const round = @import("math/round.zig").round;
pub const frexp = @import("math/frexp.zig").frexp;
pub const Frexp = @import("math/frexp.zig").Frexp;
pub const modf = @import("math/modf.zig").modf;
Expand All @@ -261,25 +256,19 @@ pub const asin = @import("math/asin.zig").asin;
pub const atan = @import("math/atan.zig").atan;
pub const atan2 = @import("math/atan2.zig").atan2;
pub const hypot = @import("math/hypot.zig").hypot;
pub const exp = @import("math/exp.zig").exp;
pub const exp2 = @import("math/exp2.zig").exp2;
pub const expm1 = @import("math/expm1.zig").expm1;
pub const ilogb = @import("math/ilogb.zig").ilogb;
pub const ln = @import("math/ln.zig").ln;
pub const log = @import("math/log.zig").log;
pub const log2 = @import("math/log2.zig").log2;
pub const log10 = @import("math/log10.zig").log10;
pub const log1p = @import("math/log1p.zig").log1p;
pub const fma = @import("math/fma.zig").fma;
pub const asinh = @import("math/asinh.zig").asinh;
pub const acosh = @import("math/acosh.zig").acosh;
pub const atanh = @import("math/atanh.zig").atanh;
pub const sinh = @import("math/sinh.zig").sinh;
pub const cosh = @import("math/cosh.zig").cosh;
pub const tanh = @import("math/tanh.zig").tanh;
pub const cos = @import("math/cos.zig").cos;
pub const sin = @import("math/sin.zig").sin;
pub const tan = @import("math/tan.zig").tan;

pub const complex = @import("math/complex.zig");
pub const Complex = complex.Complex;
Expand Down Expand Up @@ -716,17 +705,6 @@ fn testAbsInt() !void {
try testing.expect((absInt(@as(i32, 10)) catch unreachable) == 10);
}

pub const absFloat = fabs;

test "absFloat" {
try testAbsFloat();
comptime try testAbsFloat();
}
fn testAbsFloat() !void {
try testing.expect(absFloat(@as(f32, -10.05)) == 10.05);
try testing.expect(absFloat(@as(f32, 10.05)) == 10.05);
}

/// Divide numerator by denominator, rounding toward zero. Returns an
/// error on overflow or when denominator is zero.
pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
Expand Down Expand Up @@ -1400,11 +1378,6 @@ test "order.compare" {
try testing.expect(order(1, 0).compare(.neq));
}

test "comptime sin and ln" {
const v = comptime (sin(@as(f32, 1)) + ln(@as(f32, 5)));
try testing.expect(v == sin(@as(f32, 1)) + ln(@as(f32, 5)));
}

/// Returns a mask of all ones if value is true,
/// and a mask of all zeroes if value is false.
/// Compiles to one instruction for register sized integers.
Expand Down
8 changes: 4 additions & 4 deletions lib/std/math/acos.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ fn acos32(x: f32) f32 {
// x < -0.5
if (hx >> 31 != 0) {
const z = (1 + x) * 0.5;
const s = math.sqrt(z);
const s = @sqrt(z);
const w = r32(z) * s - pio2_lo;
return 2 * (pio2_hi - (s + w));
}

// x > 0.5
const z = (1.0 - x) * 0.5;
const s = math.sqrt(z);
const s = @sqrt(z);
const jx = @bitCast(u32, s);
const df = @bitCast(f32, jx & 0xFFFFF000);
const c = (z - df * df) / (s + df);
Expand Down Expand Up @@ -133,14 +133,14 @@ fn acos64(x: f64) f64 {
// x < -0.5
if (hx >> 31 != 0) {
const z = (1.0 + x) * 0.5;
const s = math.sqrt(z);
const s = @sqrt(z);
const w = r64(z) * s - pio2_lo;
return 2 * (pio2_hi - (s + w));
}

// x > 0.5
const z = (1.0 - x) * 0.5;
const s = math.sqrt(z);
const s = @sqrt(z);
const jx = @bitCast(u64, s);
const df = @bitCast(f64, jx & 0xFFFFFFFF00000000);
const c = (z - df * df) / (s + df);
Expand Down
12 changes: 6 additions & 6 deletions lib/std/math/acosh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ fn acosh32(x: f32) f32 {

// |x| < 2, invalid if x < 1 or nan
if (i < 0x3F800000 + (1 << 23)) {
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
return math.log1p(x - 1 + @sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
}
// |x| < 0x1p12
else if (i < 0x3F800000 + (12 << 23)) {
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
return @log(2 * x - 1 / (x + @sqrt(x * x - 1)));
}
// |x| >= 0x1p12
else {
return math.ln(x) + 0.693147180559945309417232121458176568;
return @log(x) + 0.693147180559945309417232121458176568;
}
}

Expand All @@ -47,15 +47,15 @@ fn acosh64(x: f64) f64 {

// |x| < 2, invalid if x < 1 or nan
if (e < 0x3FF + 1) {
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
return math.log1p(x - 1 + @sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
}
// |x| < 0x1p26
else if (e < 0x3FF + 26) {
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
return @log(2 * x - 1 / (x + @sqrt(x * x - 1)));
}
// |x| >= 0x1p26 or nan
else {
return math.ln(x) + 0.693147180559945309417232121458176568;
return @log(x) + 0.693147180559945309417232121458176568;
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/std/math/asin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ fn asin32(x: f32) f32 {
}

// 1 > |x| >= 0.5
const z = (1 - math.fabs(x)) * 0.5;
const s = math.sqrt(z);
const z = (1 - @fabs(x)) * 0.5;
const s = @sqrt(z);
const fx = pio2 - 2 * (s + s * r32(z));

if (hx >> 31 != 0) {
Expand Down Expand Up @@ -119,8 +119,8 @@ fn asin64(x: f64) f64 {
}

// 1 > |x| >= 0.5
const z = (1 - math.fabs(x)) * 0.5;
const s = math.sqrt(z);
const z = (1 - @fabs(x)) * 0.5;
const s = @sqrt(z);
const r = r64(z);
var fx: f64 = undefined;

Expand Down
12 changes: 6 additions & 6 deletions lib/std/math/asinh.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ fn asinh32(x: f32) f32 {

// |x| >= 0x1p12 or inf or nan
if (i >= 0x3F800000 + (12 << 23)) {
rx = math.ln(rx) + 0.69314718055994530941723212145817656;
rx = @log(rx) + 0.69314718055994530941723212145817656;
}
// |x| >= 2
else if (i >= 0x3F800000 + (1 << 23)) {
rx = math.ln(2 * x + 1 / (math.sqrt(x * x + 1) + x));
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
}
// |x| >= 0x1p-12, up to 1.6ulp error
else if (i >= 0x3F800000 - (12 << 23)) {
rx = math.log1p(x + x * x / (math.sqrt(x * x + 1) + 1));
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
}
// |x| < 0x1p-12, inexact if x != 0
else {
Expand All @@ -70,15 +70,15 @@ fn asinh64(x: f64) f64 {

// |x| >= 0x1p26 or inf or nan
if (e >= 0x3FF + 26) {
rx = math.ln(rx) + 0.693147180559945309417232121458176568;
rx = @log(rx) + 0.693147180559945309417232121458176568;
}
// |x| >= 2
else if (e >= 0x3FF + 1) {
rx = math.ln(2 * x + 1 / (math.sqrt(x * x + 1) + x));
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
}
// |x| >= 0x1p-12, up to 1.6ulp error
else if (e >= 0x3FF - 26) {
rx = math.log1p(x + x * x / (math.sqrt(x * x + 1) + 1));
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
}
// |x| < 0x1p-12, inexact if x != 0
else {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/math/atan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn atan32(x_: f32) f32 {
}
id = null;
} else {
x = math.fabs(x);
x = @fabs(x);
// |x| < 1.1875
if (ix < 0x3F980000) {
// 7/16 <= |x| < 11/16
Expand Down Expand Up @@ -171,7 +171,7 @@ fn atan64(x_: f64) f64 {
}
id = null;
} else {
x = math.fabs(x);
x = @fabs(x);
// |x| < 1.1875
if (ix < 0x3FF30000) {
// 7/16 <= |x| < 11/16
Expand Down
4 changes: 2 additions & 2 deletions lib/std/math/atan2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn atan2_32(y: f32, x: f32) f32 {
if ((m & 2) != 0 and iy + (26 << 23) < ix) {
break :z 0.0;
} else {
break :z math.atan(math.fabs(y / x));
break :z math.atan(@fabs(y / x));
}
};

Expand Down Expand Up @@ -198,7 +198,7 @@ fn atan2_64(y: f64, x: f64) f64 {
if ((m & 2) != 0 and iy +% (64 << 20) < ix) {
break :z 0.0;
} else {
break :z math.atan(math.fabs(y / x));
break :z math.atan(@fabs(y / x));
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/std/math/complex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn Complex(comptime T: type) type {

/// Returns the magnitude of a complex number.
pub fn magnitude(self: Self) T {
return math.sqrt(self.re * self.re + self.im * self.im);
return @sqrt(self.re * self.re + self.im * self.im);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/math/complex/atan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {

t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f32).init(w, 0.25 * math.ln(a));
return Complex(f32).init(w, 0.25 * @log(a));
}

fn redupif64(x: f64) f64 {
Expand Down Expand Up @@ -115,7 +115,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {

t = y + 1.0;
a = (x2 + (t * t)) / a;
return Complex(f64).init(w, 0.25 * math.ln(a));
return Complex(f64).init(w, 0.25 * @log(a));
}

const epsilon = 0.0001;
Expand Down
Loading

0 comments on commit 41dd2be

Please sign in to comment.