A decimal rounding method, system, circuit and computer device
By generating reference values and parallel acquisition of rounding values, combined with operation types and rounding modes, fast rounding determination of decimal addition and subtraction operations is achieved, solving the problem of low rounding efficiency of decimal operations in the existing technology, and improving Processor execution efficiency.
Patent Information
- Application Number
- CN202311352787.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-18
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2043-10-18
AI Technical Summary
The existing technology fails to effectively solve the rounding problem of calculation results in decimal addition and subtraction operations. In particular, it lacks a method to quickly implement rounding mode during processor operation, resulting in low calculation efficiency.
A rounding method for decimal operation is proposed. It generates a reference value by obtaining operation parameters and preset rules, selects the rounding value based on the operation type and rounding mode, and compares it with the target value to determine the rounding of the operation result. Using The unified logic judgment circuit realizes the rounding judgment of addition and subtraction operations.
The calculation efficiency of the processor is improved, the mantissa calculation time is reduced, the circuit area is saved, and fast and accurate rounding determination is achieved.
Smart Images

Figure CN117667008B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a decimal rounding method, system, circuit and computer equipment. Background Art
[0002] The IEEE754-2008 standard revision issued in 2008 introduced the decimal 64-digit (16-digit) and 128-digit (34-digit) formats and specifications of the decimal floating point (DFP) unit, which provides a theoretical basis for the use of decimal floating points. Decimal floating point numbers are generally not directly represented by binary numbers, but by BCD codes, which use 4 bits of binary numbers to represent 1 decimal digit, allowing for quick conversion between binary and decimal.
[0003] In actual decimal addition and subtraction calculations, due to the limited bit width of the data, in many cases, the rounding problem of the calculation results will be encountered. Different from the "rounding" in the conventional thinking, the results will be rounded differently according to different rounding modes. Of course, the rounding logic of "rounding" is also reflected in the specific rounding. If the rounded value is accurate, then the result should be the same in all rounding modes; otherwise, different results may appear according to different rounding modes. However, the IEEE standard only provides the standard mode for floating-point rounding, and there is no specific implementation method, that is, there is no method for how to quickly implement these rounding modes when the processor calculates. In fact, not only decimal floating-point operations, but also decimal fixed-point operations have the same problem. Therefore, there is an urgent need for a method to quickly determine the rounding of the digits of the calculation results of decimal operations. Summary of the invention
[0004] In order to solve the above technical problems, the present invention provides a decimal rounding method, system, circuit and computer device, which can solve the problem that the calculation results of decimal addition and subtraction operations cannot be quickly rounded, and realize the fast rounding of decimal operations in a reasonable and effective manner, thereby achieving the technical effect of improving the computing efficiency of the processor.
[0005] In a first aspect, the present invention provides a decimal rounding method, the method comprising:
[0006] Obtaining operation parameters corresponding to a decimal operation, the operation parameters including an operand, an operation type, an operation result, and a rounding mode, the operand including a first operand and a second operand, the operation type including an addition operation and a subtraction operation, and the rounding mode being a standard rounding mode for decimal numbers;
[0007] Generate a reference value according to the operation parameters and preset rules, select a corresponding rounding value according to the operation type and the rounding mode, and add the reference value to the rounding value to obtain a target value;
[0008] The target value is compared with a target threshold value, and the rounding of the operation result is determined according to the comparison result.
[0009] Furthermore, the step of generating a reference value according to the operation parameters and preset rules includes:
[0010] When the operation type is an addition operation and the rounding mode is a first rounding mode, determining whether the operation result generates a carry, and if no carry is generated, generating a reference value according to the protection number and the sticking number of the second operand; the first rounding mode includes a nearest rounding to an even value mode, a nearest rounding to a farther value mode, a nearest rounding to a closer value mode, and a farther value mode;
[0011] If a carry occurs, generating a reference value according to the sum of the units digits of the first operand and the second operand, and the guard number and the sticky number of the second operand;
[0012] When the operation type is a subtraction operation, determining whether the highest bits of the first operand and the second operand are equal, and if the highest bits are equal, generating a reference value according to the protection number and the sticking number of the second operand;
[0013] If the highest bits are not equal, a reference value is generated according to the unit digit of the second operand, the protection number and the sticky number.
[0014] Further, the step of generating a reference value according to the protection number and the sticking number of the second operand includes:
[0015] When the operation type is addition, the rounding mode is the first rounding mode, and the operation result does not generate a carry, the guard number of the second operand is represented by a four-bit binary number, and the sticky number of the second operand is represented by a one-bit binary number, wherein when the sticky number is zero, the one-bit binary number is zero, and when the sticky number is not zero, the one-bit binary number is 1; the four-bit binary number and the one-bit binary number are sequentially combined, and converted into a decimal number, and the decimal number is used as a reference value;
[0016] When the operation type is a subtraction operation, and the highest bits of the first operand and the second operand are equal, the protection number of the second operand is represented by a four-bit binary number, and the sticky number of the second operand is represented by a one-bit binary number, wherein the one-bit binary number is zero; the four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0017] Further, the step of generating a reference value according to the sum of the units digits of the first operand and the second operand, and the protection number and the sticking number of the second operand comprises:
[0018] Representing the last digit of the sum of the units digits of the first operand and the second operand as a four-bit binary number, and representing the guard number and the sticking number of the second operand as a one-bit binary number, wherein when the guard number and the sticking number are both zero, the one-bit binary number is zero, and when either the guard number or the sticking number is not zero, the one-bit binary number is 1;
[0019] The four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0020] Further, the step of generating a reference value according to the units digit, the protection number and the sticking number of the second operand includes:
[0021] Representing the unit digit of the second operand with a four-bit binary number, and representing the guard number and the sticking number of the second operand with a single-bit binary number, wherein when the guard number and the sticking number are both zero, the single-bit binary number is zero, and when either the guard number or the sticking number is not zero, the single-bit binary number is 1;
[0022] The four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0023] Furthermore, the step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result includes:
[0024] When the operation type is addition operation and the rounding mode is the first rounding mode, it is determined whether the target value is greater than a first target threshold value. If the target value is greater than the first target threshold value, the unit digit of the operation result is increased by 1. The first rounding mode includes the nearest rounding to even value mode, the nearest rounding to farther value mode, the nearest rounding to nearer value mode and the farther value mode.
[0025] When the operation type is a subtraction operation, it is determined whether the target value is less than a second target threshold value. If it is less than the second target threshold value, the unit digit of the operation result is increased by 1.
[0026] Furthermore, the step of adding the reference value to the rounded value to obtain the target value comprises:
[0027] When the operation type is a subtraction operation, the five-bit binary number corresponding to the reference value is inverted bit by bit and converted into a decimal number, and the reference value is updated according to the decimal number;
[0028] The updated reference value is added to the rounded value to obtain a target value.
[0029] Furthermore, the step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result includes:
[0030] Determine whether the target value is greater than the target threshold value. If it is greater than the target threshold value, add 1 to the unit digit of the calculation result.
[0031] Furthermore, the step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result further includes:
[0032] When the operation type is an addition operation, the rounding mode is a round-to-nearest-even mode, and the target value is equal to the target threshold, the rounding of the operation result is determined according to the unit digit of the operation result;
[0033] When the operation type is an addition operation and the rounding mode is a second rounding mode, the rounding of the operation result is determined according to the rounding mode, and the second rounding mode includes a take a closer value mode and a take a closer value shorter precision mode.
[0034] In a second aspect, the present invention provides a decimal rounding system, the system comprising:
[0035] A data acquisition module, used to acquire operation parameters corresponding to a decimal operation, wherein the operation parameters include an operand, an operation type, an operation result, and a rounding mode, wherein the operand includes a first operand and a second operand, the operation type includes an addition operation and a subtraction operation, and the rounding mode is a standard rounding mode for decimal numbers;
[0036] a numerical value generation module, configured to generate a reference value according to the operation parameters and preset rules, select a corresponding rounding value according to the operation type and the rounding mode, and add the reference value to the rounding value to obtain a target value;
[0037] The rounding judgment module is used to compare the target value with the target threshold value and determine the rounding of the operation result according to the comparison result.
[0038] In a third aspect, the present invention provides a decimal operation circuit, the operation circuit comprising:
[0039] An adding module, and a comparing module connected to the adding module;
[0040] The adding module is used for receiving a reference value and a rounding value, adding the reference value and the rounding value to obtain a target value, and sending the target value to the comparing module;
[0041] The comparison module is used to receive the target value and the target threshold, and compare the target value with the target threshold to obtain a comparison result.
[0042] Furthermore, the adding module includes an adder, and the comparing module includes a comparator.
[0043] In a fourth aspect, an embodiment of the present invention further provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the computer program.
[0044] The present invention provides a decimal operation rounding method, system, circuit and computer device. Through the rounding method provided by the present invention, the calculation result of the decimal operation can be rounded quickly, the time of the mantissa calculation in the whole arithmetic operation is reduced, thereby improving the execution efficiency of the processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Figure 1 1 is a flow chart of a method for rounding decimal operations in an embodiment of the present invention;
[0046] Figure 2 Schematic diagram of rounding of decimal floating point addition operation in an embodiment of the present invention;
[0047] Figure 3 Schematic diagram of rounding of decimal floating-point subtraction operation in an embodiment of the present invention;
[0048] Figure 4 yes Figure 1 Schematic diagram of the process of step S20;
[0049] Figure 5 1 is a schematic diagram of the structure of a rounding determination operation circuit for addition operation in an embodiment of the present invention;
[0050] Figure 6 1 is a schematic diagram of the structure of a rounding determination operation circuit for a subtraction operation in an embodiment of the present invention;
[0051] Figure 7 is a schematic diagram of the structure of a rounding system for decimal operations in an embodiment of the present invention;
[0052] Figure 8 is another structural schematic diagram of a rounding system for decimal operations in an embodiment of the present invention;
[0053] Fig. 9 1 is a third structural schematic diagram of a decimal operation rounding system in an embodiment of the present invention;
[0054] Fig.10 1 is a schematic diagram of the structure of a decimal operation circuit in an embodiment of the present invention;
[0055] Fig.11 is another structural schematic diagram of the operation circuit in the embodiment of the present invention;
[0056] Fig.12 It is a diagram of the internal structure of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION
[0057] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution 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, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0058] Since the mantissa of a floating-point number will produce more bit widths than the standard definition during the calculation process, the intermediate calculation results of the mantissa must be rounded. The rounding process is generally to add a rounding carry to the lowest L bits of the effective mantissa according to the rules defined by the standard. For computers, numbers are represented in binary, so it is necessary to determine whether a rounding carry 1 needs to be added at the L bit. The conventional processing method is to use an integer adder to complete the rounding process, but this will lead to an increase in the settlement process, which is reflected in the circuit, that is, the delay of the circuit is increased and the area of the circuit is increased. In fact, not only floating-point numbers have the above problem, but fixed-point numbers also have this problem. Therefore, in order to solve the above problem, the present invention provides a method for rounding the calculation results of decimal operations that can be quickly realized.
[0059] Before describing the technical solution of the present invention, the technical keywords involved in the technical solution are first described:
[0060] POWER: Performance Optimization With Enhanced RISC, one of the most common CPU architectures;
[0061] Standard rounding modes for decimal numbers include RNE, RNA, RNZ, RAZ, RTZ, and RPS. Decimal numbers include decimal floating-point numbers and decimal fixed-point numbers.
[0062] RNE: Round to Nearest, Ties to Even, the nearest rounding to even value mode, defined as taking the nearest value. If two values are equally close, the even value is taken.
[0063] RNA: Round to Nearest, Ties away from, round to nearest, ties away from, defined as taking the nearest value. If two values are equally close, the value farthest from zero is taken.
[0064] RNZ: Round to Nearest, Ties toward, round to nearest value mode, defined as taking the nearest value. If two values are equally close, the value closest to zero is taken.
[0065] RAZ: Round away from 0, taking the farther value mode, defined as taking the value farthest from zero;
[0066] RTZ: Round toward 0, take the closer value mode, defined as taking the value closest to zero;
[0067] RPS: Round to prepare for shorter precision, which is defined as taking the nearest value. If the selected value is not precise and the unit digit is 0 or 5, add 1 to the output.
[0068] For a quick implementation of the above standard decimal rounding mode, see Figure 1 A decimal rounding method proposed in the first embodiment of the present invention includes steps S10 to S30:
[0069] Step S10, obtaining operation parameters corresponding to decimal operation, the operation parameters including operands, operation types, operation results and rounding modes, the operands including first operands and second operands, the operation types including addition and subtraction, and the rounding mode being a standard decimal number rounding mode.
[0070] Step S20, generating a reference value according to the operation parameters and preset rules, selecting a corresponding rounding value according to the operation type and the rounding mode, and adding the reference value to the rounding value to obtain a target value.
[0071] Step S30, comparing the target value with the target threshold, and determining the rounding of the operation result according to the comparison result.
[0072] A decimal floating point number includes a sign bit, an exponent bit, and a mantissa bit. Since the exponents of different operands may be different, the operands need to be shifted. Here, it is set to shift operand A to the left. When the shift of operand A is not enough to offset the difference in the exponent, operand B is shifted to the right. The calculation process of addition and subtraction of decimal numbers and the specific representation method of decimal floating point numbers can be calculated according to conventional methods, which will not be introduced in detail here. It should be noted that since there is no difference between decimal floating point numbers and decimal fixed point numbers in rounding implementation, the difference between fixed point numbers and floating point numbers is that fixed point numbers only have sign numbers and mantissas, and do not require shifting during the calculation process. Therefore, the embodiment provided by the present invention is illustrated by taking a more complex decimal floating point number as an example. The rounding of fixed point numbers is consistent with the rounding implementation of floating point numbers, and will not be repeated later.
[0073] The rounding mode in the present invention is based on the POWER architecture, and the operands used in the calculation include operand A and operand B, wherein the L (Least significant digit) of the operand is the unit digit, the G (Guard digit) is the first digit to the right of the unit digit, and the S (Sticky digit) is the second digit to the right of the unit digit. Because operand A is always shifted left and operand B is always shifted right, the Guard digit of operand A is 0 (0000 in binary).
[0074] In actual calculations, the calculation results of decimal floating-point addition are divided into two cases. Figure 2 One is that the highest digit of the calculation result does not generate a carry, such as 222+111=333, and the result still maintains the width of the original data. The other is that the highest digit of the calculation result generates a carry, such as 222+911=1133. In this case, the data width of the result increases by one digit, that is, four bits. The result can be expressed as 113×10 1 .
[0075] Similarly, decimal floating-point subtraction is based on operand A being greater than or equal to operand B, that is, the subtraction is always sufficient, and the result is a number greater than or equal to 0. In this case, there are also two cases, see Figure 3, one is that the first digits of operand A and operand B are the same, that is, the highest bits are equal, in this case the first digit of the calculation result is 0 (0000 in binary). The other is that the first digit of operand A is greater than the first digit of operand B, that is, the highest bits are not equal, in this case the first digit of the calculation result is a number greater than 0.
[0076] The existing rounding judgment method is to first determine the rounding mode, and then judge whether rounding is required according to different input modes. However, the existing method has low execution efficiency for rounding judgment. Therefore, the present invention provides a new rounding judgment method, that is, to judge whether rounding is required by a preset rounding formula. The specific steps are: first, according to different situations of the operation type, a reference value is obtained through different generation rules, and then the corresponding rounding value is obtained according to different rounding modes. Finally, the sum of the reference value and the rounding value is compared with a preset target threshold, and whether rounding is required is judged according to the comparison result.
[0077] See also Figure 4 Based on the above steps, the reference value generation rules in different situations provided by the present invention are first described in detail:
[0078] Step S201, when the operation type is addition and the rounding mode is the first rounding mode, determine whether the operation result generates a carry, and if no carry is generated, generate a reference value according to the protection number and the sticky number of the second operand; the first rounding mode includes the nearest rounding to even value mode, the nearest rounding to farther value mode, the nearest rounding to closer value mode and the farther value mode.
[0079] In this embodiment, for the addition operation, the rounding mode is the above mode, and the operation result does not generate a carry, a specific parameter value generation method is given, that is, the protection number and the sticky number of the second operand, namely the operand B, are used to generate the corresponding parameter value, and for the other two rounding modes, namely the closer value mode RTZ and the closer value shorter precision mode RPS, it is not necessary to use the parameter value method to perform rounding judgment. This is because the RTZ mode does not round under any circumstances, and the RPS mode can judge the units digit of the result before the final result is output. If the units digit is 0 or 5 and the final result is inaccurate, then 1 is added to the result and output, otherwise it is not rounded. Therefore, only the four rounding modes in the above-mentioned first rounding mode need to be considered.
[0080] Since the operation result does not generate a carry and the guard number of operand A is 0, in this case, the rounding of the operation result is actually determined based on the guard number and sticky number of operand B and the selected rounding mode. The specific steps for generating the rounding value based on the guard number and sticky number of operand B are as follows:
[0081] Step S2011, representing the protection number of the second operand with a four-bit binary number, and representing the sticking number of the second operand with a one-bit binary number, wherein when the sticking number is zero, the one-bit binary number is zero, and when the sticking number is not zero, the one-bit binary number is 1;
[0082] Step S2012: sequentially combine the four-bit binary number and the one-bit binary number, convert them into a decimal number, and use the decimal number as a reference value.
[0083] In the present invention, the reference value is represented by a five-bit binary number, that is, a 5-bit number. In the case of no carry in addition, the first 4 bits depend on the Guard number of operand B, and the 5th bit depends on the Sticky number of operand B. For the Guard number, we need to know its value and then convert its value into a 4-bit binary number. For the Sticky number, we do not need to know its specific value, but only need to know whether it is 0: when the Sticky number is 0, the last bit of the above 5-bit number is 0; when the Sticky number is not 0, the last bit of the above 5-bit number is 1, so that the two Sticky numbers can be distinguished. Then the generated 4-bit number and 1-bit number are sequentially combined to obtain the rounding value of the five-bit binary number. After converting it into a decimal number, the corresponding reference value in the case of no carry in addition is obtained. This reference value generation rule can simply and quickly obtain the reference value under the condition of no carry in addition without increasing the calculation complexity, so as to facilitate the subsequent determination of the rounding of the calculation result.
[0084] Step S202: If a carry occurs, a reference value is generated according to the sum of the units digits of the first operand and the second operand, and the protection number and the sticky number of the second operand.
[0085] When the addition operation generates a carry, due to the different operands, the generation rules in this case are different from the generation rules in the above case where no carry is generated. The specific generation rules include:
[0086] Step S2021, representing the last digit of the sum of the units digits of the first operand and the second operand as a four-bit binary number, and representing the guard number and the sticking number of the second operand as a one-bit binary number, wherein when the guard number and the sticking number are both zero, the one-bit binary number is zero, and when either the guard number or the sticking number is not zero, the one-bit binary number is 1;
[0087] Step S2022: sequentially combine the four-bit binary number and the one-bit binary number, convert them into a decimal number, and use the decimal number as a reference value.
[0088] In this embodiment, since the calculation result generates a carry, whether the unit digit of the calculation result is increased by 1 needs to be judged based on the sum of the unit digits of operand A and operand B, the Guard number and Sticky number of operand B, and the rounding mode of addition. Similar to the above steps, since the rounding value is a 5-bit number, the sum of the unit digits of operand A and operand B can be used as the first 4 bits, and the last bit is determined by the Guard number and Sticky number of operand B.
[0089] Furthermore, in the actual execution process, the sum of the units' digits of operands A and B should be a number between 0 and 18. When the number is between 0 and 9, it can be directly converted into 4-bit binary. When it is greater than or equal to 10, a carry has actually occurred. At this time, whether to round off only needs to be determined based on the last digit of the sum, that is, the last digit of the sum can be converted into a 4-bit number. For Guard numbers and Sticky numbers, since they correspond to 1-bit numbers, we do not need to know their specific values, but only need to know whether they are both 0, that is, when the Guard number and the Sticky number are both 0, the last digit of the above 5-bit number is 0; when either the Guard number or the Sticky number is not 0, the last digit of the above 5-bit number is 1.
[0090] Then, the 4-bit number and the 1-bit number are sequentially combined to obtain a 5-bit binary number, and after converting it into a decimal number, the corresponding reference value when the addition generates a carry can be obtained. In other words, the reference value when the addition generates a carry can be quickly obtained according to the sum of the units digits of operands A and B, the guard number and the sticky number of operand B. The reference value generation rule provided in this embodiment is not only efficient and convenient, but also can provide accurate information for subsequent rounding judgments.
[0091] Step S203, when the operation type is a subtraction operation, determine whether the highest bits of the first operand and the second operand are equal, and if the highest bits are equal, generate a reference value according to the protection number and the sticking number of the second operand.
[0092] In the two cases of subtraction operation, the first case is that the highest bits of the two operands are equal. At this time, the defined ones digit is the tenths digit of the original operands A and B. The reference value generation rule corresponding to this case is to generate the reference value through the Guard number and Sticky number of operand B. That is to say, the reference value generation rule of this condition is actually the same as the reference value generation rule under the condition of no carry in addition, that is, the first 4 bits depend on the Guard number of operand B, and the 5th bit depends on the Sticky number of operand B. For the Guard number, we need to know its value and then convert its value into a 4-bit binary number. For the Sticky number, we do not need to know its specific value, but only need to know whether it is 0. Specifically, when the Sticky number is 0, the last bit of the above 5-bit number is 0; when the Sticky number is not 0, the last bit of the above 5-bit number is 1. In this way, the two Sticky numbers can be distinguished. Furthermore, in order to unify the rounding process, in the subtraction operation, since only the last two bits are retained, the L bit and the G bit are actually retained. Therefore, we can set the sticky number of operand B to 0 (0000 in binary). At this time, the last 1 bit is actually zero, that is, a binary number is zero. Therefore, we only need to determine the first 4 bits according to the sticky number of operand B, thereby maintaining the accuracy of the data while further reducing the number of calculation steps.
[0093] Finally, the generated 4-bit number and 1-bit number are sequentially combined to obtain a 5-bit binary number, which is converted into a decimal number to obtain the corresponding reference value under the condition that the highest bit of the subtraction is equal. This embodiment provides a convenient and efficient rounding value generation rule, which can provide accurate information for subsequent rounding judgments.
[0094] Step S204: if the highest bits are not equal, a reference value is generated according to the units digit, the protection number and the sticking number of the second operand.
[0095] For the second case of subtraction operation, that is, when the highest bits of the two operands are not equal, the reference value generation rule is different from the above generation rule due to the different operand conditions. The specific steps are as follows:
[0096] Step S2041, representing the unit digit of the second operand with a four-bit binary number, and representing the guard number and the sticking number of the second operand with a single-bit binary number, wherein when the guard number and the sticking number are both zero, the single-bit binary number is zero, and when either the guard number or the sticking number is not zero, the single-bit binary number is 1;
[0097] Step S2042: sequentially combine the four-bit binary number and the one-bit binary number, convert them into a decimal number, and use the decimal number as a reference value.
[0098] In this embodiment, the units digit defined in this case is also the tenths digit of the original operands A and B. When the highest digits of the subtraction are not equal, the corresponding reference value generation rule is to generate the reference value based on the units digit, guard number and sticky number of the subtrahend. At this time, the first 4 bits of the reference value correspond to the units digit of the operand B, that is, it is converted into a 4-bit binary number according to its value, and the last 1 bit depends on the Guard number and Sticky number of the operand B. The generation method of the last 1 bit is the same as the steps of generating carry in the above addition, that is, when the Guard number and Sticky number are both 0, the last 1 bit is 0, otherwise it is 1. Further, since the Sticky number of the operand B is uniformly set to 0 (binary representation is 0000), in fact, the value of the last 1 bit can be determined based on the Guard number of the operand B.
[0099] Finally, the 4-bit number and the 1-bit number are sequentially combined to obtain a 5-bit binary number, and after converting it into a decimal number, the corresponding reference value can be obtained when the highest bit of subtraction is not equal. The reference value generation rule provided in this embodiment can conveniently and efficiently obtain the reference value when the highest bit of subtraction is not equal, provide accurate information for subsequent rounding judgment, and thus improve the execution efficiency of the processor.
[0100] After obtaining the reference values under the four conditions through the above steps, the rounding value corresponding to the rounding mode can be obtained, so as to judge whether the calculation result needs to be carried according to the sum of the reference value and the rounding value. The specific steps are as follows:
[0101] Step S301a, when the operation type is addition operation and the rounding mode is the first rounding mode, determine whether the target value is greater than a first target threshold value, if greater than the first target threshold value, add 1 to the unit digit of the operation result, and the first rounding mode includes the nearest rounding to even value mode, the nearest rounding to farther value mode, the nearest rounding to nearer value mode and the farther value mode;
[0102] Step S302a, when the operation type is a subtraction operation, determine whether the target value is less than a second target threshold value, and if so, add 1 to the unit digit of the operation result.
[0103] Specifically, in this embodiment, different rounding values are set for different rounding modes under different operation types. First, for the addition operation, when the highest bit of the calculated result does not generate a carry, according to the above reference value generation rule, the reference value shown in the following Table 1 can be obtained:
[0104]
[0105] Table 1 Reference value table when addition does not generate carry
[0106] Taking the rounding mode RNZ as an example, according to its definition, it means "take the closest value. If two values are equally close, take the value closest to zero." According to its definition and the above table, we can see that: (1) the sticky number is 0 and the guard number is greater than or equal to 6; (2) the sticky number is not 0 and the guard number is greater than or equal to 5.
[0107] Both cases will generate a carry. The special case is that when the Sticky number is 0 and the Guard number is 5, it is a tie, that is, the sum of the numbers after the decimal point is 0.5, and the value closest to zero will be selected, that is, no carry will be generated. Therefore, in this rounding mode, it only needs to meet the reference value greater than 10, which covers all the cases where a carry is obtained.
[0108] Similarly, for the RAZ rounding mode, since it is defined as "taking the value farthest from zero", as long as the reference value is greater than 0, all carry situations can be covered. In RNA mode, the reference value needs to be greater than 9, and in RNE mode, the reference value needs to be greater than 10. In this case, all carry situations can be covered.
[0109] For the addition operation and the case where the highest bit of the result of the calculation generates a carry, according to the above reference value generation rule, the reference value shown in the following Table 2 can be obtained:
[0110]
[0111] Table 2 Reference value table when addition generates carry
[0112] In the case where addition generates a carry, taking the rounding mode RNZ as an example, according to its definition, it means "take the closest value. If two values are equally close, take the value closest to zero." According to its definition and the above table, we can see that: (1) The Guard number and the Sticky number are both 0, and the low digit of the sum of the units digits of operands A and B is greater than or equal to 6; (2) The Guard number and the Sticky number are not both 0, and the low digit of the sum of the units digits of operands A and B is greater than or equal to 5. In both cases, the operation result will generate a carry. The special case that may occur when a carry is generated during addition is that when the Guard number and the Sticky number are both 0, and the low digit of the sum of the units of operands A and B is equal to 5, it is a tie, that is, the sum of the numbers after the decimal point is 0.5. In this case, according to the definition of the RNZ rounding mode, the value closest to zero will be selected, that is, no carry will be generated. Therefore, for the RNZ rounding mode, it only needs to satisfy the reference value greater than 10 to cover all situations in which a carry is obtained.
[0113] Similarly, in RAZ rounding mode, the reference value needs to be greater than 0, in RNA rounding mode, the reference value needs to be greater than 9, and in RNE rounding mode, the reference value needs to be greater than 10. At this time, all carry situations can be covered.
[0114] Assuming that the rounding determination formula for the addition operation in this embodiment is that when the reference value + the rounding value > the target threshold, the rounding operation is performed, and in order to save circuit area and improve the determination efficiency, the target thresholds corresponding to different rounding modes can be set to the same value, then the following four situations can be obtained according to the above reference value:
[0115] ①RAZ rounding mode: reference value>0,0=target threshold-rounding value RAZ ;
[0116] ②RNZ rounding mode: reference value>10,10=target threshold-rounding value RNZ ;
[0117] ③RNA rounding mode: reference value > 9, 9 = target threshold - rounding value RNA ;
[0118] ④RNE rounding mode: reference value>10,10=target threshold-rounding value RNE ;
[0119] According to the rounding judgment formula for addition operations, when the target threshold is the same, it is only necessary to set different rounding values for different rounding modes to meet the judgment conditions. Of course, in order to improve the judgment efficiency, both the target value and the rounding value can be set to non-negative numbers, so there is no need to consider the judgment of the sign bit.
[0120] In this embodiment, the rounding determination formulas of the four rounding modes are unified into reference value + rounding value > target threshold value through the above steps, so that the rounding determination of the four rounding modes under addition operation can be realized through one circuit structure. Taking the rounding mode of RAZ as an example, since the determination formula only includes two operations of addition calculation and comparison calculation, the rounding determination circuit under the RAZ mode only needs to be composed of an adder and a comparator, such as Figure 5 As shown in (a), the reference value calculated according to the above steps and the rounding value corresponding to the RAZ mode are input into the adder, and the adder uses the added target value as the input data of the comparator, and the target threshold is used as another input data of the comparator. The comparator compares the target value and the target threshold, and because rounding is performed when the reference value + rounding value > the target threshold, the comparator can be set to be greater than the comparator to achieve rounding judgment under addition operation. Similarly, for the rounding modes of RNZ, RNA and RNE, the operation circuit composed of the adder and the comparator can also be used to make rounding judgments, such as Figure 5 (b), (c), and (d), at this time, only the two input data of the adder need to be replaced with the reference value and the rounding value in the corresponding mode. This embodiment is implemented by unifying the determination of the addition operation in different rounding modes into one operation circuit, which effectively saves the circuit area, and because the circuit structure is simple, the rounding determination can be quickly implemented, thereby improving the determination efficiency.
[0121] From the above four situations, we can know that the target value is non-unique. The same number can be added to both sides of the above inequality at the same time, and the inequality still holds. However, it is certain that the minimum value of the target threshold is determined by the RNZ rounding mode, that is, the minimum value of the target threshold is 10. At this time, it can be guaranteed that the above inequalities are all valid. When the target threshold is determined, the rounding values corresponding to the above different rounding modes can be obtained: rounding value RAZ =10, rounded value RNZ =0, rounded value RNA =1, rounded value RNE =0.
[0122] After setting the rounding values corresponding to different rounding modes, the rounding determination for addition operations can be performed using the following steps:
[0123] According to whether the addition operation generates a carry or not, a corresponding reference value generation rule is selected to generate a corresponding reference value, wherein, if no carry occurs, the reference value is generated according to the Guard number and the Sticky number of operand B, and if a carry occurs, the reference value is generated according to the sum of the units digits of operand A and operand B, and the Guard number and the Sticky number of operand B; the corresponding rounding value is selected according to different rounding modes, and the two are added to obtain a target value; then the target value is compared with a target threshold value, and when it is greater than the target threshold value, that is, greater than 10, it is determined that 1 needs to be added to the units digit of the calculation result at this time.
[0124] Similarly, for subtraction operations, since subtraction and addition are actually opposite operations, and since addition operations determine the addition of 1 to the unit digit of the operation result based on a larger number, for subtraction operations, the reference value can actually be regarded as the minuend. In subtraction operations, a larger number will not add 1 to the operation result, but subtracting a smaller number may produce a carry in the operation result. Based on this theory, the carry judgment in subtraction operations is actually opposite to that in addition operations. The reference values under subtraction operations in Tables 3 and 4 are used as examples for explanation:
[0125]
[0126] Table 3 Reference value table A for the case where the highest bit of subtraction is equal
[0127] When the highest bits of the two operands in a subtraction operation are equal, still taking the RNZ rounding mode as an example, we can see that when the Sticky number is 0 and the Guard number is less than or equal to 4, rounding will occur. When the Sticky number is 0 and the Guard number is equal to 5, a tie occurs. Since RNZ is defined as selecting the value closest to zero, no carry is generated in the case of a tie. That is, in the RNZ rounding mode, it only needs to satisfy that the reference value is less than 9, which covers all cases where a carry is obtained.
[0128] Similarly, in RAZ rounding mode, the reference value needs to be less than 19, in RNA rounding mode, the reference value needs to be less than 11, in RNE rounding mode, the reference value needs to be less than 10, in REZ rounding mode, the reference value needs to be less than 1, and in RPS input mode, the reference value needs to be less than 1. At this time, all carry situations can be covered. It should be noted that the reason why the reference value of RTZ rounding mode and PRS rounding mode needs to be less than 1 is that the default is to subtract the Guard number from 10. When the Guard number is 0, a carry will occur.
[0129]
[0130] Table 4 Reference value table A for the case where the highest bit of subtraction is not equal
[0131] When the highest bits of the two operands in the subtraction operation are not equal, according to the above reference values, in the RNZ rounding mode, the reference value needs to be less than 10, in the RAZ rounding mode, the reference value needs to be less than 20, in the RNA rounding mode, the reference value needs to be less than 11, in the RNE rounding mode, the reference value needs to be less than 10, in the RTZ rounding mode, the reference value needs to be less than 1, and in the RPS input mode, the reference value needs to be less than 1. At this time, all carry situations can be covered.
[0132] From the above analysis, it can be known that for subtraction operation, the rounding judgment formula and the rounding judgment formula of addition operation have opposite signs, that is, reference value + rounding value < target threshold value, and it can be seen that for the two different cases of subtraction operation, the partial rounding values corresponding to the rounding mode are also different. At this time, according to the principle of the rounding judgment formula of addition operation, the rounding judgment formula of subtraction operation can be set into two different rounding judgment formulas according to the above two cases. However, in order to reduce the circuit area, in this embodiment, the rounding values of the same rounding mode in the two cases of subtraction operation are unified, so that the two rounding judgment formulas can be merged into one. Therefore, it is also necessary to adaptively adjust the judgment condition of the reference value when the highest bit is equal without affecting the judgment result, that is, modify the judgment condition of the reference value when the highest bit of subtraction operation is equal, so as to obtain the following six cases:
[0133] ①RAZ rounding mode: reference value < 20, 20 = target threshold - rounding value RAZ ;
[0134] ②RNZ rounding mode: reference value < 10, 10 = target threshold - rounding value RNZ ;
[0135] ③RNA rounding mode: reference value < 11, 11 = target threshold - rounding value RNA ;
[0136] ④RNE rounding mode: reference value < 10, 10 = target threshold - rounding value RNE ;
[0137] ⑤RTZ rounding mode: reference value < 1, 1 = target threshold - rounding value RTZ ;
[0138] ⑥RPS rounding mode: reference value < 1, 1 = target threshold - rounding value RPS ;
[0139] Similar to the addition operation, the minimum value of the target threshold is determined by the RAZ rounding mode. That is, when the minimum value of the target threshold is 20, all carry situations can be covered. At this time, the rounding values corresponding to different rounding modes can be set to: rounding value RAZ =0, rounded value RNZ =10, rounded value RNA =9, rounded value RNE =10, rounded value RTZ =19, rounded value RPS =19.
[0140] After setting the rounding values corresponding to different rounding modes, the rounding determination for subtraction operations can be performed using the following steps:
[0141] According to whether the highest bits of the two operands of the subtraction operation are equal or not, the corresponding reference value generation rule is selected to generate the corresponding reference value, wherein, in the case of the highest bits being equal, the reference value is generated according to the Guard number and the Sticky number of the operand B, and in the case of the highest bits being unequal, the reference value is generated according to the unit digit, the Guard number and the Sticky number of the operand B; the corresponding rounding value is selected according to different rounding modes, and the two are added to obtain the target value; then the target value is compared with the target threshold value, and when it is less than the target threshold value, that is, less than 20, it is determined that 1 needs to be added to the unit digit of the calculation result at this time.
[0142] In this embodiment, the determination formulas for different rounding modes under subtraction operation are unified into reference value + rounding value < target threshold value through the above steps. Similar to the rounding determination circuit of addition operation, the rounding determination circuit of subtraction operation also only needs to be composed of an adder and a comparator, such as Figure 6 As shown in (a), the reference value calculated under different rounding modes and the corresponding rounding value are used as input data of the adder, and the target threshold and the target value output by the adder are used as input data of the comparator. The target value and the target threshold are compared by the comparator. Since the comparison sign in the subtraction rounding judgment formula is opposite to that of the addition operation, the comparator used in the subtraction circuit is a less than comparator.
[0143] This embodiment sets different reference value generation rules and different logic judgment formulas for addition and subtraction operations, and can implement rounding judgments for addition and subtraction operations in different operation situations using only two logic circuits. Not only is the judgment logic clear and the judgment result accurate, but it also has a high judgment efficiency.
[0144] In addition, in order to further improve the determination efficiency and save the circuit area of the determination circuit, in a preferred embodiment provided by the present invention, by merging the above two logic determination formulas, the rounding determination of both addition and subtraction operations can be implemented by only one determination logic, and the specific steps are as follows:
[0145] Step S205, when the operation type is a subtraction operation, the five-bit binary number corresponding to the reference value is inverted bit by bit and converted into a decimal number, and the reference value is updated according to the decimal number;
[0146] Step S206: Add the updated reference value to the rounded value to obtain a target value.
[0147] It can be seen from the above steps that the signs of the determination formula for subtraction operation and the determination formula for addition operation are opposite. In order to integrate the two circuits and generate the same determination circuit, so as to further save the circuit area, in this embodiment, after the corresponding reference value is generated according to the above reference value generation rule for subtraction operation, it is not directly added to the corresponding rounding value, but a negation operation is first performed, that is, the generated five-bit binary number is bitwise negated, so that the transformation from "<" to ">" in the determination formula can be realized. At this time, the circuit of the subtraction operation is as follows: Figure 6 As shown in (b), when the reference value is inverted and input into the adder, and the target value is obtained by adding the rounded value, rounding will be performed only when the comparison judgment at this time is “>”. Therefore, the comparator adopts the same greater than comparator as the addition operation, realizing the unification of the operation circuit for rounding judgment of addition and subtraction operations, thereby further saving circuit area.
[0148] That is, in this embodiment, the reference value is the inverse value of the above reference value. For the two cases of subtraction operation, the reference values at this time are shown in Table 5 and Table 6 below:
[0149] Number of Guards 5-bit Sticky number equals 0 Decimal number representation 0 11111 31 1 11101 29 2 11011 27 3 11001 25 4 10111 23 5 10101 21 6 10011 19 7 10001 17 8 01111 15 9 01101 13
[0150] Table 5 Reference value table B when the highest bit of subtraction is equal
[0151]
[0152] Table 6 Reference value table B when the highest bit of subtraction is not equal
[0153] According to the reference values in the above two tables and using the judgment formula of reference value + rounding value > target threshold, it can be analyzed that for the inverted reference value, in the RNZ rounding mode, the reference value needs to be greater than 21, in the RAZ rounding mode, the reference value needs to be greater than 11, in the RNA rounding mode, the reference value needs to be greater than 20, in the RNE rounding mode, the reference value needs to be greater than 21, in the RTZ rounding mode, the reference value needs to be greater than 30, and in the RPS input mode, the reference value needs to be greater than 30, which can cover all carry situations.
[0154] Similarly, in the above case, the minimum value of the target threshold is determined by the RTZ rounding mode, that is, the minimum value of the target threshold is 30. At this time, the rounding values corresponding to different rounding modes should be adjusted as follows: rounding value RAZ =19, rounded value RNZ =9, rounded value RNA =10, rounded value RNE =9, rounded value RTZ =0, rounded value RPS =0.
[0155] According to the above steps, the minimum value of the target threshold for addition operation is 10. In order to integrate the two circuits, the target threshold and rounding value of the addition operation need to be adjusted. Since the two judgment formulas for addition and subtraction need to use a common target threshold, and the minimum value of the target threshold for subtraction operation is 30, that is, the target threshold of the judgment formula is determined by the target threshold of the subtraction operation, the target threshold of the addition operation needs to be adjusted to 30. Since the reference value generation rule under the addition operation remains unchanged, the rounding values corresponding to different rounding modes under the addition operation also need to be adjusted accordingly, that is, the original rounding values under the addition operation can also be added with 20, so that the new rounding value is: rounding value RAZ =30, rounded value RNZ =20, rounded value RNA =21, rounded value RNE = 20. That is to say, through the above steps, the logic judgment circuits of the addition and subtraction operations can be integrated, and a unified circuit can be used for rounding judgment, thereby further saving circuit area and improving judgment efficiency.
[0156] Of course, while maintaining the inequality relationship, other methods can be used to adjust the parameters of the rounding determination formula under the addition operation. For example, when the target threshold is increased, both the reference value and the rounding value are increased. Since the target threshold is increased by 20, the increment can be allocated to the reference value and the rounding value respectively. For example, the reference threshold is set to 12 and the rounding threshold is set to 8. At this time, the new reference value is the original reference value plus the reference threshold, and the new rounding value is the original rounding value plus the rounding threshold. In this case, the inequality relationship can also be guaranteed to hold. Take the rounding threshold set to 8 as an example. In this case, the new rounding value of the addition operation is: rounding value RAZ =18, rounded value RNZ =8, rounded value RNA =9, rounded value RNE =8.
[0157] In addition, while the target threshold is increased, you can also choose to increase only the reference value to ensure that the inequality relationship holds. However, one thing to note when using this method is that, taking the RNZ rounding mode as an example, when the rounding value remains unchanged, the reference value needs to be increased by 20 to get a target threshold of 30. When the addition operation does not generate a carry, when the Guard number of operand B is 9 and the Sticky number is 0, the corresponding 5-bit number is 10010, which is 18 in decimal. If 20 is added at this time, it will exceed the maximum value of 31 of 5 bits. That is, when adjusting the formula parameters by increasing the reference value, it is necessary to expand the bit width of the reference value. For example, add a digit in front of the reference value represented by the five-bit binary number. The highest bit is added and set to zero to obtain a six-bit binary number, and a highest bit is added to the front of the five-bit binary number corresponding to the reference threshold and the highest bit is set to zero, the two six-bit binary numbers are added to obtain a new reference value, and the rounding value is also set to a six-bit binary representation according to the same steps, and the six-bit binary number obtained after adding the reference value and the rounding value is converted into a decimal number to obtain the correct target value. It should be noted that the above only takes the target threshold of 30 as an example, and sets the rounding values under different operation types and rounding modes. The various parameters of the above determination formula can be flexibly set and adjusted according to actual conditions based on the rounding principle of the present invention, and no excessive restrictions are made here.
[0158] After the above circuit integration, a unified logic circuit can be used in this embodiment to perform rounding determination on addition and subtraction operations, that is:
[0159] Step S301b, determine whether the target value is greater than the target threshold value, if it is greater than the target threshold value, add 1 to the unit digit of the calculation result.
[0160] The rounding determination method provided by the present invention can accurately and efficiently implement the rounding determination of calculation results under different calculation situations, effectively save circuit area, and further improve the calculation performance of the system by obtaining the reference value and the rounding value in parallel.
[0161] In addition, in the rounding determination process of the above-mentioned addition operation, it can be seen that only four of the six rounding modes are determined, and the tie state of the nearest rounding to even value mode is processed as no carry. In this regard, in another preferred embodiment provided by the present invention, a determination method different from other rounding modes is set for the above-mentioned three modes. It should be noted that the following steps are not only applicable to the embodiment of the unified logic judgment circuit, but also to the embodiment using two or more logic judgment circuits. The steps are as follows:
[0162] Step S302b, when the operation type is an addition operation, the rounding mode is a round-to-nearest-even mode, and the target value is equal to the target threshold, determining the rounding of the operation result according to the unit digit of the operation result;
[0163] Step S303b, when the operation type is addition and the rounding mode is the second rounding mode, the rounding of the operation result is determined according to the rounding mode, and the second rounding mode includes a take closer value mode and a take closer value shorter precision mode.
[0164] In this embodiment, for the nearest rounding to even value mode of the addition operation, when the target value is equal to the target threshold value, that is, a tie occurs, according to the definition of the rounding mode "take the closest value, and if two values are equally close, take the even value", it is necessary to determine according to the parity of the digits of the operation result, that is:
[0165] The unit digit is an even number: 0000, 0010, 0100, 0110, 1000; after the carry is obtained, it is 0001, 0011, 0101, 0111, 1001.
[0166] The unit digit is an odd number: 0001, 0011, 0101, 0111, 1001; after getting the carry, it is 0010, 0100, 0110, 1000, 0000.
[0167] Perform a logical AND operation on the last bit after the carry and "0" to ensure that the unit digit of the calculation result is an even number. That is, for a tie in the RNE mode, if the unit digit is an even number, the original value of the calculation result is kept unchanged; if the unit digit is an odd number, add 1 to the unit digit of the calculation result, where the unit digit refers to the unit digit of the calculation result generated by adding operands A and B.
[0168] For addition operations, when the rounding mode is the take the closer value mode RTZ or the take the closer value shorter precision mode RPS, it is necessary to determine whether to round according to the definition of the rounding mode. According to the definition of the RTZ rounding mode "take the value closest to zero", it can be seen that in this rounding mode, no rounding is performed in any case. For the RPS rounding mode, since it is defined as "take the closer value, if the selected value is not accurate, and the unit digit is 0 or 5, then add 1 to output", combined with the above reference value generation rule, it can be seen that when the reference value is 0, it means that the operation result is accurate, and the operation result can be kept unchanged. In the case of non-zero, it means that the operation result is biased. At this time, it is necessary to determine whether to round according to the deviation. That is, it is necessary to determine whether the unit digit of the operation result is 0 or 5. If so, then add 1 to the unit digit of the operation result and output it, otherwise, it is directly output without carrying, thereby satisfying the definition of the RPS rounding mode. Through the determination method provided in this embodiment, comprehensive, accurate and efficient rounding determination can be achieved in different operation types and different rounding modes.
[0169] The present embodiment provides a decimal operation rounding method, which is different from the prior art rounding method implementation that focuses on binary number operations. The present invention is aimed at decimal addition and subtraction operations, and quickly obtains corresponding reference values according to corresponding reference value generation rules in different situations, and sets rounding values and rounding judgment formulas accordingly. The standard rounding mode of decimal numbers under different operation types is quickly implemented through a unified logic judgment circuit, that is, the execution steps of the standard rounding mode are provided. The rounding method provided by the present invention can quickly and accurately implement the rounding of the operation results, reduce the calculation time of the processor, save circuit area, and thus improve the execution efficiency of the processor. Furthermore, the present invention can not only be applied to processors under the POWER architecture, but also can implement fast rounding judgment of operation results according to the rounding method provided by the present invention for various types of processor architectures, so the present invention also has extremely high adaptability; in addition, the method provided by the present invention can not only be applied to decimal floating-point operations, but also can apply the method provided by the present invention to round the operation results for decimal fixed-point addition and subtraction operations. At the same time, for actual rounding implementation, even if different data bit widths are used, the rounding method provided by the present invention can be used to implement rounding of operation results, so the technical solution of the present invention also has extremely high scalability.
[0170] See also Figure 7 Based on the same inventive concept, a decimal operation rounding system proposed in the second embodiment of the present invention includes:
[0171] The data acquisition module 10 is used to acquire operation parameters corresponding to the decimal operation, wherein the operation parameters include an operand, an operation type, an operation result, and a rounding mode, wherein the operand includes a first operand and a second operand, the operation type includes an addition operation and a subtraction operation, and the rounding mode is a standard rounding mode for decimal numbers;
[0172] A value generation module 20, for generating a reference value according to the operation parameters and preset rules, selecting a corresponding rounding value according to the operation type and the rounding mode, and adding the reference value to the rounding value to obtain a target value;
[0173] The rounding determination module 30 is used to compare the target value with the target threshold value, and determine the rounding of the operation result according to the comparison result.
[0174] The rounding system provided by the present invention can quickly round off the calculation result, reducing the time of the mantissa calculation in the entire arithmetic operation, thereby improving the execution efficiency of the processor.
[0175] See also Figure 8 In a preferred embodiment, the value generation module 20 further includes:
[0176] The first generating module 201 is used for, when the operation type is an addition operation and the rounding mode is a first rounding mode, determining whether the operation result generates a carry, and if no carry is generated, generating a reference value according to the protection number and the sticking number of the second operand; the first rounding mode includes a nearest rounding to an even value mode, a nearest rounding to a farther value mode, a nearest rounding to a closer value mode, and a farther value mode;
[0177] If a carry occurs, generating a reference value according to the sum of the units digits of the first operand and the second operand, and the guard number and the sticky number of the second operand;
[0178] A second generating module 202, configured to, when the operation type is a subtraction operation, determine whether the highest bits of the first operand and the second operand are equal, and if the highest bits are equal, generate a reference value according to a protection number and a sticking number of the second operand;
[0179] If the highest bits are not equal, a reference value is generated according to the unit digit of the second operand, the protection number and the sticky number.
[0180] In this embodiment, the calculation types of decimal addition and decimal subtraction are classified respectively, and different rounding schemes are given. By providing reference value generation rules for operations of different classifications, rounding judgment can be quickly implemented in each classification, and the accuracy of the judgment result is improved.
[0181] Furthermore, in another preferred embodiment, the first generation module 201 is also used to represent the protection number of the second operand as a four-bit binary number, and represent the adhesion number of the second operand as a single-bit binary number, wherein when the adhesion number is zero, the single-bit binary number is zero, and when the adhesion number is not zero, the single-bit binary number is 1; the four-bit binary number and the single-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0182] Furthermore, in another preferred embodiment, the first generation module 201 is also used to represent the last digit of the sum of the units digits of the first operand and the second operand as a four-bit binary number, and represent the protection number and the adhesion number of the second operand as a single-bit binary number, wherein when the protection number and the adhesion number are both zero, the single-bit binary number is zero, and when either the protection number or the adhesion number is not zero, the single-bit binary number is 1; the four-bit binary number and the single-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0183] Furthermore, in another preferred embodiment, the second generation module 202 is also used to represent the protection number of the second operand as a four-bit binary number, and represent the adhesion number of the second operand as a one-bit binary number, wherein the one-bit binary number is zero; sequentially combine the four-bit binary number and the one-bit binary number, and convert them into a decimal number, and use the decimal number as a reference value.
[0184] Furthermore, in another preferred embodiment, the second generation module 202 is also used to represent the unit digit of the second operand with a four-bit binary number, and represent the protection number and the sticking number of the second operand with a single-bit binary number, wherein when the protection number and the sticking number are both zero, the single-bit binary number is zero, and when either the protection number or the sticking number is not zero, the single-bit binary number is 1; the four-bit binary number and the single-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
[0185] In the above embodiments, specific generation rules for reference values in various operation classification situations are respectively given, which can quickly generate corresponding reference values without increasing the computational complexity, thereby providing accurate data support for rounding determination of subsequent operation results.
[0186] Further, in another preferred embodiment, the rounding determination module 30 further includes:
[0187] The first judgment module 301a is used for judging whether the target value is greater than a first target threshold value when the operation type is an addition operation and the rounding mode is a first rounding mode, and if it is greater than the first target threshold value, adding 1 to the unit digit of the operation result, wherein the first rounding mode includes a nearest rounding to an even value mode, a nearest rounding to a farther value mode, a nearest rounding to a closer value mode, and a farther value mode;
[0188] The second judgment module 302a is used to judge whether the target value is less than a second target threshold when the operation type is a subtraction operation, and if so, add 1 to the unit digit of the operation result.
[0189] This embodiment sets different reference value generation rules and different logic judgment formulas for addition and subtraction operations, and can implement rounding judgments for addition and subtraction operations in different operation situations using only two logic judgment circuits. Not only is the judgment logic clear and the judgment result accurate, but it also has a high judgment efficiency.
[0190] See also Fig. 9 In another embodiment, the value generation module 20 further includes:
[0191] The third generation module 203 is used to, when the operation type is a subtraction operation, invert the five-bit binary number corresponding to the reference value bit by bit and convert it into a decimal number, update the reference value according to the decimal number; add the updated reference value and the rounding value to obtain the target value.
[0192] In this embodiment, by inverting the reference value of the subtraction operation, the logic judgment circuit of the subtraction operation can be converted, so that the logic judgment circuit of the addition and subtraction operations can be kept consistent, further saving circuit area and improving judgment efficiency.
[0193] In another preferred embodiment, the rounding determination module 30 further includes:
[0194] The first judgment module 301b is used to judge whether the target value is greater than the target threshold value. If it is greater than the target threshold value, the unit digit of the calculation result is increased by 1.
[0195] The rounding determination method provided in this embodiment can accurately and efficiently implement the rounding determination of calculation results under different calculation situations, effectively save circuit area, and further improve the calculation performance of the system by obtaining the reference value and the rounding value in parallel.
[0196] In another preferred embodiment, the rounding determination module 30 further includes:
[0197] A second judgment module 302b is used to determine the rounding of the operation result according to the unit digit of the operation result when the operation type is an addition operation, the rounding mode is a round-to-nearest-even mode, and the target value is equal to the target threshold;
[0198] The third judgment module 303b is used to determine the rounding of the operation result according to the rounding mode when the operation type is addition operation and the rounding mode is the second rounding mode, and the second rounding mode includes a closer value mode and a closer value shorter precision mode.
[0199] Through the determination method provided in this embodiment, comprehensive, accurate and efficient rounding determination can be achieved under different operation types and different rounding modes.
[0200] In the above embodiment, rounding determination steps are provided for various combinations. Through a unified logic judgment circuit, the rounding conditions of the operation results of combinations of different rounding modes and different operation types can be quickly determined. Through the above embodiment, the implementation methods of various standard rounding modes can be quickly implemented, reducing the time for mantissa calculation in the entire arithmetic operation, thereby improving the execution efficiency of the processor.
[0201] The technical features and technical effects of the decimal operation rounding system proposed in the embodiment of the present invention are the same as those of the method proposed in the embodiment of the present invention, and are not described in detail here. Each module in the above-mentioned decimal operation rounding system can be implemented in whole or in part by software, hardware, and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0202] See also Fig.10 Based on the same inventive concept, a decimal operation circuit proposed in the third embodiment of the present invention includes:
[0203] An adding module 1, and a comparing module 2 connected to the adding module 1;
[0204] The adding module 1 is used for receiving a reference value and a rounding value, adding the reference value and the rounding value to obtain a target value, and sending the target value to the comparing module;
[0205] The comparison module 2 is used to receive the target value and the target threshold, and compare the target value with the target threshold to obtain a comparison result.
[0206] According to the above rounding method, for the four rounding modes of addition operation, the present invention only needs to add the reference value and the rounding value of different rounding modes to obtain the target value, and judge whether to carry according to whether the target value is greater than the target threshold. That is, when the rounding formula is reference value + rounding value > target threshold, please refer to Fig.11 (a), the addition module 1 provided in this embodiment receives the reference value and the rounding value, and adds them to obtain the target value. Since only two values need to be added, the addition module 1 can use an adder to complete the addition operation, and the comparison module 2 can use a comparator. Specifically, the comparison between the target value and the target threshold can be completed by using a greater than comparator.
[0207] Similarly, for subtraction operations, when the judgment condition of the rounding formula for subtraction operations is opposite to that of the rounding formula for addition operations, that is, when the rounding formula is reference value + rounding value < target threshold, please refer to Fig.11 (b), at this time, the addition module 1 can still be implemented by an adder, while the comparison module 2 needs to use a less than comparator to compare the target value with the target threshold, that is, in this embodiment, two logic circuits can be used to implement the rounding judgment of addition and subtraction operations in different operation situations.
[0208] Furthermore, in order to save circuit area, the present invention further processes the reference value of the subtraction operation so that the addition operation and the subtraction operation use the same rounding formula, that is, when the rounding formula of the addition and subtraction operations is the reference value + rounding value > target threshold, in this case, the following is used: Fig.11 The circuit structure shown in (a) can realize the rounding judgment of addition and subtraction operations. The rounding method provided by the present invention combined with the operation circuit can not only effectively save the circuit area, but also has a simple circuit structure, clear judgment logic, accurate judgment results, and extremely high judgment efficiency.
[0209] In addition, an embodiment of the present invention further provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the computer program.
[0210] See also Fig.12, an internal structure diagram of a computer device in an embodiment, the computer device may specifically be a terminal or a server. The computer device includes a processor, a memory, a network interface, a display, and an input device connected via a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a rounding method for decimal operations is implemented. The display screen of the computer device may be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device may be a touch layer covered on the display screen, or a key, trackball or touchpad provided on the housing of the computer device, or an external keyboard, touchpad or mouse, etc.
[0211] It can be understood by those skilled in the art that Fig.12 The structure shown in the figure is merely a block diagram of a portion of the structure related to the present application scheme, and does not constitute a limitation on the computer device to which the present application scheme is applied. The specific computing device may include more or fewer components than those shown in the figure, or combine certain components, or have the same component arrangement.
[0212] In summary, the embodiment of the present invention proposes a decimal operation rounding method, system and computer device, the method obtains the operation parameters corresponding to the decimal operation, the operation parameters include operands, operation types, operation results and rounding modes, the operands include first operands and second operands, the operation types include addition and subtraction, and the rounding mode is a decimal number standard rounding mode; according to the operation parameters and preset rules, a reference value is generated, according to the operation type and the rounding mode, a corresponding rounding value is selected, and the reference value is added to the rounding value to obtain a target value; the target value is compared with a target threshold, and the rounding of the operation result is determined according to the comparison result. The present invention quickly implements the decimal number standard rounding mode under different operation types through a unified logic judgment circuit, reduces the calculation time of the processor, saves circuit area, and thus improves the execution efficiency of the processor.
[0213] Each embodiment in this specification is described in a progressive manner, and the same or similar parts of each embodiment can be directly referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. It should be noted that the technical features of the above-mentioned embodiments can be combined arbitrarily. In order to make the description concise, all possible combinations of the technical features in the above-mentioned embodiments are not described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0214] The above-mentioned embodiments only express several preferred implementation modes of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for ordinary technicians in the technical field, several improvements and substitutions can be made without departing from the technical principles of the present invention, and these improvements and substitutions should also be regarded as the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be based on the protection scope of the claims.
Claims
1. A decimal rounding method, characterized in that: The method is applied to decimal operations inside a processor, and for combinations of different rounding modes and different operation types, a unified logic judgment circuit is set inside the processor to perform rounding, the logic judgment circuit includes an addition module and a comparison module connected to the addition module, and the method includes: Obtaining operation parameters corresponding to a decimal operation inside the processor, the operation parameters including an operand, an operation type, an operation result, and a rounding mode, the operand including a first operand and a second operand, the operation type including an addition operation and a subtraction operation, and the rounding mode being a standard decimal number rounding mode; Generate a reference value according to the operation parameters and preset rules, select a corresponding rounding value according to the operation type and the rounding mode, and add the reference value to the rounding value to obtain a target value; Comparing the target value with a target threshold, and determining the rounding of the operation result according to the comparison result; Wherein, the step of generating a reference value according to the operation parameters and preset rules includes: When the operation type is an addition operation and the rounding mode is a first rounding mode, determining whether the operation result generates a carry, and if no carry is generated, generating a reference value according to the protection number and the sticking number of the second operand; the first rounding mode includes a nearest rounding to an even value mode, a nearest rounding to a farther value mode, a nearest rounding to a closer value mode, and a farther value mode; If a carry occurs, generating a reference value according to the sum of the units digits of the first operand and the second operand, and the guard number and the sticky number of the second operand; When the operation type is a subtraction operation, determining whether the highest bits of the first operand and the second operand are equal, and if the highest bits are equal, generating a reference value according to the protection number and the sticking number of the second operand; If the highest bits are not equal, a reference value is generated according to the unit digit, the protection number and the sticking number of the second operand; The step of adding the reference value to the rounded value to obtain the target value comprises: When the operation type is an addition operation, the reference value and the rounding value are input into the addition module for addition to obtain a target value; When the operation type is a subtraction operation, the five-bit binary number corresponding to the reference value is inverted bit by bit and converted into a decimal number, and the reference value is updated according to the decimal number; Inputting the updated reference value and the rounding value into the adding module for addition to obtain a target value; The step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result comprises: The target value and the target threshold are input into the comparison module to determine whether the target value is greater than the target threshold. If the target value is greater than the target threshold, the unit digit of the calculation result is increased by 1.
2. The decimal rounding method according to claim 1, characterized in that: The step of generating a reference value according to the protection number and the sticking number of the second operand comprises: When the operation type is addition, the rounding mode is the first rounding mode, and the operation result does not generate a carry, the guard number of the second operand is represented by a four-bit binary number, and the sticky number of the second operand is represented by a one-bit binary number, wherein when the sticky number is zero, the one-bit binary number is zero, and when the sticky number is not zero, the one-bit binary number is 1; the four-bit binary number and the one-bit binary number are sequentially combined, and converted into a decimal number, and the decimal number is used as a reference value; When the operation type is a subtraction operation, and the highest bits of the first operand and the second operand are equal, the protection number of the second operand is represented by a four-bit binary number, and the sticky number of the second operand is represented by a one-bit binary number, wherein the one-bit binary number is zero; the four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
3. The decimal rounding method according to claim 2, characterized in that: The step of generating a reference value according to the sum of the units digits of the first operand and the second operand, and the protection number and the sticking number of the second operand comprises: Representing the last digit of the sum of the units digits of the first operand and the second operand as a four-bit binary number, and representing the guard number and the sticking number of the second operand as a one-bit binary number, wherein when the guard number and the sticking number are both zero, the one-bit binary number is zero, and when either the guard number or the sticking number is not zero, the one-bit binary number is 1; The four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
4. The decimal rounding method according to claim 3, characterized in that: The step of generating a reference value according to the units digit, the protection number and the sticking number of the second operand comprises: Representing the unit digit of the second operand with a four-bit binary number, and representing the guard number and the sticking number of the second operand with a single-bit binary number, wherein when the guard number and the sticking number are both zero, the single-bit binary number is zero, and when either the guard number or the sticking number is not zero, the single-bit binary number is 1; The four-bit binary number and the one-bit binary number are sequentially combined and converted into a decimal number, and the decimal number is used as a reference value.
5. The decimal rounding method according to claim 1, characterized in that: The step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result comprises: When the operation type is addition operation and the rounding mode is the first rounding mode, it is determined whether the target value is greater than a first target threshold value. If the target value is greater than the first target threshold value, the unit digit of the operation result is increased by 1. The first rounding mode includes the nearest rounding to even value mode, the nearest rounding to farther value mode, the nearest rounding to nearer value mode and the farther value mode. When the operation type is a subtraction operation, it is determined whether the target value is less than a second target threshold value. If it is less than the second target threshold value, the unit digit of the operation result is increased by 1.
6. The decimal rounding method according to claim 1, characterized in that: The step of comparing the target value with the target threshold and determining the rounding of the operation result according to the comparison result comprises: When the operation type is an addition operation, the rounding mode is a round-to-nearest-even mode, and the target value is equal to the target threshold, the rounding of the operation result is determined according to the unit digit of the operation result; When the operation type is an addition operation and the rounding mode is a second rounding mode, the rounding of the operation result is determined according to the rounding mode, and the second rounding mode includes a take a closer value mode and a take a closer value shorter precision mode.
7. A decimal rounding system, the system adopting the rounding method according to any one of claims 1 to 6, characterized in that: include: A data acquisition module, used to acquire operation parameters corresponding to a decimal operation, wherein the operation parameters include an operand, an operation type, an operation result, and a rounding mode, wherein the operand includes a first operand and a second operand, the operation type includes an addition operation and a subtraction operation, and the rounding mode is a standard rounding mode for decimal numbers; a numerical value generation module, configured to generate a reference value according to the operation parameters and preset rules, select a corresponding rounding value according to the operation type and the rounding mode, and add the reference value to the rounding value to obtain a target value; The rounding judgment module is used to compare the target value with the target threshold value and determine the rounding of the operation result according to the comparison result.
8. An operation circuit for decimal operation, the operation circuit is used to execute the rounding method according to any one of claims 1 to 6, characterized in that: include: An adding module, and a comparing module connected to the adding module; The adding module is used for receiving a reference value and a rounding value, adding the reference value and the rounding value to obtain a target value, and sending the target value to the comparing module; The comparison module is used to receive the target value and the target threshold, and compare the target value with the target threshold to obtain a comparison result.
9. The decimal operation circuit according to claim 8, characterized in that: The adding module includes an adder, and the comparing module includes a comparator.
10. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
