authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-16 19:57:54+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-16 19:57:54+02:00
logbea4ea5ff89ca1e9c517b3c4b00934931d280c57
treeeef503841dbba65fe92a43d2102897cb89aa31ed
parent0177cb57c03d0e10b4d0ef6a11ebf16de3dfd2b5
parentf564a7733cb20395a399caea21aac6fa54af50b3

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>

22 files changed, 36 insertions(+), 57 deletions(-)

lib/c/math.zig+28-17
......@@ -116,13 +116,9 @@ fn atanf(x: f32) callconv(.c) f32 {
116116}
117117
118118fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
119 return switch (@typeInfo(@TypeOf(x)).float.bits) {
120 16 => math.atan(@as(f16, @floatCast(x))),
121 32 => math.atan(@as(f32, @floatCast(x))),
122 64 => math.atan(@as(f64, @floatCast(x))),
123 80 => math.atan(@as(f80, @floatCast(x))),
124 128 => math.atan(@as(f128, @floatCast(x))),
125 else => unreachable,
119 return switch (@typeInfo(c_longdouble).float.bits) {
120 64 => std.c.atan(x),
121 else => math.atan(x),
126122 };
127123}
128124
......@@ -143,7 +139,10 @@ fn copysignf(x: f32, y: f32) callconv(.c) f32 {
143139}
144140
145141fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
146 return math.copysign(x, y);
142 return switch (@typeInfo(c_longdouble).float.bits) {
143 64 => std.c.copysign(x, y),
144 else => math.copysign(x, y),
145 };
147146}
148147
149148fn cosh(x: f64) callconv(.c) f64 {
......@@ -183,15 +182,18 @@ fn fdimf(x: f32, y: f32) callconv(.c) f32 {
183182}
184183
185184fn fdiml(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
186 return fdimGeneric(c_longdouble, x, y);
185 return switch (@typeInfo(c_longdouble).float.bits) {
186 64 => std.c.fdim(x, y),
187 else => fdimGeneric(c_longdouble, x, y),
188 };
187189}
188190
189191fn finite(x: f64) callconv(.c) c_int {
190 return if (math.isFinite(x)) 1 else 0;
192 return @intFromBool(math.isFinite(x));
191193}
192194
193195fn finitef(x: f32) callconv(.c) c_int {
194 return if (math.isFinite(x)) 1 else 0;
196 return @intFromBool(math.isFinite(x));
195197}
196198
197199fn frexpGeneric(comptime T: type, x: T, e: *c_int) T {
......@@ -222,7 +224,10 @@ fn frexpf(x: f32, e: *c_int) callconv(.c) f32 {
222224}
223225
224226fn frexpl(x: c_longdouble, e: *c_int) callconv(.c) c_longdouble {
225 return frexpGeneric(c_longdouble, x, e);
227 return switch (@typeInfo(c_longdouble).float.bits) {
228 64 => std.c.frexp(x, e),
229 else => frexpGeneric(c_longdouble, x, e),
230 };
226231}
227232
228233fn hypot(x: f64, y: f64) callconv(.c) f64 {
......@@ -234,19 +239,22 @@ fn hypotf(x: f32, y: f32) callconv(.c) f32 {
234239}
235240
236241fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
237 return math.hypot(x, y);
242 return switch (@typeInfo(c_longdouble).float.bits) {
243 64 => std.c.hypot(x, y),
244 else => math.hypot(x, y),
245 };
238246}
239247
240248fn isnan(x: f64) callconv(.c) c_int {
241 return if (math.isNan(x)) 1 else 0;
249 return @intFromBool(math.isNan(x));
242250}
243251
244252fn isnanf(x: f32) callconv(.c) c_int {
245 return if (math.isNan(x)) 1 else 0;
253 return @intFromBool(math.isNan(x));
246254}
247255
248256fn isnanl(x: c_longdouble) callconv(.c) c_int {
249 return if (math.isNan(x)) 1 else 0;
257 return @intFromBool(math.isNan(x));
250258}
251259
252260fn lrint(x: f64) callconv(.c) c_long {
......@@ -290,7 +298,10 @@ fn modff(x: f32, iptr: *f32) callconv(.c) f32 {
290298}
291299
292300fn modfl(x: c_longdouble, iptr: *c_longdouble) callconv(.c) c_longdouble {
293 return modfGeneric(c_longdouble, x, iptr);
301 return switch (@typeInfo(c_longdouble).float.bits) {
302 64 => std.c.modf(x, iptr),
303 else => modfGeneric(c_longdouble, x, iptr),
304 };
294305}
295306
296307fn testModf(comptime T: type) !void {
lib/compiler_rt/cos.zig-2
......@@ -173,8 +173,6 @@ pub fn cosq(x: f128) callconv(.c) f128 {
173173
174174pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {
175175 switch (@typeInfo(c_longdouble).float.bits) {
176 16 => return cosh(x),
177 32 => return cosf(x),
178176 64 => return cos(x),
179177 80 => return cosx(x),
180178 128 => return cosq(x),
lib/compiler_rt/exp.zig-2
......@@ -198,8 +198,6 @@ const expq = @import("exp_f128.zig").exp;
198198
199199pub fn expl(x: c_longdouble) callconv(.c) c_longdouble {
200200 switch (@typeInfo(c_longdouble).float.bits) {
201 16 => return __exph(x),
202 32 => return expf(x),
203201 64 => return exp(x),
204202 80 => return __expx(x),
205203 128 => return expq(x),
lib/compiler_rt/exp2.zig-2
......@@ -165,8 +165,6 @@ pub const exp2q = @import("exp_f128.zig").exp2;
165165
166166pub fn exp2l(x: c_longdouble) callconv(.c) c_longdouble {
167167 switch (@typeInfo(c_longdouble).float.bits) {
168 16 => return __exp2h(x),
169 32 => return exp2f(x),
170168 64 => return exp2(x),
171169 80 => return __exp2x(x),
172170 128 => return exp2q(x),
lib/compiler_rt/fabs.zig-2
......@@ -38,8 +38,6 @@ pub fn fabsq(a: f128) callconv(.c) f128 {
3838
3939pub fn fabsl(x: c_longdouble) callconv(.c) c_longdouble {
4040 switch (@typeInfo(c_longdouble).float.bits) {
41 16 => return __fabsh(x),
42 32 => return fabsf(x),
4341 64 => return fabs(x),
4442 80 => return __fabsx(x),
4543 128 => return fabsq(x),
lib/compiler_rt/fma.zig-2
......@@ -151,8 +151,6 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.c) f128 {
151151
152152pub fn fmal(x: c_longdouble, y: c_longdouble, z: c_longdouble) callconv(.c) c_longdouble {
153153 switch (@typeInfo(c_longdouble).float.bits) {
154 16 => return __fmah(x, y, z),
155 32 => return fmaf(x, y, z),
156154 64 => return fma(x, y, z),
157155 80 => return __fmax(x, y, z),
158156 128 => return fmaq(x, y, z),
lib/compiler_rt/fmax.zig-2
......@@ -39,8 +39,6 @@ pub fn fmaxq(x: f128, y: f128) callconv(.c) f128 {
3939
4040pub fn fmaxl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
4141 switch (@typeInfo(c_longdouble).float.bits) {
42 16 => return __fmaxh(x, y),
43 32 => return fmaxf(x, y),
4442 64 => return fmax(x, y),
4543 80 => return __fmaxx(x, y),
4644 128 => return fmaxq(x, y),
lib/compiler_rt/fmin.zig-2
......@@ -39,8 +39,6 @@ pub fn fminq(x: f128, y: f128) callconv(.c) f128 {
3939
4040pub fn fminl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
4141 switch (@typeInfo(c_longdouble).float.bits) {
42 16 => return __fminh(x, y),
43 32 => return fminf(x, y),
4442 64 => return fmin(x, y),
4543 80 => return __fminx(x, y),
4644 128 => return fminq(x, y),
lib/compiler_rt/fmod.zig-2
......@@ -251,8 +251,6 @@ pub fn fmodq(a: f128, b: f128) callconv(.c) f128 {
251251
252252pub fn fmodl(a: c_longdouble, b: c_longdouble) callconv(.c) c_longdouble {
253253 switch (@typeInfo(c_longdouble).float.bits) {
254 16 => return __fmodh(a, b),
255 32 => return fmodf(a, b),
256254 64 => return fmod(a, b),
257255 80 => return __fmodx(a, b),
258256 128 => return fmodq(a, b),
lib/compiler_rt/log.zig-2
......@@ -443,8 +443,6 @@ pub fn logq(a: f128) callconv(.c) f128 {
443443
444444pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {
445445 switch (@typeInfo(c_longdouble).float.bits) {
446 16 => return __logh(x),
447 32 => return logf(x),
448446 64 => return log(x),
449447 80 => return __logx(x),
450448 128 => return logq(x),
lib/compiler_rt/log10.zig-2
......@@ -177,8 +177,6 @@ pub fn log10q(a: f128) callconv(.c) f128 {
177177
178178pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {
179179 switch (@typeInfo(c_longdouble).float.bits) {
180 16 => return __log10h(x),
181 32 => return log10f(x),
182180 64 => return log10(x),
183181 80 => return __log10x(x),
184182 128 => return log10q(x),
lib/compiler_rt/log2.zig-2
......@@ -170,8 +170,6 @@ pub fn log2q(a: f128) callconv(.c) f128 {
170170
171171pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {
172172 switch (@typeInfo(c_longdouble).float.bits) {
173 16 => return __log2h(x),
174 32 => return log2f(x),
175173 64 => return log2(x),
176174 80 => return __log2x(x),
177175 128 => return log2q(x),
lib/compiler_rt/round.zig-2
......@@ -142,8 +142,6 @@ pub fn roundq(x_: f128) callconv(.c) f128 {
142142
143143pub fn roundl(x: c_longdouble) callconv(.c) c_longdouble {
144144 switch (@typeInfo(c_longdouble).float.bits) {
145 16 => return __roundh(x),
146 32 => return roundf(x),
147145 64 => return round(x),
148146 80 => return __roundx(x),
149147 128 => return roundq(x),
lib/compiler_rt/sin.zig-2
......@@ -189,8 +189,6 @@ pub fn sinq(x: f128) callconv(.c) f128 {
189189
190190pub fn sinl(x: c_longdouble) callconv(.c) c_longdouble {
191191 switch (@typeInfo(c_longdouble).float.bits) {
192 16 => return sinh(x),
193 32 => return sinf(x),
194192 64 => return sin(x),
195193 80 => return sinx(x),
196194 128 => return sinq(x),
lib/compiler_rt/sincos.zig-2
......@@ -292,8 +292,6 @@ pub fn sincosq(x: f128, r_sin: *f128, r_cos: *f128) callconv(.c) void {
292292
293293pub fn sincosl(x: c_longdouble, r_sin: *c_longdouble, r_cos: *c_longdouble) callconv(.c) void {
294294 switch (@typeInfo(c_longdouble).float.bits) {
295 16 => return sincosh(x, r_sin, r_cos),
296 32 => return sincosf(x, r_sin, r_cos),
297295 64 => return sincos(x, r_sin, r_cos),
298296 80 => return sincosx(x, r_sin, r_cos),
299297 128 => return sincosq(x, r_sin, r_cos),
lib/compiler_rt/sqrt.zig-2
......@@ -481,8 +481,6 @@ fn _Qp_sqrt(c: *f128, a: *f128) callconv(.c) void {
481481
482482pub fn sqrtl(x: c_longdouble) callconv(.c) c_longdouble {
483483 switch (@typeInfo(c_longdouble).float.bits) {
484 16 => return __sqrth(x),
485 32 => return sqrtf(x),
486484 64 => return sqrt(x),
487485 80 => return __sqrtx(x),
488486 128 => return sqrtq(x),
lib/compiler_rt/tan.zig-2
......@@ -164,8 +164,6 @@ pub fn tanq(x: f128) callconv(.c) f128 {
164164
165165pub fn tanl(x: c_longdouble) callconv(.c) c_longdouble {
166166 switch (@typeInfo(c_longdouble).float.bits) {
167 16 => return tanh(x),
168 32 => return tanf(x),
169167 64 => return tan(x),
170168 80 => return tanx(x),
171169 128 => return tanq(x),
lib/compiler_rt/trunc.zig-2
......@@ -99,8 +99,6 @@ pub fn truncq(x: f128) callconv(.c) f128 {
9999
100100pub fn truncl(x: c_longdouble) callconv(.c) c_longdouble {
101101 switch (@typeInfo(c_longdouble).float.bits) {
102 16 => return __trunch(x),
103 32 => return truncf(x),
104102 64 => return trunc(x),
105103 80 => return __truncx(x),
106104 128 => return truncq(x),
lib/std/Target.zig-2
......@@ -3082,8 +3082,6 @@ pub fn cTypeByteSize(t: *const Target, c_type: CType) u16 {
30823082 => @divExact(cTypeBitSize(t, c_type), 8),
30833083
30843084 .longdouble => switch (cTypeBitSize(t, c_type)) {
3085 16 => 2,
3086 32 => 4,
30873085 64 => 8,
30883086 80 => @intCast(std.mem.alignForward(usize, 10, cTypeAlignment(t, .longdouble))),
30893087 128 => 16,
lib/std/c.zig+8
......@@ -11102,6 +11102,14 @@ pub const ioctl = switch (native_os) {
1110211102 else => private.ioctl,
1110311103};
1110411104
11105// Math
11106pub extern "c" fn atan(x: f64) callconv(.c) f64;
11107pub extern "c" fn copysign(x: f64, y: f64) callconv(.c) f64;
11108pub extern "c" fn fdim(x: f64, y: f64) callconv(.c) f64;
11109pub extern "c" fn frexp(x: f64, e: *c_int) callconv(.c) f64;
11110pub extern "c" fn hypot(x: f64, y: f64) callconv(.c) f64;
11111pub extern "c" fn modf(x: f64, iptr: *f64) callconv(.c) f64;
11112
1110511113// OS-specific bits. These are protected from being used on the wrong OS by
1110611114// comptime assertions inside each OS-specific file.
1110711115
src/codegen/aarch64/Select.zig-2
......@@ -12381,8 +12381,6 @@ pub const CallAbiIterator = struct {
1238112381 .f128 => .quad,
1238212382 .c_longdouble => switch (zcu.getTarget().cTypeBitSize(.longdouble)) {
1238312383 else => unreachable,
12384 16 => .half,
12385 32 => .single,
1238612384 64 => .double,
1238712385 80 => null,
1238812386 128 => .quad,
test/behavior/cast.zig-2
......@@ -1838,8 +1838,6 @@ test "coerce between pointers of compatible differently-named floats" {
18381838 }
18391839
18401840 const F = switch (@typeInfo(c_longdouble).float.bits) {
1841 16 => f16,
1842 32 => f32,
18431841 64 => f64,
18441842 80 => f80,
18451843 128 => f128,