This function appears to be converting a 64-bit fixed-point number (split into two 32-bit parts: param_1 and param_2) into a 32-bit floating-point number represented as an unsigned integer (uint). This is likely a custom or optimized implementation of a float conversion, possibly for systems without FPU support.
Function Signature:
uint FUN_0801f508_float_conv(uint param_1, int param_2)
param_1: lower 32 bits of the fixed-point number.
param_2: upper 32 bits of the fixed-point number.
- Returns: A 32-bit floating-point value represented as an unsigned integer (
uint), i.e., the raw bits of a float.
Step-by-step Explanation:
Variables:
uVar1: Extracts sign bit from param_2.
iVar2: Biased exponent derived from top bits of param_2.
uVar3: Used for computing mantissa shifts or special values.
1. Extracting Exponent Part:
uVar1 = (uint)(param_2 << 1) >> 0x15;
- Shifts
param_2 left by 1 bit to discard the sign temporarily.
- Then shifts right by 0x15 (21 bits), leaving the next 10 bits — this mimics extracting exponent bits in a float format.
- This seems to extract a biased exponent similar to IEEE 754 float format.
2. Unbias the Exponent:
iVar2 = uVar1 - 0x3fe;
- Subtracts bias
0x3fe (1022 in decimal).
- This suggests it's trying to mimic double-precision exponent bias, but the output is a single-precision float, which uses bias
0x7f (127). So this might be scaling or internal normalization logic.
3. Handle Zero or Denormalized Cases:
if (iVar2 == 0 || uVar1 < 0x3fe) {
return 0;
}
- If the exponent is zero or less than bias, returns zero.
- Likely handling underflow or denormalized numbers.
4. Compute Mantissa Shift Amount:
uVar3 = -iVar2 + 0x20;
- Calculates how many bits to shift the mantissa to normalize it.
5. Extract Sign Bit:
uVar1 = param_2 >> 0x1f;
- Gets the sign bit from
param_2.
6. Normal Case (Exponent in Range):
if (uVar3 != 0 && iVar2 < 0x21) {
return ((param_2 << 0xb | param_1 >> 0x15 | 0x80000000) >> (uVar3 & 0xff)) + uVar1 ^ uVar1;
}
(param_2 << 0xb) shifts upper part to align with mantissa.
(param_1 >> 0x15) brings in lower bits of the fixed point number.
| 0x80000000 adds the implicit leading 1 for normalized floats.
>> (uVar3 & 0xff) shifts down based on calculated shift amount.
+ uVar1 ^ uVar1 adds sign bit back (note: uVar1 ^ uVar1 is always 0; maybe compiler optimization or placeholder).
7. Overflow / Special Case Handling:
uVar3 = -iVar2 + 0x401;
if (uVar3 != 0) {
uVar3 = ~uVar1 ^ 0x80000000;
}
return uVar3;
- If overflow, constructs a NaN or infinity value.
~uVar1 ^ 0x80000000 sets up sign bit and rest as all ones (possibly representing infinity or NaN).
Summary
Purpose:
Converts a 64-bit signed fixed-point number (composed of param_1 and param_2) into a 32-bit float (as raw bits in a uint), emulating IEEE-754 float behavior manually.
Equivalent C Pseudocode:
Here’s a more readable version of what this function does:
uint fixed_to_float(uint param_1, int param_2) {
uint exponent_raw = (uint)((param_2 << 1) >> 0x15); // Extract exponent bits
int exponent = exponent_raw - 0x3fe; // Unbias exponent
if (exponent <= 0 || exponent_raw < 0x3fe) {
return 0; // Zero or denormal
}
uint sign_bit = (uint)(param_2 >> 31); // Extract sign bit
uint mantissa_shift = -exponent + 0x20;
if (mantissa_shift != 0 && exponent < 0x21) {
// Combine high/low parts and shift to normalize
uint mantissa = (param_2 << 11) | (param_1 >> 15) | 0x80000000;
mantissa >>= (mantissa_shift & 0xFF);
return (mantissa ^ sign_bit) + sign_bit; // Apply sign
} else {
// Overflow case
return (~sign_bit ^ 0x80000000); // Possibly inf/nan
}
}
Notes:
- The function assumes a specific fixed-point format embedded in two registers.
- It avoids using actual floating-point operations, likely for compatibility with older hardware or boot code.
- The use of XOR patterns like
^ uVar1 may be intended for sign extension or debugging artifacts.
Let me know if you want a Python version or test cases!