A method and system for accelerating exponential functions based on transcendental function acceleration instructions.
By using the transcendental function acceleration instruction FEXPA and hardware lookup table technology, the problem of low efficiency in reconstructing coefficients of vector exponential functions is solved, achieving efficient vector exponential function reconstruction and improving performance and computational accuracy.
Patent Information
- Application Number
- CN202310799093.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-30
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2043-06-30
AI Technical Summary
In existing technologies for implementing vector exponential functions, traditional vector lookup operations result in performance loss, and some SIMD instruction sets lack vector aggregation and loading instructions, leading to low efficiency in reconstructing coefficient calculation and failing to meet accuracy requirements while improving performance.
By employing the transcendental function-based acceleration instruction FEXPA, the reconstruction coefficients of the vector exponential function are quickly calculated through hardware table lookup and vectorized computation, avoiding redundant calculations. Combined with Taylor series expansion, efficient reconstruction of the vector exponential function is achieved.
While meeting accuracy requirements, it greatly improves the performance of vector exponential functions, reduces the number of Taylor series expansions, and avoids the performance loss caused by traditional vector lookup table operations.
Smart Images

Figure CN116991360B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of processor data parallelism and vector exponential function calculation technology, specifically to an exponential function acceleration method and system based on transcendental function acceleration instructions. Background Technology
[0002] Modern processors include vector processing units (SMUs) capable of data-parallel computation, making them a crucial component. Instruction set architectures include a subset of instructions that operate on these SMUs; this set of instructions is called the SIMD instruction set (also known as the floating-point instruction set or vector instruction set). Vector registers, also called floating-point registers, can store multiple elements compared to ordinary general-purpose registers and are the core storage component for running SIMD instructions. A single SIMD instruction can operate on multiple elements stored in a vector register simultaneously. Common SIMD instruction sets include Intel processors' SSE2, AVX, AVX2, and AVX512 instruction sets, and ARM processors' ADVSIMD and SVE instruction sets. The SPARC64 V instruction set also has corresponding SIMD extensions. The exponential function acceleration method and system designed in this paper are applicable to all SIMD instruction sets. The industry also refers to various SIMD instructions as vector operations and vector instructions. Vector operations essentially apply ordinary scalar operations to each element of a vector register (or vector) and store the result in the corresponding element of the vector register. Because vector instructions can compute multiple data points at once (compared to ordinary scalar instructions), application developers (such as developers of math function libraries) widely utilize SIMD instruction sets to vectorize their programs, increasing computational throughput and thus significantly improving performance. Commonly used vector instructions include various types of vector floating-point arithmetic instructions (such as vector multiplication, vector addition, vector subtraction, vector floating-point rounding, and vector multiply-add instructions), vector integer operation instructions (such as vector shift operation instructions), and vector memory access instructions (such as vector clustering and loading instructions).
[0003] These commonly used vector instructions essentially perform multiple ordinary scalar operations by executing a single instruction. For example, a single vector addition instruction can perform multiple floating-point addition operations simultaneously. Therefore, except for vector clustering and scrambling instructions, these vector instructions offer a significant performance improvement over their corresponding scalar instructions. However, vector clustering and scrambling instructions need to complete memory access operations on multiple non-contiguous memory locations within a single instruction. Therefore, due to the performance limitations of memory access units in computer architecture and the spatial and temporal locality of memory in the program itself, they are destined not to achieve a significant performance improvement when the span of memory access operations is large, and may even exhibit the same performance as multiple serial scalar memory accesses.
[0004] Modern computer systems generally use floating-point numbers to represent decimals in mathematical operations. Floating-point numbers are a numerical representation and arithmetic system based on scientific notation. Each floating-point number consists of three parts: a sign bit (S), an exponent (E), and a mantissa (M). The number of bits in each part is different. The value represented by a floating-point number is equal to... The most widely used IEEE 754 floating-point standard includes four floating-point formats: half-precision, single-precision, double-precision, and quad-precision. This patent applies to the implementation of exponent functions for half-precision, single-precision, and double-precision vector floating-point numbers. Specifically: half-precision floating-point numbers consist of S = 16 binary bits, with 1 bit for the sign bit, 5 bits for the exponent, and 10 bits for the mantissa. Single-precision floating-point numbers consist of S = 32 binary bits, with 1 bit for the sign bit, 8 bits for the exponent, and 23 bits for the mantissa. Double-precision floating-point numbers consist of S = 64 binary bits, with 1 bit for the sign bit, 11 bits for the exponent, and 52 bits for the mantissa. The exponent offset e for these three floating-point formats... bias The values are 15, 127, and 1023 respectively. Floating-point arithmetic can be considered as performing arithmetic operations on two real numbers and rounding the result to the nearest floating-point number. Therefore, floating-point arithmetic generally suffers from rounding errors, and these errors have a greater impact on precision as the number of consecutive calculations increases. For example, the floating-point operation a × b + c involves two rounding operations: first, rounding the real number result of a multiplied by b to a floating-point number; second, rounding the real number result of adding the rounded result to c to a floating-point number. To reduce the impact of floating-point rounding errors on applications, the IEEE 754 floating-point standard defines floating-point multiplication and addition (also known as fused floating-point multiplication and addition). This operation combines the most frequently used floating-point multiplication and addition operations into a single floating-point multiplication and addition operation, requiring only one rounding operation to calculate the floating-point result of a × b + c, greatly improving the accuracy of the calculation. Furthermore, because floating-point numbers are a numerical representation and arithmetic system based on scientific notation, for a floating-point multiplication operation of x × 2m (where m is an integer), the integer m can be directly added to the exponent of the floating-point number x to obtain a floating-point multiplication result without rounding errors. The vector floating-point instructions in the SIMD instruction set read multiple data points from the vector register and perform floating-point arithmetic operations on these data in parallel to improve the throughput of floating-point operations.
[0005] The exponential function acceleration method described in this paper specifically refers to the implementation of floating-point exponential functions and the implementation of floating-point vector exponential functions using SIMD instruction vectorization. As a type of transcendental function, the floating-point implementation of the exponential function generally involves three steps: reduction, polynomial approximation, and reconstruction. For any input x, in calculating f(x) = e xWhen approximating the result, we first need to reduce x to a value r within a very small range of [-R, R], and establish a mathematical relationship between the input x and r, as shown below:
[0006] x=n×R+r; n=0, 1, 2, 3, 4,...,
[0007]
[0008]
[0009] f(x) = e x =e n×R+r =e n×R ×e r ,
[0010] Within the numerical range [-R, R], the exponential function result e in terms of r can be approximated by Taylor series expansion. r Finally, a numerical relationship is established between the reduced x and the reduced r, and the correct f(x) = e is reconstructed through this numerical relationship. x =e n×R+r =e n×R ×e r The approximate result. Due to the arbitrariness of x, integer multiples n of x compared to R may be very large. Therefore, in the implementation of the exponential function, how to accurately calculate the reconstruction coefficient scale = e n×R This becomes the most complex step. Traditional scalar mathematics libraries (such as GNU LIBM and CRLIBM) widely use table-based calculation methods for exponential functions: pre-calculating all e... n×R The high-precision calculation results are then stored as a constant array in the implementation code of the exponential function. The implementation code reduces any input x to obtain n and r, and then uses n as an index to retrieve the high-precision reconstructed coefficient e from this constant array. n×R Since r is reduced to a very small numerical range [-R, R], poly = e can be approximated by a Taylor series expansion of order 5 to 7. r The calculation results. When the reconstruction coefficient scale = e n×R With poly=e r After calculating the results, multiplying the two together will complete the reconstruction and yield the final f(x) = x The approximate calculation results are as follows.
[0011] The vector exponentiation function internally uses SIMD instructions or corresponding intrinsic functions for vectorization. Depending on the vector width of the SIMD instruction set used, a single call to the vector exponentiation function will compute multiple floating-point operands simultaneously, thus achieving a performance improvement of several times. The implementation of the vector exponentiation function is also divided into three steps: reduction, approximation, and reconstruction, each step using SIMD instructions for vectorization. The vectorized reduction algorithm, for the input floating-point vector vx, calculates each element x... i Perform reduction and output the corresponding result r after reduction. i With n iThe reconstructed coefficients are saved to vectors vr and vn. To obtain the reconstructed coefficients of the vector exponential function, each element of vn needs to be used as an index to look up multiple reconstructed coefficients from a pre-calculated table and stored in the corresponding element of vector vscale to complete the vector lookup operation. Some SIMD instruction sets (such as AVX512 and SVE instruction sets) provide vector clustering and scattering load instructions to support vector lookup operations. These instructions can load multiple elements from different memory locations into the corresponding elements of the destination vector according to the indication of the index vector. However, vector clustering and scattering load instructions are still limited by the performance of memory access units in the computer architecture and the spatial and temporal locality of the program itself. When the table items that the vector exponential function needs to look up are large, the memory span corresponding to each index element in the index vector vn will be large, ultimately causing the vector clustering and scattering load instructions to cause performance problems in the exponential function implementation due to frequent cache misses. Furthermore, other SIMD instruction sets (such as the widely used ADVSIMD and AVX2 instruction sets) do not provide vector convergence and divergence loading instructions. This necessitates using scalar lookup methods for each vector index element when designing vector exponential function implementations on these SIMD instruction set platforms. Consequently, the step of calculating the reconstruction coefficients in the vector exponential function cannot be vectorized. To address this issue, mainstream vector math libraries (such as the Sleef vector math library) generally adopt the method of increasing the numerical range of the reduction to [-log2 / 2, log2 / 2] (R = log2 / 2). This allows the reconstruction work to be completed by accumulating the vn generated by the reduction to the order of the Taylor series calculation result vpoly, ultimately avoiding the performance issues caused by using vector lookup operations. Since the numerical range of the reduction is expanded to [-log2 / 2, log2 / 2], the Taylor series expansion order used to approximate the calculation of vpoly in its vector exponential function implementation is correspondingly adjusted to 13 to meet the accuracy requirements. Taylor series actually consists of multiple multiplication and addition operations, which significantly increases the computational cost of vpoly. Furthermore, compared to table lookup methods, this computation-based reconstruction method is more complex because it involves multiple multiplications, additions, and shift operations. Therefore, this method is essentially an optimization that avoids vector table lookups by increasing computational cost. In many practical applications, the performance penalty from increased computation can even exceed the performance penalty from table lookup operations.
[0012] Chinese patent documents CN110321161A, "A Fast Vector Function Lookup Method, System, and Medium Using SIMD Instructions," and CN114968367A, "A Fast Vector Trigonometric Function Lookup Method and System Based on Transpose," both propose fast vector lookup implementations that do not rely on vector clustering and unpacking instructions. However, the former limits the size of the lookup entries, and the latter is only applicable to trigonometric functions; neither is suitable for vector exponential functions (the lookup entries for vector exponential functions exceed 32). To address this issue, ARM has proposed a solution for the ARM64 instruction set: a FEXPA (a type of transcendental function acceleration instruction) instruction to accelerate the calculation of the reconstructed coefficients of vector exponential functions. Specifically, the FEXPA instruction accepts the offset vector vzbias of the reduced result vector vn as input and uses the lower l bits (typically 6) of each element of vzbias as an index value to retrieve the values from the 2nd generation of the vector exponential function. 6 The element scale corresponding to vscale is obtained by looking up the table (usually 64 items). i The last digit of vzbias, then the element zbias corresponding to vzbias. i The bits l to l+E are used as the order (E is 4, 7, and 10 for half-precision, single-precision, and double-precision floating-point numbers, respectively), and concatenated with the mantissa obtained from the lookup table to generate the scale. i The result is saved to a vector and returned to the caller of the instruction. Although the ARM64 instruction set provides exponential function acceleration instructions, since these instructions were only recently published, there are currently no complete implementations of half-precision, single-precision, and double-precision floating-point vector exponential functions based on these acceleration instructions. Commonly used vector math libraries such as SLEEF, AMDMath Library, and Vector-libm also do not use this type of instruction to improve the performance of their vector exponential function implementations. Summary of the Invention
[0013] The technical problem to be solved by this invention is to provide an exponential function acceleration method and system based on transcendental function acceleration instructions, which addresses the aforementioned problems in the prior art. This invention aims to use exponential function acceleration instructions to realize the calculation of reconstructed coefficients in the implementation of vector exponential functions. While meeting the accuracy requirements, it avoids the performance loss caused by traditional vector table lookup or obtaining reconstructed coefficients through redundant calculations. It accelerates the two very time-consuming parts of polynomial calculation and reconstruction in the implementation of vector exponential functions, and greatly improves the performance of exponential functions.
[0014] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:
[0015] An exponential function acceleration method based on transcendental function acceleration instructions includes:
[0016] 1) Input vector vx;
[0017] 2) Calculate each element x of the input vector vx i multiples of ln2 / l The result is placed in the corresponding element of the vector vn, and the floating-point vector vn is converted into an integer vector vz, where l is a constant;
[0018] 3) Calculate each element x of the input vector vx using two vector floating-point multiplication and addition instructions. i The result r reduced to the numerical interval [-ln2 / , ln2 / l] i =(x i ×n i +(-Ln2hi))×n i +(-Ln2lo) and store it in the corresponding element of the vector vr, where Ln2hi is the constant ln2 / 2. l The most significant digit, Ln2lo, is a constant ln2 / 2. l The least significant digits; the multiplication operands of the first vector floating-point multiply-add instruction are the input vector vx and the vector vn, the multiplication operands of the second vector floating-point multiply-add instruction are the vector vn and the calculation result of the first multiply-add instruction, and the addition operands of the two multiply-add instructions are the constants -Ln2hi and -Ln2lo, respectively.
[0019] 4) Use vector floating-point multiplication instructions to calculate each element r in vector vr. i The square of the product of itself, r2 i = i ×r i The result is then saved to the corresponding element of vector vr2; finally, vector floating-point multiplication instructions are used to calculate each element r2 of vector vr2. i The square of the product of itself, r4 i =2 i ×r2 i The results are saved to the corresponding elements of vector vr4; based on the obtained vectors vr, vr2, and vt4, each element r in vector vr is calculated using four vector floating-point multiplication and addition instructions. i about The approximate numerical results are as follows: the two multiplication operands and the addition operands of the first to fourth vector multiply-add instructions are: vector vr, constant C5 and constant C4; vector vr, constant C3 and constant C2; vector vr2, the calculation result of the second multiply-add instruction and vector vr; vector vr4, the calculation result of the first multiply-add instruction and the calculation result of the third multiply-add instruction. The final calculation result is then plotted as poly. i =r i +r i2 ×(C2+r i ×C3)+r i 2 ×r i 2 ×(C4+r i ×C5) is saved to the corresponding element of the vector vpoly;
[0020] 5) Use the vector integer addition command to add the constant e to each element of vector vz. bias <<l obtain zbias i =z i +(e bias <<l), the calculation result is placed in the vector vzbias, where << indicates shift;
[0021] 6) Use the vector transcendental function acceleration instruction FEXPA to calculate the vector vzbias, obtaining the resulting vector vscale. Each element of the resulting vector vscale is scale. i The element n corresponding to vector vn i satisfy
[0022] 7) Use the vector multiply-accumulate command to calculate the result vector vscale, where each element is scale. i Multiply by the corresponding element poly in the vector vpoly i The result t of adding 1.0 i and the result t i Save it to the corresponding element of vector vt;
[0023] 8) Returns the vector vt.
[0024] Optionally, step 2) includes: using vector floating-point multiplication instructions to multiply each element x in the input vector vx. i Multiply by a constant 2 l / ln2, where l is a constant, and then the calculation result is rounded to an integer using vector floating-point rounding instructions. The result is stored in the corresponding elements of the floating-point vector vn to obtain each element x of the input vector vx. i multiples of ln2 / l Then, using vector floating-point to integer conversion instructions, each element n in the floating-point vector vn is converted... i Convert to integer z i The transformed result is then stored in an integer vector vz.
[0025] Optionally, step 2) includes: first multiplying each element of the input vector vx by a constant 2 using a vector floating-point multiplication instruction. lThen, use the vector floating-point addition instruction to add the floating-point constant Shift to each element of the result to obtain a floating-point vector vn. Finally, use the vector floating-point subtraction instruction to subtract the floating-point constant Shift from each element of vector vn to obtain an integer vector vz.
[0026] Optionally, if the exponential function is a half-precision exponential function floating-point vector exponential function, the floating-point constant Shift is 0x1.8p10f; if the exponential function is a single-precision exponential function floating-point vector exponential function, the floating-point constant Shift is 0x1.8p23f; if the exponential function is a double-precision exponential function floating-point vector exponential function, the floating-point constant Shift is 0x1.8p+52.
[0027] Optionally, step 6) includes: using the vector transcendental function acceleration instruction FEXPA, which accelerates exponential function calculation based on processor hardware lookup table operations, to perform exponential function calculation on any element zbias of the input vector vzbias. i The lower l bits are used as the lookup index, and the final scale is calculated through processor hardware lookup. i The last digit of vzbias, then the element zbias corresponding to vzbias. i The digits l to l+E are used as the order, and concatenated with the mantissa obtained from the lookup table to generate the scale. i The result scale i Saved to the corresponding element in the vector vscale; and the table used for lookup is 2. l Two binary data units of constant size P bits each. l The binary data is written into the hardware circuitry inside the processor so that all entries corresponding to the vector vzbias can be retrieved from the table within 2 to 3 processor clock cycles.
[0028] Optionally, if the exponential function is a half-precision exponential function floating-point vector exponential function, the constant P is 10; if the exponential function is a single-precision exponential function floating-point vector exponential function, the constant P is 23; if the exponential function is a double-precision exponential function floating-point vector exponential function, the constant P is 52.
[0029] Optionally, the constant l can take values from 5 to 7. If the exponential function is a half-precision exponential function, a floating-point vector exponential function, then the constant e... bias The value is 15; if the exponential function is a single-precision exponential function, a floating-point vector exponential function, the constant e is 15. bias The value is 127; if the exponential function is a double-precision exponential function, a floating-point vector exponential function, the constant e is... bias The value is 1023.
[0030] Optionally, steps 1) to 8) can be implemented using vector registers and assembly instructions in assembly language, or using vector variable types and vector intrinsic functions defined in each SIMD instruction set in C or C++ language.
[0031] Furthermore, the present invention also provides an exponential function acceleration system based on transcendental function acceleration instructions, comprising a microprocessor and a memory interconnected thereto, wherein the microprocessor is programmed or configured to execute the exponential function acceleration method based on transcendental function acceleration instructions.
[0032] Furthermore, the present invention also provides a computer-readable storage medium storing a computer program for being programmed or configured by a microprocessor to execute the exponential function acceleration method based on transcendental function acceleration instructions.
[0033] Compared with the prior art, the present invention has the following advantages: The present invention provides a method for calculating the reconstruction coefficients of an exponential function based on hardware lookup tables. This method can quickly and accurately calculate the reconstruction coefficients used in the implementation of the vector exponential function, avoiding the performance loss caused by using software vector lookup table operations to calculate the reconstruction coefficients, and also reducing the number of Taylor series expansions, thereby greatly improving the performance of the vector exponential function. Attached Figure Description
[0034] Figure 1 This is a basic flowchart of Embodiment 1 of the present invention.
[0035] Figure 2 This is a schematic diagram of the vector transcendental function acceleration instruction FEXPA in Embodiment 1 of the present invention. Detailed Implementation
[0036] Example 1:
[0037] like Figure 1 As shown, the exponential function acceleration method based on transcendental function acceleration instructions in this embodiment includes:
[0038] 1) Input vector vx;
[0039] 2) Calculate each element x of the input vector vx i multiples of ln2 / l The result is placed in the corresponding element of the vector vn, and the floating-point vector vn is converted into an integer vector vz, where l is a constant;
[0040] 3) Calculate each element x of the input vector vx using two vector floating-point multiplication and addition instructions. i The result r reduced to the numerical interval [-ln2 / l, ln2 / l]i =(x i ×n i +(-Ln2hi))×n i +(-Ln2lo) and store it in the corresponding element of the vector vr, where Ln2hi is the constant ln2 / 2. l The most significant digit, Ln2lo, is a constant ln2 / 2. l The least significant digits; the multiplication operands of the first vector floating-point multiply-add instruction are the input vector vx and the vector vn, the multiplication operands of the second vector floating-point multiply-add instruction are the vector vn and the calculation result of the first multiply-add instruction, and the addition operands of the two multiply-add instructions are the constants -Ln2hi and -Ln2lo, respectively.
[0041] 4) The floating-point multiplication instruction calculates each element r in the vector vr. i The square of the product of itself, r2 i =r i ×r i The result is then saved to the corresponding element of vector vr2; finally, vector floating-point multiplication instructions are used to calculate each element r2 of vector vr2. i The square of the product of itself, r4 i =r2 i ×r2 i The results are saved to the corresponding elements of vector vr4; based on the obtained vectors vr, vr2, and vr4, each element r in vector vr is calculated using four vector floating-point multiplication and addition instructions. i about The approximate numerical results are as follows: the two multiplication operands and the addition operands of the first to fourth vector multiply-add instructions are: vector vr, constant C5 and constant C4; vector vr, constant C3 and constant C2; vector vr2, the calculation result of the second multiply-add instruction and vector vr; vector vr4, the calculation result of the first multiply-add instruction and the calculation result of the third multiply-add instruction. The final calculation result is then plotted as poly. i =r i +r i 2 ×(C2+r i ×C3)+r i 2 ×r i 2 ×(C4+r i ×C5) is saved to the corresponding element of the vector vpoly;
[0042] 5) Use the vector integer addition command to add the constant e to each element of vector vz. bias <<l obtain zbias i =z i +(ebias <<l), the calculation result is placed in the vector vzbias, where << indicates shift;
[0043] 6) Use the vector transcendental function acceleration instruction FEXPA to calculate the vector vzbias, obtaining the resulting vector vscale. Each element of the resulting vector vscale is scale. i The element n corresponding to vector vn i satisfy
[0044] 7) Use the vector multiply-accumulate command to calculate the result vector vscale, where each element is scale. i Multiply by the corresponding element poly in the vector vpoly i The result t of adding 1.0 i That is: t i =scale i ×(poly i +1.0) = scale i ×poly i +scale i and the result t i Save it to the corresponding element of vector vt;
[0045] 8) Returns the vector vt.
[0046] This embodiment implements a vector exponential function based on exponential function acceleration instructions. For any input vector vx, the vector exponential function calculates all elements x of vx. i index results and t i This is then placed into the corresponding elements of the resulting vector vt and returned. Its implementation involves three steps: reduction, approximation, and reconstruction. First, all elements x in vx are... i Reduce to a very small range of operands and obtain the reduced result r. i The result is also stored in the corresponding vector vr. Within this operand range, the result can be approximated by a vectorized Taylor series expansion, which stores the result in r. i index results The vector vpoly is constructed by establishing the reduced x. i With the r after the specification i The relationship is preserved by the reconstruction coefficient scale i The vector vscale is used to reconstruct x using vscale and vpoly. i The result of the exponential function t i It will be saved to the corresponding element of the result vector vt and returned to the caller of the vector exponential function.
[0047] In this embodiment, step 2) includes: using vector floating-point multiplication instructions to multiply each element x in the input vector vx. i Multiply by a constant 2 l / ln2, where l is a constant, and then the calculation result is rounded to an integer using vector floating-point rounding instructions. The result is stored in the corresponding elements of the floating-point vector vn to obtain each element x of the input vector vx. i multiples of ln2 / l Then, using vector floating-point to integer conversion instructions, each element n in the floating-point vector vn is converted... i Convert to integer z i The converted result is then stored in an integer vector vz.
[0048] In this embodiment, the method, during specification, uses a mathematical formula to reduce x. i The normalization is r whose value is in the range [-R, R]. i This example will constantly restrict R to R = ln2 / 2. l Thus x i Reduced to a numerical range of [-ln2 / 2] l ln2 / 2 l ], then r i With n i Transformed into:
[0049] x i =n i ×R+r i =n i ×(ln2 / 2 l )+r i n = 0, 1, 2, 3, ...
[0050]
[0051] r i =x i -n i ×R=x i -n i ×(ln2 / 2 l ), r∈[-ln2 / 2 l ln2 / 2 l ],
[0052] Based on the above principles, in step 2) this embodiment uses vector floating-point multiplication instructions to multiply each element x of vx. i Multiply by a constant 2 l The result is calculated as / ln2 (where l is a constant), and then rounded to an integer using vector floating-point rounding instructions. The result is stored in the corresponding element of vector vn to obtain each element x of vector vx.i multiples of ln2 / l Then, using vector floating-point to integer conversion instructions, each element n of vn is converted... i Convert to integer z i This is stored in the vector vz for use in subsequent steps. To reduce the computation of r... i The rounding error during the process is addressed in step 3) of this embodiment by using vector multiplication and addition instructions to calculate each element x of vector vx. i Reduced to the numerical range [-ln2 / 2] l ln2 / 2 l The result Furthermore, based on the Cody-Waite reduction algorithm, the constant ln2 / 2 is... l The value is split into two floating-point constants, Ln2hi and Ln2lo, and stored separately. That is, Ln2hi stores the constant ln2 / 2. l The most significant digit, Ln2lo, stores the constant ln2 / 2. l The least significant digit. Then, r is calculated using two vector floating-point multiply-accumulate instructions. i =(x i ×n i +(-Ln2hi))×n i +(-Ln2lo). The multiplication operands of both vector multiply-add instructions are vectors vx and vn, and the addition operands of the first and second vector floating-point multiply-add instructions are the constants -Ln2hi and -Ln2lo, respectively. The pseudocode for steps 2) and 3) is shown below:
[0053]
[0054] vt0 = vmul(vx, vc1);
[0055] vn = vround(vt0);
[0056] vz = vfloat_to_int(vn);
[0057] vt1=vmadd(vx,vn,-Ln2hi);
[0058] vr=vmadd(vt1,vn,-Ln2lo);
[0059] Where vcl is a real number 2 l / ln2 represents floating-point numbers, float is for floating-point conversion, vmul is for vector multiplication, vt0 is for the result of vector multiplication, vround is for vector rounding, which rounds each floating-point number in the vector to the nearest integer floating-point number, vfloattoint is for converting floating-point numbers to integers, vn is for a floating-point vector, vz is for an integer vector, vmadd is for multiply-accumulate instructions, and vt1 is for the result of the first vector floating-point multiply-accumulate instruction.
[0060] After the regulations were agreed upon, x i The result of exponentiation It can be done Split into and The calculations for these two parts. Among them... As a result, this embodiment obtains the result through the Taylor series formula. The approximate calculation result will also be stored in the corresponding elements of the vector vpoly. To improve the accuracy of the approximate calculation, this embodiment only uses Taylor series calculation. An approximation of the polynomial. To increase instruction parallelism during computation, the polynomial computation is transformed in the embodiment. i =r i +r i 2 ×(C2+r i ×C3)+r i 2 ×r i 2 ×(C4+r i ×C5). In this embodiment, step 4) performs the following steps on the vector vr obtained in step 3). To calculate the approximate value of r, first use two vector multiplication commands to calculate r. i 2 With r i 4 Vectors vr2 and vr4 are obtained respectively. Then, four pairs of consecutive multiplication and addition operations in the polynomial calculation are completed using four vector floating-point multiply-add instructions to improve the accuracy and performance of the calculation, thereby accelerating the performance of the vector exponential function. The two multiplication operands and the addition operands of the first to fourth vector multiply-add instructions are: vector vr, constant C5, and constant C4; vector vr, constant C3, and constant C2; vector vr2, the calculation result of the second multiply-add instruction, and vector vr; vector vr4, the calculation result of the first multiply-add instruction, and the calculation result of the third multiply-add instruction. The final calculation result is poly. i Save it to the corresponding element of the vector vpoly. The pseudocode is as follows:
[0061] vr2 = vmul(vr, vr);
[0062] vr4 = vmul(vr2, vr2);
[0063] vpt1 = vmadd(vr, C5, C4);
[0064] vpt2 = vmadd(vr, C3, C2);
[0065] vpt3=vmadd(vr2,vpoly2,vr);
[0066] vpoly=vmadd(vr4, vpoly1, voly3);
[0067] Where vmul is the vector multiplication instruction, vmadd is the multiply-add instruction, and vpt1 to vpt3 are the calculation results of the first three multiply-add instructions, respectively.
[0068] And for The calculation, in this embodiment, will obtain r in the specification. i With n i After saving them to vectors vr and vn, proceed in steps 5) and 6). As shown in the pseudocode below, first use the integer vector addition instruction to add each integer element z in vz. i Add offset constant (e) bias <<l) Obtain vzbias, and finally pass vzbias to the vector FEXPA command to calculate and save the reconstruction coefficients. The vector vscale. The pseudocode is as follows:
[0069] vzbias=vadd(vz, ebias_l);
[0070] vscale = fexpa(vzbias);
[0071] Where vadd is the addition instruction and ebias_1 is the offset constant e bias << l, fexpa is the vector transcendental function acceleration instruction FEXPA.
[0072] In this embodiment, step 6) includes: using the vector transcendental function acceleration instruction FEXPA, which accelerates exponential function calculation based on processor hardware lookup table operations, to perform exponential function calculation on any element zbias of the input vector vzbias. i The lower l bits are used as the lookup index, and the final scale is calculated through processor hardware lookup. i The last digit of vzbias, then the element zbias corresponding to vzbias. i The digits l to l+E are used as the order, and concatenated with the mantissa obtained from the lookup table to generate the scale. i The result scalei Saved to the corresponding element in the vector vscale; and the table used for lookup is 2. l Two binary data units of constant size P bits each. l The binary data is written into the processor's internal hardware circuitry so that all entries corresponding to the vector vzbias can be retrieved from the table within 2 to 3 processor clock cycles. In this embodiment, when the exponential function is a half-precision exponential function floating-point vector exponential function, the constant P is 10; when the exponential function is a single-precision exponential function floating-point vector exponential function, the constant P is 23; and when the exponential function is a double-precision exponential function floating-point vector exponential function, the constant P is 52.
[0073] The vector transcendental function acceleration instruction FEXPA can use any vector instruction that performs exponential function calculations using processor hardware lookup operations. See also Figure 2 The control flow graph shown is for the vector transcendental function acceleration instruction FEXPA. Figure 2 In this context, zbias is the input vector, which stores v1 elements from vzbias.1 to vzbias.vl. The lower 1 bit of each element is represented by j.1 to j.vl, and the 1 to 1+e bits of each element are represented by the value M.vl. T represents the lookup table in the hardware circuit, which contains N=2... l There are 10 elements, each represented by T[0] to T[N-1]. Vscale is the output vector of the instruction, which contains vl floating-point elements vscale.1 to vscale.vl. The order of these floating-point numbers is represented by M.1 to M.vl, and the mantissa is represented by T.1 to T.vl respectively.
[0074] In this embodiment, the vector transcendental function acceleration instruction FEXPA can be calculated precisely. The principle is: from the perspective of binary bit pattern, each positive integer element z in the integer vector vz i It can be split into two parts, j i and M i , i.e. z i It can be viewed as a number j consisting of the lower l bits (l can be 5, 6, or 7). i The number M with digits from l to l+E i Composition (E is the width of the floating-point order minus), thus n i =M i ×2 l +j i ,So Based on the rules of floating-point multiplication, Multiply The resulting floating-point number scale i with multiplicand Having the same last digit, their order is The order plus M i .because So floating-point numbers All possible values have the same order e bias (e bias (This refers to the exponential offset of the floating-point system). j i It is z i The lower l bits of the binary representation, so j i The number of all candidate values is 2 l , further all The number of candidate values is also 2 l Therefore, it can be calculated using the following method. The values are calculated in advance for all 2. l indivual The floating-point candidate values are calculated, and their mantissas are stored in a constant array (software array or hardware circuit). The actual scale calculation... i At that time, from z i j i and M i Using j i The table is retrieved from this constant array using an index. The last digit is used as the scale i The last digit, using M i Add e bias As scale i The order is calculated by combining the mantissa and the order to obtain the floating-point number scale. i The FEXPA instruction uses the above principle; the user first uses a vector addition instruction to add e to Mi in each element of vz. bias This makes its l to l+E bits become the floating-point elements scale corresponding to vscale. i The order is calculated, and the result is stored in the corresponding element of the vector vzbias. The FEXPA command reads the input vector vzbias and stores the j of each element of vzbias. i The scale is obtained by looking up a table in the constant array located in the chip's hardware circuitry, using it as an index. i The mantissa is then used to synthesize a new floating-point number, scale, by combining the order stored in vzbias with the mantissa array obtained from the lookup table. i The result is saved to the corresponding element of the target vector vscale.
[0075] The exponential function acceleration instruction FEPXA combines the advantages of traditional vector lookup for reconstructed coefficients and computational acquisition of reconstructed coefficients. It retains the lookup operation to reduce the computational burden of Taylor series approximation (using this instruction, the Taylor series expansion only requires 6 iterations to meet accuracy requirements). Simultaneously, by utilizing the order of each element of vn, it ensures that the lookup table is limited to 64 entries, allowing the entire lookup table to be embedded in the corresponding hardware circuitry without being placed in memory. This ensures that the vector lookup operation is not affected by the performance of memory access units in the computer architecture, or by the spatial and temporal locality of the program itself, greatly improving the performance of the vector exponential function. After approximating the vector vpoly using Taylor series expansion and calculating vscale using the FEXPA instruction, this embodiment uses the vector multiply-add instruction in step 7) to calculate the scale of each element of vscale. i Multiply by the poly element corresponding to vpoly i And add the corresponding element poly to vpoly i This completes the reconstruction, yielding the exponential function result vt of the input vector vx, where each element t in vt... i The final representation is:
[0076]
[0077] The pseudocode for step 7) can be represented as: vt = vmadd(vpoly, vscale, vscale).
[0078] It should be noted that this embodiment is applicable to the implementation of half-precision, single-precision, and double-precision floating-point vector exponential functions. In this embodiment, the value range of the constant l is 5 to 7. If the exponential function is a half-precision floating-point vector exponential function, the constant e... bias The value is 15; if the exponential function is a single-precision exponential function, a floating-point vector exponential function, the constant e is 15. bias The value is 127; if the exponential function is a double-precision exponential function, a floating-point vector exponential function, the constant e is... bias The value is 1023.
[0079] It should be noted that this embodiment is applicable to the SSE, SSE2, SSE4, AVX, AVX2, and AVX512 vector instruction sets of the x86 architecture, as well as the ADVSIMD and SVE vector instruction sets of the ARM64 architecture, and the V-vector extension of RISC-V. The vector floating-point multiplication, addition, subtraction, integer addition, subtraction, rounding, and integer conversion instructions used in steps 1) to 8) are assembly instructions from the aforementioned applicable vector instruction sets. The vectors used in steps 1) to 8) of this embodiment as input for vector instructions and for storing the calculation results are vector registers from the aforementioned applicable vector instruction sets. All of these features good platform adaptability. It should be noted that steps 1) to 8) in this embodiment can be implemented using vector registers and assembly instructions in assembly language, or using vector variable types and vector intrinsic functions defined in each SIMD instruction set in C or C++. The term "vector" used in steps 1) to 8) refers to both vector registers in the SIMD instruction set and various vector types in the SIMD instruction set's intrinsic vector function. The vector operations performed in steps 1) to 8) can be performed by vector instructions in the SIMD instruction set or by the corresponding vector intrinsic functions in the SIMD instruction set's intrinsic vector function. A vector is both a vector register in assembly language and a vector type variable defined in each SIMD instruction set; there is a one-to-one correspondence between vector instructions and vector intrinsic functions. It should also be noted that the built-in functions, variable types, variable names, instruction names, and register names used in steps 1) to 3) in this embodiment are for illustrative purposes only. In actual use, the names do not need to be the same; any built-in function and assembly instruction capable of performing the operations required in steps 1) to 3) can be used in the implementation of this method.
[0080] In summary, the method in this embodiment includes taking an input vector vx as input, and processing each element x of the vector vx. i Reduced to the interval [-ln2 / l, ln2 / l], obtaining the reduced result vector vr and a vector x containing each element of vx. i Compare the vector vz to integer multiples of ln2 / l with the offset vector vzbias. Use Taylor series expansion to approximate the calculation of each element r of vector vr. i The result of subtracting 1 from the exponentiation operation This is then saved to the vector vpoly. Next, the vector vzbias is computed using the transcendental function acceleration instruction FEXPA to obtain the reconstruction coefficients vscale used in the reconstruction step. Finally, each element poly of vpoly is... i Multiply by the corresponding element scale of vscale i Adding the constant 1.0 completes the reconstruction, resulting in each element x of the vector vx. i The result of the exponential function calculation The vector exponential function acceleration method provided in this embodiment offers a method for calculating the reconstructed coefficients of the exponential function based on the hardware lookup table-based exponential function acceleration instruction FEXPA. This method can quickly and accurately calculate the reconstructed coefficients required in the implementation process of the vector exponential function, avoiding the performance loss caused by using software vector lookup tables to calculate the constructed coefficients and reducing the number of Taylor series expansions, thereby greatly improving the performance of the vector exponential function.
[0081] Furthermore, this embodiment also provides an exponential function acceleration system based on transcendental function acceleration instructions, including a microprocessor and a memory interconnected, wherein the microprocessor is programmed or configured to execute the exponential function acceleration method based on transcendental function acceleration instructions. Additionally, this embodiment also provides a computer-readable storage medium storing a computer program for being programmed or configured by a microprocessor to execute the exponential function acceleration method based on transcendental function acceleration instructions.
[0082] Example 2:
[0083] This embodiment is basically the same as Embodiment 1, the main difference being the implementation method of step 2). In this embodiment, step 2) is implemented using the fast floating-point integer conversion method. Specifically, step 2) includes: first using a vector floating-point multiplication instruction to multiply each element of the input vector vx by a constant 2. l The algorithm executes ` / ln2`, then uses the floating-point addition instruction to add the floating-point constant `Shift` to each element of the result, obtaining a floating-point vector `vn`. Finally, it uses the floating-point subtraction instruction to subtract the floating-point constant `Shift` from each element of vector `vn`, obtaining an integer vector `vz`. To improve computational accuracy and performance, the floating-point multiplication and addition instructions are replaced with floating-point multiply-add instructions. The pseudocode is shown below:
[0084] vc1 = float(2 l / ln2);
[0085] vn=vmadd(vx,vc 1,Shift);
[0086] vz = vsub(vn, Shift);
[0087] In the above formula, vc1 is a real constant 2. l The floating-point representation of the result is / ln2, float is for floating-point conversion, vmadd is for vector floating-point multiplication and addition, and vsub is for vector floating-point subtraction. It should be noted that this embodiment applies to the implementation of half-precision, single-precision, and double-precision floating-point vector exponential functions. If the exponential function is a half-precision floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p10f; if the exponential function is a single-precision floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p23f; and if the exponential function is a double-precision floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p+52.
[0088] Furthermore, this embodiment also provides an exponential function acceleration system based on transcendental function acceleration instructions, including a microprocessor and a memory interconnected, wherein the microprocessor is programmed or configured to execute the exponential function acceleration method based on transcendental function acceleration instructions. Additionally, this embodiment also provides a computer-readable storage medium storing a computer program for being programmed or configured by a microprocessor to execute the exponential function acceleration method based on transcendental function acceleration instructions.
[0089] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-readable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code. This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create a machine for implementing the process. Figure 1 One or more processes and / or boxes Figure 1The computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The functions specified in one or more boxes. These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable apparatus for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0090] The above description is merely a preferred embodiment of the present invention. The scope of protection of the present invention is not limited to the above embodiments. All technical solutions falling within the scope of the present invention's concept are within the scope of protection of the present invention. It should be noted that for those skilled in the art, any improvements and modifications made without departing from the principle of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A method for accelerating exponential functions based on transcendental function acceleration instructions, characterized in that, include: 1) Input vector vx; 2) Calculate each element of the input vector vx Comparison multiples of integers The result is placed in the corresponding element of the vector vn, and the floating-point vector vn is converted into an integer vector vz, where It is a constant; 3) Calculate each element of the input vector vx using two vector floating-point multiplication and addition instructions. Reduced to numerical range The result And stored in the corresponding elements of the vector vr, where constant The most significant digits, constant The least significant digits; the multiplication operands of the first vector floating-point multiply-add instruction are the input vector vx and vector vn, and the multiplication operands of the second vector floating-point multiply-add instruction are vector vn and the result of the first multiply-add instruction. The addition operands of the two multiply-add instructions are constants. and ; 4) Use vector floating-point multiplication instructions to calculate each element in vector vr. The result of multiplying by itself by squares The result is then saved to the corresponding element of vector vr2; finally, vector floating-point multiplication instructions are used to calculate each element of vector vr2. The result of multiplying by itself by squares The results are saved to the corresponding elements of vector vr4; based on the obtained vectors vr, vr2, and vr4, each element in vector vr is calculated using four vector floating-point multiplication and addition instructions. about The approximate numerical results are as follows: the two multiplication operands and the addition operands of the first to fourth vector multiply-add instructions are: vector vr, constant C5 and constant C4; vector vr, constant C3 and constant C2; vector vr2, the calculation result of the second multiply-add instruction and vector vr; vector vr4, the calculation result of the first multiply-add instruction and the calculation result of the third multiply-add instruction. The final calculation result is then presented. Save to the corresponding element of the vector vpoly; 5) Use the vector integer addition command to add a constant to each element of vector vz. get The calculation result is placed in the vector vzbias, where Indicates shift; 6) Use the vector transcendental function acceleration instruction FEXPA to calculate the vector vzbias, obtaining the resulting vector vscale. Each element of the resulting vector vscale... Corresponding elements of vector vn satisfy ; 7) Use the vector multiply-accumulate command to calculate each element of the resulting vector vscale. Multiply by the corresponding element in vector vpoly The result of adding 1.0 and the results Save it to the corresponding element of vector vt; 8) Returns the vector vt; Step 2) includes: first, using vector floating-point multiplication instructions to multiply each element of the input vector vx by a constant. Then, use the vector floating-point addition instruction to add the floating-point constant Shift to each element of the result to obtain a floating-point vector vn. Finally, use the vector floating-point subtraction instruction to subtract the floating-point constant Shift from each element of vector vn to obtain an integer vector vz. Step 6) includes: using the vector transcendental function acceleration instruction FEXPA, which accelerates exponential function calculations based on processor hardware lookup table operations, to process any element of the input vector vzbias. low The bit is used as the lookup index, and the final result is calculated through processor hardware lookup. The last digit, then the element corresponding to vzbias. of The digit is used as the order, and it is concatenated with the mantissa obtained from the table to generate the result. ,as a result Saved to the corresponding element in the vector vscale; and the table used for lookup is A constant P bits of binary data, this The binary data is written into the hardware circuitry inside the processor so that all entries corresponding to the vector vzbias can be retrieved from the table within 2 to 3 processor clock cycles.
2. The method for accelerating exponential functions based on transcendental function acceleration instructions according to claim 1, characterized in that, Step 2) includes: using vector floating-point multiplication instructions to multiply each element in the input vector vx Multiply by a constant Then, the calculation result is rounded to an integer using vector floating-point rounding instructions, and the result is stored in the corresponding elements of the floating-point vector vn to obtain each element of the input vector vx. Comparison multiples of integers Then, using the vector floating-point to integer conversion instruction, each element in the floating-point vector vn is converted... Convert to integer The converted result is then stored in an integer vector vz.
3. The method for accelerating exponential functions based on transcendental function acceleration instructions according to claim 1, characterized in that, If the exponential function is a half-precision exponential function or a floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p10f; if the exponential function is a single-precision exponential function or a floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p23f; if the exponential function is a double-precision exponential function or a floating-point vector exponential function, the floating-point constant Shift takes the value 0x1.8p+52.
4. The method for accelerating exponential functions based on transcendental function acceleration instructions according to claim 1, characterized in that, If the exponential function is a half-precision exponential function, the constant P is 10; if the exponential function is a single-precision exponential function, the constant P is 23; if the exponential function is a double-precision exponential function, the constant P is 52.
5. The method for accelerating exponential functions based on transcendental function acceleration instructions according to claim 1, characterized in that, The constant The value range is 5~7. If the exponential function is a half-precision exponential function, a floating-point vector exponential function, then the constant is... The value is 15; if the exponential function is a single-precision exponential function, a floating-point vector exponential function, the constant is... The value is 127; if the exponential function is a double-precision exponential function, a floating-point vector exponential function, the constant is... The value is 1023.
6. The method for accelerating exponential functions based on transcendental function acceleration instructions according to claim 1, characterized in that, Steps 1) through 8) can be implemented using vector registers and assembly instructions in assembly language, or using vector variable types and vector intrinsic functions defined in each SIMD instruction set in C or C++ language.
7. An exponential function acceleration system based on transcendental function acceleration instructions, comprising a microprocessor and a memory interconnected, characterized in that, The microprocessor is programmed or configured to execute the exponential function acceleration method based on transcendental function acceleration instructions as described in any one of claims 1 to 6.
8. A computer-readable storage medium storing a computer program, characterized in that, The computer program is used to be programmed or configured by a microprocessor to execute the exponential function acceleration method based on transcendental function acceleration instructions as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Vector function fast look-up table method and system using SIMD instruction and medium
CN110321161A
Vector trigonometric function rapid table lookup method and system based on transpose
CN114968367A
Transcendental function device and method for realizing transcendental function utilizing same
CN101630243A
Method and device for implementing 48-bit mixed precision matrix vector multiplication
CN116304513A
