Skip to content

Fix: expose std.math.ldexp as C ldexp in compiler-rt to address LLVM release-mode simplification #24006

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

Closed
wants to merge 16 commits into from
Closed
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
@@ -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");
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)));
}
68 changes: 68 additions & 0 deletions lib/compiler_rt/scalbln.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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);
}

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

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

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/ldexpf.h
try expect(scalblnf(-0x1.0223ap+3, -2) == -0x1.0223ap+1);
}

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

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/ldexpl.h
const x: c_longdouble = -0x1.02239f3c6a8f13dep+3;
const expected: c_longdouble = -0x1.02239f3c6a8f13dep+1;
try expect(scalblnl(x, -2) == expected);
}

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

comptime {
@export(&ldexp.ldexp, .{ .name = "scalbn", .linkage = common.linkage, .visibility = common.visibility });
@export(&ldexp.ldexpf, .{ .name = "scalbnf", .linkage = common.linkage, .visibility = common.visibility });
@export(&ldexp.ldexpl, .{ .name = "scalbnl", .linkage = common.linkage, .visibility = common.visibility });
}
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.

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

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/ldexp.s

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/ldexpf.s

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/ldexpl.s

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/scalbln.s

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/scalblnf.s

This file was deleted.

1 change: 0 additions & 1 deletion lib/libc/musl/src/math/i386/scalblnl.s

This file was deleted.

Loading
Oops, something went wrong.
Loading
Oops, something went wrong.