Aussie AI
Assembly Language versus Intrinsics
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Assembly Language versus Intrinsics
Assembly language, or “assembler”, is the low-level language for CPU machine instructions. Like C++, it is still a symbolic human-readable language, but unlike C++, it translates mostly one-to-one to machine code instructions. The syntax for assembler is much simpler than C++, and more obscure, but it's also very, very fast.
When to use assembly language. The first question to ask yourself before writing assembler in C++ is whether you need to. The use of assembler should only be considered for the most bottlenecking parts of the code, like deep inside the inner loops of a GEMM kernel. Otherwise, you're probably micro-optimizing something that's not that critical.
Another question is whether to use “intrinsics” instead of assembler. Each C++ compiler has literally hundreds of builtin low-level functions called “intrinsics” that are very fast, probably because the compiler-writers have written them in assembler. There are also lots of intrinsics to use for GPU operations and CPU SIMD extensions such as AVX-512. There are also intrinsics that map one-to-one to x86 CPU instruction codes on that platform. Look through the long list of C++ intrinsics for your compiler platform to see if there's one that does what you need. The use of intrinsics is via a standard C++ function call syntax, so you don't need to learn assembler to take advantage of them.
Assembly language syntax: Here are some of the basics of assembly language coding:
- Assembly code filenames usually have a suffix of “
.S
”, “.s
” or “.asm
” (but don't need to). - Inline assembly inside C++ could be via
asm("string")
,__asm__("string")
, orasm { tokens }
, depending on the compiler. - Comments start with a semicolon (but you can also use C++ comments for inline assembly).
- One line per assembly statement.
- Jump or branch labels need a suffix colon and should start a line (either their own line or before a statement).
Disadvantages of Assembly Language: The reason that the C language came into being was to overcome some of the low-level problems of programming in assembly or machine code. There are various downsides to using assembly language:
- Non-portable — assembly is specific to the CPU and many features depend on CPU sub-releases.
- Pitfalls — and you thought C++ had troubles.
- Maintainability — few programmers know assembly.
- Complexity — everything's harder at the low-level.
To summarize, there's only two reasons to use assembly language: speed and security (of your job).
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |