When will JVM use intrinsics

The answer is simple: an intrinsic function is defined in this way because a faster, native way to obtain the result of the function exists and it is applied in case thanks to a specified mapping.

That’s not something related to compilation at all. Integer.bitCount is special in the sense that implementation is marked to be replaceable with a native asm instruction POPCNT. Basically this native instruction is used when using the Integer.bitCount function (if the CPU supports that instruction), when you declare your own copy of the function the normal implementation is used.

Why JVM is able to recognize that the function can be optimized? Because it’s hardcoded somewhere in the JDK, that has nothing to do with similarity to the code.

Leave a Comment