# Arithmetic Overflow and Intrinsics

DevFeed: [Arithmetic Overflow and Intrinsics](<https://devfeed.tech/articles/arithmetic-overflow-and-intrinsics-30646.md>)

Original publisher: [Read original article](<http://bad-concurrency.blogspot.com/2012/08/arithmetic-overflow-and-intrinsics.html>)

Author: Michael Barker (noreply@blogger.com)

Published: 2012-08-30T12:05:00Z

Content type: tutorial

Language: en

Sources: [Bad Concurrency](<https://devfeed.tech/sources/bad-concurrency.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [JIT](<https://devfeed.tech/topics/jit.md>), [x86](<https://devfeed.tech/topics/x86.md>), [Assembly](<https://devfeed.tech/topics/assembly.md>), [Arm](<https://devfeed.tech/topics/arm.md>)

Tags: [arm](<https://devfeed.tech/tags/arm.md>), [bits](<https://devfeed.tech/tags/bits.md>), [java](<https://devfeed.tech/tags/java.md>), [jit](<https://devfeed.tech/tags/jit.md>), [jvm](<https://devfeed.tech/tags/jvm.md>), [x86](<https://devfeed.tech/tags/x86.md>)

## AI overview

This article explains JVM intrinsics: recognizable code patterns that the JVM can compile into more efficient machine-specific assembly. It uses Java's Integer.bitCount() and Intel's POPCNT instruction to show how an intrinsic can replace a larger sequence of CPU instructions with one instruction, and outlines how HotSpot identifies and JIT-compiles frequently used methods.

## Source excerpt

During a recent conversation on the LJC mailing list around a proposal for adding a library to the JDK that would add support for handling integer overflow a question arose. Would the JVM be able to optimise this code to make efficient use of the hardware support for overflow detection if this functionality was implemented as a library. I made the comment that this is problem is probably one best solved using intrinsics, but in the course of writing an explanation I thought it would be better explained in a blog post, so here goes... What is an Intrinsic? From the JVM perspective an intrinsic an identifiable code pattern (typically a method) where the JVM understands the intent, such that it can be complied to more optimal machine specific assembly. This is really useful when you have multiple target platforms and a subset of those targets contain instructions that may not be available on the others. E.g. Intel's X86 instruction set is quite rich when compared to a RISC-type processor, such as ARM. An Example using POPCNT One of the simplest examples of an intrinsic is the Integer.bitCount() method and the optimisation into Intel's POPCNT instruction (available on Nehalem and later), partially because it can be disabled and the effects of it not being applied are easy to observe. Lets start with some simple code that calls the Integer.bitCount() method: The implementation of the Integer.bitCount() is a reasonably complex combination of arithmetic and bit shifting in order to calculate the number of bits set to 1 within a given int1. If we run the PopCntTest class and print out the assembler generated by hotspot, we can see that this will result in quite a large number of instructions that need to be issued to the CPU. Running the class using following command line (-XX:-UsePopCountInstruction disables the intrinsic): java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly -XX:-UsePopCountInstruction PopCntTest. Generates the following assembly code: Now, lets look at