ModularM
Modular3y ago
3 replies
Leandro Lacerda

Are literals splatted in SIMD Operations?

Is there any performance difference if I write this

fn multiply_by_two[
    dtype: DType, simd_width: Int
](x: SIMD[dtype, simd_width]) -> SIMD[dtype, simd_width]:
    alias splatted_two = SIMD[dtype, simd_width](2.)
    return splatted_two * x


instead of this?

fn multiply_by_two[
    dtype: DType, simd_width: Int
](x: SIMD[dtype, simd_width]) -> SIMD[dtype, simd_width]:
    return 2.0 * x


In other words, is the literal 2 transformed into a splatted SIMD vector at runtime every time multiply_by_two is called? Or is this something that the compiler is able to optimize?
Was this page helpful?