authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-20 18:55:37+01:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-04-02 23:54:19+02:00
log811bada06d4d88d786119bab752c0e0cff345154
tree0a494fbfc628cd5936032028f9ed70fad111a648
parente15d8dd3d27047043a371421e59bc11c83a7a1b5
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

`libzigc/math`: Implement `lrint`

The changes were tested by running: ``` $ ./build/stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib $ stage4/bin/zig build test-libc -Dlibc-test-path=<LIBC-TEST-PATH> -Dtest-filter=lrint -fqemu -fwasmtime --summary line Build Summary: 737/737 steps succeeded ```

9 files changed, 5 insertions(+), 126 deletions(-)

lib/c/math.zig+5
...@@ -66,6 +66,7 @@ comptime {...@@ -66,6 +66,7 @@ comptime {
66 symbol(&finitef, "finitef");66 symbol(&finitef, "finitef");
67 symbol(&frexp, "frexp");67 symbol(&frexp, "frexp");
68 symbol(&hypot, "hypot");68 symbol(&hypot, "hypot");
69 symbol(&lrint, "lrint");
69 symbol(&modf, "modf");70 symbol(&modf, "modf");
70 symbol(&pow, "pow");71 symbol(&pow, "pow");
71 symbol(&pow10, "pow10");72 symbol(&pow10, "pow10");
...@@ -229,6 +230,10 @@ fn isnanl(x: c_longdouble) callconv(.c) c_int {...@@ -229,6 +230,10 @@ fn isnanl(x: c_longdouble) callconv(.c) c_int {
229 return if (math.isNan(x)) 1 else 0;230 return if (math.isNan(x)) 1 else 0;
230}231}
231232
233fn lrint(x: f64) callconv(.c) c_long {
234 return @intFromFloat(rint(x));
235}
236
232fn modfGeneric(comptime T: type, x: T, iptr: *T) T {237fn modfGeneric(comptime T: type, x: T, iptr: *T) T {
233 if (math.isNegativeInf(x)) {238 if (math.isNegativeInf(x)) {
234 iptr.* = -math.inf(T);239 iptr.* = -math.inf(T);
lib/libc/musl/src/math/aarch64/lrint.c deleted-10
...@@ -1,10 +0,0 @@
1#include <math.h>
2
3long lrint(double x)
4{
5 long n;
6 __asm__ (
7 "frintx %d1, %d1\n"
8 "fcvtzs %x0, %d1\n" : "=r"(n), "+w"(x));
9 return n;
10}
lib/libc/musl/src/math/i386/lrint.c deleted-8
...@@ -1,8 +0,0 @@
1#include <math.h>
2
3long lrint(double x)
4{
5 long r;
6 __asm__ ("fistpl %0" : "=m"(r) : "t"(x) : "st");
7 return r;
8}
lib/libc/musl/src/math/lrint.c deleted-72
...@@ -1,72 +0,0 @@
1#include <limits.h>
2#include <fenv.h>
3#include <math.h>
4#include "libm.h"
5
6/*
7If the result cannot be represented (overflow, nan), then
8lrint raises the invalid exception.
9
10Otherwise if the input was not an integer then the inexact
11exception is raised.
12
13C99 is a bit vague about whether inexact exception is
14allowed to be raised when invalid is raised.
15(F.9 explicitly allows spurious inexact exceptions, F.9.6.5
16does not make it clear if that rule applies to lrint, but
17IEEE 754r 7.8 seems to forbid spurious inexact exception in
18the ineger conversion functions)
19
20So we try to make sure that no spurious inexact exception is
21raised in case of an overflow.
22
23If the bit size of long > precision of double, then there
24cannot be inexact rounding in case the result overflows,
25otherwise LONG_MAX and LONG_MIN can be represented exactly
26as a double.
27*/
28
29#if LONG_MAX < 1U<<53 && defined(FE_INEXACT)
30#include <float.h>
31#include <stdint.h>
32#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1
33#define EPS DBL_EPSILON
34#elif FLT_EVAL_METHOD==2
35#define EPS LDBL_EPSILON
36#endif
37#ifdef __GNUC__
38/* avoid stack frame in lrint */
39__attribute__((noinline))
40#endif
41static long lrint_slow(double x)
42{
43 #pragma STDC FENV_ACCESS ON
44 int e;
45
46 e = fetestexcept(FE_INEXACT);
47 x = rint(x);
48 if (!e && (x > LONG_MAX || x < LONG_MIN))
49 feclearexcept(FE_INEXACT);
50 /* conversion */
51 return x;
52}
53
54long lrint(double x)
55{
56 uint32_t abstop = asuint64(x)>>32 & 0x7fffffff;
57 uint64_t sign = asuint64(x) & (1ULL << 63);
58
59 if (abstop < 0x41dfffff) {
60 /* |x| < 0x7ffffc00, no overflow */
61 double_t toint = asdouble(asuint64(1/EPS) | sign);
62 double_t y = x + toint - toint;
63 return (long)y;
64 }
65 return lrint_slow(x);
66}
67#else
68long lrint(double x)
69{
70 return rint(x);
71}
72#endif
lib/libc/musl/src/math/powerpc64/lrint.c deleted-16
...@@ -1,16 +0,0 @@
1#include <math.h>
2
3#ifdef _ARCH_PWR5X
4
5long lrint(double x)
6{
7 long n;
8 __asm__ ("fctid %0, %1" : "=d"(n) : "d"(x));
9 return n;
10}
11
12#else
13
14#include "../lrint.c"
15
16#endif
lib/libc/musl/src/math/x32/lrint.s deleted-5
...@@ -1,5 +0,0 @@
1.global lrint
2.type lrint,@function
3lrint:
4 cvtsd2si %xmm0,%rax
5 ret
lib/libc/musl/src/math/x86_64/lrint.c deleted-8
...@@ -1,8 +0,0 @@
1#include <math.h>
2
3long lrint(double x)
4{
5 long r;
6 __asm__ ("cvtsd2si %1, %0" : "=r"(r) : "x"(x));
7 return r;
8}
src/libs/musl.zig-6
...@@ -787,7 +787,6 @@ const src_files = [_][]const u8{...@@ -787,7 +787,6 @@ const src_files = [_][]const u8{
787 "musl/src/math/aarch64/llrintf.c",787 "musl/src/math/aarch64/llrintf.c",
788 "musl/src/math/aarch64/llround.c",788 "musl/src/math/aarch64/llround.c",
789 "musl/src/math/aarch64/llroundf.c",789 "musl/src/math/aarch64/llroundf.c",
790 "musl/src/math/aarch64/lrint.c",
791 "musl/src/math/aarch64/lrintf.c",790 "musl/src/math/aarch64/lrintf.c",
792 "musl/src/math/aarch64/lround.c",791 "musl/src/math/aarch64/lround.c",
793 "musl/src/math/aarch64/lroundf.c",792 "musl/src/math/aarch64/lroundf.c",
...@@ -860,7 +859,6 @@ const src_files = [_][]const u8{...@@ -860,7 +859,6 @@ const src_files = [_][]const u8{
860 "musl/src/math/i386/log1p.s",859 "musl/src/math/i386/log1p.s",
861 "musl/src/math/i386/log2l.s",860 "musl/src/math/i386/log2l.s",
862 "musl/src/math/i386/logl.s",861 "musl/src/math/i386/logl.s",
863 "musl/src/math/i386/lrint.c",
864 "musl/src/math/i386/lrintf.c",862 "musl/src/math/i386/lrintf.c",
865 "musl/src/math/i386/lrintl.c",863 "musl/src/math/i386/lrintl.c",
866 "musl/src/math/i386/remainder.c",864 "musl/src/math/i386/remainder.c",
...@@ -910,7 +908,6 @@ const src_files = [_][]const u8{...@@ -910,7 +908,6 @@ const src_files = [_][]const u8{
910 "musl/src/math/logbf.c",908 "musl/src/math/logbf.c",
911 "musl/src/math/logbl.c",909 "musl/src/math/logbl.c",
912 "musl/src/math/logl.c",910 "musl/src/math/logl.c",
913 "musl/src/math/lrint.c",
914 "musl/src/math/lrintf.c",911 "musl/src/math/lrintf.c",
915 "musl/src/math/lrintl.c",912 "musl/src/math/lrintl.c",
916 "musl/src/math/lround.c",913 "musl/src/math/lround.c",
...@@ -940,7 +937,6 @@ const src_files = [_][]const u8{...@@ -940,7 +937,6 @@ const src_files = [_][]const u8{
940 "musl/src/math/pow_data.c",937 "musl/src/math/pow_data.c",
941 "musl/src/math/powerpc64/fma.c",938 "musl/src/math/powerpc64/fma.c",
942 "musl/src/math/powerpc64/fmaf.c",939 "musl/src/math/powerpc64/fmaf.c",
943 "musl/src/math/powerpc64/lrint.c",
944 "musl/src/math/powerpc64/lrintf.c",940 "musl/src/math/powerpc64/lrintf.c",
945 "musl/src/math/powerpc64/lround.c",941 "musl/src/math/powerpc64/lround.c",
946 "musl/src/math/powerpc64/lroundf.c",942 "musl/src/math/powerpc64/lroundf.c",
...@@ -1017,7 +1013,6 @@ const src_files = [_][]const u8{...@@ -1017,7 +1013,6 @@ const src_files = [_][]const u8{
1017 "musl/src/math/x32/logl.s",1013 "musl/src/math/x32/logl.s",
1018 "musl/src/math/x32/lrintf.s",1014 "musl/src/math/x32/lrintf.s",
1019 "musl/src/math/x32/lrintl.s",1015 "musl/src/math/x32/lrintl.s",
1020 "musl/src/math/x32/lrint.s",
1021 "musl/src/math/x32/remainderl.s",1016 "musl/src/math/x32/remainderl.s",
1022 "musl/src/math/x32/rintl.s",1017 "musl/src/math/x32/rintl.s",
1023 "musl/src/math/x86_64/acosl.s",1018 "musl/src/math/x86_64/acosl.s",
...@@ -1036,7 +1031,6 @@ const src_files = [_][]const u8{...@@ -1036,7 +1031,6 @@ const src_files = [_][]const u8{
1036 "musl/src/math/x86_64/log1pl.s",1031 "musl/src/math/x86_64/log1pl.s",
1037 "musl/src/math/x86_64/log2l.s",1032 "musl/src/math/x86_64/log2l.s",
1038 "musl/src/math/x86_64/logl.s",1033 "musl/src/math/x86_64/logl.s",
1039 "musl/src/math/x86_64/lrint.c",
1040 "musl/src/math/x86_64/lrintf.c",1034 "musl/src/math/x86_64/lrintf.c",
1041 "musl/src/math/x86_64/lrintl.c",1035 "musl/src/math/x86_64/lrintl.c",
1042 "musl/src/math/x86_64/remainderl.c",1036 "musl/src/math/x86_64/remainderl.c",
src/libs/wasi_libc.zig-1
...@@ -732,7 +732,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -732,7 +732,6 @@ const libc_top_half_src_files = [_][]const u8{
732 "musl/src/math/logbf.c",732 "musl/src/math/logbf.c",
733 "musl/src/math/logbl.c",733 "musl/src/math/logbl.c",
734 "musl/src/math/logl.c",734 "musl/src/math/logl.c",
735 "musl/src/math/lrint.c",
736 "musl/src/math/lrintf.c",735 "musl/src/math/lrintf.c",
737 "musl/src/math/lrintl.c",736 "musl/src/math/lrintl.c",
738 "musl/src/math/lround.c",737 "musl/src/math/lround.c",