Skip to content

Pi and e to Float32 and Float16 #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/device/intrinsics/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

using Base: FastMath
using Base.Math: throw_complex_domainerror
import Core: Float16, Float32

# TODO:
# - wrap all intrinsics from include/metal/metal_math
# - add support for vector types
# - consider emitting LLVM intrinsics and lowering those in the back-end

### Constants
# π and ℯ
for T in (:Float16,:Float32), R in (RoundUp, RoundDown), irr in (π, ℯ)
@eval @device_override $T(::typeof($irr), ::typeof($R)) = $@eval($T($irr,$R))
end

### Common Intrinsics
@device_function clamp_fast(x::Float32, minval::Float32, maxval::Float32) = ccall("extern air.fast_clamp.f32", llvmcall, Cfloat, (Cfloat, Cfloat, Cfloat), x, minval, maxval)
@device_override Base.clamp(x::Float32, minval::Float32, maxval::Float32) = ccall("extern air.clamp.f32", llvmcall, Cfloat, (Cfloat, Cfloat, Cfloat), x, minval, maxval)
Expand Down
15 changes: 15 additions & 0 deletions test/device/intrinsics/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,21 @@ end
ir = sprint(io->(@device_code_llvm io=io dump_module=true @metal metal = v"3.0" nextafter_out_test()))
@test occursin(Regex("@air\\.sign\\.f$(8*sizeof(T))"), ir)
end

# Borrowed from the Julia "Irrationals compared with Rationals and Floats" testset
@testset "Comparisons with $irr" for irr in (π, ℯ)
@eval function convert_test(res)
res[1] = $T($irr, RoundDown) < $irr
res[2] = $T($irr, RoundUp) > $irr
res[3] = !($T($irr, RoundDown) > $irr)
res[4] = !($T($irr, RoundUp) < $irr)
return nothing
end

res = MtlArray(zeros(Bool, 4))
Metal.@sync @metal convert_test(res)
@test all(Array(res))
end
end
end

Expand Down