[libcxx-commits] [libcxx] WIP [libc++][math] Add `constexpr` to `copysign`, `copysignf`, `copysignl` (PR #216285)

Aaron Ballman via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 30 08:49:20 PDT 2026


AaronBallman wrote:

> > So then thinking along the lines of your alternative suggestion, the idea is basically to support only `__builtin_sinf` and friends as constexpr builtins, and libc++ is responsible for providing the `constexpr` definitions of the `sin` class of functions which call the builtins, and we don't recognize the library interfaces at all from Clang? This would mean there's no constexpr support for it in C language modes, but that's acceptable. But I don't think we'd lose anything else, just means there's a lot more boilerplate in libc++ in order to wrap the builtin calls?
> 
> I think we also need to think about the libstdc++ and clang combination: in that case, I would expect that libstdc++ does nothing to make `double ::sin(double)` constexpr (it's provided by, typically, glibc), and glibc probably doesn't declare it constexpr either. In order to get consistent behavior, we would presumably need to treat that overload of `sin` as being a constexpr builtin in the same cases where libstdc++ declares the other overloads as constexpr. (There are other options we could take -- clang could provide a <math.h> wrapper header that injects our own declaration, for example -- but I think builtin recognition is the better way to go.)

And we'll have to do the same dance for MS STL as well, won't we?

I'm struggling to think of how to express this sort of attribute in tablegen:
```
def Sin : FPMathTemplate, LibBuiltin<"math.h"> {
  let Spellings = ["sin"];
  let Attributes = [NoThrow, ConstIgnoringErrnoAndExceptions];
  let Prototype = "T(T)";
  let AddBuiltinPrefixedAlias = 1;
}
```
We could put `Constexpr` in the list of `Attributes` as we do elsewhere, but that goes down the road of what I was suggesting which we already think is a problem.

I almost wonder if we need a new `CxxMathLibBuiltin` or something so that we handle `math.h` and `cmath`, the `std::` prefix, and the constexpr behavior all via one place. But then what should happen to the existing `LibBuiltin` definitions we have? Are they replaced or are we making two builtin declarations for every math function?

https://github.com/llvm/llvm-project/pull/216285


More information about the libcxx-commits mailing list