Stories
Slash Boxes
Comments

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 18 submissions in the queue.
posted by Fnord666 on Thursday June 20 2019, @09:43AM   Printer-friendly
from the abusing-bits-for-fun-and...profit? dept.

Submitted via IRC for Bytram

Abusing A CPU's Adders To Optimize Bit Counting

If you like nitpicking around C code and generated assembly language — and we’ll admit that we do — you shouldn’t miss [Scaramanga’s] analysis of what’s known as Kernighan’s trick. If you haven’t heard of the trick, it’s a pretty efficient way of counting bits.

Like the Wheatstone bridge and a lot of other things, the Kernighan trick is misnamed. Brian Kernighan made it famous, but it was actually first published in 1960 and again in 1964 before he wrote about it in 1988. The naive way to count bits would be to scan through each bit position noting how many one bits you encounter, but the problem is, that takes a loop for each bit. A 64-bit word, then, takes 64 loops no matter what it contains. You can do slightly better by removing each bit you find and stopping when the word goes to zero, but that still could take 64 cycles if the last bit you test is set.

Using the trick, you make the observation that X & (X-1) will always clear the least significant bit of a word. Try a few examples:

   X      X-1   X&(X-1)
000100000000
001000010000
001100100010
101010011000
110010111000
111111101110

You can probably see where this is going. By computing X&(X-1) you clear a bit on each loop iteration and you only have to go through the number of bits that are actually set.

[...] If you like this sort of thing, be sure to check out [Sean Anderson’s] extensive list of bit hacks. It shows several different ways to count bits and do other common and uncommon tasks with different tradeoffs. For example, you could dedicate a 256-entry lookup table and do the whole thing with one loop per byte with great speed but bad memory utilization. Always a trade-off.


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 1, Interesting) by Anonymous Coward on Thursday June 20 2019, @10:39AM (8 children)

    by Anonymous Coward on Thursday June 20 2019, @10:39AM (#857846)

    was because he baked much of this stuff into C by adding more (than one) data types into B and BCPL.

    C++ took it a step too far. Go's (and plan 9's) runes are right at the edge of what you can get away with statically. Looking at Go2 they might screw it up with all those runtime interfaces... I guess that's around when people will start taking Nim seriously.

    Starting Score:    0  points
    Moderation   +1  
       Interesting=1, Total=1
    Extra 'Interesting' Modifier   0  

    Total Score:   1  
  • (Score: 4, Funny) by DannyB on Thursday June 20 2019, @03:10PM (5 children)

    by DannyB (5839) Subscriber Badge on Thursday June 20 2019, @03:10PM (#857951) Journal

    When your only hammer is C++, every problem begins to look like a thumb.

    --
    To transfer files: right-click on file, pick Copy. Unplug mouse, plug mouse into other computer. Right-click, paste.
    • (Score: -1, Troll) by Anonymous Coward on Thursday June 20 2019, @04:02PM (4 children)

      by Anonymous Coward on Thursday June 20 2019, @04:02PM (#857997)

      Says every idiot too stupid to use the language properly.

      • (Score: 0) by Anonymous Coward on Thursday June 20 2019, @05:29PM

        by Anonymous Coward on Thursday June 20 2019, @05:29PM (#858082)

        You sound like someone who ... um ... so, how's your thumb?

      • (Score: 2) by DannyB on Thursday June 20 2019, @08:17PM

        by DannyB (5839) Subscriber Badge on Thursday June 20 2019, @08:17PM (#858191) Journal

        It's not being an idiot. It's having better things to do with time, energy and money.

        I could write everything in assembly language. Yes. I really could. I have the skill. The result would be much smaller and with faster execution. The development time would suck.

        The people paying me would rather I write in a much higher level language. It's vastly cheaper to just throw an extra 64 GB of memory into a server. We're optimizing for dollars not bytes and cpu cycles.

        A language is too low level when it forces you to focus on things that are irrelevant to the problem.

        I also know how to manage memory. I did it for a couple decades before using garbage collection. Having GC is like having an automatic transmission. It is less efficient. But it eliminates several entire classes of bugs that you would call "using the language iimproperly".

        All said, I would say that using too low level a language is who is being an idiot. Both C / C++ have their place. But not for most software in the real world. The vast majority of software in the real world are invisible business applications you don't even think about. Your doctor. The place that changes your oil. A library. A bakery. A lawyer's office. Cities and local government utility billing.

        --
        To transfer files: right-click on file, pick Copy. Unplug mouse, plug mouse into other computer. Right-click, paste.
      • (Score: 0) by Anonymous Coward on Thursday June 20 2019, @11:01PM

        by Anonymous Coward on Thursday June 20 2019, @11:01PM (#858288)
      • (Score: 0) by Anonymous Coward on Friday June 21 2019, @05:22PM

        by Anonymous Coward on Friday June 21 2019, @05:22PM (#858613)

        I think there are more people who know how to use C++ properly who say that. I'm one of the people who modded it funny and I use C++. It also made me think of the old Stroustrup joke about shooting your leg off. I thought us C++ programmers had an abundance of self-irony? Not like those annoying Lisp people *wink* *wink* ;)

  • (Score: 0) by Anonymous Coward on Thursday June 20 2019, @03:20PM

    by Anonymous Coward on Thursday June 20 2019, @03:20PM (#857960)

    I wonder if Kernighan had read this?
        https://en.wikipedia.org/wiki/HAKMEM [wikipedia.org]

  • (Score: 2) by darkfeline on Friday June 21 2019, @03:05AM

    by darkfeline (1030) on Friday June 21 2019, @03:05AM (#858405) Homepage

    > Looking at Go2 they might screw it up with all those runtime interfaces

    I have no idea what this is referring to. Go 1 already has "runtime interfaces" (?), it's one of the key features of the language and to simplify is basically a way of getting duck typing in a static type system.

    The main sell of what is called Go 2 [1] is generics (a.k.a. parametric polymorphism), which can be defined entirely statically (although it can be implemented at runtime as an optimization for certain cases). Generics as proposed has nothing to do with interfaces nor runtime.

    [1] The developers made it clear that there will not be a Go version 2 that is not backward compatible with Go 1. Go 2 at this point refers to a number of significant proposed language and/or standard library features.

    --
    Join the SDF Public Access UNIX System today!