# Example 2

Example 2 demonstrates use of nested primitives and the use of an accessor inside a SIMD loop to generate efficient code.

    #include <stdio.h>
    #include <sdlt/sdlt.h>

    #define N 1024

    typedef struct XYZs {
        float x;
        float y;
        float z;
    } XYZTy;

    SDLT_PRIMITIVE(XYZTy, x, y, z)

    typedef struct RGBs {
        float r;
        float g;
        float b;
        XYZTy w;
    } RGBTy;

    SDLT_PRIMITIVE(RGBs, r, g, b, w)

    void main()
    {
        sdlt::soa1d_container<RGBTy> aContainer(N);
        auto a = aContainer.access();

        #pragma omp simd
        for (int k = 0; k<N; k++) {
            RGBTy c;
            c.r = k*1.5f;
            c.g = k*2.5f;
            c.b = k*3.5f;
            c.w.x = k*4.5f;
            c.w.y = k*5.5f;
            c.w.z = k*6.5f;
            a[k] = c;
        }
        const RGBTy c = a[10];
        printf("k = %d, a[k].r = %f, a[k].g = %f, a[k].b = %f \n",
            10, c.r, c.g, c.b);

        printf("k = %d, a[k].w.x = %f, a[k].w.y = %f, a[k].w.z = %f \n",
            10, c.w.x, c.w.y, c.w.z);

Parent topic: Examples