Hacker News new | past | comments | ask | show | jobs | submit login
C++: A jump table with a template device (monoinfinito.wordpress.com)
25 points by ingve on May 6, 2015 | hide | past | favorite | 5 comments



Isn't this just doing the integer log to base 2 of x (or put another way, determining the highest set bit). Search for bit manipulation hacks and you'll find people have already thought about how to do that as efficiently as possible. The point of the article is perhaps more to do with how gcc optimizes templates but it'd be better if the initial non-template code was less contrived.


As the commenter ploxiln on the site noted, it is questionable if the author presents the codes which are actually equivalent.

As far as I understand, at least if the less sign from one function should become greater or equal in another.

It also appears to me that the starting function by definition has a fixed end limit number over which the f() isn't called, and that this limit gets set by every Foo<x> differently.


Since you are already doing it sequentially, why not do it in a loop? Something like:

    for(int i = 2; i <= 512; i *= 2) {
        if(x < i) {
            f(x);
            break;
        }
    }
It could be the same performance with some loop unrolling also; I don't know how much gcc does that.


You can also put the x < i part in the loop's head and check in advance that x < 512. That way you can get rid of the inner if.


A more compelling example would have been nice, considering you can write a single line constexpr function that calculates "My_Size". You can do some pretty neat (and actually useful) things with parameter packs and recursive templates.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: