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 {...@@ -116,13 +116,9 @@ fn atanf(x: f32) callconv(.c) f32 {
116}116}
117117
118fn atanl(x: c_longdouble) callconv(.c) c_longdouble {118fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
119 return switch (@typeInfo(@TypeOf(x)).float.bits) {119 return switch (@typeInfo(c_longdouble).float.bits) {
120 16 => math.atan(@as(f16, @floatCast(x))),120 64 => std.c.atan(x),
121 32 => math.atan(@as(f32, @floatCast(x))),121 else => math.atan(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,
126 };122 };
127}123}
128124
...@@ -143,7 +139,10 @@ fn copysignf(x: f32, y: f32) callconv(.c) f32 {...@@ -143,7 +139,10 @@ fn copysignf(x: f32, y: f32) callconv(.c) f32 {
143}139}
144140
145fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {141fn 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 };
147}146}
148147
149fn cosh(x: f64) callconv(.c) f64 {148fn cosh(x: f64) callconv(.c) f64 {
...@@ -183,15 +182,18 @@ fn fdimf(x: f32, y: f32) callconv(.c) f32 {...@@ -183,15 +182,18 @@ fn fdimf(x: f32, y: f32) callconv(.c) f32 {
183}182}
184183
185fn fdiml(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {184fn 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 };
187}189}
188190
189fn finite(x: f64) callconv(.c) c_int {191fn finite(x: f64) callconv(.c) c_int {
190 return if (math.isFinite(x)) 1 else 0;192 return @intFromBool(math.isFinite(x));
191}193}
192194
193fn finitef(x: f32) callconv(.c) c_int {195fn finitef(x: f32) callconv(.c) c_int {
194 return if (math.isFinite(x)) 1 else 0;196 return @intFromBool(math.isFinite(x));
195}197}
196198
197fn frexpGeneric(comptime T: type, x: T, e: *c_int) T {199fn frexpGeneric(comptime T: type, x: T, e: *c_int) T {
...@@ -222,7 +224,10 @@ fn frexpf(x: f32, e: *c_int) callconv(.c) f32 {...@@ -222,7 +224,10 @@ fn frexpf(x: f32, e: *c_int) callconv(.c) f32 {
222}224}
223225
224fn frexpl(x: c_longdouble, e: *c_int) callconv(.c) c_longdouble {226fn 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 };
226}231}
227232
228fn hypot(x: f64, y: f64) callconv(.c) f64 {233fn hypot(x: f64, y: f64) callconv(.c) f64 {
...@@ -234,19 +239,22 @@ fn hypotf(x: f32, y: f32) callconv(.c) f32 {...@@ -234,19 +239,22 @@ fn hypotf(x: f32, y: f32) callconv(.c) f32 {
234}239}
235240
236fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {241fn 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 };
238}246}
239247
240fn isnan(x: f64) callconv(.c) c_int {248fn isnan(x: f64) callconv(.c) c_int {
241 return if (math.isNan(x)) 1 else 0;249 return @intFromBool(math.isNan(x));
242}250}
243251
244fn isnanf(x: f32) callconv(.c) c_int {252fn isnanf(x: f32) callconv(.c) c_int {
245 return if (math.isNan(x)) 1 else 0;253 return @intFromBool(math.isNan(x));
246}254}
247255
248fn isnanl(x: c_longdouble) callconv(.c) c_int {256fn isnanl(x: c_longdouble) callconv(.c) c_int {
249 return if (math.isNan(x)) 1 else 0;257 return @intFromBool(math.isNan(x));
250}258}
251259
252fn lrint(x: f64) callconv(.c) c_long {260fn lrint(x: f64) callconv(.c) c_long {
...@@ -290,7 +298,10 @@ fn modff(x: f32, iptr: *f32) callconv(.c) f32 {...@@ -290,7 +298,10 @@ fn modff(x: f32, iptr: *f32) callconv(.c) f32 {
290}298}
291299
292fn modfl(x: c_longdouble, iptr: *c_longdouble) callconv(.c) c_longdouble {300fn 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 };
294}305}
295306
296fn testModf(comptime T: type) !void {307fn testModf(comptime T: type) !void {
lib/compiler_rt/cos.zig-2
...@@ -173,8 +173,6 @@ pub fn cosq(x: f128) callconv(.c) f128 {...@@ -173,8 +173,6 @@ pub fn cosq(x: f128) callconv(.c) f128 {
173173
174pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {174pub fn cosl(x: c_longdouble) callconv(.c) c_longdouble {
175 switch (@typeInfo(c_longdouble).float.bits) {175 switch (@typeInfo(c_longdouble).float.bits) {
176 16 => return cosh(x),
177 32 => return cosf(x),
178 64 => return cos(x),176 64 => return cos(x),
179 80 => return cosx(x),177 80 => return cosx(x),
180 128 => return cosq(x),178 128 => return cosq(x),
lib/compiler_rt/exp.zig-2
...@@ -198,8 +198,6 @@ const expq = @import("exp_f128.zig").exp;...@@ -198,8 +198,6 @@ const expq = @import("exp_f128.zig").exp;
198198
199pub fn expl(x: c_longdouble) callconv(.c) c_longdouble {199pub fn expl(x: c_longdouble) callconv(.c) c_longdouble {
200 switch (@typeInfo(c_longdouble).float.bits) {200 switch (@typeInfo(c_longdouble).float.bits) {
201 16 => return __exph(x),
202 32 => return expf(x),
203 64 => return exp(x),201 64 => return exp(x),
204 80 => return __expx(x),202 80 => return __expx(x),
205 128 => return expq(x),203 128 => return expq(x),
lib/compiler_rt/exp2.zig-2
...@@ -165,8 +165,6 @@ pub const exp2q = @import("exp_f128.zig").exp2;...@@ -165,8 +165,6 @@ pub const exp2q = @import("exp_f128.zig").exp2;
165165
166pub fn exp2l(x: c_longdouble) callconv(.c) c_longdouble {166pub fn exp2l(x: c_longdouble) callconv(.c) c_longdouble {
167 switch (@typeInfo(c_longdouble).float.bits) {167 switch (@typeInfo(c_longdouble).float.bits) {
168 16 => return __exp2h(x),
169 32 => return exp2f(x),
170 64 => return exp2(x),168 64 => return exp2(x),
171 80 => return __exp2x(x),169 80 => return __exp2x(x),
172 128 => return exp2q(x),170 128 => return exp2q(x),
lib/compiler_rt/fabs.zig-2
...@@ -38,8 +38,6 @@ pub fn fabsq(a: f128) callconv(.c) f128 {...@@ -38,8 +38,6 @@ pub fn fabsq(a: f128) callconv(.c) f128 {
3838
39pub fn fabsl(x: c_longdouble) callconv(.c) c_longdouble {39pub fn fabsl(x: c_longdouble) callconv(.c) c_longdouble {
40 switch (@typeInfo(c_longdouble).float.bits) {40 switch (@typeInfo(c_longdouble).float.bits) {
41 16 => return __fabsh(x),
42 32 => return fabsf(x),
43 64 => return fabs(x),41 64 => return fabs(x),
44 80 => return __fabsx(x),42 80 => return __fabsx(x),
45 128 => return fabsq(x),43 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 {...@@ -151,8 +151,6 @@ pub fn fmaq(x: f128, y: f128, z: f128) callconv(.c) f128 {
151151
152pub fn fmal(x: c_longdouble, y: c_longdouble, z: c_longdouble) callconv(.c) c_longdouble {152pub fn fmal(x: c_longdouble, y: c_longdouble, z: c_longdouble) callconv(.c) c_longdouble {
153 switch (@typeInfo(c_longdouble).float.bits) {153 switch (@typeInfo(c_longdouble).float.bits) {
154 16 => return __fmah(x, y, z),
155 32 => return fmaf(x, y, z),
156 64 => return fma(x, y, z),154 64 => return fma(x, y, z),
157 80 => return __fmax(x, y, z),155 80 => return __fmax(x, y, z),
158 128 => return fmaq(x, y, z),156 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 {...@@ -39,8 +39,6 @@ pub fn fmaxq(x: f128, y: f128) callconv(.c) f128 {
3939
40pub fn fmaxl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {40pub fn fmaxl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
41 switch (@typeInfo(c_longdouble).float.bits) {41 switch (@typeInfo(c_longdouble).float.bits) {
42 16 => return __fmaxh(x, y),
43 32 => return fmaxf(x, y),
44 64 => return fmax(x, y),42 64 => return fmax(x, y),
45 80 => return __fmaxx(x, y),43 80 => return __fmaxx(x, y),
46 128 => return fmaxq(x, y),44 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 {...@@ -39,8 +39,6 @@ pub fn fminq(x: f128, y: f128) callconv(.c) f128 {
3939
40pub fn fminl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {40pub fn fminl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
41 switch (@typeInfo(c_longdouble).float.bits) {41 switch (@typeInfo(c_longdouble).float.bits) {
42 16 => return __fminh(x, y),
43 32 => return fminf(x, y),
44 64 => return fmin(x, y),42 64 => return fmin(x, y),
45 80 => return __fminx(x, y),43 80 => return __fminx(x, y),
46 128 => return fminq(x, y),44 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 {...@@ -251,8 +251,6 @@ pub fn fmodq(a: f128, b: f128) callconv(.c) f128 {
251251
252pub fn fmodl(a: c_longdouble, b: c_longdouble) callconv(.c) c_longdouble {252pub fn fmodl(a: c_longdouble, b: c_longdouble) callconv(.c) c_longdouble {
253 switch (@typeInfo(c_longdouble).float.bits) {253 switch (@typeInfo(c_longdouble).float.bits) {
254 16 => return __fmodh(a, b),
255 32 => return fmodf(a, b),
256 64 => return fmod(a, b),254 64 => return fmod(a, b),
257 80 => return __fmodx(a, b),255 80 => return __fmodx(a, b),
258 128 => return fmodq(a, b),256 128 => return fmodq(a, b),
lib/compiler_rt/log.zig-2
...@@ -443,8 +443,6 @@ pub fn logq(a: f128) callconv(.c) f128 {...@@ -443,8 +443,6 @@ pub fn logq(a: f128) callconv(.c) f128 {
443443
444pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {444pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {
445 switch (@typeInfo(c_longdouble).float.bits) {445 switch (@typeInfo(c_longdouble).float.bits) {
446 16 => return __logh(x),
447 32 => return logf(x),
448 64 => return log(x),446 64 => return log(x),
449 80 => return __logx(x),447 80 => return __logx(x),
450 128 => return logq(x),448 128 => return logq(x),
lib/compiler_rt/log10.zig-2
...@@ -177,8 +177,6 @@ pub fn log10q(a: f128) callconv(.c) f128 {...@@ -177,8 +177,6 @@ pub fn log10q(a: f128) callconv(.c) f128 {
177177
178pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {178pub fn log10l(x: c_longdouble) callconv(.c) c_longdouble {
179 switch (@typeInfo(c_longdouble).float.bits) {179 switch (@typeInfo(c_longdouble).float.bits) {
180 16 => return __log10h(x),
181 32 => return log10f(x),
182 64 => return log10(x),180 64 => return log10(x),
183 80 => return __log10x(x),181 80 => return __log10x(x),
184 128 => return log10q(x),182 128 => return log10q(x),
lib/compiler_rt/log2.zig-2
...@@ -170,8 +170,6 @@ pub fn log2q(a: f128) callconv(.c) f128 {...@@ -170,8 +170,6 @@ pub fn log2q(a: f128) callconv(.c) f128 {
170170
171pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {171pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {
172 switch (@typeInfo(c_longdouble).float.bits) {172 switch (@typeInfo(c_longdouble).float.bits) {
173 16 => return __log2h(x),
174 32 => return log2f(x),
175 64 => return log2(x),173 64 => return log2(x),
176 80 => return __log2x(x),174 80 => return __log2x(x),
177 128 => return log2q(x),175 128 => return log2q(x),
lib/compiler_rt/round.zig-2
...@@ -142,8 +142,6 @@ pub fn roundq(x_: f128) callconv(.c) f128 {...@@ -142,8 +142,6 @@ pub fn roundq(x_: f128) callconv(.c) f128 {
142142
143pub fn roundl(x: c_longdouble) callconv(.c) c_longdouble {143pub fn roundl(x: c_longdouble) callconv(.c) c_longdouble {
144 switch (@typeInfo(c_longdouble).float.bits) {144 switch (@typeInfo(c_longdouble).float.bits) {
145 16 => return __roundh(x),
146 32 => return roundf(x),
147 64 => return round(x),145 64 => return round(x),
148 80 => return __roundx(x),146 80 => return __roundx(x),
149 128 => return roundq(x),147 128 => return roundq(x),
lib/compiler_rt/sin.zig-2
...@@ -189,8 +189,6 @@ pub fn sinq(x: f128) callconv(.c) f128 {...@@ -189,8 +189,6 @@ pub fn sinq(x: f128) callconv(.c) f128 {
189189
190pub fn sinl(x: c_longdouble) callconv(.c) c_longdouble {190pub fn sinl(x: c_longdouble) callconv(.c) c_longdouble {
191 switch (@typeInfo(c_longdouble).float.bits) {191 switch (@typeInfo(c_longdouble).float.bits) {
192 16 => return sinh(x),
193 32 => return sinf(x),
194 64 => return sin(x),192 64 => return sin(x),
195 80 => return sinx(x),193 80 => return sinx(x),
196 128 => return sinq(x),194 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 {...@@ -292,8 +292,6 @@ pub fn sincosq(x: f128, r_sin: *f128, r_cos: *f128) callconv(.c) void {
292292
293pub fn sincosl(x: c_longdouble, r_sin: *c_longdouble, r_cos: *c_longdouble) callconv(.c) void {293pub fn sincosl(x: c_longdouble, r_sin: *c_longdouble, r_cos: *c_longdouble) callconv(.c) void {
294 switch (@typeInfo(c_longdouble).float.bits) {294 switch (@typeInfo(c_longdouble).float.bits) {
295 16 => return sincosh(x, r_sin, r_cos),
296 32 => return sincosf(x, r_sin, r_cos),
297 64 => return sincos(x, r_sin, r_cos),295 64 => return sincos(x, r_sin, r_cos),
298 80 => return sincosx(x, r_sin, r_cos),296 80 => return sincosx(x, r_sin, r_cos),
299 128 => return sincosq(x, r_sin, r_cos),297 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 {...@@ -481,8 +481,6 @@ fn _Qp_sqrt(c: *f128, a: *f128) callconv(.c) void {
481481
482pub fn sqrtl(x: c_longdouble) callconv(.c) c_longdouble {482pub fn sqrtl(x: c_longdouble) callconv(.c) c_longdouble {
483 switch (@typeInfo(c_longdouble).float.bits) {483 switch (@typeInfo(c_longdouble).float.bits) {
484 16 => return __sqrth(x),
485 32 => return sqrtf(x),
486 64 => return sqrt(x),484 64 => return sqrt(x),
487 80 => return __sqrtx(x),485 80 => return __sqrtx(x),
488 128 => return sqrtq(x),486 128 => return sqrtq(x),
lib/compiler_rt/tan.zig-2
...@@ -164,8 +164,6 @@ pub fn tanq(x: f128) callconv(.c) f128 {...@@ -164,8 +164,6 @@ pub fn tanq(x: f128) callconv(.c) f128 {
164164
165pub fn tanl(x: c_longdouble) callconv(.c) c_longdouble {165pub fn tanl(x: c_longdouble) callconv(.c) c_longdouble {
166 switch (@typeInfo(c_longdouble).float.bits) {166 switch (@typeInfo(c_longdouble).float.bits) {
167 16 => return tanh(x),
168 32 => return tanf(x),
169 64 => return tan(x),167 64 => return tan(x),
170 80 => return tanx(x),168 80 => return tanx(x),
171 128 => return tanq(x),169 128 => return tanq(x),
lib/compiler_rt/trunc.zig-2
...@@ -99,8 +99,6 @@ pub fn truncq(x: f128) callconv(.c) f128 {...@@ -99,8 +99,6 @@ pub fn truncq(x: f128) callconv(.c) f128 {
9999
100pub fn truncl(x: c_longdouble) callconv(.c) c_longdouble {100pub fn truncl(x: c_longdouble) callconv(.c) c_longdouble {
101 switch (@typeInfo(c_longdouble).float.bits) {101 switch (@typeInfo(c_longdouble).float.bits) {
102 16 => return __trunch(x),
103 32 => return truncf(x),
104 64 => return trunc(x),102 64 => return trunc(x),
105 80 => return __truncx(x),103 80 => return __truncx(x),
106 128 => return truncq(x),104 128 => return truncq(x),
lib/std/Target.zig-2
...@@ -3082,8 +3082,6 @@ pub fn cTypeByteSize(t: *const Target, c_type: CType) u16 {...@@ -3082,8 +3082,6 @@ pub fn cTypeByteSize(t: *const Target, c_type: CType) u16 {
3082 => @divExact(cTypeBitSize(t, c_type), 8),3082 => @divExact(cTypeBitSize(t, c_type), 8),
30833083
3084 .longdouble => switch (cTypeBitSize(t, c_type)) {3084 .longdouble => switch (cTypeBitSize(t, c_type)) {
3085 16 => 2,
3086 32 => 4,
3087 64 => 8,3085 64 => 8,
3088 80 => @intCast(std.mem.alignForward(usize, 10, cTypeAlignment(t, .longdouble))),3086 80 => @intCast(std.mem.alignForward(usize, 10, cTypeAlignment(t, .longdouble))),
3089 128 => 16,3087 128 => 16,
lib/std/c.zig+8
...@@ -11102,6 +11102,14 @@ pub const ioctl = switch (native_os) {...@@ -11102,6 +11102,14 @@ pub const ioctl = switch (native_os) {
11102 else => private.ioctl,11102 else => private.ioctl,
11103};11103};
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
11105// OS-specific bits. These are protected from being used on the wrong OS by11113// OS-specific bits. These are protected from being used on the wrong OS by
11106// comptime assertions inside each OS-specific file.11114// comptime assertions inside each OS-specific file.
1110711115
src/codegen/aarch64/Select.zig-2
...@@ -12381,8 +12381,6 @@ pub const CallAbiIterator = struct {...@@ -12381,8 +12381,6 @@ pub const CallAbiIterator = struct {
12381 .f128 => .quad,12381 .f128 => .quad,
12382 .c_longdouble => switch (zcu.getTarget().cTypeBitSize(.longdouble)) {12382 .c_longdouble => switch (zcu.getTarget().cTypeBitSize(.longdouble)) {
12383 else => unreachable,12383 else => unreachable,
12384 16 => .half,
12385 32 => .single,
12386 64 => .double,12384 64 => .double,
12387 80 => null,12385 80 => null,
12388 128 => .quad,12386 128 => .quad,
test/behavior/cast.zig-2
...@@ -1838,8 +1838,6 @@ test "coerce between pointers of compatible differently-named floats" {...@@ -1838,8 +1838,6 @@ test "coerce between pointers of compatible differently-named floats" {
1838 }1838 }
18391839
1840 const F = switch (@typeInfo(c_longdouble).float.bits) {1840 const F = switch (@typeInfo(c_longdouble).float.bits) {
1841 16 => f16,
1842 32 => f32,
1843 64 => f64,1841 64 => f64,
1844 80 => f80,1842 80 => f80,
1845 128 => f128,1843 128 => f128,