Hi! Today’s lecture, we learned SIMD - Single Instruction Multiple Data. This is a great tool to process data in a bulk fashion. So, instead of doing one by one, based on the variable size, we can do 16, 8, 4 or 2 at the time. This technique is called auto-vectorization resources, and it falls into the category of machine instruction optimization that I mentioned in my last post.
If the machine is SIMD enabled, the compiler can use it when translating a sum loop, for example. If we are summing 8 bits numbers, using SIMD, it will be 16 times faster. However, the compiler can figure that it is not safe to use SIMD due to overlapping or non-aligned data. In fact, the compiler will not apply SIMD in most cases, so we need to get our hands dirty and inject some assembly. I’ll show you how to do it in a second.
Here are the lanes of the 128-bit AArch64 Advanced SIMD:
16 x 8 bits
8 x 16 bits
4 x 32 bits
2 x 64 bits
1 x 128 bits
Reading the ARM manual, we can find a lot of SIMD functions. Bringing back the volume example, we can process 8 values each time, and not worry about the overflow. The magic instruction is SQDMULH – Signed Integer Saturating Doubling Multiply returning High Half. With that name, it must make coffee too! Well, no. It multiplies the first parameter with the second. It puts the result into the third, discarding the fraction portion and not overflowing – on overflow, it will keep the minimum or maximum value. It is precisely what we need to deal with the volume in one instruction.
Now let’s mix some C and Assembly, shall we?
The syntax is:
__asm__ ("assembly code" : outputs : inputs : clobbers);
Warning: this will break the portability. It is a good idea to have compiler flags to “pick” the right portion of the code based on the architecture being compiled. Here we are not doing that.
This is the code provided by our instructor. Do you see the loop in C and the ASM instruction inside? The line 52 is doing 8 values per iteration using the magic single instruction SQDMULH. It is fast! The code, as it is, will only work on Arch64, though.
If you don’t like assembly like me, intrinsics will help. The GCC compiler has some sort of functions representations of the assembly instructions. I think that it helps, but it also has its limitations. Here is the same example, but using intrinsics. Take a look at line 42.
This is it for today. I’m working on profiling my awk build. Stay tuned!
If the machine is SIMD enabled, the compiler can use it when translating a sum loop, for example. If we are summing 8 bits numbers, using SIMD, it will be 16 times faster. However, the compiler can figure that it is not safe to use SIMD due to overlapping or non-aligned data. In fact, the compiler will not apply SIMD in most cases, so we need to get our hands dirty and inject some assembly. I’ll show you how to do it in a second.
Here are the lanes of the 128-bit AArch64 Advanced SIMD:
16 x 8 bits
8 x 16 bits
4 x 32 bits
2 x 64 bits
1 x 128 bits
Reading the ARM manual, we can find a lot of SIMD functions. Bringing back the volume example, we can process 8 values each time, and not worry about the overflow. The magic instruction is SQDMULH – Signed Integer Saturating Doubling Multiply returning High Half. With that name, it must make coffee too! Well, no. It multiplies the first parameter with the second. It puts the result into the third, discarding the fraction portion and not overflowing – on overflow, it will keep the minimum or maximum value. It is precisely what we need to deal with the volume in one instruction.
Now let’s mix some C and Assembly, shall we?
The syntax is:
__asm__ ("assembly code" : outputs : inputs : clobbers);
Warning: this will break the portability. It is a good idea to have compiler flags to “pick” the right portion of the code based on the architecture being compiled. Here we are not doing that.
This is the code provided by our instructor. Do you see the loop in C and the ASM instruction inside? The line 52 is doing 8 values per iteration using the magic single instruction SQDMULH. It is fast! The code, as it is, will only work on Arch64, though.
If you don’t like assembly like me, intrinsics will help. The GCC compiler has some sort of functions representations of the assembly instructions. I think that it helps, but it also has its limitations. Here is the same example, but using intrinsics. Take a look at line 42.
This is it for today. I’m working on profiling my awk build. Stay tuned!
Comments
Post a Comment