3 ms·
ANSI C doesn't expose any intrinsics, but once you get used to GCC extensions and attributes, it starts to feel a lot more "low-level". Think of __builtin_expec
by bluetomcat 12y ago
ANSI C doesn't expose any intrinsics, but once you get used to GCC extensions and attributes, it starts to feel a lot more "low-level". Think of __builtin_expect, __builtin_prefetch, alignment attributes, etc.
- pjmlp 12y agoThat is compiler specific without any portability guarantee to another compiler, you can just write Assembly instead, for what is worth.
- bluetomcat 12y agoIt's not quite like writing assembly because these intrinsics are meant to translate to the "best" instructions for the particular ISA that the code is targeted for. You are able to compile the same chunk of code for two different architectures, for example. If you were to write inline assembly, you would be forced to have a separate chunk of code for each architecture and surround them with #ifdefs.
- pjmlp 12y agoAssuming the same compiler is used.
- falcolas 12y agoThe code can be made portable with a few simple macros (which is why they are implemented they way they are in GCC), though the optimizations will not be. From the Linux kernel: struct boot_params boot_params __attribute__((aligned(16))); #if !__GNUC__ #define __attribute__(x) #endif I think that's a better way to do it than ASM (which totally hoses your portability).
- pjmlp 12y agoFor alignment yes, but that is not the case across all extensions. Some of them are no different than just writing plain Assembly.
- falcolas 12y agoThe __builtin_ methods mentioned by the grandparent are functionally identical to the associated libc methods, so they can be #defined away as well.