Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/intrinsics/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SPIRVIntrinsics"
uuid = "71d1d633-e7e8-4a92-83a1-de8814b09ba8"
authors = ["Tim Besard <tim.besard@gmail.com>"]
version = "0.5.7"
version = "0.5.8"

[deps]
ExprTools = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
Expand Down
32 changes: 32 additions & 0 deletions lib/intrinsics/src/math.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Math Functions

using Base: @assume_effects

# TODO: vector types
const generic_types = [Float16, Float32, Float64]
const generic_types_float = [Float32]
Expand Down Expand Up @@ -183,6 +185,36 @@ end
# pown(x::Float64{n}, y::Int32{n}) = @builtin_ccall("pown", Float64{n}, (Float64{n}, Int32{n}), x, y)
@device_override Base.:(^)(x::Float64, y::Int32) = @builtin_ccall("pown", Float64, (Float64, Int32), x, y)

# Base's `^(::Union{Float16,Float32,Float64}, ::Int64)` widens Float32/16
# through Float64 (broken on backends without FP64) and in all cases leaves a
# runtime `pown` in the generated code. Mark the override `:foldable` so
# literal expressions like `Float32(2)^(-32)` const-fold to a compile-time
# constant, and recurse into the existing `::Int32` overrides for the tail.
@device_override @assume_effects :foldable @inline function Base.:(^)(x::Float16, y::Int64)
y == -1 && return inv(x)
y == 0 && return one(x)
y == 1 && return x
y == 2 && return x * x
y == 3 && return x * x * x
x ^ (y % Int32)
end
@device_override @assume_effects :foldable @inline function Base.:(^)(x::Float32, y::Int64)
y == -1 && return inv(x)
y == 0 && return one(x)
y == 1 && return x
y == 2 && return x * x
y == 3 && return x * x * x
x ^ (y % Int32)
end
@device_override @assume_effects :foldable @inline function Base.:(^)(x::Float64, y::Int64)
y == -1 && return inv(x)
y == 0 && return one(x)
y == 1 && return x
y == 2 && return x * x
y == 3 && return x * x * x
x ^ (y % Int32)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't this normally overflow to Inf instead of wrapping? Maybe replace y%Int32 with Float32(y)?

julia> 2.0^(Int64(2)^32 % Int32)
1.0

julia> 2.0^(Int64(2)^32)
Inf

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah OK that's sloppy. Thanks; I'll create a follow-up PR.

end

# remquo(x::Float32{n}, y::Float32{n}, Int32{n} *quo) = @builtin_ccall("remquo", Float32{n}, (Float32{n}, Float32{n}, Int32{n} *), x, y, quo)
# remquo(x::Float32, y::Float32, Int32 *quo) = @builtin_ccall("remquo", Float32, (Float32, Float32, Int32 *), x::Float32, y, quo)
# remquo(x::Float64{n}, y::Float64{n}, Int32{n} *quo) = @builtin_ccall("remquo", Float64{n}, (Float64{n}, Float64{n}, Int32{n} *), x, y, quo)
Expand Down
Loading