# \[ANN\] SIMDMathFunctions.jl : Fast vectorized mathematical functions for SIMD.jl , using SLEEFPirates.jl

**URL:** <https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021>\
**Category:** Package Announcements\
**Tags:** package, announcement\
**Created:** [March 1, 2024, 3:13pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021 "2024-03-01T15:13:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 1, 2024, 3:13pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/1 "2024-03-01T15:13:05Z")

</div>

Feedback welcome, especially on the package name which I intend to register in a matter of days. The name could be `SIMDFunctions` for instance.

From the README ([GitHub - ClimFlows/SIMDMathFunctions.jl](https://github.com/ClimFlows/SIMDFastMath)):

```julia
] add https://github.com/ClimFlows/SIMDFastMath

```

Then

```julia
using SIMD: VecRange
using SIMDFastMath: is_supported, is_fast, fast_functions
using BenchmarkTools

function exp!(xs::Vector{T}, ys::Vector{T}) where {T}
    @inbounds for i in eachindex(xs,ys)
        xs[i] = @fastmath exp(ys[i])
    end
end

function exp!(xs::Vector{T}, ys::Vector{T}, ::Val{N}) where {N, T}
    @assert length(ys) == length(xs)
    @assert length(xs) % N == 0
    @assert is_supported(@fastmath exp)
    @inbounds for istart in 1:N:length(xs)
        i = VecRange{N}(istart)
        xs[i] = @fastmath exp(ys[i])
    end
end

y=randn(Float32, 1024*1024); x=similar(y);

@benchmark exp!($x, $y)
@benchmark exp!($x, $y, Val(8))
@benchmark exp!($x, $y, Val(16))
@benchmark exp!($x, $y, Val(32))

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 1, 2024, 11:21pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/2 "2024-03-01T23:21:07Z")

</div>

SIMDMathFunctions is clear.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 2, 2024, 8:14am UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/3 "2024-03-02T08:14:29Z")

</div>

Your README gives `erf` as an example for adding support for a function without an optimized implementation.

`erf` does have one, though:

> <https://github.com/JuliaSIMD/VectorizationBase.jl/blob/master/src/special/verf.jl>

---

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 2, 2024, 3:40pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/4 "2024-03-02T15:40:51Z")

</div>

Sounds like a better name indeed.

---

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 2, 2024, 3:50pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/5 "2024-03-02T15:50:37Z")

</div>

Hum, I did not catch that one, thanks. However one difficulty is that `erf` is not in `Base`, it is provided by `SpecialFunctions.jl` . `SIMDFastMaths` could depend on `SpecialFunctions` and implement `erf` for `SIMD.Vec` arguments by calling `VectorizationBase.verf`, but it looks backwards to me. Should a vectorized `erf` not rather be part of `SpecialFunctions.jl` ?

Anyway I will improve the example in the README.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 2, 2024, 7:49pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/6 "2024-03-02T19:49:10Z")

</div>

That’s a usecase for a pkg extension.  
See [here](https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions)) for more details.  
But the short of it is, you can write code in one package that only gets loaded when another package does.

LoopVectorization does this for SpecialFunctions, so that `Base.erf(::VectorizatoinBase.AbstractSIMD)` gets defined to use `VextorizationBase.verf`

> <https://github.com/JuliaSIMD/LoopVectorization.jl/blob/9ea87422cd9ae0e11e7768be26153840e5f488c8/ext/SpecialFunctionsExt.jl#L6>

This really should have been an extension to VectorizationBase instead of LoopVectorization. However, in the pre-pkg extension era, this code was loaded unconditionally, and I didn’t want VB to pay the cost. It should be moved, but that’d make version compatibility a little awkward.

---

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 3, 2024, 8:40am UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/7 "2024-03-03T08:40:20Z")

</div>

How about making this an extension of SpecialFunctions instead ?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 3, 2024, 3:02pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/8 "2024-03-03T15:02:24Z")

</div>

I have no permissions there. You’d have to ask the maintainers.

---

<div class="post-metadata">

**Author:** ![viralbshah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/viralbshah/32/54_2.png) [@viralbshah](https://discourse.julialang.org/u/viralbshah)\
**Post date:** [March 3, 2024, 3:35pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/9 "2024-03-03T15:35:07Z")

</div>

Happy to give you access to SpecialFunctions.jl. What’s your github ID? Alternately, open an issue on the repo and ping me - ViralBShah.

---

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 4, 2024, 3:58pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/10 "2024-03-04T15:58:26Z")

</div>

Thanks for offering. I guess I will first rename and register the package. Then I can open a PR for a weak dep of SpecialFunctions. This would however cover only `erf`, among the many functions provided. Is it worth it ?

---

<div class="post-metadata">

**Author:** ![dubosipsl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dubosipsl/32/52998_2.png) [@dubosipsl](https://discourse.julialang.org/u/dubosipsl)\
**Post date:** [March 18, 2024, 3:26pm UTC](https://discourse.julialang.org/t/ann-simdmathfunctions-jl-fast-vectorized-mathematical-functions-for-simd-jl-using-sleefpirates-jl/111021/11 "2024-03-18T15:26:44Z")

</div>

The package is now registered as [SIMDMathFunctions](https://github.com/ClimFlows/SIMDMathFunctions.jl)
