Why do java intrinsic functions still have code?
Asked Answered
D

3

10

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?

Drewdrewett answered 13/4, 2014 at 9:28 Comment(6)
what is the definition of intrinsics as per your understanding?Unseat
My current understanding is they are specially marked functions by the compiler, which are not compiled using the languages code, but the compiler already has an implementation for them, and uses that as a substitution. I'm curious, if that is the case, why there is still language code associated with the function.Drewdrewett
So which specific intrinsic functions are you talking about? And what makes you think Integer.bitCount() is none of them? Your question just appears to embody a self-contradiction.Eyeglass
@Keppil this is not a dupe, that other question is asking when it will be applied, this is why there is still byte code associated with it (I presume from context).Clad
@owlstead: The questions are very similar though, and this one is answered in the other question's answers.Seldon
That's true, especially the line " in the sense that implementation is marked to be replaceable with a native asm instruction" hints that the byte code is there, but that it is replaced. But that's a hint, not a full blown answer imho.Clad
U
11

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.

Unseat answered 13/4, 2014 at 9:45 Comment(0)
P
6

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.

Phyllous answered 13/4, 2014 at 9:44 Comment(0)
T
-2

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.

https://en.wikipedia.org/wiki/Intrinsic_function

Teresaterese answered 29/10, 2020 at 11:10 Comment(2)
I think this is just the wikipedia arcticle quote. Can you give a more specific explanation to the question?Elsie
@cuh: Not only is this just quoting wikipedia, the accepted answer already quotes this from wikipedia. This answer adds nothing, and is possibly copying another answer. Also, this part of the quote doesn't explain at all why there would still be pure-Java code to define such functions. The compiler handles it specially, so a real definition shouldn't be needed...Zawde

© 2022 - 2024 — McMap. All rights reserved.