Merge pull request 'zigc: long double: call double function if long double and double are equivalent' (#31775) from rpkak/zig:zigc-long-double into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31775
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
This commit is contained in:
Andrew Kelley
2026-04-16 19:57:54 +02:00
22 changed files with 36 additions and 57 deletions
+28 -17
View File
@@ -116,13 +116,9 @@ fn atanf(x: f32) callconv(.c) f32 {
}
fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
return switch (@typeInfo(@TypeOf(x)).float.bits) {
16 => math.atan(@as(f16, @floatCast(x))),
32 => math.atan(@as(f32, @floatCast(x))),
64 => math.atan(@as(f64, @floatCast(x))),
80 => math.atan(@as(f80, @floatCast(x))),
128 => math.atan(@as(f128, @floatCast(x))),
else => unreachable,
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.atan(x),
else => math.atan(x),
};
}
@@ -143,7 +139,10 @@ fn copysignf(x: f32, y: f32) callconv(.c) f32 {
}
fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
return math.copysign(x, y);
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.copysign(x, y),
else => math.copysign(x, y),
};
}
fn cosh(x: f64) callconv(.c) f64 {
@@ -183,15 +182,18 @@ fn fdimf(x: f32, y: f32) callconv(.c) f32 {
}
fn fdiml(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
return fdimGeneric(c_longdouble, x, y);
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.fdim(x, y),
else => fdimGeneric(c_longdouble, x, y),
};
}
fn finite(x: f64) callconv(.c) c_int {
return if (math.isFinite(x)) 1 else 0;
return @intFromBool(math.isFinite(x));
}
fn finitef(x: f32) callconv(.c) c_int {
return if (math.isFinite(x)) 1 else 0;
return @intFromBool(math.isFinite(x));
}
fn frexpGeneric(comptime T: type, x: T, e: *c_int) T {
@@ -222,7 +224,10 @@ fn frexpf(x: f32, e: *c_int) callconv(.c) f32 {
}
fn frexpl(x: c_longdouble, e: *c_int) callconv(.c) c_longdouble {
return frexpGeneric(c_longdouble, x, e);
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.frexp(x, e),
else => frexpGeneric(c_longdouble, x, e),
};
}
fn hypot(x: f64, y: f64) callconv(.c) f64 {
@@ -234,19 +239,22 @@ fn hypotf(x: f32, y: f32) callconv(.c) f32 {
}
fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
return math.hypot(x, y);
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.hypot(x, y),
else => math.hypot(x, y),
};
}
fn isnan(x: f64) callconv(.c) c_int {
return if (math.isNan(x)) 1 else 0;
return @intFromBool(math.isNan(x));
}
fn isnanf(x: f32) callconv(.c) c_int {
return if (math.isNan(x)) 1 else 0;
return @intFromBool(math.isNan(x));
}
fn isnanl(x: c_longdouble) callconv(.c) c_int {
return if (math.isNan(x)) 1 else 0;
return @intFromBool(math.isNan(x));
}
fn lrint(x: f64) callconv(.c) c_long {
@@ -290,7 +298,10 @@ fn modff(x: f32, iptr: *f32) callconv(.c) f32 {
}
fn modfl(x: c_longdouble, iptr: *c_longdouble) callconv(.c) c_longdouble {
return modfGeneric(c_longdouble, x, iptr);
return switch (@typeInfo(c_longdouble).float.bits) {
64 => std.c.modf(x, iptr),
else => modfGeneric(c_longdouble, x, iptr),
};
}
fn testModf(comptime T: type) !void {
-2
View File
@@ -173,8 +173,6 @@ pub fn cosq(x: f128) callconv(.c) f128 {
pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return cosh(x),
32 => return cosf(x),
64 => return cos(x),
80 => return cosx(x),
128 => return cosq(x),
-2
View File
@@ -198,8 +198,6 @@ const expq = @import("exp_f128.zig").exp;
pub fn expl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __exph(x),
32 => return expf(x),
64 => return exp(x),
80 => return __expx(x),
128 => return expq(x),
-2
View File
@@ -165,8 +165,6 @@ pub const exp2q = @import("exp_f128.zig").exp2;
pub fn exp2l(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __exp2h(x),
32 => return exp2f(x),
64 => return exp2(x),
80 => return __exp2x(x),
128 => return exp2q(x),
-2
View File
@@ -38,8 +38,6 @@ pub fn fabsq(a: f128) callconv(.c) f128 {
pub fn fabsl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __fabsh(x),
32 => return fabsf(x),
64 => return fabs(x),
80 => return __fabsx(x),
128 => return fabsq(x),
-2
View File
@@ -151,8 +151,6 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.c) f128 {
pub fn fmal(x: c_longdouble, y: c_longdouble, z: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __fmah(x, y, z),
32 => return fmaf(x, y, z),
64 => return fma(x, y, z),
80 => return __fmax(x, y, z),
128 => return fmaq(x, y, z),
-2
View File
@@ -39,8 +39,6 @@ pub fn fmaxq(x: f128, y: f128) callconv(.c) f128 {
pub fn fmaxl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __fmaxh(x, y),
32 => return fmaxf(x, y),
64 => return fmax(x, y),
80 => return __fmaxx(x, y),
128 => return fmaxq(x, y),
-2
View File
@@ -39,8 +39,6 @@ pub fn fminq(x: f128, y: f128) callconv(.c) f128 {
pub fn fminl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __fminh(x, y),
32 => return fminf(x, y),
64 => return fmin(x, y),
80 => return __fminx(x, y),
128 => return fminq(x, y),
-2
View File
@@ -251,8 +251,6 @@ pub fn fmodq(a: f128, b: f128) callconv(.c) f128 {
pub fn fmodl(a: c_longdouble, b: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __fmodh(a, b),
32 => return fmodf(a, b),
64 => return fmod(a, b),
80 => return __fmodx(a, b),
128 => return fmodq(a, b),
-2
View File
@@ -443,8 +443,6 @@ pub fn logq(a: f128) callconv(.c) f128 {
pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __logh(x),
32 => return logf(x),
64 => return log(x),
80 => return __logx(x),
128 => return logq(x),
-2
View File
@@ -177,8 +177,6 @@ pub fn log10q(a: f128) callconv(.c) f128 {
pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __log10h(x),
32 => return log10f(x),
64 => return log10(x),
80 => return __log10x(x),
128 => return log10q(x),
-2
View File
@@ -170,8 +170,6 @@ pub fn log2q(a: f128) callconv(.c) f128 {
pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __log2h(x),
32 => return log2f(x),
64 => return log2(x),
80 => return __log2x(x),
128 => return log2q(x),
-2
View File
@@ -142,8 +142,6 @@ pub fn roundq(x_: f128) callconv(.c) f128 {
pub fn roundl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __roundh(x),
32 => return roundf(x),
64 => return round(x),
80 => return __roundx(x),
128 => return roundq(x),
-2
View File
@@ -189,8 +189,6 @@ pub fn sinq(x: f128) callconv(.c) f128 {
pub fn sinl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return sinh(x),
32 => return sinf(x),
64 => return sin(x),
80 => return sinx(x),
128 => return sinq(x),
-2
View File
@@ -292,8 +292,6 @@ pub fn sincosq(x: f128, r_sin: *f128, r_cos: *f128) callconv(.c) void {
pub fn sincosl(x: c_longdouble, r_sin: *c_longdouble, r_cos: *c_longdouble) callconv(.c) void {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return sincosh(x, r_sin, r_cos),
32 => return sincosf(x, r_sin, r_cos),
64 => return sincos(x, r_sin, r_cos),
80 => return sincosx(x, r_sin, r_cos),
128 => return sincosq(x, r_sin, r_cos),
-2
View File
@@ -481,8 +481,6 @@ fn _Qp_sqrt(c: *f128, a: *f128) callconv(.c) void {
pub fn sqrtl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __sqrth(x),
32 => return sqrtf(x),
64 => return sqrt(x),
80 => return __sqrtx(x),
128 => return sqrtq(x),
-2
View File
@@ -164,8 +164,6 @@ pub fn tanq(x: f128) callconv(.c) f128 {
pub fn tanl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return tanh(x),
32 => return tanf(x),
64 => return tan(x),
80 => return tanx(x),
128 => return tanq(x),
-2
View File
@@ -99,8 +99,6 @@ pub fn truncq(x: f128) callconv(.c) f128 {
pub fn truncl(x: c_longdouble) callconv(.c) c_longdouble {
switch (@typeInfo(c_longdouble).float.bits) {
16 => return __trunch(x),
32 => return truncf(x),
64 => return trunc(x),
80 => return __truncx(x),
128 => return truncq(x),
-2
View File
@@ -3082,8 +3082,6 @@ pub fn cTypeByteSize(t: *const Target, c_type: CType) u16 {
=> @divExact(cTypeBitSize(t, c_type), 8),
.longdouble => switch (cTypeBitSize(t, c_type)) {
16 => 2,
32 => 4,
64 => 8,
80 => @intCast(std.mem.alignForward(usize, 10, cTypeAlignment(t, .longdouble))),
128 => 16,
+8
View File
@@ -11102,6 +11102,14 @@ pub const ioctl = switch (native_os) {
else => private.ioctl,
};
// Math
pub extern "c" fn atan(x: f64) callconv(.c) f64;
pub extern "c" fn copysign(x: f64, y: f64) callconv(.c) f64;
pub extern "c" fn fdim(x: f64, y: f64) callconv(.c) f64;
pub extern "c" fn frexp(x: f64, e: *c_int) callconv(.c) f64;
pub extern "c" fn hypot(x: f64, y: f64) callconv(.c) f64;
pub extern "c" fn modf(x: f64, iptr: *f64) callconv(.c) f64;
// OS-specific bits. These are protected from being used on the wrong OS by
// comptime assertions inside each OS-specific file.
-2
View File
@@ -12381,8 +12381,6 @@ pub const CallAbiIterator = struct {
.f128 => .quad,
.c_longdouble => switch (zcu.getTarget().cTypeBitSize(.longdouble)) {
else => unreachable,
16 => .half,
32 => .single,
64 => .double,
80 => null,
128 => .quad,
-2
View File
@@ -1838,8 +1838,6 @@ test "coerce between pointers of compatible differently-named floats" {
}
const F = switch (@typeInfo(c_longdouble).float.bits) {
16 => f16,
32 => f32,
64 => f64,
80 => f80,
128 => f128,