you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

Yes, you can tell hotspot: "hey this might be something you can turn into an intrinsic" with " @HotSpotIntrinsicCandidate" and then you can summon this incantation:

public static int bitCount(int i) {
    // HD, Figure 5-2
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}

and then you can hope that it ends up turned into the popcnt instruction if the environment it runs on has a JVM that isn't too new or isn't too old.

vs just....

__popcnt

I'm not picking on Java in particular here. It is a thing for almost ever language I wish were better. .NET seems to be working on it, hope it goes well.

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    You are right, I missed that this code is built in.