Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • That explains it, also set me off onto an interesting path towards bit manipulations and compiler optimizations. Here is a great take on that topic: graphics.stanford.edu/~seander/bithacks.html Commented Feb 19, 2021 at 9:33
  • 1
    @DanielKrajnik Many of those bit-twiddling tricks are no longer useful in practice since CPUs offer many special-purpose instructions. For example, to count the number of bits set in an integer, we don't need a lookup table or a loop, we just need the POPCNT instruction (accessible in GCC with the __builtin_popcount() intrinsic function, available in all desktop CPUs of the last 8–14 years). Of course, compilers can sometimes use such functions automatically when they recognize suitable patterns in the code. Commented Feb 19, 2021 at 9:58
  • Thanks for clarifying that as well. Interesting, do intrinsics exist also in RISC processors like ARM? Also do compilers know when to "bit twiddle", for example would gcc possibly translate: r = (v < 0) ? -(unsigned)v : v into r = (v ^ mask) - mask Commented Feb 19, 2021 at 10:11
  • 1
    @DanielKrajnik Yes, GCC offers some ARM-specific intrinsics, but there's far fewer platform-specific stuff there. GCC will use exactly that bit-twiddling in your example on x86-64, but not on ARM since ARM offers super cheap conditional execution. I've toyed with some examples here: godbolt.org/z/rP13qz Commented Feb 19, 2021 at 14:25
  • amazing, thanks a lot for "godbolting" this example. I misunderstood at first, but now I see that intrinsic functions are built into the compiler and not CPUs. Commented Feb 19, 2021 at 18:07