2

I'm reasking a question that had too little attention I think:

Why does this simple code (simply a call to Math.log()):

Double thisdouble = Math.log(10);

With a breakpoint on line 275 of Math.class of the jdk1.7.0_11:

274 public static double log(double a) {
275    return StrictMath.log(a); // default impl. delegates to StrictMath
276 }

Not stop execution in debug mode? Can somebody try this on his/her own machine (I'm using Eclipse)?

Calling Math.exp() and debugging the Math.exp(line 254) function does work...

EDIT: The answer to the above is that Math.log is replaced by an intrinsic call by the Hotspot VM so the code in the Math class is never reached. The question which remains now is why Math.exp is not replaced by an intrinsic... FWIW I'm on a Core i5 M520 (Arrandale), but I would seriously doubt that that processor has support for log and not for exp...

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
reverse_engineer
  • 4,239
  • 4
  • 18
  • 27
  • 2
    The hotspot VM substitutes many of the methods in java.lang.Math with intrinsics, which means that instead of going through all the hoops needed for a method call it just calculates the result. What surprises me is that you can set a breakpoint in Math.exp, which is supposed to be an intrinsic as well. – Joni Feb 27 '13 at 06:45
  • Interesting point, but aren't intrinsics just the fact that the natives that are called (in StrictMath) are different depending on your architecture? Or does the hotspot VM (whatever that is) really replace parts of the code, and that would explain why we don't go through `Math.log`? And then why doesn't that happen for `Math.exp`? – reverse_engineer Feb 27 '13 at 08:16
  • My understanding of intrinsics is that the JVM executes a bunch of internal code instead of calling a native or a Java method. And it should happen with Math.exp [and many other methods in java.lang.Math too](http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/file/6e9aa487055f/src/share/vm/classfile/vmSymbols.hpp) so the question is if the intrinsic is not being used for `exp`, or if the debugger disables the intrinsic for `exp` but not for `log`. In either case this looks like a bug. – Joni Feb 27 '13 at 09:26
  • Yes, so the question is indeed more why he would stop in exp... Well, you basically answered my question. Thanks, my next question will be why is log intrinsicized (is that a word?) and exp not... – reverse_engineer Feb 27 '13 at 16:21

1 Answers1

2

I would assume that the code in the Math class is only a fallback code, used by those architectures where the method invocation isn't substituted by a call to some native floating point operation instead. So the method doesn't actually get called in your case. I must confess I don't have evidence tu support this assumption, though.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • Thank you for your answer. Indeed Joni already answered the question more or less. A call to Math.log is replaced by an intrinsic method, so indeed the Math class is not used there. What I wonder now is why Math.exp does use the Math class... – reverse_engineer Feb 28 '13 at 07:45
  • @reverse_engineer: The difference between `log` and `exp` should be [removed](http://hg.openjdk.java.net/jdk7u/jdk7u/hotspot/rev/6759698e3140) in more recent releases of OpenJDK. – MvG Feb 28 '13 at 08:57