Skip to content

Commit 9eff7d2

Browse files
committed
Add nextafter intrinsic
1 parent e3d369e commit 9eff7d2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/device/intrinsics/math.jl

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

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

test/device/intrinsics.jl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +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+
163162
hypot, # NOT MSL but tested the same
164163
]
165164

@@ -353,6 +352,25 @@ end
353352
vec = Array(expm1.(buffer))
354353
@test vec expm1.(arr)
355354
end
355+
356+
357+
let # nextafter
358+
if Metal.is_macos(v"14")
359+
N = 4
360+
function nextafter_test(X, y)
361+
idx = thread_position_in_grid_1d()
362+
X[idx] = Metal.nextafter(X[idx], y)
363+
return nothing
364+
end
365+
arr = rand(T, N)
366+
buffer = MtlArray(arr)
367+
Metal.@sync @metal threads = N nextafter_test(buffer, typemax(T))
368+
@test Array(buffer) == nextfloat.(arr)
369+
370+
Metal.@sync @metal threads = N nextafter_test(buffer, typemin(T))
371+
@test Array(buffer) == arr
372+
end
373+
end
356374
end
357375
end
358376

0 commit comments

Comments
 (0)