1. Field of the Invention
The present invention generally relates to a technique for rapid program compilation, and more particularly to a method for providing accurate floating-point computation.
2. Description of the Related Art
In a floating-point computation in Java® (a registered trademark of Sun Microsystems Incorporated), precisions for a single precision computation and a double precision computation are defined by a language specification (24 bits mantissa and 8 bits exponent for the single precision computation, and 53 bits mantissa and 11 bits exponent for the double precision computation, respectively).
With CPUs of IA32 (Intel Architecture-32) architecture (the x86 family of Intel Corporation and compatible CPUs), it has been required to take either of the following approaches in order to execute both single precision computations and double precision computations:
(Approach 1) switching the CPU's floating-point computation mode by setting the floating-point control word (hereinafter, referred to as FCW); or
(Approach 2) executing the computation in the double precision mode, and if a computation result is to be obtained in single precision, storing the value on a floating-point register into a memory in single precision and reading it to degrade the computation precision.
A method executing the single precision computation is considered by way of example. Generally, this method may be called in the double precision computation CPU mode (hereinafter, referred to as a double precision mode) or in the single precision computation CPU mode (hereinafter, referred to as a single precision mode). If the method is called in the single precision mode, it is more efficient to execute the code assuming the single precision mode. If the method is called in the double precision mode, it is necessary to prevent loss of computation precision according to Approach 1 or Approach 2 described above. One means of improving efficiency is to analyze the program to compare execution costs for the case using Approach 1 and for the case using Approach 2, and to execute the faster Approach, i.e. the one with less overhead (this analysis is hereinafter referred to as an inter-method analysis).
In a separate compilation environment for compiling a method having a high execution frequency, as in a dynamic compiler such as a JIT (Just In Time) compiler for Java®, it is not known whether the method being compiled will be called in the single precision mode or in the double precision mode. Therefore, for example, the precision mode is fixed as double precision at method boundaries (that is, all methods are called in the double precision mode), and if the single precision computation is executed, a code is generated according to the above described cost calculation.
A technique for the above described cost calculation and code generation is described in the following prior art document, incorporated here by reference:
M. Paleczny, C. Vick, and C. Click. The Java® HotSpot Server Compiler. The USENIX Association: Proceedings of the Java® Virtual Machine Research and Technology Symposium (JVM '01).
The above described prior art has a disadvantage in that the execution cost is high in either Approach 1 or Approach 2 and large overhead is incurred.
Specifically, in the case of Approach 1, a switch instruction for the FCW, with a form of, for example, (fldcw word ptr [mem]), is a high cost instruction. In addition, once the FCW is switched, it is necessary to switch the FCW again before and after a method call in that range and return the mode to the double precision mode as a standard, which may cause further overhead.
In the case of Approach 2, instructions for writing into and reading from a memory are executed whenever the single precision computation is executed; memory access may cause overhead and degrade overall execution performance.
In addition, in the dynamic compilation environment, an inter-method analysis may, for example, have excessive compilation time overhead, and so may not be effective for choosing a calculation Approach. Moreover, since new classes can be loaded dynamically, an analysis result may become invalid later, thus wasting computation resources.
Therefore, it is an object of the present invention to provide an efficient mechanism for preventing loss of precision for both a single precision computation and a double precision computation in a floating-point computation, while avoiding execution overhead and invalidation of the analysis result.