View Single Post
  #4  
Old 03-22-2025, 04:42 PM
paulyc
Fire Beetle
 
Join Date: Mar 2025
Posts: 3
Default

Quote:
Originally Posted by paulyc View Post
Next question, EQ19ToFloat. The largest positive value we can store in a 19-bit integer is 0x3FFFF and the largest (magnitude) negative value we can store is 0x40000. But remember, our value is stored into only the lowest 19 bits of the 32-bit variable EQ19Value. Stored as a 32-bit int, 0x40000 is not a negative number at all, to properly store a negative 19-bit value into 32-bits the high bit needs to be sign extended all the way to the left, 0xFFFC0000.

So the first line of this function tests if the value is supposed to be negative, if it is, the second line does a little bit of trickery to sign extend the 19-bits into 32 (same as 0xFFF80000 | EQ19Value), now we have a proper signed native int.

The last line converts that properly sign extended 32-bit integer into a 32-bit float. Yes, (1<<3) is the same thing as 8, I think it is written like this to make it more clear what is going on. Whats going on is the lowest 3 bits of this integer are a fractional part. For example EQ locs are shown with one digit decimal value after the point, doesnt map perfectly to the range of .0 to .9 since 3 bits is 7 values so theres a little rounding error but

000 = .0
001 = .125
010 = .25
011 = .375
100 = .5
101 = .625
110 = .75
111 = .875
By the way, this type of number format, which has some integer bits and some fractional bits, is known as fixed-point. It is not as commonly used as floating-point and not typically implemented in hardware, which is why this function exists, to convert the value into a format that the CPU can natively manipulate.

So to summarize, EQ19ToFloat takes a 19-bit fixed-point number, which has 16 integer bits and 3 fractional bits, and converts it to a 32-bit floating-point number.
Reply With Quote