Why does Swift not expose math functions?

I was reading code from the standard library to figure out the float type name for Builtin intrinsics of floating point operations, and when I found them it was in standard library functions prefixed with an underscore. They are public and work but don't show up in Xcode. Why?

public extension Float32 {
    var sin: Self { _sin(self) } // Ok
    var cos: Self { cos(self) } // Error
}

Why does Numerics declare these in a C header which uses C intrinsics if they already exist?


I also can't use the llvm.tan intrinsic, I thought they were all accessible


Somewhat related question, how would I (if possible) refer to llvm array types in Swift Builtin syntax?

1 Like

The underscored operations that you found are vestigial hooks from the early days of Swift that can't be removed for API/ABI reasons (we could clean them up a bit, but it hasn't been an issue so far).

Numerics doesn't use them for a few reasons. Principally, the Builtins module wasn't publicly available when I wrote that part of Numerics (i.e. -enable-experimental-feature BuiltinsModule didn't exist yet), but also only a subset of the C math library is available there, and I prefer to have uniform handling of how we get at these operations rather than two different mechanisms that people would have to understand to work on them.

4 Likes