These additions might overflow the signed range for large input values. Converting to unsigned before the addition rather than after avoids such undefined behaviour. The result under normal two's complement wraparound remains unchanged.
Unified · Split
| @@ -30,13 +30,13 @@ · 29 unmodified lines | |||
| 30 | #include "intfloat_readwrite.h" | 30 | #include "intfloat_readwrite.h" |
| 31 | 31 | ||
| 32 | double av_int2dbl(int64_t v){ | 32 | double av_int2dbl(int64_t v){ |
| 33 | if(v+v > 0xFFEULL<<52) | 33 | if((uint64_t)v+v > 0xFFEULL<<52) |
| 34 | return NAN; | 34 | return NAN; |
| 35 | return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075); | 35 | return ldexp(((v&((1LL<<52)-1)) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075); |
| 36 | } | 36 | } |
| 37 | 37 | ||
| 38 | float av_int2flt(int32_t v){ | 38 | float av_int2flt(int32_t v){ |
| 39 | if(v+v > 0xFF000000U) | 39 | if((uint32_t)v+v > 0xFF000000U) |
| 40 | return NAN; | 40 | return NAN; |
| 41 | return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150); | 41 | return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150); |
| 42 | } | 42 | } |