An Adaptive Optimization Method and Device for Converting Floating Point to Integer in a JavaScript Engine
By adaptively generating floating point to integer instructions, the problems of low efficiency and poor stability of floating point to integer operations in the JavaScript engine are solved, and efficient and correct conversion results are achieved.
Patent Information
- Application Number
- CN202211418042.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-14
- Publication Date
- 2025-07-11
- Estimated Expiration
- 2042-11-14
AI Technical Summary
When JavaScript engines use floating point to integer operations, there are inefficiency and stability problems caused by processor platform differences, especially when floating point to integer instructions overflow, the existing technology cannot handle them efficiently and correctly.
By obtaining floating point to integer conversion information, reading processor hardware information to determine its support for the truncation mode, adaptively generate different floating point to integer instructions, and optimize floating point to integer operation for different processor platforms.
It improves the computing efficiency and stability of the JavaScript engine, and can generate the most efficient instruction code based on the hardware instruction characteristics, and correctly handle overflow values in various situations.
Smart Images

Figure CN115794053B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to an adaptive floating-point to integer optimization method and device for a JavaScript engine, belonging to the technical field of JavaScript just-in-time compilation. Background Art
[0002] In JavaScript, all numerical types are stored in the form of 64-bit double-precision floating-point numbers (Double). However, when performing bitwise operations in JavaScript, the operands and return values must be 32-bit signed integers (Int32), and JavaScript also supports regular floating-point to integer operations. Therefore, two types of floating-point to integer conversions occur when JavaScript code is executed: Double to Int32 for bitwise operations and regular floating-point to integer conversions. The JavaScript engine has different processing methods for these two conversions: First, the JIT compiler matches the floating-point to integer module at runtime, and then determines which type of conversion it is. When it is a regular floating-point to integer conversion, it is distributed to the corresponding regular floating-point to integer operation module, and these modules directly perform the conversion. When it is a bitwise operation Double to Int32 conversion, it is distributed to the bitwise operation Double to Int32 module. In this conversion, a regular conversion is first performed and it is determined whether there is an exception. If there is an exception, the Double value is then converted to the Int32 truncation value through a mathematical splicing method.
[0003] When the floating-point number is not within the integer range, the JavaScript engine has different requirements for the conversion result values of these two conversions: In the bitwise operation Double to Int32 module, when an overflow occurs, the required target value is the truncation value of the target format; in the regular floating-point to integer module, the required result value is the maximum finite value of the target format.
[0004] On most current processor platforms, the target value obtained when the floating-point to integer instruction overflows is the maximum finite value of the target format. At this time, in the bitwise operation Double to Int32 module, multiple processing is required for floating-point values (not-a-number NAN, maximum and minimum values INF, different exponent values) that exceed the Int32 range, there are multiple branch jumps, the operation is complex, and the generated code volume is large and the efficiency is low. However, there are also some other processor platforms where the result obtained when the floating-point to integer instruction overflows is the truncation value of the target format. At this time, in the regular floating-point to integer operation, separate processing is required when an overflow occurs, otherwise there are correctness hidden dangers. Summary of the Invention
[0005] The object of the present invention is to overcome the deficiencies in the prior art and provide an adaptive floating-point to integer optimization method and device for a JavaScript engine. In this method, the JavaScript engine determines the strong / weak support of the processor for truncation through the processor hardware characteristics. In different support modes, different processing instructions are adaptively generated for the conventional floating-point to integer conversion and the bitwise operation Double to Int32 conversion to obtain an efficient and correct conversion value.
[0006] To achieve the above object, the present invention is implemented by the following technical solutions:
[0007] In the first aspect, the present invention provides an adaptive floating-point to integer optimization method for a JavaScript engine, including:
[0008] Obtain floating-point to integer conversion information;
[0009] Read the processor hardware information, and judge the strong / weak support of the processor for the truncation mode by comparing the processor information. Among them, the strong support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the truncated value of the target format, and the weak support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the largest finite value of the target format;
[0010] Under the strong / weak support of the processor for the truncation mode, respectively judge the floating-point to integer conversion information and match it to the bitwise operation Double to Int32 conversion module and the conventional floating-point to integer conversion module, and adaptively generate different floating-point to integer instructions.
[0011] Further, when the processor strongly supports the truncation mode, match the floating-point to integer conversion information to all floating-point to integer templates;
[0012] Judge whether it matches the bitwise operation Double to Int32 conversion template;
[0013] According to the judgment result, respectively select to perform the bitwise operation Double to Int32 conversion and the conventional floating-point to integer conversion on the floating-point to integer conversion information, assign a value to the result value, and end the process.
[0014] Further, the step of respectively selecting to perform the bitwise operation Double to Int32 conversion and the conventional floating-point to integer conversion on the floating-point to integer conversion information according to the judgment result includes:
[0015] If it is judged that it matches the bitwise operation Double to Int32 conversion template, perform the bitwise operation Double to Int32 conversion on the floating-point to integer conversion information, assign a value to the result value, and end the process;
[0016] If it is determined that the bitwise operation Double-to-Int32 conversion template is not matched, perform a conventional floating-point-to-integer conversion on the floating-point-to-integer conversion information, and determine whether an overflow occurs during the conversion. When an overflow occurs during the floating-point-to-integer conversion, assign the result value to the maximum finite value of the target format; if not, directly assign the result value and end the process.
[0017] Further, when the processor weakly supports the truncation mode, match the floating-point-to-integer conversion information to all floating-point-to-integer templates;
[0018] Determine whether the bitwise operation Double-to-Int32 conversion template is matched;
[0019] According to the judgment result, respectively select to perform a bitwise operation Double-to-Int32 conversion and a conventional floating-point-to-integer conversion on the floating-point-to-integer conversion information, assign a value to the result value, and end the process.
[0020] Further, the step of respectively selecting to perform a bitwise operation Double-to-Int32 conversion and a conventional floating-point-to-integer conversion on the floating-point-to-integer conversion information according to the judgment result includes:
[0021] If it is determined that the bitwise operation Double-to-Int32 conversion template is matched, perform a bitwise operation Double-to-Int32 conversion on the floating-point-to-integer conversion information, and then judge the range of the Double value. When the Double value is within the Int32 range, perform a corresponding conventional Double-to-Int32 instruction sequence conversion on all bitwise operation Double-to-Int32 conversions within the range. When it exceeds the Int32 range, obtain the correct result value through mathematical splicing;
[0022] If it is determined that the bitwise operation Double-to-Int32 conversion template is not matched, perform a conventional floating-point-to-integer conversion on the floating-point-to-integer conversion information, perform a conventional Double-to-Int32 instruction sequence conversion on the conventional floating-point-to-integer conversion, assign a value to the result value, and end the process.
[0023] Further, the step of judging the range of the Double value, when the Double value is within the Int32 range, performing a corresponding conventional Double-to-Int32 instruction sequence conversion on all bitwise operation Double-to-Int32 conversions within the range, and when it exceeds the Int32 range, obtaining the correct result value through mathematical splicing includes:
[0024] During the conversion process, by reading the information value in the floating-point status control register, it is determined whether an exception occurs in the conversion. If no exception occurs in the conversion, it indicates that this Double value is within the Int32 range, and the value obtained by the conventional Double-to-Int32 instruction sequence conversion is the correct result value. At this time, the result value is assigned, and the process ends;
[0025] If an exception occurs in the conversion, it is determined whether the floating-point number is a NaN (Not a Number) and infinity. The exponent values of NaN and infinity are special values, and it is determined by comparing the exponent value of the floating-point number. If the floating-point number is a NaN or infinity, the result value is assigned 0, and the process ends;
[0026] If the floating-point number is not a NaN or infinity, it is determined whether the exponent of the floating-point number is greater than the set value. If the exponent of the floating-point number is greater than the set value, it indicates that the last 32 bits of the floating-point number are 0, and the result value is assigned 0, and the process ends;
[0027] If the exponent of the floating-point number is not greater than the set value, the sign bit, exponent bit, and mantissa bit are respectively taken from the floating-point number within the remaining range, and the floating-point number is converted into an integer value by means of mathematical splicing. The result value is assigned the truncated value in the form of an integer Int32, and the process ends.
[0028] In a second aspect, the present invention provides an adaptive JavaScript engine floating-point to integer optimization device for a JavaScript engine, including:
[0029] A conversion information acquisition module for acquiring floating-point to integer conversion information;
[0030] A judgment module for reading the processor hardware information and judging the strong / weak support of the processor for the truncation mode by comparing the processor information. Among them, the strong support of the processor for the truncation mode means that the target value obtained by the floating-point to integer instruction when an overflow occurs is the truncated value of the target format, and the weak support of the processor for the truncation mode means that the target value obtained by the floating-point to integer instruction when an overflow occurs is the maximum finite value of the target format;
[0031] A conversion module for respectively judging and matching the floating-point to integer conversion information to a bitwise operation Double-to-Int32 conversion module and a conventional floating-point to integer conversion module under the strong / weak support of the processor for the truncation mode, and adaptively generating different floating-point to integer instructions.
[0032] In a third aspect, the present invention provides an electronic device, including a processor and a storage medium;
[0033] The storage medium is used for storing instructions;
[0034] The processor is used to operate according to the instructions to execute the steps of the method according to any one of the foregoing.
[0035] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, and when the program is executed by a processor, the steps of the method according to any one of the foregoing are implemented.
[0036] Compared with the prior art, the beneficial effects achieved by the present invention are as follows:
[0037] The present invention provides an adaptive floating-point to integer optimization method and device for a JavaScript engine. 1) When the platform strongly supports truncation instructions, directly convert the Double to Int32 of bit operations, omit branch operations in various cases, improve the operation efficiency, and add overflow processing to the conventional floating-point to integer operation to improve the stability of the engine; 2) When the platform weakly supports truncation instructions, directly convert the conventional floating-point to integer operation, omit the overflow value processing, and add various cases of processing to the Double to Int32 of bit operations to obtain the correct result.
[0038] By adopting the optimization method in the present invention, all floating-point to integer cases in the JavaScript engine can be uniformly processed, the most efficient instruction code can be generated according to the different characteristics of hardware instructions, the performance of the JavaScript engine can be improved, and the overflow values in various cases can be correctly processed, improving the stability and performance of the JavaScript engine. Description of the Drawings
[0039] Figure 1 It is a flowchart of floating-point to integer conversion for a JavaScript engine in an embodiment of the present invention.
[0040] Figure 2 It is a flowchart of the conversion from Double to Int32 for bit operations when the platform weakly supports the floating-point to integer truncation mode in the present invention. Detailed Embodiments
[0041] The present invention will be further described below with reference to the drawings. The following embodiments are only used to more clearly illustrate the technical solutions of the present invention and should not be used to limit the protection scope of the present invention.
[0042] Embodiment 1
[0043] This embodiment introduces an adaptive floating-point to integer optimization method for a JavaScript engine, which is used for a JavaScript engine and includes:
[0044] Obtain floating-point to integer conversion information;
[0045] Read the processor hardware information. By comparing the processor information, determine the strong / weak support of the processor for the truncation mode. Among them, strong support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the truncated value of the target format, and weak support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the maximum finite value of the target format;
[0046] Under the strong / weak support of the processor for the truncation mode, respectively judge the floating-point to integer conversion information and match it to the bitwise operation Double to Int32 conversion module and the conventional floating-point to integer conversion module, and adaptively generate different floating-point to integer instructions.
[0047] As Figure 1 shown, the adaptive floating-point to integer optimization method of the JavaScript engine provided in this embodiment specifically involves the following steps in its application process:
[0048] S1: The strong / weak support judgment module of the JavaScript engine for the processor truncation mode.
[0049] During the process of the JavaScript engine compiling the JavaScript source code, read the processor information. By comparing the specific attributes of the hardware, determine the strong / weak support of the processor for truncation. When the processor strongly supports truncation, jump to the S2 truncation strong support module for execution; if the processor weakly supports the truncation mode, jump to the S3 module for execution.
[0050] S2: The floating-point to integer module with strong truncation support.
[0051] S21: The floating-point to integer template matching module of the JavaScript engine.
[0052] Under strong support of the truncation mode, match all floating-point to integer templates. Include single-precision floating-point to word, single-precision floating-point to long word, double-precision floating-point to word, double-precision floating-point to long word, and bitwise operation double-precision floating-point to word.
[0053] S22: The bitwise operation Double to Int32 conversion template judgment module.
[0054] When it is judged that the bitwise operation Double to Int32 conversion template is matched, jump to the S221 module for execution, otherwise jump to the S222 module for execution.
[0055] S221: The bitwise operation Double to Int32 conversion module.
[0056] Perform the conversion of the conventional Double to Int32 instruction sequence, assign a value to the result value, and end the process.
[0057] S222: Conventional floating-point to integer conversion module.
[0058] Perform the conversion of the conventional floating-point to word instruction sequence, and determine whether an overflow occurs during the conversion. When an overflow occurs during the floating-point to integer conversion, assign the result value to the maximum finite value of the target format; if not, directly assign the result value. End the process.
[0059] S3: Truncated weakly supported floating-point to integer module.
[0060] S31: JavaScript engine floating-point to integer template matching module.
[0061] Similar to the S21 module, under the weak support of the truncation mode, match all floating-point to integer templates. Include single-precision floating-point to word, single-precision floating-point to long word, double-precision floating-point to word, double-precision floating-point to long word, and bitwise operation double-precision floating-point to word.
[0062] S32: Bitwise operation Double to Int32 conversion template judgment module.
[0063] When it is judged that the bitwise operation Double to Int32 conversion template is matched, jump to the S321 module to execute, otherwise jump to the S322 module to execute.
[0064] S321: Bitwise operation Double to Int32 conversion module.
[0065] Under the weak support of the truncation mode, the bitwise operation Double to Int32 conversion needs to determine the conversion method according to the range of the Double value: when the Double value is within the Int32 range, the conventional Double to Int32 conversion instruction sequence can be used; when it exceeds the Int32 range, the correct result value can be obtained only by means of mathematical splicing. The specific conversion process is as Figure 2 shown:
[0066] Step 100: Perform the conversion using the conventional Double to Int32 instruction and judge the exception. During the conversion, the information value in the floating-point status control register can be read to judge whether an exception occurs during the conversion. If no exception occurs, it means that this Double value is within the Int32 range, and the value obtained by the conventional Double to Int32 instruction is the correct result value. At this time, assign the result value and end the process. Otherwise, jump to step 200.
[0067] Step 200: Process the floating-point number with conversion exception. First, judge whether the floating-point number is a NaN and infinity. The exponent values of NaN and infinity are special values, and they are judged by comparing the exponent value of the floating-point number. If it is a NaN and infinity, assign the result value 0 and end the process. Otherwise, jump to step 300.
[0068] Step 300: Process floating-point numbers with an exponent greater than 84. If the exponent is greater than 84, it indicates that the last 32 bits of the floating-point number are 0. Assign 0 to the result value (DEST) and end the process. Otherwise, jump to Step 400.
[0069] Step 400: Process floating-point numbers within the remaining range. Extract the sign bit, exponent bit, and mantissa bit respectively, and convert the floating-point number into an integer value through mathematical splicing. Assign the result value in the form of an integer Int32 truncated value and end the process.
[0070] S322: Conventional floating-point to integer conversion module.
[0071] Perform a conventional floating-point to word instruction sequence conversion, assign a value to the result value, and end the process.
[0072] The technical problem to be solved by the present invention is aimed at the problem that there are two overflow requirements in the floating-point to integer conversion in the JavaScript engine, and the existing algorithms cannot be efficiently implemented. The adaptive floating-point to integer optimization method for the JavaScript engine proposed by the present invention generates corresponding processing instructions through different mode selections, improving the conversion efficiency and stability. The key points involved are:
[0073] 1. Truncation splicing processing for bitwise operations
[0074] When the platform has weak support for the truncation mode, in the Double to Int32 conversion of bitwise operations, all Double value ranges are covered, and each case of Double can be correctly processed.
[0075] 2. Boundary value processing for conventional conversions
[0076] When the platform has strong support for the truncation mode, add an overflow value processing to the conventional floating-point to integer operation to increase the stability of the engine.
[0077] 3. Full-coverage adaptive technology
[0078] The conversion method of the present invention covers all processing cases of floating-point to integer conversion information, and the code can be adaptively generated, which can improve the conversion efficiency and stability.
[0079] Embodiment 2
[0080] This embodiment provides an adaptive floating-point to integer optimization device for the JavaScript engine, which is used for the JavaScript engine and includes:
[0081] A conversion information acquisition module, which is used to acquire floating-point to integer conversion information;
[0082] A judgment module, configured to read the hardware information of a processor, and determine the strong / weak support of the processor for the truncation mode by comparing the processor information, where the strong support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the truncated value of the target format, and the weak support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the maximum finite value of the target format;
[0083] A conversion module, configured to respectively judge and match the floating-point to integer conversion information to the bitwise operation Double to Int32 conversion module and the conventional floating-point to integer conversion module under the strong / weak support of the processor for the truncation mode, and adaptively generate different floating-point to integer instructions.
[0084] Embodiment 3
[0085] This embodiment provides an electronic device, including a processor and a storage medium;
[0086] The storage medium is used to store instructions;
[0087] The processor is configured to operate according to the instructions to execute the steps of the method according to any one of Embodiment 1.
[0088] Embodiment 4
[0089] This embodiment provides a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the steps of the method according to any one of Embodiment 1 are implemented.
[0090] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and deformations can be made, and these improvements and deformations should also be regarded as the protection scope of the present invention.
Claims
1. An adaptive floating-point to integer optimization method for a JavaScript engine, characterized in that, For a JavaScript engine, including: Obtain floating-point to integer conversion information; Read the processor hardware information, and by comparing the processor hardware information, judge the strong / weak support of the processor for the truncation mode; Under the strong / weak support of the processor for the truncation mode, respectively judge and match the floating-point to integer conversion information to the bitwise operation Double to Int32 conversion module and the conventional floating-point to integer conversion module, and adaptively generate different floating-point to integer instructions; Among them, the strong support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the truncated value of the target format, and the weak support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the maximum finite value of the target format.
2. The adaptive JavaScript engine floating-point to integer optimization method according to claim 1, characterized in that: When the processor strongly supports the truncation mode, match the floating-point to integer conversion information to all floating-point to integer templates; Judge whether it matches the bitwise operation Double to Int32 conversion template; According to the judgment result, respectively select to perform bitwise operation Double to Int32 conversion and conventional floating-point to integer conversion on the floating-point to integer conversion information, assign a value to the result value, and end the process.
3. The adaptive floating-point to integer optimization method for a JavaScript engine according to claim 2, characterized in that The respectively selecting to perform bitwise operation Double to Int32 conversion and conventional floating-point to integer conversion on the floating-point to integer conversion information according to the judgment result includes: If it is judged that it matches the bitwise operation Double to Int32 conversion template, perform bitwise operation Double to Int32 conversion on the floating-point to integer conversion information, assign a value to the result value, and end the process; If it is judged that it does not match the bitwise operation Double to Int32 conversion template, perform conventional floating-point to integer conversion on the floating-point to integer conversion information, and judge whether an overflow occurs during the conversion. When an overflow occurs during the floating-point to integer conversion, assign the result value to the maximum finite value of the target format; if not, directly assign a value to the result value and end the process.
4. The adaptive JavaScript engine floating-point to integer optimization method according to claim 1, characterized in that: When the processor weakly supports the truncation mode, match the floating-point to integer conversion information to all floating-point to integer templates; Judge whether it matches the bitwise operation Double to Int32 conversion template; According to the judgment result, respectively select to perform bitwise operation Double to Int32 conversion and conventional floating-point to integer conversion on the floating-point to integer conversion information, assign a value to the result value, and end the process.
5. The adaptive floating-point to integer optimization method for a JavaScript engine according to claim 2, wherein The respectively selecting to perform bitwise operation Double to Int32 conversion and conventional floating-point to integer conversion on the floating-point to integer conversion information according to the judgment result includes: If it is determined that the bitwise operation Double to Int32 conversion template is matched, perform a bitwise operation Double to Int32 conversion on the floating-point to integer conversion information. First, judge the range of the Double value. When the Double value is within the Int32 range, perform a corresponding conventional Double to Int32 instruction sequence conversion on all bitwise operation Double to Int32 conversions within the range. When it exceeds the Int32 range, obtain the correct result value through mathematical concatenation; If it is determined that the bitwise operation Double to Int32 conversion template is not matched, perform a conventional floating-point to integer conversion on the floating-point to integer conversion information, perform a conventional Double to Int32 instruction sequence conversion on the conventional floating-point to integer conversion, assign a value to the result value, and end the process.
6. The adaptive JavaScript engine floating-point to integer optimization method according to claim 5, characterized in that The judgment of the range of the Double value, when the Double value is within the Int32 range, perform a corresponding conventional Double to Int32 instruction sequence conversion on all bitwise operation Double to Int32 conversions within the range. When it exceeds the Int32 range, obtain the correct result value through mathematical concatenation, including: During the conversion process, judge whether an exception occurs in the conversion by reading the information value in the floating-point status control register. If no exception occurs in the conversion, it means that this Double value is within the Int32 range, and the value obtained by the conventional Double to Int32 instruction sequence conversion is the correct result value. At this time, assign a value to the result value and end the process; If an exception occurs in the conversion, judge whether the floating-point number is a NaN (Not a Number) and infinity. The exponent values of NaN and infinity are special values. Judge by comparing the exponent value of the floating-point number. If the floating-point number is a NaN and infinity, assign a value of 0 to the result value and end the process; If the floating-point number is not a NaN and infinity, judge whether the exponent of the floating-point number is greater than the set value. If the exponent of the floating-point number is greater than the set value, it means that the last 32 bits of the floating-point number are 0, assign a value of 0 to the result value, and end the process; If the exponent of the floating-point number is not greater than the set value, respectively extract the sign bit, exponent bit, and mantissa bit from the floating-point number within the remaining range, convert the floating-point number into an integer value through mathematical concatenation, and assign a value to the result value in the form of a truncated value of integer Int32 form, and end the process.
7. An adaptive floating-point to integer optimization device for a JavaScript engine, characterized in that, For use in a JavaScript engine, including: A conversion information acquisition module for acquiring floating-point to integer conversion information; A judgment module for reading the processor hardware information and judging the strong / weak support of the processor for the truncation mode by comparing the processor hardware information. Among them, the strong support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the truncated value of the target format, and the weak support of the processor for the truncation mode means that the target value obtained when the floating-point to integer instruction overflows is the maximum finite value of the target format; A conversion module, which is used to respectively judge and match the floating-point to integer conversion information to a bitwise operation Double to Int32 conversion module and a conventional floating-point to integer conversion module under the strong / weak support of the truncation mode by the processor, and adaptively generate different floating-point to integer instructions.
8. An electronic device, characterized in that: It includes a processor and a storage medium; The storage medium is used to store instructions; The processor is used to operate according to the instructions to execute the steps of the method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Standalone floating-point conversion unit
CN105468331A
Apparatus and methods to avoid floating point control instructions in floating point to integer conversion
CN1894665A