Skip to content

Commit 586c7ea

Browse files
andrewrkkubkon
authored andcommitted
compiler-rt: math functions reorg
* 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`.
1 parent 0026e5d commit 586c7ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2622
-2874
lines changed

CMakeLists.txt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ set(ZIG_STAGE2_SOURCES
445445
"${CMAKE_SOURCE_DIR}/lib/std/math/big.zig"
446446
"${CMAKE_SOURCE_DIR}/lib/std/math/big/int.zig"
447447
"${CMAKE_SOURCE_DIR}/lib/std/math/float.zig"
448-
"${CMAKE_SOURCE_DIR}/lib/std/math/floor.zig"
449448
"${CMAKE_SOURCE_DIR}/lib/std/math/frexp.zig"
450449
"${CMAKE_SOURCE_DIR}/lib/std/math/isinf.zig"
451450
"${CMAKE_SOURCE_DIR}/lib/std/math/isnan.zig"
@@ -482,20 +481,40 @@ set(ZIG_STAGE2_SOURCES
482481
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt.zig"
483482
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/absv.zig"
484483
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/addXf3.zig"
484+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/addo.zig"
485+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/arm.zig"
485486
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/atomics.zig"
487+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/aulldiv.zig"
488+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/aullrem.zig"
486489
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/bswap.zig"
490+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/ceil.zig"
487491
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/clear_cache.zig"
488492
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/cmp.zig"
489493
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/compareXf2.zig"
494+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/cos.zig"
490495
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/count0bits.zig"
491496
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divdf3.zig"
492497
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divsf3.zig"
493498
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divtf3.zig"
494499
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divti3.zig"
500+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/divxf3.zig"
501+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/emutls.zig"
502+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/exp.zig"
503+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/exp2.zig"
495504
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/extendXfYf2.zig"
505+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/extend_f80.zig"
506+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fabs.zig"
496507
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fixXfYi.zig"
497508
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/floatXiYf.zig"
509+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/floor.zig"
510+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fma.zig"
511+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmax.zig"
512+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmin.zig"
513+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/fmod.zig"
498514
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/int.zig"
515+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log.zig"
516+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log10.zig"
517+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/log2.zig"
499518
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/modti3.zig"
500519
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/mulXf3.zig"
501520
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/muldi3.zig"
@@ -507,9 +526,22 @@ set(ZIG_STAGE2_SOURCES
507526
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/os_version_check.zig"
508527
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/parity.zig"
509528
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/popcount.zig"
529+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2.zig"
530+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2_large.zig"
531+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/rem_pio2f.zig"
532+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/round.zig"
510533
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/shift.zig"
534+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sin.zig"
535+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sincos.zig"
536+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sparc.zig"
537+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/sqrt.zig"
511538
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/stack_probe.zig"
539+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/subo.zig"
540+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/tan.zig"
541+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trig.zig"
542+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trunc.zig"
512543
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/truncXfYf2.zig"
544+
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/trunc_f80.zig"
513545
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivmod.zig"
514546
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivmodti4.zig"
515547
"${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/udivti3.zig"

lib/std/fmt/errol.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn errolSlow(val: f64, buffer: []u8) FloatDecimal {
113113
// normalize the midpoint
114114

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

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

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

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

193193
buffer[buf_index] = mdig + '0';

lib/std/math.zig

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn approxEqAbs(comptime T: type, x: T, y: T, tolerance: T) bool {
138138
if (isNan(x) or isNan(y))
139139
return false;
140140

141-
return fabs(x - y) <= tolerance;
141+
return @fabs(x - y) <= tolerance;
142142
}
143143

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

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

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

234234
pub const isNan = @import("math/isnan.zig").isNan;
235235
pub const isSignalNan = @import("math/isnan.zig").isSignalNan;
236-
pub const fabs = @import("math/fabs.zig").fabs;
237-
pub const ceil = @import("math/ceil.zig").ceil;
238-
pub const floor = @import("math/floor.zig").floor;
239-
pub const trunc = @import("math/trunc.zig").trunc;
240-
pub const round = @import("math/round.zig").round;
241236
pub const frexp = @import("math/frexp.zig").frexp;
242237
pub const Frexp = @import("math/frexp.zig").Frexp;
243238
pub const modf = @import("math/modf.zig").modf;
@@ -261,25 +256,19 @@ pub const asin = @import("math/asin.zig").asin;
261256
pub const atan = @import("math/atan.zig").atan;
262257
pub const atan2 = @import("math/atan2.zig").atan2;
263258
pub const hypot = @import("math/hypot.zig").hypot;
264-
pub const exp = @import("math/exp.zig").exp;
265-
pub const exp2 = @import("math/exp2.zig").exp2;
266259
pub const expm1 = @import("math/expm1.zig").expm1;
267260
pub const ilogb = @import("math/ilogb.zig").ilogb;
268261
pub const ln = @import("math/ln.zig").ln;
269262
pub const log = @import("math/log.zig").log;
270263
pub const log2 = @import("math/log2.zig").log2;
271264
pub const log10 = @import("math/log10.zig").log10;
272265
pub const log1p = @import("math/log1p.zig").log1p;
273-
pub const fma = @import("math/fma.zig").fma;
274266
pub const asinh = @import("math/asinh.zig").asinh;
275267
pub const acosh = @import("math/acosh.zig").acosh;
276268
pub const atanh = @import("math/atanh.zig").atanh;
277269
pub const sinh = @import("math/sinh.zig").sinh;
278270
pub const cosh = @import("math/cosh.zig").cosh;
279271
pub const tanh = @import("math/tanh.zig").tanh;
280-
pub const cos = @import("math/cos.zig").cos;
281-
pub const sin = @import("math/sin.zig").sin;
282-
pub const tan = @import("math/tan.zig").tan;
283272

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

719-
pub const absFloat = fabs;
720-
721-
test "absFloat" {
722-
try testAbsFloat();
723-
comptime try testAbsFloat();
724-
}
725-
fn testAbsFloat() !void {
726-
try testing.expect(absFloat(@as(f32, -10.05)) == 10.05);
727-
try testing.expect(absFloat(@as(f32, 10.05)) == 10.05);
728-
}
729-
730708
/// Divide numerator by denominator, rounding toward zero. Returns an
731709
/// error on overflow or when denominator is zero.
732710
pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
@@ -1400,11 +1378,6 @@ test "order.compare" {
14001378
try testing.expect(order(1, 0).compare(.neq));
14011379
}
14021380

1403-
test "comptime sin and ln" {
1404-
const v = comptime (sin(@as(f32, 1)) + ln(@as(f32, 5)));
1405-
try testing.expect(v == sin(@as(f32, 1)) + ln(@as(f32, 5)));
1406-
}
1407-
14081381
/// Returns a mask of all ones if value is true,
14091382
/// and a mask of all zeroes if value is false.
14101383
/// Compiles to one instruction for register sized integers.

lib/std/math/acos.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ fn acos32(x: f32) f32 {
6464
// x < -0.5
6565
if (hx >> 31 != 0) {
6666
const z = (1 + x) * 0.5;
67-
const s = math.sqrt(z);
67+
const s = @sqrt(z);
6868
const w = r32(z) * s - pio2_lo;
6969
return 2 * (pio2_hi - (s + w));
7070
}
7171

7272
// x > 0.5
7373
const z = (1.0 - x) * 0.5;
74-
const s = math.sqrt(z);
74+
const s = @sqrt(z);
7575
const jx = @bitCast(u32, s);
7676
const df = @bitCast(f32, jx & 0xFFFFF000);
7777
const c = (z - df * df) / (s + df);
@@ -133,14 +133,14 @@ fn acos64(x: f64) f64 {
133133
// x < -0.5
134134
if (hx >> 31 != 0) {
135135
const z = (1.0 + x) * 0.5;
136-
const s = math.sqrt(z);
136+
const s = @sqrt(z);
137137
const w = r64(z) * s - pio2_lo;
138138
return 2 * (pio2_hi - (s + w));
139139
}
140140

141141
// x > 0.5
142142
const z = (1.0 - x) * 0.5;
143-
const s = math.sqrt(z);
143+
const s = @sqrt(z);
144144
const jx = @bitCast(u64, s);
145145
const df = @bitCast(f64, jx & 0xFFFFFFFF00000000);
146146
const c = (z - df * df) / (s + df);

lib/std/math/acosh.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ fn acosh32(x: f32) f32 {
2929

3030
// |x| < 2, invalid if x < 1 or nan
3131
if (i < 0x3F800000 + (1 << 23)) {
32-
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
32+
return math.log1p(x - 1 + @sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
3333
}
3434
// |x| < 0x1p12
3535
else if (i < 0x3F800000 + (12 << 23)) {
36-
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
36+
return @log(2 * x - 1 / (x + @sqrt(x * x - 1)));
3737
}
3838
// |x| >= 0x1p12
3939
else {
40-
return math.ln(x) + 0.693147180559945309417232121458176568;
40+
return @log(x) + 0.693147180559945309417232121458176568;
4141
}
4242
}
4343

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

4848
// |x| < 2, invalid if x < 1 or nan
4949
if (e < 0x3FF + 1) {
50-
return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
50+
return math.log1p(x - 1 + @sqrt((x - 1) * (x - 1) + 2 * (x - 1)));
5151
}
5252
// |x| < 0x1p26
5353
else if (e < 0x3FF + 26) {
54-
return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1)));
54+
return @log(2 * x - 1 / (x + @sqrt(x * x - 1)));
5555
}
5656
// |x| >= 0x1p26 or nan
5757
else {
58-
return math.ln(x) + 0.693147180559945309417232121458176568;
58+
return @log(x) + 0.693147180559945309417232121458176568;
5959
}
6060
}
6161

lib/std/math/asin.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ fn asin32(x: f32) f32 {
6060
}
6161

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

6767
if (hx >> 31 != 0) {
@@ -119,8 +119,8 @@ fn asin64(x: f64) f64 {
119119
}
120120

121121
// 1 > |x| >= 0.5
122-
const z = (1 - math.fabs(x)) * 0.5;
123-
const s = math.sqrt(z);
122+
const z = (1 - @fabs(x)) * 0.5;
123+
const s = @sqrt(z);
124124
const r = r64(z);
125125
var fx: f64 = undefined;
126126

lib/std/math/asinh.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ fn asinh32(x: f32) f32 {
3939

4040
// |x| >= 0x1p12 or inf or nan
4141
if (i >= 0x3F800000 + (12 << 23)) {
42-
rx = math.ln(rx) + 0.69314718055994530941723212145817656;
42+
rx = @log(rx) + 0.69314718055994530941723212145817656;
4343
}
4444
// |x| >= 2
4545
else if (i >= 0x3F800000 + (1 << 23)) {
46-
rx = math.ln(2 * x + 1 / (math.sqrt(x * x + 1) + x));
46+
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
4747
}
4848
// |x| >= 0x1p-12, up to 1.6ulp error
4949
else if (i >= 0x3F800000 - (12 << 23)) {
50-
rx = math.log1p(x + x * x / (math.sqrt(x * x + 1) + 1));
50+
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
5151
}
5252
// |x| < 0x1p-12, inexact if x != 0
5353
else {
@@ -70,15 +70,15 @@ fn asinh64(x: f64) f64 {
7070

7171
// |x| >= 0x1p26 or inf or nan
7272
if (e >= 0x3FF + 26) {
73-
rx = math.ln(rx) + 0.693147180559945309417232121458176568;
73+
rx = @log(rx) + 0.693147180559945309417232121458176568;
7474
}
7575
// |x| >= 2
7676
else if (e >= 0x3FF + 1) {
77-
rx = math.ln(2 * x + 1 / (math.sqrt(x * x + 1) + x));
77+
rx = @log(2 * x + 1 / (@sqrt(x * x + 1) + x));
7878
}
7979
// |x| >= 0x1p-12, up to 1.6ulp error
8080
else if (e >= 0x3FF - 26) {
81-
rx = math.log1p(x + x * x / (math.sqrt(x * x + 1) + 1));
81+
rx = math.log1p(x + x * x / (@sqrt(x * x + 1) + 1));
8282
}
8383
// |x| < 0x1p-12, inexact if x != 0
8484
else {

lib/std/math/atan.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn atan32(x_: f32) f32 {
7373
}
7474
id = null;
7575
} else {
76-
x = math.fabs(x);
76+
x = @fabs(x);
7777
// |x| < 1.1875
7878
if (ix < 0x3F980000) {
7979
// 7/16 <= |x| < 11/16
@@ -171,7 +171,7 @@ fn atan64(x_: f64) f64 {
171171
}
172172
id = null;
173173
} else {
174-
x = math.fabs(x);
174+
x = @fabs(x);
175175
// |x| < 1.1875
176176
if (ix < 0x3FF30000) {
177177
// 7/16 <= |x| < 11/16

lib/std/math/atan2.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn atan2_32(y: f32, x: f32) f32 {
108108
if ((m & 2) != 0 and iy + (26 << 23) < ix) {
109109
break :z 0.0;
110110
} else {
111-
break :z math.atan(math.fabs(y / x));
111+
break :z math.atan(@fabs(y / x));
112112
}
113113
};
114114

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

lib/std/math/complex.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn Complex(comptime T: type) type {
115115

116116
/// Returns the magnitude of a complex number.
117117
pub fn magnitude(self: Self) T {
118-
return math.sqrt(self.re * self.re + self.im * self.im);
118+
return @sqrt(self.re * self.re + self.im * self.im);
119119
}
120120
};
121121
}

lib/std/math/complex/atan.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn atan32(z: Complex(f32)) Complex(f32) {
6666

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

7272
fn redupif64(x: f64) f64 {
@@ -115,7 +115,7 @@ fn atan64(z: Complex(f64)) Complex(f64) {
115115

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

121121
const epsilon = 0.0001;

0 commit comments

Comments
 (0)