Here is some pseudocode for what I would like to do:
template SIMD(actions:untyped) =
if AVX2_Available():
import AVX2 # use the avx2 version of Add/Mul etc.
actions
else:
import SSE # use the sse version of Add/Mul etc.
actions
SIMD:
let c = SIMD_Add(a,b)
SIMD_Mul(b,c)
But I can't do that because imports have to be top level. I can do include instead of import, that will lead to massive code size if someone had multiple uses of the template in their code, as it will reproduce the same functions over and over.
Is there some way to accomplish this?
Have you tried something like
when AVX2_Available():
Of course that can only work when your AVX2_Available() is known at compile time...I would be interested to know how you check that AVX2 is available at runtime, a try ... except block?
At compile-time you can use the approach done in QEX.