Data Overflow/Underflow During the Conversion from the Double-Precision Floating-Point Type to the Integer Type
Symptom
When a C or C++ double-precision floating-point value is converted to an integer, the behavior of the Kunpeng platform is different from that of the x86 platform if the integer is out of the integer range.
long aa = (long)0x7FFFFFFFFFFFFFFF; long bb; bb = (long)(aa*(double)10); // long->double->long //x86: aa=9223372036854775807, bb=-9223372036854775808 //arm64:aa=9223372036854775807, bb=9223372036854775807
Cause
The two platforms use two different CPU architectures. The ways of implementing the arithmetic logic unit, OS, and compiler may be different. The instruction for converting a floating-point number to an integer in x86 defines an indefinite integer value (64-bit 0x8000000000000000). In most cases, the x86 platform actually complies with this principle. However, when the double data is converted to an unsigned integer, different results occur. The processing of Kunpeng is very clear and simple. When overflow or underflow occurs, the maximum or minimum value that can be represented by an integer is retained. Developers do not face uncertain or unexpected results.
Procedure
Adjust the code implementation based on the following data conversion table.
Conversion from double data to long data
CPU |
Double Value |
Long Value |
Description |
|---|---|---|---|
x86 |
The positive value is out of the long range. |
0x8000000000000000 |
Indefinite integer value |
x86 |
The negative value is out of the long range. |
0x8000000000000000 |
Indefinite integer value |
Kunpeng |
The positive value is out of the long range. |
0x7FFFFFFFFFFFFFFF |
Kunpeng assigns the maximum positive value to the long variable. |
Kunpeng |
The negative value is out of the long range. |
0x8000000000000000 |
Kunpeng assigns the minimum negative value to the long variable. |
Conversion from double data to unsigned long data
CPU |
Double Value |
Unsigned Long Value |
Description |
|---|---|---|---|
x86 |
The positive value is out of the long range. |
0x0000000000000000 |
x86 assigns the minimum value 0 to the long variable. |
x86 |
The negative value is out of the long range. |
0x8000000000000000 |
Indefinite integer value |
Kunpeng |
The positive value is out of the long range. |
0xFFFFFFFFFFFFFFFF |
Kunpeng assigns the maximum value to the unsigned long variable. |
Kunpeng |
The negative value is out of the long range. |
0x0000000000000000 |
Kunpeng assigns the minimum value to the unsigned long variable. |
Conversion from double data to int data
CPU |
Double Value |
Int Value |
Description |
|---|---|---|---|
x86 |
The positive value is out of the int range. |
0x80000000 |
Indefinite integer value |
x86 |
The negative value is out of the int range. |
0x80000000 |
Indefinite integer value |
Kunpeng |
The positive value is out of the int range. |
0x7FFFFFFF |
Kunpeng assigns the maximum positive value to the int variable. |
Kunpeng |
The negative value is out of the int range. |
0x80000000 |
Kunpeng assigns the minimum negative number to the int variable. |
Conversion from double data to unsigned int data
CPU |
Double Value |
Unsigned Int Value |
Description |
|---|---|---|---|
x86 |
The positive value is out of the unsigned int range. |
The integer part of double modulo 2^32 |
x86 assigns the minimum negative value to the unsigned int variable. |
x86 |
The negative value is out of the unsigned int range. |
The integer part of double modulo 2^32 |
x86 assigns the minimum negative value to the unsigned int variable. |
Kunpeng |
The positive value is out of the unsigned int range. |
0xFFFFFFFF |
Kunpeng assigns the maximum positive number to the unsigned int variable. |
Kunpeng |
The negative value is out of the unsigned int range. |
0x00000000 |
Kunpeng assigns the minimum negative value to the unsigned int variable. |