Skip to content

Commit

Permalink
Define quirks for complex sqrt (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Jun 20, 2024
1 parent 6f91c32 commit 59c7f1a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/device/quirks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ end
@print_and_throw "This operation requires a complex input to return a complex result"
@device_override @noinline Base.Math.throw_exp_domainerror(x) =
@print_and_throw "Exponentiation yielding a complex result requires a complex argument"
@device_override function Base.Math.exponent(x::T) where T<:Base.IEEEFloat
xs = reinterpret(Unsigned, x) & ~Base.sign_mask(T)
xs >= Base.exponent_mask(T) && @print_and_throw "Cannot be NaN or Inf."
k = Int(xs >> Base.significand_bits(T))
if k == 0 # x is subnormal
xs == 0 && @print_and_throw "Cannot be ±0.0."
m = leading_zeros(xs) - Base.exponent_bits(T)
k = 1 - m
end
return k - Base.exponent_bias(T)
end

# intfuncs.jl
@device_override @noinline Base.throw_domerr_powbysq(::Any, p) =
Expand Down Expand Up @@ -57,3 +68,18 @@ end
@print_and_throw "Out-of-bounds access of scalar value"
x
end

# complex.jl
@device_override function Base.ssqs(x::T, y::T) where T<:Real
k::Int = 0
ρ = x*x + y*y
if !isfinite(ρ) && (isinf(x) || isinf(y))
ρ = convert(T, Inf)
elseif isinf(ρ) ||==0 && (x!=0 || y!=0)) || ρ<nextfloat(zero(T))/(2*eps(T)^2)
m::T = max(abs(x), abs(y))
k = m==0 ? 0 : exponent(m)
xk, yk = ldexp(x,-k), ldexp(y,-k)
ρ = xk*xk + yk*yk
end
ρ, k
end
9 changes: 9 additions & 0 deletions test/device/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ end

############################################################################################

@testset "complex" begin
a = rand(ComplexF32,4)
bufferA = MtlArray(a)
vecA = Array(sqrt.(bufferA))
@test vecA sqrt.(a)
end

############################################################################################

@testset "synchronization" begin
# host/device synchronization
let
Expand Down

0 comments on commit 59c7f1a

Please sign in to comment.