Implementation method of exp exponential function based on SIMD instruction set

By using the SIMD instruction set of the Beijing Junzheng T40 chip, loading data, range limiting, and splitting the exponential function into two parts for multiplication, combined with Taylor expansion calculation, the problem of slow operation of the exp exponential function on the chip is solved, and efficient exp exponential function calculation is achieved.

CN117492699BActive Publication Date: 2026-07-03INGENIC SEMICON CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
INGENIC SEMICON CO LTD
Filing Date
2022-07-25
Publication Date
2026-07-03

AI Technical Summary

Technical Problem

The exp function in existing programming languages ​​can run on PCs, but it cannot run on platforms or chips that do not support these programming languages. Furthermore, it results in slow computation speed on platforms with limited cache resources, failing to meet the computation speed requirements of some chips.

Method used

The SIMD instruction set supported by the Beijing Junzheng T40 chip is used. The exponential function is split into two parts for multiplication by loading data, range limitation, and using SIMD instructions and Taylor expansion to calculate the exp exponential function, including the use of the Ingenic_simd512 instruction set.

Benefits of technology

The Beijing Junzheng T40 chip has achieved efficient calculation of the exp exponential function, with a calculation speed 29 times that of the C language exp function and an accuracy loss of only 0.001%, meeting the chip's accuracy and calculation speed requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117492699B_ABST
    Figure CN117492699B_ABST
Patent Text Reader

Abstract

The application provides an implementation method of exp exponential function based on a SIMD instruction set, which comprises the following steps: S1, loading data; S2, limiting the range of input data between-88.7 and 88.7; S3, splitting x into two parts and adding them, that is, x=x_bas+x_rem formula (4), then multiplying e x into two parts, that is, e x =exp_bas*exp_rem formula (8), wherein exp_bas=2 x_int formula (6), exp_rem=e x_rem formula (7), first, the first part exp_bas of e x is obtained; S4, the second part exp_rem of e x is obtained: the Taylor expansion formula (7) is obtained by using the Taylor expansion, the Taylor formula of the exponential function with e as the base is formula (9), e x_rem is expanded into formula (10); S5, the obtained exp_bas is multiplied by exp_rem, and the result of formula (8) is obtained; S6, the calculation result is saved from the register to the memory.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of image processing technology, and specifically relates to a method for implementing the exp exponential function based on SIMD instructions. Background Technology

[0002] Integrated circuit technology is increasingly becoming a focal point of technological development, and many chip manufacturers are developing their own chips. Chips based on SIMD instructions / instruction sets are also commonly used today. SIMD stands for Single Instruction Multiple Data, which is a set of instructions that can process multiple operands at once and pack them into a large register.

[0003] Furthermore, commonly used algorithms include exponential functions, which are important functions in mathematics. This function applied to the value e is written as exp(x). It can also be equivalently written as e raised to the power of x, i.e., e^x. x Here, e is a mathematical constant, which is the base of the natural logarithm, approximately equal to 2.718281828, and is also called Euler's number.

[0004] When calculating e to the power of x on a PC, you can directly multiply e by x consecutively, for example, to calculate e to the power of 3, multiply e by 3 consecutively to get the result. However, when x is large, such as x=100, multiplying 100 times consecutively is very time-consuming, even if written as a loop function, looping 100 times will still take a long time. Therefore, when calculating e to the power of x, the exp function from the programming language library is called directly.

[0005] In C, you would call the `double exp(double x)` function from the `math.h` library, for example: `double result = exp(4.0)`;

[0006] In Python, you can call the exp(x) function from the math library, for example: result = math.exp(0);

[0007] In Java, you would call the `double exp(double x)` function in the `Math` class, for example: `double result = Math.exp(4.0)`;

[0008] MATLAB also has an exp function. If you enter exp(0) in the command window, the output will be 1.

[0009] The R language also has an exp function, which has the same usage and function as its MATLAB counterpart.

[0010] However, existing technologies have the following drawbacks:

[0011] 1. The exp function in existing programming languages ​​can run on PCs, but it cannot run on some platforms or chips that do not support these programming languages. In chip applications, chip manufacturers also encounter their own problems in their chip designs. For example, chips from Beijing Junzheng Integrated Circuit Co., Ltd. (hereinafter referred to as Beijing Junzheng) only support C and assembly languages. Directly using C programs is slow, and inappropriate use of the limited instructions also leads to slow execution time.

[0012] 2. Since C language can only process one piece of data at a time, running C language to process a large amount of continuous data on platforms or chips with limited cache resources (here we use Beijing Junzheng's T40 chip) will cause cache waste and thus slow down the operation speed. For some applications that require high operation speed, running the doubleexp(double x) function in the C language math.h library directly on the T40 chip will not meet the requirements.

[0013] The following are commonly used terms in the prior art:

[0014] 1. exp, in advanced mathematics, is an exponential function with the natural constant e (approximately equal to 2.718281828) as its base. The full name is Exponential (exponential curve). The exp exponential function is monotonically increasing, its domain is R, and its range is (0, +∞). Summary of the Invention

[0015] To address the aforementioned issues, the purpose of this application is to propose an implementation method for the exp exponential function based on the SIMD instruction set. This method utilizes the SIMD instruction set supported by Beijing Junzheng's t40 chip to complete the exp exponential function.

[0016] Specifically, the present invention provides a method for implementing the exp exponential function based on the SIMD instruction set, the method comprising the following steps:

[0017] S1: Load data in multiples of 32 bits. A register can load a maximum of 512 bits of data. Single-precision floating-point numbers (float) are 32 bits. A register can load 16 single-precision floating-point numbers. Therefore, a single SIMD instruction can perform calculations on 16 single-precision floating-point numbers simultaneously.

[0018] S2: Limit the range of input data to -88.7 to 88.7;

[0019] S3: To speed up the operation of the exp function, e x The input data is split into two parts and multiplied together, where x is the range-limited input data from step S2. Therefore, x needs to be split into two parts and added together, as shown in the following formula:

[0020] x_int=int(x / ln 2) Formula (1)

[0021] x_bas=x_int*ln 2 Formula (2)

[0022] Formula (3) is x_rem = x - x_bas

[0023] In formula (1), int(x / ln2) converts the result of x divided by ln2 into int type, that is, retains the integer part of the result and discards the decimal part, and x_int represents the quotient of x divided by ln2;

[0024] In formula (2), x_bas represents the result of multiplying the quotient x_int by the divisor ln2;

[0025] In formula (3), x_rem represents the remainder when x is divided by ln2;

[0026] From the formula: dividend = quotient * divisor + remainder, we can obtain:

[0027] x = x_bas + x_rem Formula (4)

[0028] After splitting x into two parts and adding them together, e x The steps to split the problem into two parts and multiply them are as follows:

[0029]

[0030] Let exp_bas = 2 x_int Formula (6)

[0031] Let exp_rem = e x_rem Formula (7)

[0032] e x =exp_bas*exp_rem Formula (8)

[0033] 1) Use SIMD instructions to calculate formula (1);

[0034] 2) Use SIMD instructions to calculate exp_bas in formula (6);

[0035] S4: Find ex The second part, exp_rem:

[0036] 1) Use SIMD instructions to calculate formula (3);

[0037] 2) Use the Taylor expansion to find formula (7). The Taylor formula for an exponential function with base e is:

[0038]

[0039] e x_rem Expand as

[0040]

[0041] S5: Use SIMD instructions to multiply exp_bas obtained in the above steps with exp_rem to get the result of formula (8);

[0042] S6: Save the calculation result from the register to memory.

[0043] The method uses the Ingenic_simd512 instruction set supported by the Beijing Junzheng T40 chip. This instruction set can load 512 bits of data into a 512-bit register at once, and can load 1024 bits of cached data into two registers at once. There are 32 registers available for use, which can be reused. During calculation, the same operation can be performed on 512 bits of data at once. This instruction set includes arithmetic operations such as addition, subtraction, multiplication, absolute value, maximum value, and minimum value; logical operations such as AND, OR, NOT, XOR, and shift; and comparison operations such as less than, equal to, and less than or equal to, which meet general calculation needs. The exp exponential function requires the combination of multiple instructions.

[0044] In step S1, the SIMD instruction is set as follows:

[0045] Register1=Ingenic_simd512_load((float)data)

[0046] Ingenic_simd512_load is the data loading instruction; (float)data is the input of 16 32-bit single-precision floating-point numbers; Register1 is register 1, which stores the input data.

[0047] In step S2, the range of single-precision floating-point numbers is -3.4e38 to 3.4e38. To prevent the calculation result from overflowing and exceeding the range of single-precision floating-point numbers, the input data is limited in range. In C language, exp(88.7) = 3.3e38. Correspondingly, the maximum input data for the exp function in this method is also 88.7. Data greater than 88.7 is set to equal 88.7. When the input data approaches -∞, the function value approaches 0 infinitely. Calculating input data that is too small is meaningless. Therefore, when the input data is less than -88.7, it is set to equal -88.7.

[0048] In step S2, the SIMD instruction for implementing range limitation is set as follows:

[0049] Register2=Ingenic_simd512_load_float(-88.7)

[0050] Register3=Ingenic_simd512_load_float(88.7)

[0051] Register1=Ingenic_simd512_max(Register1, Register2)

[0052] Register1=Ingenic_simd512_min(Register1, Register3)

[0053] Ingenic_simd512_load_float is a floating-point instruction that loads and copies floating-point numbers. After using this instruction, Register2 contains 16 -88.7 values ​​and Register3 contains 16 88.7 values.

[0054] Ingenic_simd512_max is the maximum value instruction. This instruction compares the 16 data in Register1 with the 16 -88.7 in Register2 and takes the maximum value. That is, data greater than -88.7 remains unchanged, and data less than -88.7 is taken as -88.7. The result is stored in Register1.

[0055] Ingenic_simd512_min is the minimum value instruction. This instruction compares the 16 data points in Register1 and the 16 88.7 values ​​in Register3 from the previous step and takes the minimum value. That is, data less than 88.7 remains unchanged, and data greater than 88.7 is taken as 88.7. The result is still stored in Register1.

[0056] In step S3, the

[0057] 1) Use SIMD instructions to calculate formula (1), where x / ln2 is equivalent to x*(1 / ln2), and 1 / ln2 = 1.442695;

[0058] Set up SIMD instructions:

[0059] Register2=Ingenic_simd512_load_float(1.442695)

[0060] Register3=Ingenic_simd512_float_mul(Register1, Register2)

[0061] Register3=Ingenic_simd512_float_to_int_truncate(Register3)

[0062] First, load the floating-point number 1.442695 into Register2;

[0063] Ingenic_simd512_float_mul is a floating-point multiplication instruction that multiplies the 16 input data in Register1 with the 16 corresponding 1.442695 values ​​in Register2 and places the result in Register3.

[0064] Ingenic_simd512_float_to_int_truncate is a floating-point to integer conversion instruction. This instruction converts the floating-point number in Register3 into an integer, directly discards the data after the decimal point, and keeps the integer part to obtain x_int in formula (1). The result is stored in Register3.

[0065] 2) Using SIMD instructions to calculate exp_bas in formula (6), float data occupies 4 bytes, or 32 bits, including 1 sign bit, 8 bits for the exponent part, and 23 bits for the mantissa part. A floating-point number is equal to the power of 2 plus the mantissa. When float is stored in the computer, the exponent is the actual exponent plus 127. The SIMD instructions use the float structure to add 127 to x_int and then shift it left by 23 bits to get the floating-point number 2. x_int SIMD instructions are set to:

[0066] Register4=Ingenic_simd512_load_immediate(127)

[0067] Register4=Ingenic_simd512_int_add(Register3, Register4)

[0068] Register4=Ingenic_simd512_shift_letf(Register4, 23)

[0069] Ingenic_simd512_load_immediate is an instruction to load an immediate value, which loads the integer 127 and copies it 16 times, storing it in Register4.

[0070] Ingenic_simd512_int_add is an integer addition instruction that adds the data in Register3 and Register4 and stores the result in Register4;

[0071] Ingenic_simd512_shift_letf is a left shift instruction that shifts all 16 integers in Register4 to the left by 23 bits.

[0072] In step S4, the

[0073] 1) Use SIMD instructions to calculate formula (3), where ln2 is 6.931472e-1

[0074] Register2=Ingenic_simd512_load_float(6.931472e-1)

[0075] Register3=Ingenic_simd512_int_to_float(Register3)

[0076] Register2=Ingenic_simd512_float_mul(Register2, Register3)

[0077] Register1=Ingenic_simd512_float_sub(Register1, Register2)

[0078] Ingenic_simd512_load_float loads 6.931472e-1 and copies it to Register2;

[0079] Ingenic_simd512_int_to_float is an instruction for converting integers to floating-point numbers. It converts the 16 integers x_int in Register3 into floating-point numbers because integers cannot be directly operated on with floating-point numbers in the Ingenic_simd512 instruction set. Integers need to be converted to floating-point numbers first, and then floating-point instructions are used for operation.

[0080] Ingenic_simd512_float_mul multiplies the 16 ln2 values ​​in Register2 with the 16 data values ​​in Register3 to obtain x_bas, and stores the result in Register2;

[0081] Ingenic_simd512_float_sub is a floating-point subtraction instruction that subtracts the data in Register1 from the data in Register2 to obtain x_rem, and stores the result in Register1;

[0082] 2) Use the Taylor expansion to find formula (7). The Taylor formula for an exponential function with base e is:

[0083]

[0084] When n is 6, the accuracy requirement of the exp function in this method is met. This method does not calculate the case where n exceeds 6, otherwise it will result in a large amount of computation and slow operation speed; therefore, e x_rem Expanded into formula (10):

[0085]

[0086] In formula (10), 1 / 6! is 1.388889e-3, 1 / 5! is 8.333333e-3, 1 / 4! is 4.166667e-2, 1 / 3! is 1.666667e-1, and 1 / 2! is 0.5;

[0087] The steps to implement formula (10) using SIMD are as follows, and the SIMD instructions are set as follows:

[0088] Register2=Ingenic_simd512_load_float(1.388889e-3)

[0089] Register3=Ingenic_simd512_float_mul(Register2, Register1)

[0090] Register2=Ingenic_simd512_load_float(8.333333e-3)

[0091] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0092] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0093] Register2=Ingenic_simd512_load_float(4.166667e-2)

[0094] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0095] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0096] Register2=Ingenic_simd512_load_float(1.666667e-1)

[0097] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0098] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0099] Register2=Ingenic_simd512_load_float(0.5)

[0100] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0101] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0102] Register2=Ingenic_simd512_load_float(1.0)

[0103] Register3=Ingenic_simd512_float_add(Register2, Register3)

[0104] Register3=Ingenic_simd512_float_mul(Register3, Register1)

[0105] Register3=Ingenic_simd512_float_add(Register2, Register3)

[0106] Ingenic_simd512_load_float loads 1.388889e-3 and copies it to Register2. Ingenic_simd512_float_mul multiplies the 16 x_rem data in Register1 with the 16 1.388889e-3 data in Register2 and stores the result in Register3.

[0107] Load 8.333333e-3 into Register2.

[0108] Ingenic_simd512_float_add is a floating-point addition instruction that adds the 16 data in Register3 to the 16 data in Register2 and stores the result in Register3; then it multiplies Register3 by the 16 x_rem values ​​in Register1 and stores the result in Register3.

[0109] This process continues until all calculations in formula (10) are completed. The result of exp_rem, calculated using SIMD instructions, is finally stored in Register3.

[0110] The SIMD instruction in step S5 is set as follows:

[0111] Register3=Ingenic_simd512_float_mul(Register3, Register4)

[0112] Ingenic_simd512_float_mul is a floating-point multiplication instruction that multiplies the results of Register3 and Register4.

[0113] The SIMD instructions used in step S6 are set as follows:

[0114] Result=Ingenic_simd512_store(Register3)

[0115] Ingenic_simd512_store is a data storage instruction that saves 16 data entries from Register3 to Result memory, which can store 16 data entries.

[0116] Therefore, the advantages of this application are: the method is simple and easy to operate. When running a large amount of continuous data on the Beijing Junzheng T40 chip, the improved method, under the same input data, only loses 0.001% of the accuracy compared with the exp function in the C language math.h library, while the operation speed can reach 29 times that of the C language exp function, thus meeting the requirements of the Beijing Junzheng T40 chip for the accuracy and operation speed of the exp function. Attached Figure Description

[0117] The accompanying drawings, which are provided to further illustrate the invention and form part of this application, are not intended to limit the scope of the invention.

[0118] Figure 1 This is a flowchart illustrating the method of the present invention. Detailed Implementation

[0119] To better understand the technical content and advantages of the present invention, the present invention will now be described in further detail with reference to the accompanying drawings.

[0120] The SIMD instruction set used in this embodiment is the Ingenic_simd512 instruction set supported by the Beijing Junzheng T40 chip. This instruction set can load 512 bits of data into a 512-bit register at once, and can load 1024 bits of cached data into two registers at once. A total of 32 registers are available and can be reused. During calculation, the same operation can be performed on 512 bits of data at once. This instruction set includes arithmetic operations such as addition, subtraction, multiplication, absolute value, maximum value, and minimum value; logical operations such as AND, OR, NOT, XOR, and shift; and comparison operations such as less than, equal to, and less than or equal to, satisfying general computational needs. The exp exponential function requires a combination of multiple instructions.

[0121] Specifically, such as Figure 1 As shown, implementing the exp exponential function using SIMD instructions involves the following steps:

[0122] S1: Load data, in multiples of 32 bits. A register can load a maximum of 512 bits of data. Single-precision floating-point numbers (float) are 32 bits, and a register can load 16 single-precision floating-point numbers. Therefore, a single SIMD instruction can perform calculations on 16 single-precision floating-point numbers simultaneously. The SIMD instruction is:

[0123] Register1=Ingenic_simd512_load((float)data)

[0124] Ingenic_simd512_load is the data loading instruction; (float)data is the input of 16 32-bit single-precision floating-point numbers; Register1 is register 1, which stores the input data.

[0125] S2: The range of input data is limited to -88.7 to 88.7. Although the range of the exp function is (0, +∞), the Beijing Junzheng T40 chip only supports single-precision floating-point operations, and the range of single-precision floating-point numbers is -3.4e38 to 3.4e38. To prevent the calculation result from overflowing (exceeding the range of single-precision floating-point numbers), the range of input data is limited. In C language, exp(88.7) = 3.3e38. Correspondingly, the maximum input data for the exp function in this invention is also 88.7. Data greater than 88.7 is set to 88.7. When the input data approaches -∞, the function value approaches 0 infinitely, and calculating input data that is too small is meaningless. Therefore, when the input data is less than -88.7, it is set to -88.7. The SIMD instruction is:

[0126] Register2=Ingenic_simd512_load_float(-88.7)

[0127] Register3=Ingenic_simd512_load_float(88.7)

[0128] Register1=Ingenic_simd512_max(Register1, Register2)

[0129] Register1=Ingenic_simd512_min(Register1, Register3)

[0130] `Ingenic_simd512_load_float` is a floating-point instruction that loads and copies floating-point numbers. After using this instruction, Register2 contains 16 values ​​of -88.7, and Register3 contains 16 values ​​of 88.7. `Ingenic_simd512_max` is a maximum value instruction that compares the 16 data points in Register1 with the 16 values ​​of -88.7 in Register2 and takes the maximum value (data points greater than -88.7 remain unchanged, and data points less than -88.7 are taken as -88.7), and the result is stored in Register1. `Ingenic_simd512_min` is a minimum value instruction that compares the 16 data points in Register1 with the 16 values ​​of 88.7 in Register3 and takes the minimum value (data points less than 88.7 remain unchanged, and data points greater than 88.7 are taken as 88.7), and the result is still stored in Register1.

[0131] S3: To speed up the calculation of the exp function, e raised to the power of x can be split into two parts (x is the input data after range restriction in step S2) and multiplied together. Therefore, x needs to be split into two parts and added together, as shown in the following formula:

[0132] x_int=int(x / ln 2) Formula (1)

[0133] x_bas=x_int*ln 2 Formula (2)

[0134] Formula (3) is x_rem = x - x_bas

[0135] In formula (1), int(x / ln2) converts the result of x divided by ln2 into an int type (i.e., retaining the integer part of the result and discarding the decimal part), and x_int represents the quotient of x divided by ln2; in formula (2), x_bas represents the result of multiplying the quotient x_int by the divisor ln2; in formula (3), x_rem represents the remainder of x divided by ln2. From the formula: dividend = quotient * divisor + remainder, we can obtain:

[0136] x = x_bas + x_rem Formula (4)

[0137] After splitting x into two parts and adding them together, e x It can be broken down into two parts multiplied together, as follows:

[0138]

[0139] Let exp_bas = 2 x_int Formula (6)

[0140] Let exp_rem = e x_remFormula (7)

[0141] e x =exp_bas*exp_rem Formula (8)

[0142] 1) Use SIMD instructions to calculate formula (1), where x / ln2 is equivalent to x*(1 / ln2), and 1 / ln2 = 1.442695

[0143] Register2=Ingenic_simd512_load_float(1.442695)

[0144] Register3=Ingenic_simd512_float_mul(Register1, Register2)

[0145] Register3=Ingenic_simd512_float_to_int_truncate(Register3)

[0146] First, the floating-point number 1.442695 is loaded into Register2; Ingenic_simd512_float_mul is a floating-point multiplication instruction, which multiplies the 16 input data in Register1 and the 16 corresponding 1.442695 in Register2 and places the result in Register3; Ingenic_simd512_float_to_int_truncate is a floating-point to integer conversion instruction, which converts the floating-point number in Register3 into an integer, directly discards the data after the decimal point, retains the integer part, and obtains x_int of formula (1), and the result is stored in Register3.

[0147] 2) Use SIMD instructions to calculate exp_bas in formula (6). Float data occupies 4 bytes (32 bits), including 1 sign bit, 8 exponent bits, and 23 mantissa bits. A floating-point number equals 2 raised to the power of 2 plus the mantissa. When stored in a computer, the exponent of a float is the actual exponent plus 127. SIMD instructions can use the float structure to add 127 to x_int and then shift it left by 23 bits to obtain the floating-point number 2. x_int .

[0148] Register4=Ingenic_simd512_load_immediate(127)

[0149] Register4=Ingenic_simd512_int_add(Register3, Register4)

[0150] Register4=Ingenic_simd512_shift_letf(Register4, 23)

[0151] Ingenic_simd512_load_immediate is an immediate load instruction that loads the integer 127 and copies it 16 times, storing it in Register4. Ingenic_simd512_int_add is an integer addition instruction that adds the data in Register3 and Register4, storing the result in Register4. Ingenic_simd512_shift_letf is a left shift instruction that shifts all 16 integers in Register4 to the left by 23 bits.

[0152] S4: Find the second part exp_rem of e raised to the power of x.

[0153] 1) Use SIMD instructions to calculate formula (3), where ln2 is 6.931472e-1

[0154] Register2=Ingenic_simd512_load_float(6.931472e-1)

[0155] Register3=Ingenic_simd512_int_to_float(Register3)

[0156] Register2=Ingenic_simd512_float_mul(Register2, Register3)

[0157] Register1=Ingenic_simd512_float_sub(Register1, Register2)

[0158] `Ingenic_simd512_load_float` loads 6.931472e-1 and copies it to `Register2`. `Ingenic_simd512_int_to_float` is an instruction to convert integers to floating-point numbers, converting the 16 integers `x_int` in `Register3` to floating-point numbers. In the `Ingenic_simd512` instruction set, integers cannot be directly operated on with floating-point numbers; they must first be converted to floating-point numbers before using floating-point instructions. `Ingenic_simd512_float_mul` multiplies the 16 `ln2` values ​​in `Register2` with the 16 data values ​​in `Register3` to obtain `x_bas`, and stores the result in `Register2`. `Ingenic_simd512_float_sub` is a floating-point subtraction instruction that subtracts the data in `Register1` from the data in `Register2` to obtain `x_rem`, and stores the result in `Register1`.

[0159] 2) Use the Taylor expansion to find formula (7). The Taylor formula for an exponential function with base e is:

[0160]

[0161] When n is 6, the accuracy requirements of the exp function of the Beijing Junzheng T40 chip can be met. This invention will not calculate the case where n exceeds 6, otherwise it will cause a large amount of calculation and slow down the operation speed.

[0162] Therefore e x_rem This can be expanded into formula (10):

[0163]

[0164] In formula (10), 1 / 6! is 1.388889e-3, 1 / 5! is 8.333333e-3, 1 / 4! is 4.166667e-2, 1 / 3! is 1.666667e-1, and 1 / 2! is 0.5. The steps to implement formula (10) using SIMD are as follows:

[0165] Register2=Ingenic_simd512_load_float(1.388889e-3)

[0166] Register3=Ingenic_simd512_float_mul(Register2, Register1)

[0167] Register2=Ingenic_simd512_load_float(8.333333e-3)

[0168] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0169] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0170] Register2=Ingenic_simd512_load_float(4.166667e-2)

[0171] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0172] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0173] Register2=Ingenic_simd512_load_float(1.666667e-1)

[0174] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0175] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0176] Register2=Ingenic_simd512_load_float(0.5)

[0177] Register3=Ingenic_simd512_float_add(Register2,Register3)

[0178] Register3=Ingenic_simd512_float_mul(Register3,Register1)

[0179] Register2=Ingenic_simd512_load_float(1.0)

[0180] Register3=Ingenic_simd512_float_add(Register2, Register3)

[0181] Register3=Ingenic_simd512_float_mul(Register3, Register1)

[0182] Register3=Ingenic_simd512_float_add(Register2, Register3)

[0183] Ingenic_simd512_load_float loads 1.388889e-3 and copies it to Register2. Ingenic_simd512_float_mul multiplies the 16 x_rem data in Register1 with the 16 1.388889e-3 data in Register2, and saves the result in Register3. 8.333333e-3 is loaded into Register2. Ingenic_simd512_float_add is a floating-point addition instruction. The 16 data in Register3 are added to the 16 data in Register2, and the result is saved in Register3. Register3 is then multiplied with the 16 x_rem data in Register1, and the result is still saved in Register3. This process continues until all calculations in formula (10) are completed. The result of exp_rem calculated by SIMD instructions is finally saved in Register3.

[0184] S5: Use the SIMD instruction to multiply exp_bas and exp_rem obtained in the above steps to get the result of formula (8), and save the result in Register3.

[0185] Register3=Ingenic_simd512_float_mul(Register3, Register4)

[0186] S6: Save the calculation result from the register to memory.

[0187] Result=Ingenic_simd512_store(Register3)

[0188] Ingenic_simd512_store is a data storage instruction that saves 16 data entries from Register3 to Result memory, which can store 16 data entries.

[0189] In summary, the key point of the method in this application is that...

[0190] 1. The exp exponential function is implemented using the SIMD instruction set, especially the Ingenic_simd512 instruction set from the chip developed by Beijing Junzheng. The Ingenic_simd512 instruction set can load 512 bits of data at a time, and a single instruction can perform calculations on 512 bits of data simultaneously, which greatly improves the calculation speed. It can run efficiently on chips with limited hardware resources and high calculation speed requirements. In comparison, on the same T40 chip, the calculation speed can reach 29 times that of the C language exp function.

[0191] 2. In step S2, to prevent overflow of the calculation result, the input data range is limited to -88.7 to 88.7, ensuring that the maximum value of the calculation result does not exceed the maximum value of a single-precision floating-point number, and that excessively small values ​​are not calculated for negative numbers, thus avoiding invalid calculations. The maximum and minimum values ​​are used to limit the data range.

[0192] 3. In step S3, in order to speed up the calculation, the input data is divided into two parts and added together. The derivation process is shown in formulas (1) to (4). Thus, e raised to the power of x can be divided into two parts and multiplied together. The derivation process is shown in formulas (5) to (8).

[0193] 4. In step S3, the first part of e raised to the power of x, exp_bas, is calculated. Using a float data type, this occupies 4 bytes (32 bits): 1 sign bit, 8 bits for the exponent, and 23 bits for the mantissa. A floating-point number equals 2 raised to the power of the power plus the mantissa. When stored in a computer, the exponent of a float is the actual exponent plus 127. The SIMD instruction uses the float structure to add 127 to x_int and then left-shifts it by 23 bits to obtain the floating-point number 2. x_int This method calculates 2 x_int It has a smaller computational complexity and faster computation speed than directly calculating 2 to the power of x_int.

[0194] 5. In step S4, calculate the second part exp_rem of e raised to the power of x. Use Taylor expansion to find the x_rem power of e in formula (7), where x_rem is the remainder of x / ln2, which is a relatively small number. Performing Taylor expansion on the x_rem power of e is less computationally intensive than performing Taylor expansion on the x power of e directly, and the calculation speed will also be improved.

[0195] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations can be made to the embodiments of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for implementing the exp exponential function based on the SIMD instruction set, characterized in that, The method includes S1: Load data in multiples of 32 bits. A register can load a maximum of 512 bits of data. Single-precision floating-point numbers (float) are 32 bits. A register can load 16 single-precision floating-point numbers. Therefore, a single SIMD instruction can perform calculations on 16 single-precision floating-point numbers simultaneously. S2: Limit the range of input data to -88.7 to 88.7; S3: To speed up the operation of the exp function, e x The input data is split into two parts and multiplied together, where x is the range-limited input data from step S2. Therefore, x needs to be split into two parts and added together, as shown in the following formula: x_int=int(x / ln2) Formula (1) x_bas=x_int*ln2 Formula (2) Formula (3) is x_rem = x - x_bas In formula (1), int(x / ln2) converts the result of x divided by ln2 into int type, that is, retains the integer part of the result and discards the decimal part, and x_int represents the quotient of x divided by ln2; In formula (2), x_bas represents the result of multiplying the quotient x_int by the divisor ln2; In formula (3), x_rem represents the remainder when x is divided by ln2; From the formula: dividend = quotient * divisor + remainder, we can obtain: x = x_bas + x_rem Formula (4) After splitting x into two parts and adding them together, e x The steps to split the problem into two parts and multiply them are as follows: make make e x =exp_bas*exp_rem Formula (8) 1) Use SIMD instructions to calculate formula (1); 2) Use SIMD instructions to calculate exp_bas in formula (6); S4: Find e x The second part, exp_rem: 1) Use SIMD instructions to calculate formula (3); 2) Use the Taylor expansion to find formula (7). The Taylor formula for an exponential function with base e is: e x_rem Expand as Formula (10); S5: Use SIMD instructions to multiply exp_bas obtained in the above steps with exp_rem to get the result of formula (8); S6: Save the calculation result from the register to memory.

2. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 1, characterized in that, The SIMD instruction set used in the method can load 512 bits of data into a 512-bit register at once, and can load 1024 bits of cached data into two registers at once. There are 32 registers available for use, which can be reused. During calculation, the same operation can be performed on 512 bits of data at once. The instruction set includes arithmetic operations such as addition, subtraction, multiplication, absolute value, maximum value, and minimum value; logical operations such as AND, OR, NOT, XOR, and shift; and comparison operations such as less than, equal to, and less than or equal to, which meet general calculation needs. The exp exponential function requires the combination of multiple instructions.

3. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 2, characterized in that, The SIMD instruction set is the Ingenic_simd512 instruction set supported by Beijing Junzheng's T40 chip.

4. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 2, characterized in that, In step S1, the SIMD instruction is set as follows: Register1=Ingenic_simd512_load((float)data) Ingenic_simd512_load is the data loading instruction; (float)data is the input of 16 32-bit single-precision floating-point numbers; Register1 is register 1, which stores the input data.

5. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 4, characterized in that, In step S2, the range of single-precision floating-point numbers is -3.4e38 to 3.4e38. To prevent the calculation result from overflowing and exceeding the range of single-precision floating-point numbers, the input data is limited in range. In C language, exp(88.7) = 3.3e38. Correspondingly, the maximum input data for the exp function in this method is also 88.

7. Data greater than 88.7 is set to equal 88.

7. When the input data approaches -∞, the function value approaches 0 infinitely. Calculating input data that is too small is meaningless. Therefore, when the input data is less than -88.7, it is set to equal -88.

7.

6. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 4, characterized in that, In step S2, the SIMD instruction for implementing range limitation is set as follows: Register2=Ingenic_simd512_load_float(-88.7) Register3=Ingenic_simd512_load_float(88.7) Register1=Ingenic_simd512_max(Register1, Register2) Register1=Ingenic_simd512_min(Register1, Register3) Ingenic_simd512_load_float is a floating-point instruction that loads and copies floating-point numbers. After using this instruction, Register2 contains 16 -88.7 values ​​and Register3 contains 16 88.7 values. Ingenic_simd512_max is the maximum value instruction. This instruction compares the 16 data in Register1 with the 16 -88.7 in Register2 and takes the maximum value. That is, data greater than -88.7 remains unchanged, and data less than -88.7 is taken as -88.

7. The result is stored in Register1. Ingenic_simd512_min is the minimum value instruction. This instruction compares the 16 data points in Register1 and the 16 88.7 values ​​in Register3 from the previous step and takes the minimum value. That is, data less than 88.7 remains unchanged, and data greater than 88.7 is taken as 88.

7. The result is still stored in Register1.

7. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 6, characterized in that, In step S3, the 1) Use SIMD instructions to calculate formula (1), where x / ln2 is equivalent to x*(1 / ln2), and 1 / ln2 = 1.442695; Set up SIMD instructions: Register2=Ingenic_simd512_load_float(1.442695) Register3=Ingenic_simd512_float_mul(Register1, Register2) Register3=Ingenic_simd512_float_to_int_truncate(Register3) First, load the floating-point number 1.442695 into Register2; Ingenic_simd512_float_mul is a floating-point multiplication instruction that multiplies the 16 input data in Register1 with the 16 corresponding 1.442695 values ​​in Register2 and places the result in Register3; Ingenic_simd512_float_to_int_truncate is a floating-point to integer conversion instruction that converts the floating-point numbers in Register3 into integers, discards the data after the decimal point, and keeps the integer part to obtain x_int in formula (1), and stores the result in Register3; 2) Using SIMD instructions to calculate exp_bas in formula (6), float data occupies 4 bytes, or 32 bits, including 1 sign bit, 8 bits for the exponent part, and 23 bits for the mantissa part. A floating-point number is equal to the power of 2 plus the mantissa. When float is stored in the computer, the exponent is the actual exponent plus 127. The SIMD instructions use the float structure to add 127 to x_int and then shift it left by 23 bits to get the floating-point number 2. x_int SIMD instructions are set to: Register4=Ingenic_simd512_load_immediate(127) Register4=Ingenic_simd512_int_add(Register3, Register4) Register4=Ingenic_simd512_shift_letf(Register4, 23) Ingenic_simd512_load_immediate is an instruction to load an immediate value, which loads the integer 127 and copies it 16 times, storing it in Register4. Ingenic_simd512_int_add is an integer addition instruction that adds the data in Register3 and Register4 and stores the result in Register4; Ingenic_simd512_shift_letf is a left shift instruction that shifts all 16 integers in Register4 to the left by 23 bits.

8. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 7, characterized in that, In step S4, the 1) Use SIMD instructions to calculate formula (3), where ln2 is 6.931472e-1. Let... Register2=Ingenic_simd512_load_float(6.931472e-1) Register3=Ingenic_simd512_int_to_float(Register3) Register2=Ingenic_simd512_float_mul(Register2, Register3) Register1=Ingenic_simd512_float_sub(Register1, Register2) Ingenic_simd512_load_float loads 6.931472e-1 and copies it to Register2; Ingenic_simd512_int_to_float is an instruction for converting integers to floating-point numbers. It converts the 16 integers x_int in Register3 into floating-point numbers because integers cannot be directly operated on with floating-point numbers in the Ingenic_simd512 instruction set. Integers need to be converted to floating-point numbers first, and then floating-point instructions are used for operation. Ingenic_simd512_float_mul multiplies the 16 ln2 values ​​in Register2 with the 16 data values ​​in Register3 to obtain x_bas, and stores the result in Register2; Ingenic_simd512_float_sub is a floating-point subtraction instruction that subtracts the data in Register1 from the data in Register2 to obtain x_rem, and stores the result in Register1; 2) Use the Taylor expansion to find formula (7). The Taylor formula for an exponential function with base e is: When n is 6, the accuracy requirement of the exp function in this method is met. This method does not calculate the case where n exceeds 6, otherwise it will result in a large amount of computation and slow operation speed; therefore, e x_rem Expanded into formula (10): Formula (10); In formula (10), 1 / 6! is 1.388889e-3, 1 / 5! is 8.333333e-3, 1 / 4! is 4.166667e-2, 1 / 3! is 1.666667e-1, and 1 / 2! is 0.5; The steps to implement formula (10) using SIMD are as follows, and the SIMD instructions are set as follows: Register2=Ingenic_simd512_load_float(1.388889e-3) Register3=Ingenic_simd512_float_mul(Register2, Register1) Register2=Ingenic_simd512_load_float(8.333333e-3) Register3=Ingenic_simd512_float_add(Register2, Register3) Register3=Ingenic_simd512_float_mul(Register3, Register1) Register2=Ingenic_simd512_load_float(4.166667e-2) Register3=Ingenic_simd512_float_add(Register2, Register3) Register3=Ingenic_simd512_float_mul(Register3, Register1) Register2=Ingenic_simd512_load_float(1.666667e-1) Register3=Ingenic_simd512_float_add(Register2, Register3) Register3=Ingenic_simd512_float_mul(Register3, Register1) Register2=Ingenic_simd512_load_float(0.5) Register3=Ingenic_simd512_float_add(Register2, Register3) Register3=Ingenic_simd512_float_mul(Register3, Register1) Register2=Ingenic_simd512_load_float(1.0) Register3=Ingenic_simd512_float_add(Register2, Register3) Register3=Ingenic_simd512_float_mul(Register3, Register1) Register3=Ingenic_simd512_float_add(Register2, Register3) Ingenic_simd512_load_float loads 1.388889e-3 and copies it to Register2. Ingenic_simd512_float_mul multiplies the 16 x_rem data in Register1 with the 16 1.388889e-3 data in Register2 and stores the result in Register3. Load 8.333333e-3 into Register2. Ingenic_simd512_float_add is a floating-point addition instruction that adds the 16 data in Register3 to the 16 data in Register2 and stores the result in Register3; then it multiplies Register3 by the 16 x_rem values ​​in Register1 and stores the result in Register3. This process continues until all calculations in formula (10) are completed. The result of exp_rem, calculated using SIMD instructions, is finally stored in Register3.

9. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 8, characterized in that, The SIMD instruction in step S5 is set as follows: Register3=Ingenic_simd512_float_mul(Register3, Register4) Ingenic_simd512_float_mul is a floating-point multiplication instruction that multiplies the results of Register3 and Register4.

10. The method for implementing the exp exponential function based on the SIMD instruction set according to claim 9, characterized in that, The SIMD instructions used in step S6 are set as follows: Result=Ingenic_simd512_store(Register3) Ingenic_simd512_store is a data storage instruction that saves 16 data entries from Register3 to Result memory, which can store 16 data entries.