Home
Admin | Edit

Code snippets

Note : Most of this is C code compiled with GCC.

C / Shell : Standalone single file compilation / execution (self-executable C file)

A C / Shell trick to pack the code, compilation and execution of a C program into a single file which is useful for short programs, prototypes, debugging or to show off C examples, put this into a test.c file and run it with sh test.c :
#ifdef d
#!/bin/sh
gcc -Wall -O0 test.c -o out
./out
exit
#endif

// your usual C code

C / x86 : SIMD debugging / exploration (with intrinsics)

This is a program i did as an exploration / debugging tool when i started to use SIMD instructions, it use compiler intrinsics (see the intrinsics guide) and just print the content of SIMD registers (here AVX), quite useful to quickly evaluate the result of some SIMD code. Just put it into a simd.c file and run it with sh simd.c :
#ifdef d
#!/bin/sh
gcc -Wall -mavx2 -m32 -O0 simd.c -o out
./out
exit
#endif

#include <stdio.h>
#include <x86intrin.h>

float input2[8] = {
    0.75f, 0.5f, 0.25f, 0.0f
};
float output[8] = {
    0
};

int main (void) {
    __m256i i1i = _mm256_set_epi32(0, 0, 0, 0, 0, 6, 4, 2);
    __m256 v1 = _mm256_cvtepi32_ps(i1i);
    __m256 i2 = _mm256_loadu_ps(&input2[0]);
    __m256 v2 = _mm256_mul_ps(v1, i2);
    __m256 v3 = _mm256_hadd_ps(v2, v2);
    _mm256_storeu_ps((float *)output, v3);

    for (int i = 0; i < 8; i += 1) {
        printf("%f ", output[i]);
    }
    printf("\n");

    return 0;
}

back to topLicence Creative Commons