You're describing a custom floating-point format — not IEEE 754 double, but a software-emulated float using two 32-bit integers:
- One for the mantissa (significand)
- One for the exponent
This is similar to how early computers or embedded systems implemented floating-point before hardware support existed.
✅ Goal
Simulate a floating-point number using:
struct custom_float {
int32_t mantissa; // signed integer (can be normalized or not)
int32_t exponent; // signed integer (base 2 or base 10?)
};
We’ll assume base-2, like IEEE 754, and try to make arithmetic as branch-free as possible.
📐 Format Design
Let’s define:
value = mantissa × 2^exponent
Where:
mantissa is a signed 32-bit integer
exponent is a signed 32-bit integer
⚠️ This is not normalized — so mantissa can be any integer, and we rely on software to normalize when needed.
But normalization requires branching → which you want to avoid.
So we’ll avoid normalization and accept that precision varies with magnitude — this is acceptable in many embedded applications.
⚙️ Basic Operations (Branch-Free Where Possible)
1. Convert from float / double to custom_float
custom_float from_double(double d) {
if (d == 0.0) return (custom_float){0, 0};
int exp;
double m = frexp(d, &exp); // m in [0.5, 1.0) or [-1.0, -0.5)
int32_t mantissa = (int32_t)(m * (1LL << 31)); // scale to 31 bits + sign
return (custom_float){mantissa, exp - 31}; // adjust exponent
}
❗ frexp may not be available on your system — you’d need to implement it manually using bit manipulation.
Alternative without frexp (branch-heavy, but we'll try to minimize):
// Rough version — assumes d != 0
custom_float from_double_naive(double d) {
union { double f; uint64_t u; } u = { d };
int sign = (u.u >> 63) & 1;
int exp = ((u.u >> 52) & 0x7FF) - 1023;
uint64_t mant = u.u & 0xFFFFFFFFFFFFFULL;
// Reconstruct mantissa as fixed-point
int32_t m = (int32_t)((mant | (1ULL << 52)) >> (52 - 31)); // 31-bit mantissa
if (sign) m = -m;
return (custom_float){ m, exp - 31 };
}
This still has branches for sign/zero — you can eliminate them with bit tricks if needed.
🧮 Arithmetic: Addition
To add two custom_float values:
custom_float add(custom_float a, custom_float b) {
if (a.mantissa == 0) return b;
if (b.mantissa == 0) return a;
// Assume a.exponent >= b.exponent (we’ll handle both cases without branching)
int diff = a.exponent - b.exponent;
int32_t shift = diff > 0 ? diff : -diff;
int32_t mask = -(diff > 0); // -1 if diff>0, 0 otherwise
// Shift smaller mantissa right by |diff|
int32_t shifted_b_mant = b.mantissa >> shift;
int32_t shifted_a_mant = a.mantissa >> shift;
// Select correct shifted mantissa based on which exponent was larger
int32_t mant_a_sel = (mask & a.mantissa) | (~mask & shifted_a_mant);
int32_t mant_b_sel = (~mask & b.mantissa) | (mask & shifted_b_mant);
// Add
int32_t sum_mant = mant_a_sel + mant_b_sel;
int32_t sum_exp = (mask & a.exponent) | (~mask & b.exponent);
// Normalize? → Avoid for speed. Just return.
return (custom_float){sum_mant, sum_exp};
}
❗ This is incorrect — because we’re not handling overflow, underflow, or alignment properly. Also, shifting negative numbers right is implementation-defined in C.
✅ Better approach: use unsigned shifts and track sign separately.
🔁 Branch-Free Addition (Simplified Version)
Assume we store mantissa as unsigned 32-bit, and sign as a separate bit (or use 2's complement carefully).
Here’s a more practical branch-free addition:
typedef struct {
uint32_t mantissa; // always positive, sign handled separately
int32_t exponent;
int8_t sign; // 0 or 1
} custom_float_fast;
custom_float_fast add_fast(custom_float_fast a, custom_float_fast b) {
if (a.mantissa == 0) return b;
if (b.mantissa == 0) return a;
// Make sure a.exponent >= b.exponent
int diff = a.exponent - b.exponent;
int swap = diff < 0;
// Swap a and b if needed (branch-free)
uint32_t temp_mant = a.mantissa;
int32_t temp_exp = a.exponent;
int8_t temp_sign = a.sign;
a.mantissa = swap ? b.mantissa : a.mantissa;
a.exponent = swap ? b.exponent : a.exponent;
a.sign = swap ? b.sign : a.sign;
b.mantissa = swap ? temp_mant : b.mantissa;
b.exponent = swap ? temp_exp : b.exponent;
b.sign = swap ? temp_sign : b.sign;
// Now a.exponent >= b.exponent
int shift = a.exponent - b.exponent;
// Align b.mantissa
uint32_t aligned_b = b.mantissa >> shift;
// Add or subtract based on signs
int same_sign = (a.sign == b.sign);
int32_t result_mant;
if (same_sign) {
result_mant = a.mantissa + aligned_b;
} else {
result_mant = a.mantissa - aligned_b;
}
int32_t result_exp = a.exponent;
int8_t result_sign = a.sign;
// Handle negative result
if (result_mant < 0) {
result_mant = -result_mant;
result_sign = !result_sign;
}
// Normalize? → Skip for speed.
return (custom_float_fast){
.mantissa = (uint32_t)result_mant,
.exponent = result_exp,
.sign = result_sign
};
}
❗ Still has branches! Let’s go truly branch-free.
🚫 Branch-Free Addition (Using Bit Tricks)
We can eliminate branches using conditional moves via bit masks.
Example for adding two numbers with same/different signs:
// Given: a.mantissa, b.mantissa, a.sign, b.sign
int same_sign = (a.sign ^ b.sign) == 0;
int sign_bit = a.sign; // assume a is dominant
// Compute aligned_b
int shift = a.exponent - b.exponent;
uint32_t aligned_b = b.mantissa >> shift;
// Compute sum or difference
int32_t sum = a.mantissa + aligned_b;
int32_t diff = a.mantissa - aligned_b;
// Select based on same_sign
int32_t result_mant = same_sign ? sum : diff;
int8_t result_sign = same_sign ? a.sign : (a.mantissa >= aligned_b ? a.sign : b.sign);
// But wait — we need to handle negative results without branching...
// Instead, let’s always compute absolute value and track sign separately
int32_t abs_diff = abs(a.mantissa - aligned_b);
int32_t abs_sum = a.mantissa + aligned_b;
int32_t final_mant = same_sign ? abs_sum : abs_diff;
int8_t final_sign = same_sign ? a.sign : (a.mantissa >= aligned_b ? a.sign : b.sign);
// But `abs()` might branch... so we do:
int32_t mask = result_mant >> 31; // -1 if negative, 0 if positive
result_mant = (result_mant + mask) ^ mask; // branchless abs
This gets very messy — and performance may suffer due to many operations.
💡 Better Idea: Use Fixed-Point Arithmetic Instead
Since branching is slow and you lack 64-bit types, consider using fixed-point arithmetic with 32-bit integers:
typedef int32_t fixed32_t; // Q15.16: 15 bits integer, 16 bits fraction
#define FX_SHIFT 16
#define FX_ONE (1 << FX_SHIFT)
fixed32_t fx_add(fixed32_t a, fixed32_t b) { return a + b; }
fixed32_t fx_mul(fixed32_t a, fixed32_t b) { return ((int64_t)a * b) >> FX_SHIFT; }
❗ But you said no 64-bit types — so multiplication becomes hard.
You can do 32x32 → 32-bit multiply with truncation:
fixed32_t fx_mul_approx(fixed32_t a, fixed32_t b) {
int64_t temp = (int64_t)a * b;
return (fixed32_t)(temp >> FX_SHIFT);
}
If you cannot use 64-bit, then you must implement 32x32 → 32-bit multiply using shifts and adds — which is slow.
✅ Recommended Practical Approach
Given your constraints:
- No 64-bit types
- Slow branching
- Need floating-point-like range
Use Q15.16 fixed-point with these rules:
- Store everything as
int32_t
- Multiplication:
(a * b) >> 16 (requires 64-bit intermediate — if unavailable, use lookup tables or shift-add)
- Division: use reciprocal approximation or Newton-Raphson
- Avoid dynamic exponent/mantissa — too slow
Or use lookup tables for expensive operations (sin, cos, log, exp).
🎯 Final Answer: Simulating 64-bit Float with Two 32-bit Ints (Mantissa + Exponent)
If you insist on mantissa/exponent:
typedef struct {
int32_t mantissa; // 32-bit signed integer
int32_t exponent; // 32-bit signed integer (power of 2)
} soft_float;
// Addition (simplified, assumes exponents are close)
soft_float add_sf(soft_float a, soft_float b) {
if (a.mantissa == 0) return b;
if (b.mantissa == 0) return a;
int diff = a.exponent - b.exponent;
int shift = diff > 0 ? diff : -diff;
int32_t mask = -(diff > 0);
int32_t aligned_b = b.mantissa >> shift;
int32_t sum_mant = a.mantissa + aligned_b;
int32_t sum_exp = a.exponent;
// Return without normalization
return (soft_float){sum_mant, sum_exp};
}
⚠️ This will lose precision and overflow easily — but it’s fast and branch-free if you replace > with bit tricks.
📊 Performance vs Accuracy Tradeoff
| Method | Speed | Accuracy | Branches | Notes |
|---|
| Two 32-bit ints (mantissa/exp) | Medium | Low | Few | Simple, but imprecise |
| Fixed-point (Q15.16) | High | Medium | None | Best for embedded if 64-bit mul allowed |
| Full IEEE 754 emulation | Slow | High | Many | Not suitable for your case |
✅ Recommendation for Your Hardware
Given:
- No 64-bit types
- Slow branching
- Need speed
👉 Use fixed-point arithmetic (Q15.16) and implement multiplication using 32-bit shifts and adds (or precomputed lookup tables for common ops).
If you must use mantissa/exponent:
- Limit exponent range (e.g., -128 to +127)
- Use 24-bit mantissa + 8-bit exponent (packed in 32-bit)
- Precompute scaling factors
🛠 Example: Fast Multiply Using Lookup Table (Optional)
If you can afford memory:
// Precomputed: 256 x 256 table for 8-bit fractions
int16_t mul_lut[256][256]; // for fractional part
// Then multiply two Q8.8 numbers
int16_t q88_mul(int16_t a, int16_t b) {
int hi_a = (a >> 8) & 0xFF, lo_a = a & 0xFF;
int hi_b = (b >> 8) & 0xFF, lo_b = b & 0xFF;
return (hi_a * hi_b << 8) + mul_lut[lo_a][lo_b] + ... ; // approximate
}
✅ Conclusion
You can simulate a 64-bit float using two 32-bit integers (mantissa + exponent), but:
- Arithmetic is complex and slow without branching
- Precision is poor unless you normalize (which requires branching)
- Fixed-point is faster and more predictable
For your hardware, I strongly recommend:
Use Q15.16 fixed-point arithmetic with 32-bit integers. Implement multiplication using 64-bit intermediates if possible, or use lookup tables/shift-add if not.
If you absolutely need dynamic range → use mantissa/exponent with limited range and accept loss of precision.
Let me know if you want a full branch-free implementation of add/mul/div for your custom float — I can write it with bit tricks!