Skip to content

compiler-rt math functions reorg #11532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 15 additions & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -8026,7 +8026,7 @@ fn func(y: *i32) void {
only rounds once, and is thus more accurate.
</p>
<p>
Supports Floats and Vectors of floats.
Supports {#link|Floats#} and {#link|Vectors#} of floats.
</p>
{#header_close#}

Expand Down Expand Up @@ -9440,6 +9440,7 @@ fn doTheTest() !void {
<a href="https://github.com/ziglang/zig/issues/4026">some float operations are not yet implemented for all float types</a>.
</p>
{#header_close#}

{#header_open|@cos#}
<pre>{#syntax#}@cos(value: anytype) @TypeOf(value){#endsyntax#}</pre>
<p>
Expand All @@ -9451,6 +9452,19 @@ fn doTheTest() !void {
<a href="https://github.com/ziglang/zig/issues/4026">some float operations are not yet implemented for all float types</a>.
</p>
{#header_close#}

{#header_open|@tan#}
<pre>{#syntax#}@tan(value: anytype) @TypeOf(value){#endsyntax#}</pre>
<p>
Tangent trigonometric function on a floating point number.
Uses a dedicated hardware instruction when available.
</p>
<p>
Supports {#link|Floats#} and {#link|Vectors#} of floats, with the caveat that
<a href="https://github.com/ziglang/zig/issues/4026">some float operations are not yet implemented for all float types</a>.
</p>
{#header_close#}

{#header_open|@exp#}
<pre>{#syntax#}@exp(value: anytype) @TypeOf(value){#endsyntax#}</pre>
<p>
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
2 changes: 1 addition & 1 deletion lib/std/json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ fn parseInternal(
if (numberToken.is_integer)
return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);
const float = try std.fmt.parseFloat(f128, numberToken.slice(tokens.slice, tokens.i - 1));
if (std.math.round(float) != float) return error.InvalidNumber;
if (@round(float) != float) return error.InvalidNumber;
if (float > std.math.maxInt(T) or float < std.math.minInt(T)) return error.Overflow;
return @floatToInt(T, float);
},
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)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

math.ln and @log are not equivalent. ln is base e and log is base 10. You would need to divide by @log(e).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@log does refer to the natural logarithm. See https://ziglang.org/documentation/0.9.1/#log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I should have checked before commenting. Sorry for that!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still a fair point; we may want to play with the naming to make it less ambiguous. Although we are a bit limited by libc here since we do need to match the symbol names, and libc has log as natural log.

}
// |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
Loading