Data Overflow 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 performance 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
In the two platforms, there are two CPU architectures. The ways of implementing the arithmetic logic unit, OS, and compilers ay 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 reserved. 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. |