Log function lookup table method based on float data format
By separating the base and exponent of a floating-point number, constructing a lookup table, and directly retrieving the pre-calculated result, the precision loss problem of the floating-point lookup table method in the prior art is solved, and higher precision and faster floating-point calculation are achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HEFEI JUNZHENG TECH CO LTD
- Filing Date
- 2025-01-23
- Publication Date
- 2026-07-24
Smart Images

Figure CN122450252A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of computer science and numerical computation technology, and specifically relates to a log function lookup table method based on float data format. Background Technology
[0002] With the development of technology, especially the advent of the artificial intelligence era, big data computing is becoming increasingly important. Floating-point arithmetic is a common requirement in modern computer computing, but traditional floating-point calculation methods are inefficient when processing large amounts of data. Lookup table methods improve computation speed by pre-compiling and storing results, but existing lookup table methods suffer from precision loss when processing floating-point numbers. In particular, existing lookup table methods typically use float values directly for table lookups, which leads to even greater precision loss.
[0003] In addition, commonly used technical terms include:
[0004] Lookup Table (LUT): A data structure used to store pre-computed results for fast retrieval.
[0005] Floating-point number: A numerical representation used to represent real numbers, including a sign bit, an exponent bit, and a mantissa bit.
[0006] Pre-computation: Calculating and storing certain values before the program is executed.
[0007] Hardware acceleration: using dedicated hardware to improve program execution speed. Precision loss: in numerical computation, limitations in data representation lead to a decrease in the precision of the calculation results. Summary of the Invention
[0008] To address the aforementioned issues, the purpose of this application is to significantly reduce the lookup range and improve accuracy by utilizing the float data format. This method can be applied to projects requiring fast floating-point calculations, such as graphics processing, scientific computing, and financial analysis.
[0009] Specifically, this invention provides a method for looking up a table for the log function based on float data format. This method utilizes the storage format of floating-point data to quickly calculate the value of the log2 function by looking up a table; it includes the following steps:
[0010] S1, Data Separation Stage: Separate the base and exponent of the input floating-point number; only look up the table for the base;
[0011] S2, Lookup table construction: Construct a lookup table where each entry contains a floating-point number and its corresponding pre-computed result;
[0012] S3, Fast Retrieval: When the result of a floating-point number needs to be calculated, the corresponding pre-calculated result is retrieved directly from the lookup table, avoiding complex floating-point calculations.
[0013] Step S1 further includes:
[0014] First, extract the mantissa and exponent of the data;
[0015] Then, the last digit is calculated by looking up a table, and the exponent is directly taken.
[0016] Since the input to log2 is restricted to be greater than 0, the sign bit here is always 0; according to the computational properties of log2:
[0017] log2f = log2(2 E-127 *M)=log22 E-127 +log2M=E-127+log2M
[0018] The original range of f was from zero to positive infinity, while the range of the mantissa M is now from zero to two. Therefore, the input range for calculating log2 has become smaller. At this point, calculating log2M by looking up a table will be more accurate than calculating log2f directly.
[0019] The storage format of the floating-point data includes:
[0020] Single-precision floating-point numbers are a 32-bit (4-byte) floating-point representation defined by the IEEE 754 standard; the 32-bit structure of a single-precision floating-point number is as follows:
[0021] 1) Sign bit: 1 bit, the leftmost bit, used to indicate whether the value is positive or negative; 0 represents a positive number, and 1 represents a negative number;
[0022] 2) Exponent: 8 bits, used to represent the range of values; this part uses offset binary representation, where the offset bias is 127, and the actual exponent value needs to be obtained by subtracting the offset from the value represented by the exponent.
[0023] 3) Mantissa: 23 bits, used to represent the precision of the value; the mantissa usually represents a fraction, and its integer part is 1 by default, i.e., the hidden bit, unless the exponent bits are all 0, which means it represents a subnormal number or a number close to 0, in which case the integer part is 0;
[0024] A float number can be represented as:
[0025]
[0026] The Python code implementation for step S1 is as follows:
[0027] def log2_lut(num,table_zeropoint_value=1.5, table_neg_num=16, table_pos_num=16):
[0028] Define a list to store many indices, represented as: E = []
[0029] The last digit M is represented as: M = []
[0030] The input num is usually not a single number but a series of numbers. Traversing the entire array is represented as: for iinrange(len(num));
[0031] binary_repr=np.binary_repr(num[i].view('uint32'))
[0032] zero_num=32-len(binary_repr)
[0033] binary_repr=zero_num*'0't+binary_repr
[0034] Extract bits 23-31 of the target data, then parse it into an integer and subtract 127. Use this method to calculate the exponent, represented as: E.append(int(binary_repr[-31:-23],2)-127)
[0035] m value = 1.0
[0036] Iterating through each digit of the last digit is represented as:
[0037] for iin range(23):
[0038] if binary_repr[-23:][i]=='1';
[0039] m_value=m_value+1 / (2**(i+1))
[0040] M.append(m value)
[0041] E=np.array(E).astype(np.foat32)
[0042] M=np.array(M),astype(np.float32)
[0043] According to the formula in S3, the exponent is directly taken as follows:
[0044] base data = E.
[0045] In step S2, the implementation of generating a lookup table based on the log2 function includes: creating two lookup tables, one for the negative interval and one for the positive interval;
[0046] S2.1, Determine the table entry width
[0047] table_pos_item_bitwhidth=math.log2(table_pos_num)
[0048] table_neg_item_bitwhidth=math.log2(table_neg_num)
[0049] The bit width of the table entries is calculated here. Assuming that table_pos_num and table_neg_num are both 16, then the bit width is 4 bits.
[0050] S2.2, Create an array of input values
[0051] x_neg=np.linspace(1.0,table_zeropoint_value,table_neg_num+1,endpoint=True)
[0052] x_neg = np.flipud(x_neg)
[0053] x_pos=np.linspace(table_zeropoint_value,2.0,table_pos_num+1,endpoint=True)
[0054] Create an array with equal spacing from 1.0 to 1.5 for the negative interval, and then flip it;
[0055] Create an equally spaced array from 1.5 to 2.0 for the positive interval;
[0056] S2.3, Calculate the logarithm
[0057] y_neg = np.log2(x_neg)
[0058] y_pos = np.log2(x_pos);
[0059] S2.4, Calculate the interpolation parameters
[0060]
[0061] This part of the code calculates two important interpolation parameters for each interval:
[0062] dy: The difference in y-values between two adjacent points, used for linear interpolation;
[0063] y0: The starting y value for each interval.
[0064] The curve of the log2 function and the distribution of the lookup table LUT on the function in step S2 lookup table construction further include:
[0065] The curve of the log2 function lies in a coordinate system with the input value as the horizontal axis (x-axis) and the log2 value as the vertical axis (y-axis);
[0066] The sampling points in the lookup table are not evenly distributed on the x-axis, which reflects the design of the code: a) In the negative interval (1.0 to 1.5): the points are more densely distributed because the log2 function changes more rapidly in this interval;
[0067] b) Positive interval (1.5 to 2.0): The points are more sparsely distributed because the log2 function changes more slowly in this interval;
[0068] In actual use, the code selects an appropriate interval based on the input value, and then performs linear interpolation between two adjacent sampling points to obtain an approximate value of the log2 function.
[0069] Step S3, quick table lookup, further includes:
[0070] S3.1, Quantization input x:
[0071] Convert the input values to a fixed-point format for easier subsequent processing;
[0072] S3.2, Calculate the table index:
[0073] Using bitwise operations to quickly calculate the lookup table index, this step divides the input range into several intervals. The number of intervals is variable; the more intervals there are, the more accurate the data, but the lower the efficiency. Let's assume we take 32 intervals, with each interval corresponding to an entry in the LUT.
[0074] S3.3, look up table entries dy and y0:
[0075] The calculated index is used to retrieve dy (slope) and y0 (initial value) from a pre-calculated lookup table. These values represent a linear approximation within the current interval.
[0076] S3.4, Extract the decimal part:
[0077] At the same time, the decimal part is extracted from the quantized input; this decimal part will be used for precise interpolation within the found interval.
[0078] S3.5, Linear Interpolation:
[0079] Using the found y0 and dy, and the extracted decimal part, linear interpolation is performed; this step accurately calculates the logarithm of the input value.
[0080] S3.6, Return the interpolation result:
[0081] The calculated result is an approximate logarithmic value of the input value.
[0082] The code implementation involved in step S3 is as follows:
[0083] The scaling factors for both positive and negative directions are calculated based on the zero-point value (table_zeropoint_value) to normalize the data; this is represented as follows:
[0084] pos_scale=1 / abs(2.0-table_zeropoint_value)
[0085] neg scale=1 / abs(1.0-table zeropoint value)
[0086] First, zero-point calibration is performed on the input value M, expressed as:
[0087] x_fixed=(M-table_zeropoint_value)
[0088] Different scaling factors are used depending on whether the value is positive or negative, as shown below:
[0089] x fixed=np.where(x fixed>o,xfixed*pos_scale,x fixed*neg_scale)
[0090] The result is quantized to a 15-bit fixed-point range and expressed as:
[0091] x fixed=np.round(x fixed*(2**15))
[0092] Using clip to limit the range of values and prevent overflow is represented as:
[0093] x_fixed=np.cLip(x_fixed,-(2**15-1),(2**15)-1)
[0094] Different parameters are used for calculations of negative and positive values, and the cal_y function is used for table lookup calculations, taking into account different bit width parameter configurations, as shown below:
[0095] neg_y_new
[0096] =cal_y(x_fixed.clip(-2**3l,0),neg_dy,neg_y0,table_neg_item_bitwhidth)pos_y_new
[0097] =cal_y(x_fixed.clip(0,2**31),pos_dy,pos_y0,table_pos_item_bitwhidth) selects the corresponding calculation result based on the sign of the value, as shown below:
[0098] extend_data=np.where(x_fixed>=0,pos_y_new,neg_y_new)
[0099] The final result is obtained by adding the calculation result to the base data (base_data), and is expressed as:
[0100] ret = base data + extended data
[0101] return ret
[0102] The code above reflects the construction of the corresponding S2 lookup table;
[0103] def cal_y(x,dy,yo,table_item_bitwhidth)
[0104] Step S3.1 quantization, represented as: x_quantize = np.abs(x).astype(np.int64)
[0105] Step S3.2 is represented as: x indices = x quantize >> int(15 - table_item bitwise)
[0106] x_indices=x_indices.astype(np.int64)
[0107] Step S3.3 is represented as: dy = np.array(dy)[x_indices]
[0108] y0 = np.array(yo)[x indices]
[0109] Step S3.4 is represented as: ftoat_bitwidth = int(15 - table_item_bitwidth)
[0110] mask foat=int(2**float bitwidth-1)
[0111] ceof = x_quantize&mask_foat
[0112] Step S3.5 is represented as: y_new = y0 + ceof * dy / (2**foat_bitwidth)
[0113] return y_new
[0114] The code above reflects the content of steps S3.1-S3.6.
[0115] The method can be applied to projects that require fast floating-point calculations, including graphics processing, scientific computing, and financial analysis.
[0116] Therefore, the advantage of this application is that it effectively overcomes the loss of precision and avoids affecting the accuracy of the calculation results, achieving higher precision than direct table lookup. For projects such as image processing, scientific computing, and financial analysis, it can effectively improve calculation speed and enhance system efficiency. 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 schematic diagram of the process of this method.
[0119] Figure 2 This is a code diagram of step S1, the data separation stage, of this method.
[0120] Figure 3 This is a schematic diagram illustrating the working principle of the lookup table in step S2 of this method, which involves constructing the lookup table.
[0121] Figure 4 This is a schematic diagram illustrating the log2 function lookup table construction process in step S2 of this method.
[0122] Figure 5 This is a schematic diagram of the code for generating the lookup table using the log2 function in step S2 of this method.
[0123] Figure 6 This is a code diagram illustrating the fast retrieval process in step S3 of this method.
[0124] Figure 7 This method is illustrated by the example: 0.15625 = (+1) * 2 -3 *1.25 Schematic diagram of binary representation. Detailed Implementation
[0125] 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.
[0126] This application relates to the fields of computer science and numerical computation, particularly the fast lookup and computation of floating-point numbers.
[0127] This application proposes a lookup table method based on float data format, such as... Figure 1 As shown, the specific steps include:
[0128] S1, Data Separation Stage: Separate the base and exponent of the input floating-point number; only look up the table for the base;
[0129] S2, Lookup table construction: Construct a lookup table where each entry contains a floating-point number and its corresponding pre-computed result;
[0130] S3, Fast Retrieval: When the result of a floating-point number needs to be calculated, the corresponding pre-calculated result is retrieved directly from the lookup table, avoiding complex floating-point calculations.
[0131] The data separation stage in step S1 further includes:
[0132] First, let's introduce the float data storage format.
[0133] Single-precision floating-point format is a 32-bit (4-byte) floating-point number representation method defined by the IEEE 754 standard. It is widely used in modern computer systems because it provides a reasonable balance between numerical range and precision, while occupying relatively little storage space.
[0134] The 32-bit structure of a single-precision floating-point number is as follows:
[0135] 1. Sign bit: 1 bit, the leftmost bit, used to indicate whether the number is positive or negative. 0 represents a positive number, and 1 represents a negative number.
[0136] 2. Exponent bits: 8 bits, used to represent the range of values. This part uses biased binary representation, where the bias is 127. The actual exponent value is obtained by subtracting the bias from the value represented by the exponent bits.
[0137] 3. Mantissa bits (or fraction bits): 23 bits, used to represent the precision of a numerical value. The mantissa bits typically represent a fraction, with the integer part defaulting to 1 (implicit bits), unless the exponent bits are all 0 (representing a subnormal number or a number close to 0), in which case the integer part is 0. A fraction can be represented as:
[0138]
[0139] For example, 0.15625 can be represented as:
[0140] 0.15625 = (+1) * 2 -3 *1.25
[0141] Binary representation as follows Figure 7 As shown.
[0142] This method utilizes the floating-point data storage format to quickly calculate the value of log2 by looking up a table. First, the mantissa and exponent of the data need to be extracted. Then, the mantissa is calculated using a lookup table, while the exponent is directly retrieved.
[0143] Because the input to log2 is restricted to be greater than 0, the sign bit here is always 0. Based on the computational properties of log2:
[0144] log2f = log2(2 E-127 *M)=log22 E-127 +log2M=E-127+log2M
[0145] The original range of f was from zero to positive infinity, while the range of the mantissa M is now from zero to two. Therefore, the input range for calculating log2 has decreased. In this case, calculating log2M using a lookup table will yield significantly higher accuracy than directly using log2f.
[0146] The specific code for the data separation stage in step S1 is as follows: Figure 2 As shown:
[0147] def log2_lut(num,table_zeropoint_value=1.5, table_neg_num=16, table_pos_num=16):
[0148] Define a list to store many indices, represented as: E = []
[0149] The last digit M is represented as: M = []
[0150] The input num is usually not a single number but a large number. Traversing the entire array is represented as: for i inrange(len(num));
[0151] binary_repr=np.binary_repr(num[i].view('uint32'))
[0152] zero_num=32-len(binary_repr)
[0153] binary_repr=zero_num*'0't+binary_repr
[0154] Extract bits 23-31 of the target data, then parse it into an integer and subtract 127. Use this method to calculate the exponent, represented as: E.append(int(binary_repr[-31:-23],2)-127)
[0155] m value = 1.0
[0156] Iterating through each digit of the last digit is represented as:
[0157] for i in range(23):
[0158] if binary_repr[-23:][i]=='1';
[0159] m_value=m_value+1 / (2**(i+1))
[0160] M.append(m value)
[0161] E=np.array(E).astype(np.foat32)
[0162] M=np.array(M),astype(np.float32)
[0163] According to the formula in S3, the exponent is directly taken as follows:
[0164] base data = E.
[0165] The step S2, table construction, further includes:
[0166] like Figure 3 As shown, this two-dimensional coordinate system diagram illustrates how lookup tables work, especially when implementing functions like log2; a detailed explanation of each part of this diagram follows:
[0167] 1. Coordinate system:
[0168] a) The x-axis represents the input value;
[0169] b) The y-axis represents the function value (log2 value in this example);
[0170] 2. Blue curve:
[0171] a) This represents the actual log2 function;
[0172] b) In practical applications, we hope to approximate this function;
[0173] 3. Red dot:
[0174] a) These are the pre-computed points in the lookup table (LUT);
[0175] b) Each point represents a table entry, storing the function value corresponding to a specific input value;
[0176] c) Note that the distribution of these points on the x-axis may be uneven, which reflects the strategy of using different sampling densities for different intervals in the code.
[0177] 4. Green dashed lines and dots:
[0178] a) The dashed line represents a specific input value.
[0179] b), where the green dot represents the interpolation result corresponding to this input value.
[0180] 5. Purple line segment:
[0181] a) This represents a linear interpolation process;
[0182] b) connects the two nearest LUT points on either side of the input value.
[0183] Working principle:
[0184] 1. Input quantization:
[0185] Given an input value (green dashed line), first determine which two LUT points it falls between.
[0186] 2. Look up the table:
[0187] Get the values of these two LUT points (the two endpoints of the purple line segment).
[0188] 3. Linear interpolation:
[0189] Linear interpolation is performed based on the relative positions of the input values between the two LUT points (green dots on the purple line segment).
[0190] 4. Output results:
[0191] The interpolated value (y-coordinate of the green dot) is the approximate function value.
[0192] Advantages of this method:
[0193] a) Speed: It avoids complex function calculations, requiring only simple table lookups and linear interpolation operations. b) Precision control: By increasing the number of LUT points, the accuracy of the approximation can be improved.
[0194] c) Flexibility: The sampling density can be adjusted according to the characteristics of the function in different intervals, such as using denser sampling in regions where the function changes rapidly.
[0195] The log2 function lookup table in this application is as follows: Figure 4 As shown:
[0196] This graph shows the curve of the log2 function and the distribution of lookup tables (LUTs) on the function.
[0197] The following explains the elements in the diagram:
[0198] 1. Blue curve: This is the graph of the log2 function.
[0199] 2. Red dots: These dots represent sampling points in the lookup table. Notice that these dots are not evenly distributed along the x-axis, reflecting the design of the code:
[0200] a) Negative interval (1.0 to 1.5): The points are more densely distributed because the log2 function changes more rapidly in this interval.
[0201] b) Positive interval (1.5 to 2.0): The points are sparsely distributed because the log2 function changes slowly in this interval.
[0202] 3. Note: The positions of the negative interval LUT and the positive interval LUT are marked in the figure.
[0203] This non-uniform sampling method has several advantages:
[0204] 1. Precision control: In regions where the function changes rapidly (such as the part close to 1), the sampling points are more densely packed, which can provide higher precision.
[0205] 2. Storage efficiency: In regions where the function changes slowly, sampling points can be relatively sparse, saving storage space.
[0206] 3. Computational efficiency: By using this piecewise method, the entire function can be approximated with fewer sampling points, which improves the efficiency of table lookup and interpolation.
[0207] In practical use, the code selects an appropriate interval based on the input value and then performs linear interpolation between two adjacent sampling points to obtain an approximate value of the log2 function. This method significantly improves computation speed while maintaining a certain level of accuracy, making it particularly suitable for scenarios requiring frequent logarithm calculations.
[0208] The code for generating a lookup table based on the log2 function is as follows: Figure 5 As shown: This code snippet is the core of building the logarithmic lookup table (LUT). It creates two lookup tables, one for the negative interval and one for the positive interval. A detailed explanation of how this code works is provided below.
[0209] S2.1, Determine the table entry width
[0210] table_pos_item_bitwhidth=math.log2(table_pos_num)
[0211] table_neg_item_bitwhidth=math.log2(table_neg_num)
[0212] This calculates the bit width of the table entries. Assuming both table_pos_num and table_neg_num are 16, then the bit width is 4 bits for each entry.
[0213] S2.2, Create an array of input values
[0214] x_neg=np.linspace(1.0,table_zeropoint_value,table_neg_num+1,endpoint=True)
[0215] x_neg = np.flipud(x_neg)
[0216] x_pos=np.linspace(table_zeropoint_value,2.0,table_pos_num+1,endpoint=True)
[0217] Create an equally spaced array from 1.0 to 1.5 for the negative interval, and then flip it.
[0218] Create an equally spaced array from 1.5 to 2.0 for the positive interval.
[0219] S2.3, Calculate the logarithm
[0220] y_neg = np.log2(x_neg)
[0221] y_pos = np.log2(x_pos)
[0222] S2.4, Calculate the interpolation parameters
[0223]
[0224] This part of the code calculates two important interpolation parameters for each interval:
[0225] dy: The difference in y-values between two adjacent points, used for linear interpolation.
[0226] y0: The starting y value for each interval.
[0227] The aforementioned step S3, fast retrieval (table lookup), further includes:
[0228] S3.1, Quantization input x:
[0229] Convert the input values to a fixed-point format for easier subsequent processing.
[0230] S3.2, Calculate the table index:
[0231] The lookup table index is calculated quickly using bitwise operations. This step effectively divides the input range into several intervals, each corresponding to an entry in the LUT.
[0232] S3.3, look up table entries dy and y0:
[0233] The calculated index is used to retrieve dy (slope) and y0 (initial value) from a pre-computed lookup table. These values represent a linear approximation within the current interval.
[0234] S3.4, Extract the decimal part:
[0235] Simultaneously, the decimal part is extracted from the quantized input. This decimal part will be used for precise interpolation within the found interval.
[0236] S3.5, Linear Interpolation:
[0237] Using the found y0 and dy, along with the extracted decimal parts, linear interpolation is performed. This step accurately calculates the logarithmic value corresponding to the input value.
[0238] S3.6, Return the interpolation result:
[0239] The calculated result is an approximate logarithmic value of the input value.
[0240] The code involved in step S3 is as follows: Figure 6 As shown, it includes:
[0241] The scaling factors for both positive and negative directions are calculated based on the zero-point value (table_zeropoint_value) to normalize the data; this is represented as follows:
[0242] pos_scale=1 / abs(2.0-table_zeropoint_value)
[0243] neg scale=1 / abs(1.0-table zeropoint value)
[0244] First, zero-point calibration is performed on the input value M, expressed as:
[0245] x_fixed=(M-table_zeropoint_value)
[0246] Different scaling factors are used depending on whether the value is positive or negative, as shown below:
[0247] x fixed=np.where(x fixed>o,xfixed*pos_scale,x fixed*neg_scale)
[0248] The result is quantized to a 15-bit fixed-point range and expressed as:
[0249] x fixed=np.round(x fixed*(2**15))
[0250] Using clip to limit the range of values and prevent overflow is represented as:
[0251] x_fixed=np.cLip(x_fixed,-(2**15-1),(2**15)-1)
[0252] Different parameters are used for calculations of negative and positive values, and the cal_y function is used for table lookup calculations, taking into account different bit width parameter configurations, as shown below:
[0253] neg_y_new
[0254] =cal_y(x_fixed.clip(-2**3l,0),neg_dy,neg_y0,table_neg_item_bitwhidth)pos_y_new
[0255] =cal_y(x_fixed.clip(0,2**31),pos_dy,pos_y0,table_pos_item_bitwhidth) selects the corresponding calculation result based on the sign of the value, as shown below:
[0256] extend_data=np.where(x_fixed>=0,pos_y_new,neg_y_new)
[0257] The final result is obtained by adding the calculation result to the base data (base_data), and is expressed as:
[0258] ret = base data + extended data
[0259] return ret
[0260] The code above reflects the construction of the corresponding S2 lookup table;
[0261] def cal_y(x,dy,yo,table_item_bitwhidth)
[0262] Step S3.1 quantization, represented as: x_quantize = np.abs(x).astype(np.int64)
[0263] Step S3.2 is represented as: x indices = x quantize >> int(15 - table_item bitwise)
[0264] x_indices=x_indices.astype(np.int64)
[0265] Step S3.3 is represented as: dy = np.array(dy)[x_indices]
[0266] y0 = np.array(yo)[x indices]
[0267] Step S3.4 is represented as: ftoat_bitwidth = int(15 - table_item_bitwidth)
[0268] mask foat=int(2**float bitwidth-1)
[0269] ceof = x_quantize & mask_foat
[0270] Step S3.5 is represented as: y_new = y0 + ceof * dy / (2**foat_bitwidth)
[0271] return y_new
[0272] The code above reflects the content of steps S3.1-S3.6.
[0273] Although preferred embodiments of the present application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments of the present application.
[0274] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal device that includes said element.
[0275] 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 looking up a table using the log function based on float data format, characterized in that, The method utilizes the floating-point data storage format to quickly look up and calculate the value of the log2 function; it includes the following steps: S1, Data Separation Stage: Separate the base and exponent of the input floating-point number; only look up the table for the base; S2, Lookup table construction: Construct a lookup table where each entry contains a floating-point number and its corresponding pre-computed result; S3, Fast Retrieval: When the result of a floating-point number needs to be calculated, the corresponding pre-calculated result is retrieved directly from the lookup table, avoiding complex floating-point calculations.
2. The method for looking up a table using a log function based on float data format according to claim 1, characterized in that, Step S1 further includes: First, extract the mantissa and exponent of the data; Then, the last digit is calculated by looking up a table, and the exponent is directly taken. Since the input to log2 is restricted to be greater than 0, the sign bit here is always 0; according to the computational properties of log2: log2f=log2(2 E-127 *M)=log22 E-127 +log2M=E-127+log2M The original range of f was from zero to positive infinity, while the range of the mantissa M is now from zero to two. Therefore, the input range for calculating log2 has become smaller. At this point, calculating log2M by looking up a table will be more accurate than calculating log2f directly.
3. The method for looking up a table using a log function based on float data format according to claim 2, characterized in that, The storage format of the floating-point data includes: Single-precision floating-point numbers are a 32-bit (4-byte) floating-point representation defined by the IEEE 754 standard; the 32-bit structure of a single-precision floating-point number is as follows: 1) Sign bit: 1 bit, the leftmost bit, used to indicate whether the value is positive or negative; 0 represents a positive number, and 1 represents a negative number; 2) Exponent: 8 bits, used to represent the range of values; this part uses offset binary representation, where the offset bias is 127, and the actual exponent value needs to be obtained by subtracting the offset from the value represented by the exponent. 3) Mantissa: 23 bits, used to represent the precision of the value; the mantissa usually represents a fraction, and its integer part is 1 by default, i.e., the hidden bit, unless the exponent bits are all 0, which means it represents a subnormal number or a number close to 0, in which case the integer part is 0; A float number can be represented as:
4. The method for looking up a table using a log function based on float data format according to claim 3, characterized in that, The code implementation for step S1 is as follows: `deflog2_lut(num, table_zeropoint_value = 1.5, table_neg_num = 16, table_pos_num = 16):` defines a list to store many exponents, represented as: `E = []`. The last digit M is represented as: M = [] The input num is usually not a single number but a large number. Traversing the entire array is represented as: for iin range(len(num)); binary_repr=np.binary_repr(num[i].view('uint32')) zero_num=32-len(binary_repr) binary_repr=zero_num*'0't+binary_repr Extract bits 23-31 of the target data, then parse it into an integer and subtract 127. Use this method to calculate the exponent, represented as: E.append(int(binary_repr[-31:-23],2)-127) m value = 1.0 Iterating through each digit of the last digit is represented as: for iin range(23): if binary_repr[-23:][i]=='1'; m_value=m_value+1 / (2**(i+1)) M.append(m value) E=np.array(E).astype(np.foat32) M=np.array(M),astype(np.float32) According to the formula in S3, the exponent is directly taken as follows: base data = E.
5. The method for looking up a table using a log function based on float data format according to claim 1, characterized in that, In step S2, the implementation of generating the lookup table based on the log2 function includes: Two lookup tables were created, one for the negative interval and one for the positive interval; S2.1, Determine the table entry width table_pos_item_bitwhidth=math.log2(table_pos_num) table_neg_item_bitwhidth=math.log2(table_neg_num) The bit width of the table entries is calculated here. Assuming that table_pos_num and table_neg_num are both 16, then the bit width is 4 bits. S2.2, Create an array of input values x_neg=np.linspace(1.0,table_zeropoint_value,table_neg_num+1,endpoint=True) x_neg = np.flipud(x_neg) x_pos=np.linspace(table_zeropoint_value,2.0,table_pos_num+1,endpoint=True) Create an array with equal spacing from 1.0 to 1.5 for the negative interval, and then flip it; Create an array with equal intervals from 1.5 to 2.0 for the positive interval; S2.3, Calculate the logarithm y_neg = np.log2(x_neg) y_pos = np.log2(x_pos); S2.4, Calculate the interpolation parameters neg_dy=[ neg_y0=[] for index in range(0,int(2**table_neg_item_bitwhidth)): neg_dy.append(y_neg[index+1]-y_neg[index]) neg_y0.append(y_neg[index]) pos_dy=[] pos_y0=[] for index in range(0,int(2**table_pos_item_bitwhidth)): pos_dy.append(y_pos[index+1]-y_pos[index]) pos_y0.append(y_pos[index]) This part of the code calculates two important interpolation parameters for each interval: dy: The difference in y-values between two adjacent points, used for linear interpolation; y0: The starting y value for each interval.
6. The method for looking up a table using a log function based on float data format according to claim 5, characterized in that, The curve of the log2 function and the distribution of the lookup table LUT on the function in step S2 lookup table construction further include: The curve of the log2 function lies in a coordinate system with the input value as the horizontal axis (x-axis) and the log2 value as the vertical axis (y-axis); The sampling points in the lookup table are not evenly distributed along the x-axis, which reflects the design of the code: a) Negative interval (1.0 to 1.5): The points are more densely distributed because the log2 function changes rapidly in this interval; b) Positive interval (1.5 to 2.0): The points are more sparsely distributed because the log2 function changes more slowly in this interval; In actual use, the code selects an appropriate interval based on the input value, and then performs linear interpolation between two adjacent sampling points to obtain an approximate value of the log2 function.
7. The method for looking up a table using a log function based on float data format according to claim 1, characterized in that, Step S3, quick table lookup, further includes: S3.1, Quantization input x: Convert the input values to a fixed-point format for easier subsequent processing; S3.2, Calculate the table index: Using bitwise operations to quickly calculate the lookup table index, this step divides the input range into several intervals. The number of intervals is variable; the more intervals there are, the more accurate the data, but the lower the efficiency. Let's assume we take 32 intervals, with each interval corresponding to an entry in the LUT. S3.3, look up table entries dy and y0: The calculated index is used to retrieve dy (slope) and y0 (initial value) from a pre-calculated lookup table. These values represent a linear approximation within the current interval. S3.4, Extract the decimal part: At the same time, the decimal part is extracted from the quantized input; this decimal part will be used for precise interpolation within the found interval. S3.5, Linear Interpolation: Using the found y0 and dy, and the extracted decimal part, linear interpolation is performed; this step accurately calculates the logarithm of the input value. S3.6, Return the interpolation result: The calculated result is an approximate logarithmic value of the input value.
8. The method for looking up a table using a log function based on float data format according to claim 7, characterized in that, The code implementation involved in step S3 is as follows: The scaling factors for both positive and negative directions are calculated based on the zero-point value (table_zeropoint_value) to normalize the data; this is represented as follows: pos_scale=1 / abs(2.0-table_zeropoint_value) neg scale=1 / abs(1.0-table zeropoint value) First, zero-point calibration is performed on the input value M, expressed as: x_fixed=(M-table_zeropoint_value) Different scaling factors are used depending on whether the value is positive or negative, as shown below: x fixed=np.where(x fixed>o,xfixed*pos_scale,x fixed*neg_scale) The result is quantized to a 15-bit fixed-point range and expressed as: x fixed=np.round(x fixed*(2**15)) Using clip to limit the range of values and prevent overflow is represented as: x_fixed=np.cLip(x_fixed,-(2**15-1),(2**15)-1) Different parameters are used for calculations of negative and positive values, and the cal_y function is used for table lookup calculations, taking into account different bit width parameter configurations, as shown below: neg_y_new =cal_y(x_fixed.clip(-2**3l,0),neg_dy,neg_y0,table_neg_item_bitwhidth) pos_y_new =cal_y(x_fixed.clip(0,2**31),pos_dy,pos_y0,table_pos_item_bitwhidth) The calculation result is selected based on the sign of the numerical value, and is represented as follows: extend_data=np.where(x_fixed>=0,pos_y_new,neg_y_new) The final result is obtained by adding the calculation result to the base data (base_data), and is expressed as: ret = base data + extended data return ret The code above reflects the construction of the corresponding S2 lookup table; def cal_y(x,dy,yo,table_item_bitwhidth) Step S3.1 quantization, represented as: x_quantize = np.abs(x).astype(np.int64) Step S3.2 is represented as: x indices = x quantize >> int(15 - table_item bitwise) x_indices = x_indices.astype(np.int64) Step S3.3 is represented as: dy = np.array(dy)[x_indices] y0 = np.array(yo)[x indices] Step S3.4 is represented as: ftoat_bitwidth = int(15 - table_item_bitwidth) mask foat=int(2**float bitwidth-1) ceof = x_quantize & mask_foat Step S3.5 is represented as: y_new = y0 + ceof * dy / (2**foat_bitwidth) return y_new The code above reflects the content of steps S3.1-S3.
6.
9. The method for looking up a table using a log function based on float data format according to claim 1, characterized in that, The method can be applied to projects that require fast floating-point calculations, including graphics processing, scientific computing, and financial analysis.