Skip to content
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

Add nextafter intrinsic #529

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions src/device/intrinsics/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ end
@device_override Base.trunc(x::Float32) = ccall("extern air.trunc.f32", llvmcall, Cfloat, (Cfloat,), x)
@device_override Base.trunc(x::Float16) = ccall("extern air.trunc.f16", llvmcall, Float16, (Float16,), x)

@static if Metal.is_macos(v"14")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible to target different Metal versions by passing kwargs to @metal, so I'm not sure if we should hard-code the dependency on a specific macOS version. The idea was to do these branches, semantically at least, at run time using metal_version(). The failure branch could then simply error, but maybe we ought to add a GPUCompiler intrinsic that eagerly aborts compilation to facilitate debugging this (which now wouldn't be great either, triggering a MethodError when calling the intrinsic from an unsupported macOS version).

@device_function nextafter(x::Float32, y::Float32) = ccall("extern air.nextafter.f32", llvmcall, Cfloat, (Cfloat, Cfloat), x, y)
@device_function nextafter(x::Float16, y::Float16) = ccall("extern air.nextafter.f16", llvmcall, Float16, (Float16, Float16), x, y)
end

# hypot without use of double
#
# taken from Cosmopolitan Libc
Expand Down
20 changes: 19 additions & 1 deletion test/device/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ MATH_INTR_FUNCS_2_ARG = [
# frexp, # T frexp(T x, Ti &exponent)
# ldexp, # T ldexp(T x, Ti k)
# modf, # T modf(T x, T &intval)
# nextafter, # T nextafter(T x, T y) # Metal 3.1+
hypot, # NOT MSL but tested the same
]

Expand Down Expand Up @@ -353,6 +352,25 @@ end
vec = Array(expm1.(buffer))
@test vec ≈ expm1.(arr)
end


let # nextafter
if Metal.is_macos(v"14")
N = 4
function nextafter_test(X, y)
idx = thread_position_in_grid_1d()
X[idx] = Metal.nextafter(X[idx], y)
return nothing
end
arr = rand(T, N)
buffer = MtlArray(arr)
Metal.@sync @metal threads = N nextafter_test(buffer, typemax(T))
@test Array(buffer) == nextfloat.(arr)

Metal.@sync @metal threads = N nextafter_test(buffer, typemin(T))
@test Array(buffer) == arr
end
end
end
end

Expand Down