Skip to content

Commit 54e1adc

Browse files
committed
Add nextafter intrinsic
1 parent 0a78ec7 commit 54e1adc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/device/intrinsics/math.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ end
295295
@device_override Base.trunc(x::Float32) = ccall("extern air.trunc.f32", llvmcall, Cfloat, (Cfloat,), x)
296296
@device_override Base.trunc(x::Float16) = ccall("extern air.trunc.f16", llvmcall, Float16, (Float16,), x)
297297

298+
@static if Metal.is_macos(v"14")
299+
@device_function nextafter(x::Float32, y::Float32) = ccall("extern air.nextafter.f32", llvmcall, Cfloat, (Cfloat, Cfloat), x, y)
300+
@device_function nextafter(x::Float16, y::Float16) = ccall("extern air.nextafter.f16", llvmcall, Float16, (Float16, Float16), x, y)
301+
end
302+
298303
# hypot without use of double
299304
#
300305
# taken from Cosmopolitan Libc

test/device/intrinsics.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ MATH_INTR_FUNCS_2_ARG = [
159159
# frexp, # T frexp(T x, Ti &exponent)
160160
# ldexp, # T ldexp(T x, Ti k)
161161
# modf, # T modf(T x, T &intval)
162-
# nextafter, # T nextafter(T x, T y) # Metal 3.1+
163-
# sincos,
164162
hypot, # NOT MSL but tested the same
165163
]
166164

@@ -358,6 +356,25 @@ end
358356
vec = Array(expm1.(buffer))
359357
@test vec expm1.(arr)
360358
end
359+
360+
361+
let # nextafter
362+
if Metal.is_macos(v"14")
363+
N = 4
364+
function nextafter_test(X, y)
365+
idx = thread_position_in_grid_1d()
366+
X[idx] = Metal.nextafter(X[idx], y)
367+
return nothing
368+
end
369+
arr = rand(T, N)
370+
buffer = MtlArray(arr)
371+
Metal.@sync @metal threads = N nextafter_test(buffer, typemax(T))
372+
@test Array(buffer) == nextfloat.(arr)
373+
374+
Metal.@sync @metal threads = N nextafter_test(buffer, typemin(T))
375+
@test Array(buffer) == arr
376+
end
377+
end
361378
end
362379
end
363380

0 commit comments

Comments
 (0)