High-precision 64-bit unsigned integer extraction method based on 16-bit lookup table
By adopting a high-precision 64-bit unsigned integer square recording method based on a 16-bit lookup table in the smart meter, the problem of slow square calculation speed in the existing technology is solved, efficient calculation is achieved, and the high-precision metering needs of smart meter are met.
Patent Information
- Application Number
- CN202411586366.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2025-03-04
AI Technical Summary
Existing smart meters are difficult to meet the efficiency requirements of square calculations at high sampling rates, resulting in slow calculation speed and unable to meet the requirements of high-precision measurement.
A high-precision 64-bit unsigned integer squared method based on a 16-bit lookup table is adopted. This method quickly finds the square root approximation of the input variable through the lookup table, avoiding complex iterative calculations, and significantly improving the calculation speed.
On the premise of ensuring square accuracy, the calculation time is significantly shortened, the data processing efficiency of the metering module is improved, and the measurement needs of high-precision smart meters are met.
Smart Images

Figure CN119558331A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of metering algorithm implementation of high-precision smart electric meters, and in particular to a high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table. Background Art
[0002] In the field of smart meters, in order to meet the high-precision requirements of smart meters, a higher ADC sampling rate is required to obtain the current and voltage data in the power grid. At the same time, a large amount of data calculations are inevitably performed in the metering module, among which reactive power, current and voltage mean square values, etc. all require square root calculations. The square root algorithm in the ANSI 'C' standard library has high precision, but the operation time is relatively long, which cannot meet the calculation requirements of high-precision meters at high sampling rates. Therefore, it is necessary to increase the calculation speed of the square root operation under high sampling rates. At present, most manufacturers use hardware accelerators to improve the calculation speed. The present invention provides a high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table. This method greatly improves the calculation speed of the square root while ensuring the square root accuracy, and meets the metering requirements of high-precision meters. Summary of the invention
[0003] The purpose of the present invention is to provide a high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table. This high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table is applied to a high-precision smart meter metering module. It can greatly shorten the MPU calculation time while ensuring the square root accuracy, greatly improve the data processing efficiency of the metering module, and meet the high-precision metering requirements of the smart meter.
[0004] To achieve the above object, the present invention provides the following technical solutions:
[0005] The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table includes the following steps:
[0006] S1: Input 64-bit unsigned integer variable Xin;
[0007] S2: When Xin<2^16, Xin is directly used as the index value Index1 of table Tab1, and the table is looked up to obtain the value Q1, which is the square root result of Xin.
[0008] S3: When Xin ≥ 2^16, find the first non-zero bit n of the high order of Xin (the lowest bit of Xin is recorded as bit 0);
[0009] S4: Get table index: Cut off the 16-bit index Xin[n,n-15] from position n downward as the index Index1 of table Tab1, calculate the remaining low-order bits m=n-15 of Xin after cutting off the 16-bit index as the index Index2, Index3 of table 2 and table 3;
[0010] S5: Lookup table: obtain value Q1 from table 1 through index1; obtain value Q2 from table 1 through index1+1; obtain value Q3 from table 2 through index2; obtain value Q4 from table 3 through index3; obtain the valid value Q5 of the remaining low-order data after the 16-bit index bit;
[0011] S6: Calculate the intermediate value Q=Q1*Q3;
[0012] S7: Calculate the final square root result Q6=Q1*Q3+(Q2-Q1)*Q5*Q4.
[0013] Preferably, the table Tab1 in S4 comprises arrive The precomputed value of .
[0014] Preferably, the table Tab2 in S4 comprises arrive The precomputed value of .
[0015] Preferably, the table Tab3 in S4 comprises arrive The precomputed value of .
[0016] Preferably, Q in S4 and S6 represents a rough estimate of the square root of the input variable Xin.
[0017] Preferably, (Q2-Q1)*Q5*Q4 in S4 represents a correction factor of Q, which is used to correct errors introduced by truncation and shift rounding in S4 and S5 for further refined calculations.
[0018] Preferably, the error magnitude of the calculation result of the method reaches 10e-6.
[0019] Preferably, the method is applicable to a metering module of a high-precision smart meter.
[0020] In summary, due to the adoption of the above technology, the beneficial effects of the present invention are:
[0021] 1. This high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table is applied to the high-precision smart meter measurement module. It can greatly shorten the MPU calculation time while ensuring the square root accuracy, greatly improve the data processing efficiency of the measurement module, and meet the high-precision measurement requirements of the smart meter.
[0022] 2. The maximum calculation time of this method is reduced to 646 system clock cycles, which is about 82.9% faster than the 3782 clock cycles of the square root algorithm in the ANSI'C' standard library. At the same time, the square root result error reaches 10e-6, which meets the measurement requirements of high-precision electricity meters.
[0023] 3. Improve calculation speed: Through the lookup table, the approximate value of the square root of a 64-bit unsigned integer can be quickly found, avoiding complex iterative calculations, thereby significantly improving the calculation speed.
[0024] 4. Simplify algorithm implementation: Using a lookup table can simplify the implementation of the square root algorithm, making the algorithm more intuitive and easy to understand, while also reducing the error rate during programming.
[0025] 5. Ensure a certain accuracy: Although the lookup table provides approximate values, by properly designing the lookup table, it can be guaranteed to meet the required accuracy requirements within a certain range and meet actual application needs.
[0026] 6. Optimize resource utilization: In scenarios where hardware resources are limited or performance requirements are high, using a lookup table can optimize the utilization of memory and computing resources while ensuring accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention, so that other features, purposes and advantages of the present invention become more obvious. The accompanying drawings of the exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:
[0028] Figure 1 To implement the flowchart for obtaining the high 0 digit of Xin;
[0029] Figure 2 A flowchart for obtaining the valid value of the low-order data after the 16-bit index bit;
[0030] Figure 3 Implement a flow chart for a high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table. DETAILED DESCRIPTION
[0031] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work belong to the scope of protection of the present invention. Therefore, the following detailed description of the embodiments of the present invention provided in the drawings is not intended to limit the scope of the invention claimed for protection, but merely represents the selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work belong to the scope of protection of the present invention.
[0032] In the description of the present invention, it is necessary to understand that the terms indicating orientation or positional relationship are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation on the present invention.
[0033] In the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", "connected", "fixed" and the like should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, it can be the internal connection of two elements or the interaction relationship between two elements. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to the specific circumstances of the specification.
[0034] The present invention provides Figure 1 The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table is shown. The basic steps are as follows:
[0035] S1: Input 64-bit unsigned integer variable Xin.
[0036] S2: When Xin<2^16, Xin is directly used as the index value Index1 of table Tab1, and the table is looked up to obtain the value Q1, which is the square root result of Xin.
[0037] S3: When Xin ≥ 2^16, find the first non-zero bit n of the high order of Xin (the lowest bit of Xin is recorded as bit 0);
[0038] S4: Get table index: Cut off the 16-bit index Xin[n,n-15] from position n downward as the index Index1 of table Tab1, calculate the remaining low-order bits m=n-15 of Xin after cutting off the 16-bit index as the index Index2, Index3 of table 2 and table 3;
[0039] S5: Lookup table: obtain value Q1 from table 1 through index1; obtain value Q2 from table 1 through index1+1; obtain value Q3 from table 2 through index2; obtain value Q4 from table 3 through index3; obtain the valid value Q5 of the remaining low-order data after the 16-bit index bit;
[0040] S6: Calculate the intermediate value Q=Q1*Q3;
[0041] S7: Calculate the final square root result Q6 = Q1*Q3+(Q2-Q1)*Q5*Q4; Notes.
[0042] 1. Lookup table size and precision: Choosing 16 bits as the lookup table index is to balance the lookup table size and precision. If the lookup table is too large, it will take up too much memory; if it is too small, the precision will be insufficient;
[0043] 2. The data in the lookup table is stored in floating-point data type, and the data accuracy can be adjusted according to needs.
[0044] Through the above method, high-precision 64-bit unsigned integer square root based on a 16-bit lookup table can be achieved, taking into account both speed and accuracy.
[0045] Examples of specific code operation steps:
[0046] Example 1
[0047] Assume that the input variable Xin is 999 (decimal), that is, Xin=999.
[0048] 1. Input variables
[0049] · Input: Xin = 999
[0050] 2. Determine the value of Xin
[0051] · Check condition: 999<2^16
[0052] · Use Xin directly as index index1: index1 = 999
[0053] 3. Find table Tab1
[0054] ·Lookup value: Q1 = Tab1
[999] = 31.606962203979492187500000
[0055] 5. Output results
[0056] · Since Xin<2^16, the output square root result is:
[0057] Q1=31.606962203979492187500000.
[0058] Example 2
[0059] Assume that the input variable Xin is 955500 (decimal), that is, Xin=955500.
[0060] 1. Input variables
[0061] · Input: Xin = 955500
[0062] 2. Determine the value of Xin
[0063] · Check condition: 955500>2^16
[0064] 3. Find the high non-zero bit
[0065] · The binary number of 955500 is 1110 1001 0100 0110 1100. Find the first non-zero bit n=19 (the lowest bit is recorded as bit 0).
[0066] 4. Data interception
[0067] · Intercept the high 16 bits (from n to n-15), that is: 1110 1001 0100
[0068] 0110 (from position 19 to position 4)
[0069] · Calculate index Index1: Index1 = 1110 1001 0100 0110 (binary) = 59718 (decimal)
[0070] · The remaining number of digits is calculated as 19-15=4, so Index2=Index3=4.
[0071] 5. Lookup Table
[0072] · Lookup value:
[0073] Q1=Tab1
[59718] =244.37266540527343750000000;
[0074] Q2=Tab1[59718+1]=244.37471008300781250000000;
[0075] Q3 = Tab2[4] = 4;
[0076] Q4=Tab3[4]=0.25,
[0077] Q5 = 1100 (binary) = 12 (decimal);
[0078] 6. Calculate intermediate results
[0079] · Calculate Q:
[0080] Q=Q1×Q3=244.37266540527343750000000×4=977.49066162109375
[0081] 7. Calculate the final result
[0082] · Calculate Q6: Q6 = Q + (Q2-Q1) * Q5 * Q4
[0083] · Final calculation:
[0084] Q6=977.49066162109375+0.002044677734375*12*0.25
[0085] =977.496795654296875
[0086] Summarize:
[0087] The above examples show the specific steps of how to perform square root calculations based on different input values, including conditional judgment, data interception, table lookup to obtain values, calculation of intermediate results and final results. These steps provide a detailed execution flow to help understand how the algorithm works.
[0088] Example:
[0089] This method is suitable for performing square root calculation on 64-bit unsigned integer variables. This method is mainly based on the implementation of lookup tables. The tables involved are Tab1, Tab2, and Tab3. The data type in the table is single-precision floating point. 16-bit data is intercepted from the input variable. On this basis, the indexes of tables Tab1, Tab2, and Tab3 are obtained. Q1, Q2, Q3, and Q4 are obtained by table lookup, and the effective value Q5 after the 16-bit index data bit is obtained. Finally, Q1, Q2, Q3, Q4, and Q5 are substituted into the formula to obtain the square root result of the input variable, and the result data type is single-precision floating point. The maximum calculation time of this method is reduced to 646 system clock cycles. Compared with the 3782 clock cycles of the square root algorithm in the ANSI'C' standard library, the MPU calculation speed is increased by about 82.9%. At the same time, the square root result error magnitude reaches 10e-6, which meets the measurement requirements of high-precision electricity meters.
[0090] The method is applicable to a smart meter measurement module, wherein a specific embodiment includes the following steps:
[0091] Step 1: Determine whether the input variable Xin is less than 2^16.
[0092] Case 1: Xin is less than 2^16.
[0093] Step 2: Use Xin as the index value of Tab1 and look up the table to obtain the value Q1, which is the output result of the square root of Xin.
[0094] Case 2: Xin is greater than or equal to 2^16.
[0095] Step 2: Extract 16-bit data Index1 from the first non-zero bit in the high order of Xin as the index value of Tab1, and obtain Q1 by looking up the table according to the index value.
[0096] Step 3: Use Index+1 as the index value of Tab1, and look up the table to get Q2 based on the index value.
[0097] Step 4: Get the number of low-order bits after the 16-bit index data bit of Tab1 in Xin, use it as the index value of Tab2 and Tab3, and look up the table according to the index value to get Q3 and Q4.
[0098] Step 5: Get the valid value Q5 of the low-order data after the 16-bit index data bit of Tab1 in Xin.
[0099] Step 6: Calculate the output result of Xin square root according to Q1, Q2, Q3, Q4, Q5. Q6 = Q1*Q3+(Q2-Q1)*Q5*Q4, Q6 is the output result of Xin square root.
[0100] This high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table is applied to the high-precision smart meter measurement module. It can greatly shorten the MPU calculation time while ensuring the square root accuracy, greatly improve the data processing efficiency of the measurement module, and meet the high-precision measurement requirements of the smart meter.
[0101] A high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table includes the following steps:
[0102] Step 1: Define arrays Tab1, Tab2, and Tab3.
[0103] in:
[0104]
[0105] Step 2: Enter Xin;
[0106] Step 3: Determine the new boundary conditions;
[0107] Case 1: Xin is less than 2 16 .
[0108] Step 4: Use Xin as the index of Tab1, and look up the table to obtain Q1=Tab1[Xin]. At this time, Q1 is the square root of Xin.
[0109] Case 2: Xin is greater than or equal to 2 16 .
[0110] Step 4: Get the number of leading zeros in Xin. The implementation process is as follows: Figure 1 As shown;
[0111] Step 5: Get the position n of the first non-zero bit in the high order of Xin through LeadingZeros (the lowest bit of Xin is recorded as bit 0);
[0112] Step 6: Cut off the 16-bit index bit Xin[n,n-15] from position n downward as the index Index1 of table Tab1, calculate the remaining low-order bits of Xin after cutting off the 16-bit index bit m=n-15 as the indexes Index2 and Index3 of table Tab2 and table Tab3; Step 5: Get the value Q1 from table 1 through index1; get the value Q2 from table 1 through index1+1; get the value Q3 from table 2 through index2; get the value Q4 from table 3 through index3;
[0113] Step 6: Get the valid value Q5 of the remaining low-order data after the 16-bit index bit. The implementation process is as follows: Figure 2 As shown;
[0114] Step 7: Calculate the square root of Xin, Q6=Q1*Q3+(Q2-Q1)*Q5*Q4; Q6 is the square root of Xin.
[0115] The implementation flow chart of the above steps is as follows Figure 3 shown.
Claims
1. A high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table, characterized in that: The method comprises the following steps: S1: Input 64-bit unsigned integer variable Xin; S2: When Xin<2^16, Xin is directly used as the index value Index1 of table Tab1, and the table is looked up to obtain the value Q1, which is the square root result of Xin. S3: When Xin ≥ 2^16, find the first non-zero bit n of the high order of Xin (the lowest bit of Xin is recorded as bit 0); S4: Get table index: Cut off the 16-bit index Xin[n,n-15] from position n downward as the index Index1 of table Tab1, calculate the remaining low-order bits m=n-15 of Xin after cutting off the 16-bit index as the index Index2, Index3 of table 2 and table 3; S5: Lookup table: obtain value Q1 from table 1 through index1; obtain value Q2 from table 1 through index1+1; obtain value Q3 from table 2 through index2; obtain value Q4 from table 3 through index3; obtain the valid value Q5 of the remaining low-order data after the 16-bit index bit; S6: Calculate the intermediate value Q=Q1*Q3; S7: Calculate the final square root result Q6=Q1*Q3+(Q2-Q1)*Q5*Q4.
2. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: Table Tab1 contains arrive The precomputed value of .
3. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: Table Tab2 contains arrive The precomputed value of .
4. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: Table Tab3 contains arrive The precomputed value of .
5. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: In S6, Q represents a rough estimate of the square root of the input variable Xin.
6. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: (Q2-Q1)*Q5*Q4 represents the correction factor of Q, which is used to correct the errors introduced by truncation and shift rounding in S4 and S5 for further refined calculation.
7. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: The error level of the calculation results described therein reaches 10e-6.
8. The high-precision 64-bit unsigned integer square root method based on a 16-bit lookup table according to claim 1, characterized in that: The method is applicable to a metering module of a high-precision smart meter.