The alignment pragma at type level is in heavy development at
[https://github.com/nim-lang/Nim/pull/11077](https://github.com/nim-lang/Nim/pull/11077)
Otherwise you have to declare all your variable like so
{.pragma: align16, codegenDecl: "$# $# __attribute__((aligned(16)))".}
let a {.align16.} = [float32 1.0, 2.0, 3.0, 4.0]
Run
I suggest you use one of the following to enforce alignment at the moment:
# X86-only
when defined(vcc):
{.pragma: x86_type, byCopy, header:"<intrin.h>".}
{.pragma: x86, noDecl, header:"<intrin.h>".}
else:
{.pragma: x86_type, byCopy, header:"<x86intrin.h>".}
{.pragma: x86, noDecl, header:"<x86intrin.h>".}
type
vector4_t* {.importc: "__m128", x86_type.} = array[4, float32]
template x(v: vector4_t): float32 =
v[0]
template y(v: vector4_t): float32 =
v[1]
template z(v: vector4_t): float32 =
v[2]
template w(v: vector4_t): float32 =
v[3]
template `x=`(v: vector4_t, val: float32) =
v[0] = val
template `y=`(v: vector4_t, val: float32) =
v[1] = val
template `z=`(v: vector4_t, val: float32) =
v[2] = val
template `w=`(v: vector4_t, val: float32) =
v[3] = val
var a: vector4_t
a.x = 1
a.y = 2
a.z = 3
a.w = 4
echo a.x
echo a.y
echo a.z
echo a.w
## Note "echo a" seems to be broken
Run
Or
# GCC/CLang only but works on X86, ARM, MIPS ...
{.emit:"""
typedef float vector4_t __attribute__ ((vector_size (16)));
"""
}
type vector4_t {.bycopy, importc.} = array[4, float32]
template x(v: vector4_t): float32 =
v[0]
template y(v: vector4_t): float32 =
v[1]
template z(v: vector4_t): float32 =
v[2]
template w(v: vector4_t): float32 =
v[3]
template `x=`(v: vector4_t, val: float32) =
v[0] = val
template `y=`(v: vector4_t, val: float32) =
v[1] = val
template `z=`(v: vector4_t, val: float32) =
v[2] = val
template `w=`(v: vector4_t, val: float32) =
v[3] = val
var a: vector4_t
a.x = 1
a.y = 2
a.z = 3
a.w = 4
echo a.x
echo a.y
echo a.z
echo a.w
## Note "echo a" seems to be broken
Run