ziglang/zig
261
6.0k
Fork
You've already forked zig
772

libzigc: Implement 12 more math functions #31604

Merged
andrewrk merged 20 commits from mihael/zig:libzigc/more-math-functions-2 into master 2026年04月03日 12:04:35 +02:00
Contributor
Copy link

Implemented functions:

  • lrintf
  • rintf
  • lrint
  • finite
  • finitef
  • frexp
  • frexpf
  • frexpl

Implemented more precise/correct versions in compiler_rt:

  • cosl
  • sinl
  • tanl
  • sincosl

Tests were run at each commit and results, as well as more info, are included with every commit.
Other than the implementations, there's two things I'd point out:

  • The only exception to testing are finite/finitef functions since there are no tests for them in libc-test. However, these are very straightforward and obsolete, anyways. But musl still has them.
  • rint tests were re-enabled since the respective libc-test tests are passing now when using the Zig-native implementation.

Contributes to #30978



I've noticed that the fpu exception functions in std.math are no-ops, and git blame tells me they haven't been touched in 8 years. I'm not sure what the plans are, but if we want a libzigc, we'll need the ability to manipulate them.

For example, the nearbyint acts exactly like rint, except it mustn't raise the INEXACT exception -- which means we need to be able to clear the flag in the code if that happens.

Musl implementation:

double nearbyint(double x)
{
#ifdef FE_INEXACT
	#pragma STDC FENV_ACCESS ON
	int e;
	e = fetestexcept(FE_INEXACT);
#endif
	x = rint(x);
#ifdef FE_INEXACT
	if (!e)
		feclearexcept(FE_INEXACT);
#endif
	return x;
}

For this reason, nearbyint tests in libc-test are failing now.

As another example, if I enable llrint tests, I get a bunch of errors about a missing INVALID exception:

bad fp exception: RN llrint(nan)=0, want INVALID got 0
bad fp exception: RN llrint(inf)=0, want INVALID got 0
bad fp exception: RN llrint(-inf)=0, want INVALID got 0
bad fp exception: RN llrint(0x1p+100)=0, want INVALID got 0
bad fp exception: RN llrint(-0x1p+100)=0, want INVALID got

We should probably raise the INVALID exception on these arguments in the rint function, if we had the support for it.

Ultimately, adding support for managing the floating point environment also works towards #30978, because we're the reimplementing the musl's fenv module.

I'd try tackling this (I'd reuse the assembly code from musl), but I'm not sure what the plans are regarding this. Obviously, there was intent to do something about it. Or maybe it's already done and I just missed it somewhere in the docs? :D

I'd try adding support for handling these exceptions to Zig's std, then wrap it in libzigc, and then use them as needed elsewhere.

Implemented functions: - `lrintf` - `rintf` - `lrint` - `finite` - `finitef` - `frexp` - `frexpf` - `frexpl` Implemented more precise/correct versions in `compiler_rt`: - `cosl` - `sinl` - `tanl` - `sincosl` Tests were run at each commit and results, as well as more info, are included with every commit. Other than the implementations, there's two things I'd point out: - The only exception to testing are `finite`/`finitef` functions since there are no tests for them in `libc-test`. However, these are very straightforward and obsolete, anyways. But `musl` still has them. - `rint` tests were re-enabled since the respective `libc-test` tests are passing now when using the Zig-native implementation. Contributes to #30978 <hr> <hr> I've noticed that the [fpu exception functions in `std.math`](https://codeberg.org/ziglang/zig/blame/branch/master/lib/std/math.zig#L179) are no-ops, and git blame tells me they haven't been touched in 8 years. I'm not sure what the plans are, but if we want a `libzigc`, we'll need the ability to manipulate them. For example, the `nearbyint` acts exactly like `rint`, except it mustn't raise the `INEXACT` exception -- which means we need to be able to clear the flag in the code if that happens. Musl implementation: ```c double nearbyint(double x) { #ifdef FE_INEXACT #pragma STDC FENV_ACCESS ON int e; e = fetestexcept(FE_INEXACT); #endif x = rint(x); #ifdef FE_INEXACT if (!e) feclearexcept(FE_INEXACT); #endif return x; } ``` For this reason, `nearbyint` tests in `libc-test` are failing now. As another example, if I enable `llrint` tests, I get a bunch of errors about a missing `INVALID` exception: ``` bad fp exception: RN llrint(nan)=0, want INVALID got 0 bad fp exception: RN llrint(inf)=0, want INVALID got 0 bad fp exception: RN llrint(-inf)=0, want INVALID got 0 bad fp exception: RN llrint(0x1p+100)=0, want INVALID got 0 bad fp exception: RN llrint(-0x1p+100)=0, want INVALID got ``` We should probably raise the `INVALID` exception on these arguments in the `rint` function, if we had the support for it. Ultimately, adding support for managing the floating point environment also works towards #30978, because we're the reimplementing the `musl`'s `fenv` module. I'd try tackling this (I'd reuse the assembly code from `musl`), but I'm not sure what the plans are regarding this. Obviously, there was intent to do something about it. Or maybe it's already done and I just missed it somewhere in the docs? :D I'd try adding support for handling these exceptions to Zig's std, then wrap it in `libzigc`, and then use them as needed elsewhere.

if we want a libgzigc, we'll need the ability to manipulate them.

a... what?

> if we want a `libgzigc`, we'll need the ability to manipulate them. a... what?
Author
Contributor
Copy link

@andrewrk wrote in #31604 (comment):

if we want a libgzigc, we'll need the ability to manipulate them.

a... what?

Is it the typo or the whole sentence? 😂 It's late here. libzigc as in a complete Zig libc implementation, without having to bundle code, and "them" refers to floating point exceptions.

@andrewrk wrote in https://codeberg.org/ziglang/zig/pulls/31604#issuecomment-11867433: > > if we want a `libgzigc`, we'll need the ability to manipulate them. > > a... what? Is it the typo or the whole sentence? 😂 It's late here. `libzigc` as in a complete Zig `libc` implementation, without having to bundle code, and "them" refers to floating point exceptions.

Oh, lol, I thought you were referencing a zig version of libgcc or something like that xD

Oh, lol, I thought you were referencing a zig version of libgcc or something like that xD
mihael changed title from (削除) libzigc: Implement 10 more math functions (削除ここまで) to WIP: libzigc: Implement 10 more math functions 2026年03月21日 12:27:15 +01:00
Author
Contributor
Copy link

Curious, the tanl and sincosl functions are now also failing on my machine, I had this problem with sinl and cosl so I didn't include them. I'll take a shot at implementing more correct <trig>q functions in compiler_rt (porting from musl) and then get back to this.

I'll tackle sinl and cosl too while at it.

Curious, the `tanl` and `sincosl` functions are now also failing on my machine, I had this problem with `sinl` and `cosl` so I didn't include them. I'll take a shot at implementing more correct `<trig>q` functions in `compiler_rt` (porting from `musl`) and then get back to this. I'll tackle `sinl` and `cosl` too while at it.
mihael force-pushed libzigc/more-math-functions-2 from 1d56b4ac70
Some checks failed
ci / x86_64-netbsd-release (pull_request) Successful in 34m39s
ci / x86_64-freebsd-release (pull_request) Successful in 45m17s
ci / aarch64-macos-release (pull_request) Successful in 45m30s
ci / x86_64-freebsd-debug (pull_request) Successful in 55m25s
ci / x86_64-netbsd-debug (pull_request) Successful in 59m8s
ci / aarch64-macos-debug (pull_request) Successful in 1h5m26s
ci / x86_64-openbsd-release (pull_request) Successful in 1h8m30s
ci / x86_64-linux-debug (pull_request) Successful in 1h11m26s
ci / x86_64-openbsd-debug (pull_request) Successful in 1h19m49s
ci / x86_64-windows-debug (pull_request) Successful in 1h26m25s
ci / powerpc64le-linux-release (pull_request) Successful in 1h30m1s
ci / aarch64-linux-release (pull_request) Successful in 1h36m17s
ci / s390x-linux-release (pull_request) Successful in 1h53m16s
ci / aarch64-linux-debug (pull_request) Successful in 2h46m38s
ci / s390x-linux-debug (pull_request) Successful in 3h4m23s
ci / x86_64-windows-release (pull_request) Successful in 1h16m57s
ci / x86_64-linux-release (pull_request) Failing after 2h18m35s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 2h31m21s
ci / loongarch64-linux-release (pull_request) Successful in 2h8m55s
ci / loongarch64-linux-debug (pull_request) Successful in 3h16m19s
ci / powerpc64le-linux-debug (pull_request) Successful in 5h1m0s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / aarch64-netbsd-debug (pull_request) Successful in 3h39m50s
ci / aarch64-netbsd-release (pull_request) Successful in 2h46m45s
ci / aarch64-freebsd-debug (pull_request) Successful in 3h12m51s
ci / aarch64-freebsd-release (pull_request) Successful in 2h32m36s
to 26f39313a3
Some checks failed
ci / x86_64-freebsd-release (pull_request) Failing after 25m15s
ci / x86_64-netbsd-release (pull_request) Failing after 26m3s
ci / aarch64-macos-release (pull_request) Failing after 32m16s
ci / x86_64-freebsd-debug (pull_request) Failing after 32m26s
ci / x86_64-netbsd-debug (pull_request) Failing after 34m53s
ci / x86_64-linux-debug (pull_request) Failing after 36m58s
ci / x86_64-windows-release (pull_request) Failing after 38m23s
ci / x86_64-openbsd-release (pull_request) Failing after 42m25s
ci / x86_64-openbsd-debug (pull_request) Failing after 47m45s
ci / aarch64-macos-debug (pull_request) Failing after 49m15s
ci / s390x-linux-release (pull_request) Failing after 59m8s
ci / x86_64-windows-debug (pull_request) Failing after 1h12m30s
ci / aarch64-linux-release (pull_request) Failing after 1h18m15s
ci / x86_64-linux-release (pull_request) Failing after 1h41m33s
ci / aarch64-linux-debug (pull_request) Failing after 1h55m32s
ci / s390x-linux-debug (pull_request) Failing after 2h7m32s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 2h30m22s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / powerpc64le-linux-release (pull_request) Failing after 1h22m43s
ci / powerpc64le-linux-debug (pull_request) Failing after 2h51m17s
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
2026年03月27日 00:14:23 +01:00
Compare
mihael changed title from (削除) WIP: libzigc: Implement 10 more math functions (削除ここまで) to libzigc: Implement 12 more math functions 2026年03月27日 00:20:03 +01:00
Author
Contributor
Copy link

I’m sorry for ballooning the PR a bit, but I didn’t expect I’d be implementing trigonometric functions which are more precise for long double.
The implementations are more or less direct ports of musl; I tried to simplify it a bit in some places, but I was conservative. Also added and polished unit tests where applicable.

The good news is that all libc-test suite tests are passing now, and I ran the full suite too for ease of mind, since a lot of changes were done:

$ stage4/bin/zig build test-libc -Dlibc-test-path=<PATH-TO-LIBC-TEST> -fqemu -fwasmtime --summary line
Build Summary: 48729/48729 steps succeeded

I'm thinking of cleaning it up and lifting it up to standard library, so that sin, cos, and tan also finally get support for f80 and f128. I was thinking of doing it similarly to acos and asin. Though, with those functions being builtins, perhaps better f80/f128 support should be implemented some place else from std.math. Or maybe just doing it in compiler_rt was enough? I'm open to suggestions.

I’m sorry for ballooning the PR a bit, but I didn’t expect I’d be implementing trigonometric functions which are more precise for `long double`. The implementations are more or less direct ports of `musl`; I tried to simplify it a bit in some places, but I was conservative. Also added and polished unit tests where applicable. The good news is that all `libc-test` suite tests are passing now, and I ran the full suite too for ease of mind, since a lot of changes were done: ``` $ stage4/bin/zig build test-libc -Dlibc-test-path=<PATH-TO-LIBC-TEST> -fqemu -fwasmtime --summary line Build Summary: 48729/48729 steps succeeded ``` I'm thinking of cleaning it up and lifting it up to standard library, so that `sin`, `cos`, and `tan` also finally get support for `f80` and `f128`. I was thinking of doing it similarly to [`acos`](https://codeberg.org/ziglang/zig/src/branch/master/lib/std/math/acos.zig) and [`asin`](https://codeberg.org/ziglang/zig/src/branch/master/lib/std/math/asin.zig). Though, with those functions being builtins, perhaps better `f80`/`f128` support should be implemented some place else from `std.math`. Or maybe just doing it in `compiler_rt` was enough? I'm open to suggestions.
andrewrk requested changes 2026年03月27日 00:46:56 +01:00
Dismissed
andrewrk left a comment
Copy link
re: math_utils.zig: https://ziglang.org/documentation/master/#Avoid-Redundancy-in-Names
Author
Contributor
Copy link

@andrewrk wrote in #31604 (comment):

re: math_utils.zig: https://ziglang.org/documentation/master/#Avoid-Redundancy-in-Names

I could move the contents to trig.zig since these are "internal" functions used by the trig implementations, for now.
If I were to categorize, I'd move ldSignExponent and ldMantissaTop to long_double.zig and pi_4 to constants.zig.

Does either of the two sound good?

The functions were inspired by ldshape in the internal libm.h file and the constant was taken from math.h. Hence the inspired naming. :D

@andrewrk wrote in https://codeberg.org/ziglang/zig/pulls/31604#issuecomment-12195573: > re: math_utils.zig: https://ziglang.org/documentation/master/#Avoid-Redundancy-in-Names I could move the contents to `trig.zig` since these are "internal" functions used by the trig implementations, for now. If I were to categorize, I'd move `ldSignExponent` and `ldMantissaTop` to `long_double.zig` and `pi_4` to `constants.zig`. Does either of the two sound good? The functions were inspired by [`ldshape` in the internal `libm.h` file](https://git.musl-libc.org/cgit/musl/tree/src/internal/libm.h) and the constant was [taken from math.h](https://git.musl-libc.org/cgit/musl/tree/include/math.h#n377). Hence the inspired naming. :D
mihael force-pushed libzigc/more-math-functions-2 from 26f39313a3
Some checks failed
ci / x86_64-freebsd-release (pull_request) Failing after 25m15s
ci / x86_64-netbsd-release (pull_request) Failing after 26m3s
ci / aarch64-macos-release (pull_request) Failing after 32m16s
ci / x86_64-freebsd-debug (pull_request) Failing after 32m26s
ci / x86_64-netbsd-debug (pull_request) Failing after 34m53s
ci / x86_64-linux-debug (pull_request) Failing after 36m58s
ci / x86_64-windows-release (pull_request) Failing after 38m23s
ci / x86_64-openbsd-release (pull_request) Failing after 42m25s
ci / x86_64-openbsd-debug (pull_request) Failing after 47m45s
ci / aarch64-macos-debug (pull_request) Failing after 49m15s
ci / s390x-linux-release (pull_request) Failing after 59m8s
ci / x86_64-windows-debug (pull_request) Failing after 1h12m30s
ci / aarch64-linux-release (pull_request) Failing after 1h18m15s
ci / x86_64-linux-release (pull_request) Failing after 1h41m33s
ci / aarch64-linux-debug (pull_request) Failing after 1h55m32s
ci / s390x-linux-debug (pull_request) Failing after 2h7m32s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 2h30m22s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / powerpc64le-linux-release (pull_request) Failing after 1h22m43s
ci / powerpc64le-linux-debug (pull_request) Failing after 2h51m17s
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
to 87948f9af1
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-netbsd-debug (pull_request) Has been cancelled
ci / x86_64-netbsd-release (pull_request) Has been cancelled
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
2026年03月27日 22:47:35 +01:00
Compare
mihael force-pushed libzigc/more-math-functions-2 from 87948f9af1
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-openbsd-release (pull_request) Has been cancelled
ci / x86_64-openbsd-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-netbsd-debug (pull_request) Has been cancelled
ci / x86_64-netbsd-release (pull_request) Has been cancelled
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to 71968b6df5
Some checks failed
ci / x86_64-netbsd-release (pull_request) Failing after 25m41s
ci / x86_64-freebsd-release (pull_request) Failing after 26m24s
ci / aarch64-macos-release (pull_request) Failing after 34m35s
ci / x86_64-netbsd-debug (pull_request) Failing after 34m52s
ci / x86_64-linux-debug (pull_request) Failing after 37m7s
ci / x86_64-freebsd-debug (pull_request) Failing after 39m11s
ci / x86_64-openbsd-release (pull_request) Failing after 40m7s
ci / x86_64-windows-release (pull_request) Failing after 43m42s
ci / x86_64-openbsd-debug (pull_request) Failing after 54m25s
ci / aarch64-macos-debug (pull_request) Failing after 1h2m58s
ci / x86_64-windows-debug (pull_request) Failing after 1h3m19s
ci / aarch64-linux-release (pull_request) Failing after 1h8m31s
ci / aarch64-linux-debug (pull_request) Failing after 1h50m44s
ci / x86_64-linux-release (pull_request) Failing after 1h53m12s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 1h53m54s
ci / s390x-linux-release (pull_request) Failing after 1h32m36s
ci / s390x-linux-debug (pull_request) Failing after 2h34m48s
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
2026年03月27日 22:51:48 +01:00
Compare
mihael force-pushed libzigc/more-math-functions-2 from 71968b6df5
Some checks failed
ci / x86_64-netbsd-release (pull_request) Failing after 25m41s
ci / x86_64-freebsd-release (pull_request) Failing after 26m24s
ci / aarch64-macos-release (pull_request) Failing after 34m35s
ci / x86_64-netbsd-debug (pull_request) Failing after 34m52s
ci / x86_64-linux-debug (pull_request) Failing after 37m7s
ci / x86_64-freebsd-debug (pull_request) Failing after 39m11s
ci / x86_64-openbsd-release (pull_request) Failing after 40m7s
ci / x86_64-windows-release (pull_request) Failing after 43m42s
ci / x86_64-openbsd-debug (pull_request) Failing after 54m25s
ci / aarch64-macos-debug (pull_request) Failing after 1h2m58s
ci / x86_64-windows-debug (pull_request) Failing after 1h3m19s
ci / aarch64-linux-release (pull_request) Failing after 1h8m31s
ci / aarch64-linux-debug (pull_request) Failing after 1h50m44s
ci / x86_64-linux-release (pull_request) Failing after 1h53m12s
ci / x86_64-linux-debug-llvm (pull_request) Failing after 1h53m54s
ci / s390x-linux-release (pull_request) Failing after 1h32m36s
ci / s390x-linux-debug (pull_request) Failing after 2h34m48s
ci / aarch64-freebsd-debug (pull_request) Has been cancelled
ci / aarch64-freebsd-release (pull_request) Has been cancelled
ci / aarch64-netbsd-debug (pull_request) Has been cancelled
ci / aarch64-netbsd-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / powerpc64le-linux-debug (pull_request) Has been cancelled
ci / powerpc64le-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
to b6a263d570
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-netbsd-release (pull_request) Successful in 37m19s
ci / x86_64-freebsd-release (pull_request) Successful in 37m55s
ci / x86_64-netbsd-debug (pull_request) Successful in 44m52s
ci / x86_64-freebsd-debug (pull_request) Successful in 48m27s
ci / x86_64-windows-release (pull_request) Successful in 50m27s
ci / aarch64-macos-release (pull_request) Successful in 51m53s
ci / x86_64-openbsd-release (pull_request) Successful in 54m59s
ci / x86_64-openbsd-debug (pull_request) Successful in 1h1m49s
ci / x86_64-windows-debug (pull_request) Successful in 1h4m13s
ci / x86_64-linux-debug (pull_request) Successful in 1h5m30s
ci / aarch64-macos-debug (pull_request) Successful in 1h12m15s
ci / aarch64-linux-release (pull_request) Successful in 1h23m47s
ci / powerpc64le-linux-release (pull_request) Successful in 1h28m37s
ci / s390x-linux-release (pull_request) Successful in 1h41m2s
ci / loongarch64-linux-release (pull_request) Successful in 1h41m43s
ci / x86_64-linux-release (pull_request) Successful in 2h29m6s
ci / aarch64-linux-debug (pull_request) Successful in 2h30m50s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h37m36s
ci / s390x-linux-debug (pull_request) Successful in 2h43m29s
ci / loongarch64-linux-debug (pull_request) Successful in 2h51m7s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h29m38s
ci / aarch64-freebsd-debug (pull_request) Successful in 3h28m9s
ci / aarch64-freebsd-release (pull_request) Successful in 2h37m1s
ci / aarch64-netbsd-debug (pull_request) Successful in 4h36m2s
ci / aarch64-netbsd-release (pull_request) Successful in 2h29m39s
2026年03月30日 21:52:30 +02:00
Compare
Author
Contributor
Copy link

I went down the middle road and put the pi/4 constant into trig.zig since it's a single constant and only used across trig function implementations. Didn't really see a more appropriate place and having a file for that felt wildly silly.

On the other hand, I left the long double bit slicing in a long_double.zig file since there's more code and it is used across multiple files, afterall. Having those functions in trig.zig felt a little awkward.

I went down the middle road and put the `pi/4` constant into `trig.zig` since it's a single constant and only used across trig function implementations. Didn't really see a more appropriate place and having a file for that felt wildly silly. On the other hand, I left the `long double` bit slicing in a `long_double.zig` file since there's more code and it is used across multiple files, afterall. Having those functions in `trig.zig` felt a little awkward.
andrewrk requested changes 2026年04月01日 23:15:29 +02:00
Dismissed
andrewrk left a comment
Copy link

Hi thank you for the changes. I still have some more requests-

Hi thank you for the changes. I still have some more requests-
@ -10,0 +12,4 @@
// https://git.musl-libc.org/cgit/musl/tree/src/math/__tanl.c
/// pi divided by 4
pubconstpi_4=0.78539816339744830962;
Owner
Copy link

should be std.math.pi / 4

should be `std.math.pi / 4`
@ -73,6 +79,52 @@ pub fn __cosdf(x: f64) f32 {
return@floatCast(((1.0+z*C0)+w*C1)+(w*z)*r);
}
pubfn__cosl(comptimeT:type,x:T,y:T)T{
Owner
Copy link

no underscore prefixes in names please

no underscore prefixes in names please
Author
Contributor
Copy link

I'm aware of the Zig guidelines on this, but these internal functions were also named with underscores in musl and there were already functions like that in trig.zig. Would you like me to rename all these instances so it's consistent?

I'm aware of the Zig guidelines on this, but these internal functions were also named with underscores in `musl` and there were already functions like that in `trig.zig`. Would you like me to rename all these instances so it's consistent?
Owner
Copy link

I understand the desire to make the code similar to the code it is ported from to more easily check for divergence at a source code level. But ultimately, this is a different codebase, and I want to set a good example for people who read this project's source code. To land this pull request, it is sufficient to apply these changes to only your changes here. In fact I would request that. But a follow up change to do that would be welcome.

I understand the desire to make the code similar to the code it is ported from to more easily check for divergence at a source code level. But ultimately, this is a different codebase, and I want to set a good example for people who read this project's source code. To land this pull request, it is sufficient to apply these changes to only your changes here. In fact I would request that. But a follow up change to do that would be welcome.
@ -274,0 +379,4 @@
pubfn__tanl(comptimeT:type,x_:T,y_:T,odd:i32)T{
varx=x_;
vary=y_;
constimpl=switch(T){
Owner
Copy link

this should just be two different functions, making it generic doesn't provide any value

this should just be two different functions, making it generic doesn't provide any value
Author
Contributor
Copy link

I'm assuming this goes for other __<trig>l functions in trig.zig as well?

I'm assuming this goes for other `__<trig>l` functions in `trig.zig` as well?
mihael force-pushed libzigc/more-math-functions-2 from b6a263d570
All checks were successful
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / x86_64-netbsd-release (pull_request) Successful in 37m19s
ci / x86_64-freebsd-release (pull_request) Successful in 37m55s
ci / x86_64-netbsd-debug (pull_request) Successful in 44m52s
ci / x86_64-freebsd-debug (pull_request) Successful in 48m27s
ci / x86_64-windows-release (pull_request) Successful in 50m27s
ci / aarch64-macos-release (pull_request) Successful in 51m53s
ci / x86_64-openbsd-release (pull_request) Successful in 54m59s
ci / x86_64-openbsd-debug (pull_request) Successful in 1h1m49s
ci / x86_64-windows-debug (pull_request) Successful in 1h4m13s
ci / x86_64-linux-debug (pull_request) Successful in 1h5m30s
ci / aarch64-macos-debug (pull_request) Successful in 1h12m15s
ci / aarch64-linux-release (pull_request) Successful in 1h23m47s
ci / powerpc64le-linux-release (pull_request) Successful in 1h28m37s
ci / s390x-linux-release (pull_request) Successful in 1h41m2s
ci / loongarch64-linux-release (pull_request) Successful in 1h41m43s
ci / x86_64-linux-release (pull_request) Successful in 2h29m6s
ci / aarch64-linux-debug (pull_request) Successful in 2h30m50s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h37m36s
ci / s390x-linux-debug (pull_request) Successful in 2h43m29s
ci / loongarch64-linux-debug (pull_request) Successful in 2h51m7s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h29m38s
ci / aarch64-freebsd-debug (pull_request) Successful in 3h28m9s
ci / aarch64-freebsd-release (pull_request) Successful in 2h37m1s
ci / aarch64-netbsd-debug (pull_request) Successful in 4h36m2s
ci / aarch64-netbsd-release (pull_request) Successful in 2h29m39s
to 4ccac1de41
All checks were successful
ci / x86_64-freebsd-release (pull_request) Successful in 36m17s
ci / x86_64-freebsd-debug (pull_request) Successful in 48m28s
ci / x86_64-netbsd-release (pull_request) Successful in 36m49s
ci / x86_64-netbsd-debug (pull_request) Successful in 46m35s
ci / aarch64-macos-release (pull_request) Successful in 54m9s
ci / aarch64-macos-debug (pull_request) Successful in 1h20m10s
ci / aarch64-linux-release (pull_request) Successful in 1h28m17s
ci / x86_64-openbsd-debug (pull_request) Successful in 1h1m15s
ci / x86_64-openbsd-release (pull_request) Successful in 51m10s
ci / x86_64-linux-debug (pull_request) Successful in 1h10m53s
ci / aarch64-linux-debug (pull_request) Successful in 3h15m50s
ci / x86_64-windows-debug (pull_request) Successful in 52m40s
ci / x86_64-windows-release (pull_request) Successful in 54m35s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h57m29s
ci / x86_64-linux-release (pull_request) Successful in 2h53m2s
ci / s390x-linux-release (pull_request) Successful in 1h35m46s
ci / s390x-linux-debug (pull_request) Successful in 3h1m4s
ci / powerpc64le-linux-release (pull_request) Successful in 1h41m55s
ci / loongarch64-linux-debug (pull_request) Successful in 3h24m16s
ci / loongarch64-linux-release (pull_request) Successful in 1h55m16s
ci / powerpc64le-linux-debug (pull_request) Successful in 4h42m26s
ci / aarch64-freebsd-debug (pull_request) Successful in 3h39m6s
ci / aarch64-freebsd-release (pull_request) Successful in 2h42m7s
ci / aarch64-netbsd-debug (pull_request) Successful in 4h1m15s
ci / aarch64-netbsd-release (pull_request) Successful in 2h48m48s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
2026年04月02日 23:54:48 +02:00
Compare
Author
Contributor
Copy link

Thanks, addressed the above comments. I went with the assumption to make the long double implementations of trigonometric functions non-generic in general.
I also removed the dunders for the trig functions I touched with my changes (even for f32, f64, etc.) since it was easier to just make the changes than to ignore while grepping lol.

Some casual grepping also revealed quite a lot of dunder methods in compiler_rt, I'd be happy to remedy that in some follow-up PR.


I've ran both the compiler_rt unit tests:

$ stage4/bin/zig test lib/compiler_rt.zig
161/299 compiler_rt.os_version_check.test.isPlatformVersionAtLeast...SKIP
162/299 compiler_rt.emutls.test.simple_allocator...SKIP
163/299 compiler_rt.emutls.test.__emutls_get_address zeroed...SKIP
164/299 compiler_rt.emutls.test.__emutls_get_address with default_value...SKIP
165/299 compiler_rt.emutls.test.test default_value with different sizes...SKIP
166/299 compiler_rt.arm.test.__aeabi_frsub...SKIP
167/299 compiler_rt.arm.test.__aeabi_drsub...SKIP
292 passed; 7 skipped; 0 failed.

and the full libc test suite:

stage4/bin/zig build test-libc -Dlibc-test-path=<PATH-TO-LIBC-TEST> -fqemu -fwasmtime --summary line
Build Summary: 48737/48737 steps succeeded
Thanks, addressed the above comments. I went with the assumption to make the `long double` implementations of trigonometric functions non-generic in general. I also removed the dunders for the trig functions I touched with my changes (even for `f32`, `f64`, etc.) since it was easier to just make the changes than to ignore while grepping lol. Some casual grepping also revealed quite a lot of dunder methods in `compiler_rt`, I'd be happy to remedy that in some follow-up PR. <hr> I've ran both the `compiler_rt` unit tests: ``` $ stage4/bin/zig test lib/compiler_rt.zig 161/299 compiler_rt.os_version_check.test.isPlatformVersionAtLeast...SKIP 162/299 compiler_rt.emutls.test.simple_allocator...SKIP 163/299 compiler_rt.emutls.test.__emutls_get_address zeroed...SKIP 164/299 compiler_rt.emutls.test.__emutls_get_address with default_value...SKIP 165/299 compiler_rt.emutls.test.test default_value with different sizes...SKIP 166/299 compiler_rt.arm.test.__aeabi_frsub...SKIP 167/299 compiler_rt.arm.test.__aeabi_drsub...SKIP 292 passed; 7 skipped; 0 failed. ``` and the full `libc` test suite: ``` stage4/bin/zig build test-libc -Dlibc-test-path=<PATH-TO-LIBC-TEST> -fqemu -fwasmtime --summary line Build Summary: 48737/48737 steps succeeded
andrewrk left a comment
Copy link

Okay, nice I think this is ready to land. Thank you for working with me on this.

Okay, nice I think this is ready to land. Thank you for working with me on this.

33 down, 1935 to go!

33 down, 1935 to go!
Sign in to join this conversation.
No reviewers
Labels
Clear labels
abi/f32
abi/ilp32
abi/sf
accepted
This proposal is planned.
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
The web application for interactive documentation and generation of its assets.
backend/c
The C backend outputs C source code.
backend/llvm
The LLVM backend outputs an LLVM bitcode module.
backend/self-hosted
The self-hosted backends produce machine code directly.
binutils
Zig's included binary utilities: zig ar, zig dlltool, zig lib, zig ranlib, zig objcopy, and zig rc.
breaking
Implementing this issue could cause existing code to no longer compile or have different behavior.
build system
The Zig build system - zig build, std.Build, the build runner, and package management.
debug info
An issue related to debug information (e.g. DWARF) produced by the Zig compiler.
docs
An issue with documentation, e.g. the language reference or standard library doc comments.
error message
This issue points out an error message that is unhelpful and should be improved.
frontend
Tokenization, parsing, AstGen, ZonGen, Sema, Legalize, and Liveness.
fuzzing
An issue related to Zig's integrated fuzz testing.
incremental
Reuse of internal compiler state for faster compilation.
lib/c
This issue relates to Zig's libc implementation and/or vendored libcs.
lib/compiler-rt
This issue relates to Zig's compiler-rt library.
lib/cxx
This issue relates to Zig's vendored libc++ and/or libc++abi.
lib/std
This issue relates to Zig's standard library.
lib/tsan
This issue relates to Zig's vendored libtsan.
lib/ubsan-rt
This issue relates to Zig's ubsan-rt library.
lib/unwind
This issue relates to Zig's vendored libunwind.
linking
Zig's integrated object file and incremental linker.
miscompilation
The compiler reports success but produces semantically incorrect code.
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
This issue suggests language modifications. If it also has the "accepted" label then it is planned.
release notes
This issue or pull request should be mentioned in the release notes.
testing
This issue is related to testing the compiler, standard library, or other parts of Zig.
zig cc
Zig as a drop-in C-family compiler.
zig fmt
The Zig source code formatter.
zig reduce
The Zig source code reduction tool.
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig!31604
Reference in a new issue
ziglang/zig
No description provided.
Delete branch "mihael/zig:libzigc/more-math-functions-2"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?