Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do java intrinsic functions still have code?

There are many methods in the Java API that are intrinsics, but still have code associated with them when looking at the source code.

For an example, Integer.bitCount() is an intrinsic, but if you open the Integer class file, you can see code with it.

What purposes might this code serve if it is not necessarily used by the compiler/jvm?

like image 911
user3507579 Avatar asked Apr 13 '14 09:04

user3507579


People also ask

What are intrinsic functions in Java?

An intrinsic function is a function that performs a mathematical, character, or logical operation. You can use intrinsic functions to make reference to a data item whose value is derived automatically during execution.

What is intrinsic code?

If a function is an intrinsic, the code for that function is usually inserted inline, avoiding the overhead of a function call and allowing highly efficient machine instructions to be emitted for that function.

Which is intrinsic function used in JavaScript?

JavaScript provides intrinsic (or "built-in") objects. They are the Array , Boolean , Date , Error , Function , Global , JSON , Math , Number , Object , RegExp , and String objects.


2 Answers

As per wiki, the definition of Intrinsic Function is as follows:

In compiler theory, an intrinsic function is a function available for use in a given programming language whose implementation is handled specially by the compiler. Typically, it substitutes a sequence of automatically generated instructions for the original function call, similar to an inline function. Unlike an inline function though, the compiler has an intimate knowledge of the intrinsic function and can therefore better integrate it and optimize it for the situation. This is also called builtin function in many languages.

Further it says, important and relevant to your question:

Compilers that implement intrinsic functions generally enable them only when the user has requested optimization, falling back to a default implementation provided by the language runtime environment otherwise.

So, it means a default implementation is used most of the time until optimization is not requested or possible (this depends on which machine/configuration JVM is running on). JVM can replace the whole Integer.bitCount() code to an optimized machine code instruction.

Further, check this discussion which explains the point nicely with an example code.

like image 107
sakura Avatar answered Oct 06 '22 04:10

sakura


A default implementation is provided in case there's no possibility of using the intrinsic version, i.e running on a platform for which no intrinsic version is provided.

like image 32
perencia Avatar answered Oct 06 '22 02:10

perencia