5 ms·
Why was there no standard operator for this in C since the 1970s? You got various bit shifts and bitwise logic operators, so why not this? std::popcount exists
by Aardwolf 3y ago
Why was there no standard operator for this in C since the 1970s? You got various bit shifts and bitwise logic operators, so why not this? std::popcount exists in C++ now since C++20, but that took a while if this instruction was in CPU's that long.
- dataflow 3y agoMaybe because it hasn't been in x86 CPUs that long. Like Intel added it in 2009 or so. And there were intrinsics for it so I'm guessing it wasn't a high priority.
- Someone 3y agoBecause almost nobody had a computer that supported it when C was created. That article mentions 3 cpu architectures that have the instruction in the 1970s: 1961: IBM Stretch 1964: CDC 6000 1975: Cray-1 After that, it mentions further support in 2005, with Sparc. Reading https://en.wikipedia.org/wiki/IBM_7030_Stretch https://en.wikipedia.org/wiki/IBM_7030_Stretch, a grand total of 9 IBM Stretch’s were sold. In comparison, the CDC 600 was a wild success, with “100+” sales (https://en.wikipedia.org/wiki/CDC_6600 https://en.wikipedia.org/wiki/CDC_6600) The Cray-1 scores similarly with “over 100” (https://en.wikipedia.org/wiki/Cray-1 https://en.wikipedia.org/wiki/Cray-1) C is from 1972 and started life for the PDP-7 and was soon ported to the PDP-11, of which “around 600,000” were sold (https://en.wikipedia.org/wiki/PDP-11 https://en.wikipedia.org/wiki/PDP-11; I guess most of these will have been sold late in the 1970s and early 1980s, but I think it’s a safe bet they were almost immediately more common than those 3 machines)
- matthews2 3y agoAt least GCC and Clang can recognise most sane implementations of popcount and replace it with an instruction.
- pantalaimon 3y agoWe do have `stdc_popcount()` now with C23
- sigsev_251 3y agoIsnt't it stdc_count_ones?