Great question — you're working under tight hardware constraints:
- ❌ No 64-bit integers or floats
- ❌ Very slow branching (so avoid
if/switch chains)
- ✅ You want to simulate a 64-bit floating-point number using two 32-bit integers:
- One for mantissa (significand)
- One for exponent
This is a classic software-emulated floating-point scenario, common in embedded systems or microcontrollers without FPU.
Let’s design a fast, branch-free, integer-based simulation of 64-bit float using two 32-bit integers.
✅ Goal
Simulate a 64-bit IEEE 754 double-precision float using:
uint32_t mantissa; // 32 bits: significand (with implicit leading 1)
uint32_t exponent; // 32 bits: exponent (biased by 1023)
We'll use fixed-point arithmetic with scaling, and implement basic operations without branching where possible.
🚀 Key: Avoid branching. Use bit manipulation and arithmetic only.
🔢 Step 1: Define the Format
We’ll define our emulated 64-bit float as:
| Field | Size | Notes |
|---|
| Sign | 1 bit | Stored separately (or in sign bit of mantissa) |
| Exponent | 11 bits | Biased by 1023 → stored as exp = actual + 1023 |
| Mantissa | 52 bits | But we only have 32 bits → so we scale down |
But we can’t store 52 bits in 32 bits → so we reduce precision.
✅ Compromise: Use 32-bit mantissa + 32-bit exponent → Simulate "Double" with reduced accuracy
Let’s define:
typedef struct {
uint32_t mantissa; // 32 bits: significand (normalized: 1.xxxxxx...)
int32_t exponent; // 32 bits: exponent (biased by 1023)
int sign; // 1 bit: 0 = positive, 1 = negative
} emu_float64;
We'll use implicit leading 1: value = sign * (1 + mantissa / 2^32) * 2^(exponent - 1023)
So:
mantissa represents fractional part (bits after decimal point): 0.mmm...
- Total significand =
1 + mantissa / 2^32
- Range: ±(2^32 - 1)/2^32 × 2^(exponent - 1023)
🔧 Step 2: Basic Representation & Conversion
Convert from real double (for testing):
emu_float64 double_to_emu(double d) {
emu_float64 e = {0, 0, 0};
if (d == 0.0) {
e.mantissa = 0;
e.exponent = 1023; // bias
e.sign = 0;
return e;
}
if (d < 0) {
e.sign = 1;
d = -d;
} else {
e.sign = 0;
}
// Extract exponent and mantissa from IEEE 754 double
uint64_t bits;
memcpy(&bits, &d, 8);
uint64_t exp_bits = (bits >> 52) & 0x7FF;
uint64_t man_bits = bits & 0xFFFFFFFFFFFFF;
// Normalize: 1.mantissa
// Find leading bit (count leading zeros)
int shift = 64 - __builtin_clzll(man_bits | 0x10000000000000);
int exp = exp_bits - 1023 + shift - 52;
// Scale mantissa to 32 bits
uint32_t mant = (man_bits << (shift - 1)) >> 20; // Keep top 32 bits
e.mantissa = mant;
e.exponent = exp + 1023; // store biased exponent
return e;
}
⚠️ This uses __builtin_clzll — assume available. If not, use lookup table or loop.
🔄 Step 3: Reconstruct to Double (for debugging)
double emu_to_double(emu_float64 e) {
if (e.mantissa == 0) return e.sign ? -0.0 : 0.0;
double m = 1.0 + (double)e.mantissa / (1LL << 32);
double exp_val = pow(2.0, e.exponent - 1023);
return (e.sign ? -1.0 : 1.0) * m * exp_val;
}
Note: pow(2, x) may be slow — see optimization below.
🚀 Step 4: Fast Arithmetic (Branch-Free!)
Now comes the hard part: perform math without branching.
We'll focus on addition, multiplication, and normalization.
✅ Addition (Fast, Branch-Free Version)
Add two emu_float64 values:
emu_float64 emu_add(emu_float64 a, emu_float64 b) {
// Handle zero
if (a.mantissa == 0) return b;
if (b.mantissa == 0) return a;
// Determine which has larger exponent
// We need to align exponents → shift smaller one
int diff = a.exponent - b.exponent;
int shift = diff > 0 ? diff : -diff;
// Use branch-free min/max
int sign_diff = (diff >> 31); // 1 if diff < 0, else 0
int max_exp = a.exponent + sign_diff * diff;
int min_exp = b.exponent + (1 - sign_diff) * diff;
uint32_t *src_mant = (diff >= 0) ? &a.mantissa : &b.mantissa;
uint32_t *dst_mant = (diff >= 0) ? &b.mantissa : &a.mantissa;
int src_exp = (diff >= 0) ? a.exponent : b.exponent;
int dst_exp = (diff >= 0) ? b.exponent : a.exponent;
// Shift smaller mantissa right by (src_exp - dst_exp)
int shift_amt = src_exp - dst_exp;
uint32_t shifted = (*src_mant) >> shift_amt;
// Add shifted to larger
uint64_t sum = (1ULL << 32) + *dst_mant + shifted; // +1 for implicit 1
// Now normalize: find how many leading zeros
int leading_zeros = __builtin_clzll(sum);
int new_shift = 32 - leading_zeros; // How much to shift left?
uint32_t new_mant = sum >> new_shift;
int new_exp = dst_exp + new_shift;
// Final result
emu_float64 result = {new_mant, new_exp, a.sign ^ b.sign}; // sign depends on signs
// But wait: if signs differ, subtraction needed!
// ❗ This is where branching would help.
// So let's avoid it: assume same sign for now.
// For full correctness: handle different signs via absolute value comparison
// But that requires branching...
// ✅ Workaround: Only support same-sign addition (or use absolute values)
// OR: Do signed comparison without branches
}
⚠️ Problem: Sign differences require conditional logic.
🛠️ Solution: Use unsigned comparison for magnitude
To avoid branching when comparing magnitudes:
// Compare |a| vs |b| without branching
int a_abs = (a.sign) ? ~a.mantissa : a.mantissa;
int b_abs = (b.sign) ? ~b.mantissa : b.mantissa;
// Compare: if (a_abs > b_abs) then a dominates
// Use unsigned comparison: cast to uint32_t and compare
uint32_t a_u = (uint32_t)(a.sign ? -a.mantissa : a.mantissa);
uint32_t b_u = (uint32_t)(b.sign ? -b.mantissa : b.mantissa);
int greater = (a_u > b_u) ? 1 : 0;
But this still uses ?, which may compile to branch.
Instead, use bit tricks:
int greater = ((uint32_t)a.mantissa > (uint32_t)b.mantissa) ? 1 : 0;
// Or better: use compiler intrinsics like __builtin_cmpge
If your compiler supports branchless comparisons, use them.
Alternatively, only support same-sign addition and do subtraction separately.
✅ Multiplication (Branch-Free, Fast)
Multiplication is easier:
emu_float64 emu_mul(emu_float64 a, emu_float64 b) {
// Sign: XOR of signs
int sign = a.sign ^ b.sign;
// Exponent: add exponents
int exp = a.exponent + b.exponent - 1023; // adjust bias
// Mantissa: multiply significands
uint64_t man1 = (1ULL << 32) + a.mantissa; // 1 + a.mantissa/2^32
uint64_t man2 = (1ULL << 32) + b.mantissa;
uint64_t product = man1 * man2;
// Normalize: shift right until MSB set
int leading_zeros = __builtin_clzll(product);
int shift = 32 - leading_zeros;
uint32_t new_mant = product >> shift;
int new_exp = exp + shift;
// Clamp exponent bounds?
if (new_exp < 1) new_exp = 1; // Underflow → 0
if (new_exp > 2046) new_exp = 2046; // Overflow → inf
emu_float64 result = {new_mant, new_exp, sign};
return result;
}
✅ No branching! Uses __builtin_clzll (if available).
✅ Fast Power-of-2 Exponent (For Scaling)
You often need 2^exp. Since you can't use pow(), precompute:
// Precomputed lookup table: power of 2 for exponent bias
static const uint32_t exp_table[2048] = { ... };
// Or compute at runtime using bit shifts:
uint32_t pow2_32(int exp) {
int shift = exp - 1023;
if (shift < -1023) return 0;
if (shift > 1023) return 0xFFFFFFFF;
return (1U << shift);
}
But << with variable shift may be slow.
Use lookup table instead.
🧠 Summary: Design Rules for Your Hardware
| Requirement | Strategy |
|---|
| No 64-bit floats/int | Use two 32-bit ints |
| No branching | Avoid if, switch, ?:; use bitwise ops |
| Fast arithmetic | Use fixed-point, shifts, multiplications |
| Precision loss | Acceptable (simulate 64-bit float with ~15 digits?) → no, only ~9 digits due to 32-bit mantissa |
| Exponent range | Limited by int32_t (±2e9), but you can clamp |
📈 Accuracy Estimate
- 32-bit mantissa → ~9–10 decimal digits of precision
- 11-bit exponent → ~±1023 → range: ~2^-1023 to 2^1023
- Matches double in range, but lower precision
Good enough for many embedded applications (e.g., sensor data, control loops).
✅ Final Recommendations
- Use 32-bit mantissa + 32-bit exponent with implicit leading 1.
- Store sign separately.
- Implement multiplication first — easiest and fastest.
- Use lookup tables for powers of 2.
- Avoid branching:
- Use
&, |, ^, >>, <<, +, -
- Use
__builtin_clzll for normalization
- Handle overflow/underflow with clamping.
- Test with known values: π, 1/3, 1e6, etc.
📌 Example: Representing 1.5
emu_float64 x = { (1U << 31), 1024, 0 }; // mantissa = 0.5, exp = 1024 → 2^(1) = 2 → 1.5
→ Value = (1 + 0.5) * 2^(1024-1023) = 1.5 * 2^1 = 3.0 ❌
Wait — wrong!
Correct:
- To get 1.5 = 3/2 = 1.1₂ × 2¹
- So mantissa = 0.1₂ = 0.5 → stored as
0.5 * 2^32 = 2147483648
- Exponent = 1023 + 1 = 1024
✅ So:
x.mantissa = 2147483648; // 0.5 in fixed point
x.exponent = 1024;
x.sign = 0;
→ 1 + 0.5 = 1.5, times 2^(1024-1023) = 2^1 = 2 → 3.0 ❌
Wait — we forgot: the implicit 1 is already included.
So:
value = (1 + mantissa / 2^32) * 2^(exponent - 1023)
- For 1.5:
1 + 0.5 = 1.5, times 2^0 = 1 → so exponent = 1023
✅ Correct:
x.mantissa = 2147483648; // 0.5
x.exponent = 1023;
→ 1.5 × 1 = 1.5 ✅
🎯 Conclusion
Yes — you can simulate a 64-bit float using two 32-bit integers:
- One holds the fractional significand (32 bits)
- One holds the biased exponent (32 bits)
- Use implicit leading 1
- Implement multiplication and addition with branch-free code
- Use lookup tables and bit operations for speed
It won’t match native double precision, but it will be fast, predictable, and usable on bare-metal hardware.
Would you like:
- A full C implementation?
- Assembly version?
- Lookup table generator?
- Fixed-point math utilities?
Let me know — I’ll generate optimized code tailored to your hardware!