Skip to content

Fix: expose std.math.ldexp as C ldexp, scalbn and scalbln in compiler-rt to address LLVM release-mode simplification #24142

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lib/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ comptime {
_ = @import("compiler_rt/fmax.zig");
_ = @import("compiler_rt/fmin.zig");
_ = @import("compiler_rt/fmod.zig");
_ = @import("compiler_rt/ldexp.zig");
_ = @import("compiler_rt/scalbln.zig");
_ = @import("compiler_rt/scalbn.zig");
_ = @import("compiler_rt/log.zig");
_ = @import("compiler_rt/log10.zig");
_ = @import("compiler_rt/log2.zig");
Expand Down
70 changes: 70 additions & 0 deletions lib/compiler_rt/ldexp.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const std = @import("std");
const expect = std.testing.expect;
const math = std.math;
const common = @import("common.zig");

comptime {
@export(&ldexp, .{ .name = "ldexp", .linkage = common.linkage, .visibility = common.visibility });
@export(&ldexpf, .{ .name = "ldexpf", .linkage = common.linkage, .visibility = common.visibility });
@export(&ldexpl, .{ .name = "ldexpl", .linkage = common.linkage, .visibility = common.visibility });
}

pub fn ldexp(x: f64, n: i32) callconv(.c) f64 {
return math.ldexp(x, n);
}

test "ldexp" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/sanity/ldexp.h
try expect(ldexp(-0x1.02239f3c6a8f1p+3, -2) == -0x1.02239f3c6a8f1p+1);
}

test "ldexp.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/special/ldexp.h
try expect(math.isNan(ldexp(math.nan(f64), 0)));
try expect(math.isPositiveInf(ldexp(math.inf(f64), 0)));
}

pub fn ldexpf(x: f32, n: i32) callconv(.c) f32 {
return math.ldexp(x, n);
}

test "ldexpf" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/sanity/ldexpf.h
try expect(ldexpf(-0x1.0223ap+3, -2) == -0x1.0223ap+1);
}

test "ldexpf.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/special/ldexpf.h
try expect(math.isNan(ldexpf(math.nan(f32), 0)));
try expect(math.isPositiveInf(ldexpf(math.inf(f32), 0)));
}

pub fn ldexpl(x: c_longdouble, n: i32) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return math.ldexp(@as(f16, x), n),
32 => return math.ldexp(@as(f32, x), n),
64 => return math.ldexp(@as(f64, x), n),
80 => return math.ldexp(@as(f80, x), n),
128 => return math.ldexp(@as(f128, x), n),
else => @compileError("unreachable"),
}
}

test "ldexpl" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/sanity/ldexpl.h
const x: c_longdouble = -0x1.02239f3c6a8f13dep+3;
const expected: c_longdouble = -0x1.02239f3c6a8f13dep+1;
try expect(ldexpl(x, -2) == expected);
}

test "ldexpl.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/special/ldexpl.h
try expect(math.isNan(ldexpl(math.nan(c_longdouble), 0)));
try expect(math.isPositiveInf(ldexpl(math.inf(c_longdouble), 0)));
}
97 changes: 97 additions & 0 deletions lib/compiler_rt/scalbln.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const std = @import("std");
const expect = std.testing.expect;
const math = std.math;
const common = @import("common.zig");

comptime {
@export(&scalbln, .{ .name = "scalbln", .linkage = common.linkage, .visibility = common.visibility });
@export(&scalblnf, .{ .name = "scalblnf", .linkage = common.linkage, .visibility = common.visibility });
@export(&scalblnl, .{ .name = "scalblnl", .linkage = common.linkage, .visibility = common.visibility });
}

pub fn scalbln(x: f64, n: c_long) callconv(.c) f64 {
// mirror musl implementation - clamp c_long to i32
const clamped_n: i32 = @intCast(math.clamp(n, math.minInt(i32), math.maxInt(i32)));
return math.ldexp(x, clamped_n);
}
Comment on lines +12 to +16
Copy link
Member

Choose a reason for hiding this comment

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

Is this clamping behavior part of the C standard? We need to be careful about whether it should apply regardless of target.


test "scalbln" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/sanity/scalbln.h
try expect(scalbln(-0x1.02239f3c6a8f1p+3, -2) == -0x1.02239f3c6a8f1p+1);
try expect(scalbln(0x1.161868e18bc67p+2, -1) == 0x1.161868e18bc67p+1);
try expect(scalbln(0x1.288bbb0d6a1e6p+3, 2) == 0x1.288bbb0d6a1e6p+5);
try expect(scalbln(0x1.52efd0cd80497p-1, 3) == 0x1.52efd0cd80497p+2);
try expect(scalbln(0x1.1f9ef934745cbp-1, 5) == 0x1.1f9ef934745cbp+4);
try expect(scalbln(0x1.8c5db097f7442p-1, 6) == 0x1.8c5db097f7442p+5);
}

test "scalbln.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/special/scalbln.h
try expect(math.isNan(scalbln(math.nan(f64), 0)));
try expect(math.isPositiveInf(scalbln(math.inf(f64), 0)));
try expect(scalbln(0x1p+0, 0) == 0x1p+0);
try expect(scalbln(0x1p+0, 1) == 0x1p+1);
try expect(scalbln(0x1p+0, -1) == 0x1p-1);
}

pub fn scalblnf(x: f32, n: c_long) callconv(.c) f32 {
const clamped_n: i32 = @intCast(math.clamp(n, math.minInt(i32), math.maxInt(i32)));
return math.ldexp(x, clamped_n);
}

test "scalblnf" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/sanity/scalbnf.h
try expect(scalblnf(-0x1.0223ap+3, -2) == -0x1.0223ap+1);
try expect(scalblnf(0x1.161868p+2, -1) == 0x1.161868p+1);
try expect(scalblnf(0x1.52efdp-1, 3) == 0x1.52efdp+2);
try expect(scalblnf(0x1.1f9efap-1, 5) == 0x1.1f9efap+4);
try expect(scalblnf(0x1.8c5dbp-1, 6) == 0x1.8c5dbp+5);
}

test "scalblnf.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:/src/math/special/scalbnf.h
try expect(math.isNan(scalblnf(math.nan(f32), 0)));
try expect(math.isPositiveInf(scalblnf(math.inf(f32), 0)));
try expect(scalblnf(0x1p+0, 0) == 0x1p+0);
try expect(scalblnf(0x1p+0, 1) == 0x1p+1);
try expect(scalblnf(0x1p+0, -1) == 0x1p-1);
}

pub fn scalblnl(x: c_longdouble, n: c_long) callconv(.c) c_longdouble {
const clamped_n: i32 = @intCast(math.clamp(n, math.minInt(i32), math.maxInt(i32)));
return math.ldexp(x, clamped_n);
}

test "scalblnl" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:src/math/sanity/scalblnl.h
const cases = [_]struct { x: c_longdouble, n: c_int, expected: c_longdouble }{
.{ .x = -0x1.02239f3c6a8f13dep+3, .n = -2, .expected = -0x1.02239f3c6a8f13dep+1 },
.{ .x = 0x1.161868e18bc67782p+2, .n = -1, .expected = 0x1.161868e18bc67782p+1 },
.{ .x = -0x1.0c34b3e01e6e682cp+3, .n = 0, .expected = -0x1.0c34b3e01e6e682cp+3 },
.{ .x = -0x1.a206f0a19dcc3948p+2, .n = 1, .expected = -0x1.a206f0a19dcc3948p+3 },
.{ .x = 0x1.288bbb0d6a1e5bdap+3, .n = 2, .expected = 0x1.288bbb0d6a1e5bdap+5 },
.{ .x = 0x1.52efd0cd80496a5ap-1, .n = 3, .expected = 0x1.52efd0cd80496a5ap+2 },
.{ .x = -0x1.a05cc754481d0bdp-2, .n = 4, .expected = -0x1.a05cc754481d0bdp+2 },
.{ .x = 0x1.1f9ef934745cad6p-1, .n = 5, .expected = 0x1.1f9ef934745cad6p+4 },
.{ .x = 0x1.8c5db097f744257ep-1, .n = 6, .expected = 0x1.8c5db097f744257ep+5 },
.{ .x = -0x1.5b86ea8118a0e2bcp-1, .n = 7, .expected = -0x1.5b86ea8118a0e2bcp+6 },
};
for (cases) |case| {
try expect(scalblnl(case.x, case.n) == case.expected);
}
}

test "scalblnl.special" {
// Ported from libc-test
// https://repo.or.cz/libc-test.git/blob/HEAD:src/math/special/scalblnl.h
try expect(math.isNan(scalblnl(math.nan(c_longdouble), 0)));
try expect(math.isPositiveInf(scalblnl(math.inf(c_longdouble), 0)));
try expect(scalblnf(0x1p+0, 0) == 0x1p+0);
try expect(scalblnf(0x1p+0, 1) == 0x1p+1);
try expect(scalblnf(0x1p+0, -1) == 0x1p-1);
}
22 changes: 22 additions & 0 deletions lib/compiler_rt/scalbn.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const std = @import("std");
const expect = std.testing.expect;
const common = @import("common.zig");
const ldexp = @import("ldexp.zig");

comptime {
@export(&scalbn, .{ .name = "scalbn", .linkage = common.linkage, .visibility = common.visibility });
@export(&scalbnf, .{ .name = "scalbnf", .linkage = common.linkage, .visibility = common.visibility });
@export(&scalbnl, .{ .name = "scalbnl", .linkage = common.linkage, .visibility = common.visibility });
}

pub fn scalbn(x: f64, n: i32) callconv(.c) f64 {
return ldexp.ldexp(x, n);
}

pub fn scalbnf(x: f32, n: i32) callconv(.c) f32 {
return ldexp.ldexpf(x, n);
}

pub fn scalbnl(x: c_longdouble, n: i32) callconv(.c) c_longdouble {
return ldexp.ldexpl(x, n);
}
16 changes: 0 additions & 16 deletions lib/libc/mingw/math/arm-common/ldexpl.c

This file was deleted.

13 changes: 0 additions & 13 deletions lib/libc/mingw/math/ldexpf.c

This file was deleted.

23 changes: 0 additions & 23 deletions lib/libc/mingw/math/x86/ldexp.c

This file was deleted.

23 changes: 0 additions & 23 deletions lib/libc/mingw/math/x86/ldexpl.c

This file was deleted.

41 changes: 0 additions & 41 deletions lib/libc/mingw/math/x86/scalbn.S

This file was deleted.

40 changes: 0 additions & 40 deletions lib/libc/mingw/math/x86/scalbnf.S

This file was deleted.

Loading