Skip to content

Commit

Permalink
Working code
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangnrd committed Feb 7, 2025
1 parent f4b4a58 commit bb6e7fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/device/intrinsics/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ end
if metal_version() >= sv"3.1" # macOS 14+
ccall("extern air.nextafter.f32", llvmcall, Cfloat, (Cfloat, Cfloat), x, y)
else
reinterpret(Float32, reinterpret(UInt32, x) + sign(y-x))
nextfloat(x, unsafe_trunc(Int32, sign(y - x)))
end
end
@device_function function nextafter(x::Float16, y::Float16)
if metal_version() >= sv"3.1" # macOS 14+
ccall("extern air.nextafter.f16", llvmcall, Float16, (Float16, Float16), x, y)
else
reinterpret(Float16, reinterpret(UInt16, x) + sign(y-x))
nextfloat(x, unsafe_trunc(Int16, sign(y - x)))
end
end

Expand Down
26 changes: 12 additions & 14 deletions test/device/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,19 @@ 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
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
Expand Down

0 comments on commit bb6e7fa

Please sign in to comment.