Is a lithium binary floating-point integer cheaper than the inverse?

I know from articles like “ Why you should never throw floats in ints ”, and many others like that casting a float to a signed int is expensive. I also know that some conversion instructions or vector SIMD instructions on some architectures can speed up the process. I am curious that converting an integer to a floating point is also expensive, since all the material I found on this issue only talks about how expensive it is to convert a floating point to integer.

Before anyone says, “Why don't you just check it out?” I'm not talking about performance on a particular architecture, I'm interested in the algorithmic behavior of conversions on multiple platforms, adhering to the IEEE 754-2008 standard. Is there something inherent in the conversion algorithm that affects overall performance?

Intuitively, I would think that converting from an integer to a floating point would be easier in general for the following reasons:

  • Rounding is only necessary if the precision of the integer is greater than the precision of a binary floating-point number, for example. A 32-bit integer with a 32-bit float may require rounding, but there will be no 32-bit integer and 64-bit float, and none of the 32-bit integers will only use 24-bit values.

  • There is no need to check for NAN or +/- INF or +/- 0.

  • There is no danger of overflow or overflow.

What are the reasons why converting from int to float can lead to poor performance between platforms, if any (other than a platform simulating floating point numbers in software)? Converting from int to float is usually cheaper than float to int?

+4
2

Intel " ", CVTSI2SD 3-4 ( 1 ) / Core2. .

, , . , . , , , ( !), .

( ) (CLZ) : 0 INT_MIN ( MSB ). 0 INT_MIN ( ). , , ; ( ). , 1-2 .

CLZ. , . ( CLZ (-x) CLZ (x), x < 0? , 1 .)

(1 , barrel shifter), , 1 (, 3 24- , № 26). - ( shrelter , waaaay , - OoO). , 3 .

. 4 (mantissa LSB, guard, round sticky) OTOH, ( 1). (RZ) // . -∞ (RMI) + ∞ (RPI) . ∞ 1 . , (RNE): x000... x011 → discard; x101... x111 → 1; 0100 → ; 1100 → add 1. , ( , ), 4 .

(, 1111 → 10000), . , ( 1), ( ) ( 2 CLZ , , 4). , 5 .

int float , float to int?

, . binary32 int32 (). , NaN, INF , , INT_MIN (-2147483648). :

: S - ; BE - ; M - (); . " " ( ) , : BE >= 158 ( NaN INF). "" , BE < 127 ( (x) < 1) {RZ, (x > 0 RMI), (x < 0 RPI)}; , BE < 126 ( (x) < 0,5) RNE; , BE = 126, value = 0 ( ) RNE. +1 -1 : BE < 127 : x < 0 RMI; x > 0 RPI; BE = 126 RNE. . 157-BE 2.

, abs (x) >= 1, BE >= 127, BE <= 157 ( abs (x) < 2 ** 31). 157-BE 1, . , , int → float, ( ) 3 . 2 .

(. ). 3 , . . , 31 .

, , x ( = 1). 4 .

, , Intel. , , ( , , ).

Berkeley Softfloat library - . ui32_to_f32.c. , .

+2

. @Netch , . FPU , int- > FP , CPU . FP FWAIT (Intel).

0

Source: https://habr.com/ru/post/1618462/


All Articles