29,6 → 29,28 |
#include<sftypes.h> |
#include<common.h> |
|
/* Table for fast leading zeroes counting */ |
char zeroTable[256] = { |
8, 7, 7, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, \ |
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, \ |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, \ |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
}; |
|
|
|
/** Take fraction shifted by 10 bits to left, round it, normalize it and detect exceptions |
* @param exp exponent with bias |
* @param cfrac fraction shifted 10 places left with added hidden bit |
62,7 → 84,7 |
|
cfrac += (0x1 << (64 - FLOAT64_FRACTION_SIZE - 3)); |
|
if (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_HIDDEN_BIT_MASK - 1)))) { |
if (!(cfrac & (FLOAT64_HIDDEN_BIT_MASK << (64 - FLOAT64_FRACTION_SIZE - 1)))) { |
|
result.parts.fraction = ((cfrac >>(64 - FLOAT64_FRACTION_SIZE - 2) ) & (~FLOAT64_HIDDEN_BIT_MASK)); |
return result; |
93,3 → 115,91 |
return result; |
} |
|
/** Counts leading zeroes in 64bit unsigned integer |
* @param i |
*/ |
int countZeroes64(__u64 i) |
{ |
int j; |
for (j =0; j < 64; j += 8) { |
if ( i & (0xFFll << (56 - j))) { |
return (j + countZeroes8(i >> (56 - j))); |
} |
} |
|
return 64; |
} |
|
/** Counts leading zeroes in 32bit unsigned integer |
* @param i |
*/ |
int countZeroes32(__u32 i) |
{ |
int j; |
for (j =0; j < 32; j += 8) { |
if ( i & (0xFF << (24 - j))) { |
return (j + countZeroes8(i >> (24 - j))); |
} |
} |
|
return 32; |
} |
|
/** Counts leading zeroes in byte |
* @param i |
*/ |
int countZeroes8(__u8 i) |
{ |
return zeroTable[i]; |
} |
|
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 30. bit |
* @param exp exponent |
* @param fraction part with hidden bit shifted to 30. bit |
*/ |
void roundFloat32(__s32 *exp, __u32 *fraction) |
{ |
/* rounding - if first bit after fraction is set then round up */ |
(*fraction) += (0x1 << 6); |
|
if ((*fraction) & (FLOAT32_HIDDEN_BIT_MASK << 8)) { |
/* rounding overflow */ |
++(*exp); |
(*fraction) >>= 1; |
}; |
|
if (((*exp) >= FLOAT32_MAX_EXPONENT ) || ((*exp) < 0)) { |
/* overflow - set infinity as result */ |
(*exp) = FLOAT32_MAX_EXPONENT; |
(*fraction) = 0; |
return; |
} |
|
return; |
} |
|
/** Round and normalize number expressed by exponent and fraction with first bit (equal to hidden bit) at 62. bit |
* @param exp exponent |
* @param fraction part with hidden bit shifted to 62. bit |
*/ |
void roundFloat64(__s32 *exp, __u64 *fraction) |
{ |
/* rounding - if first bit after fraction is set then round up */ |
(*fraction) += (0x1 << 9); |
|
if ((*fraction) & (FLOAT64_HIDDEN_BIT_MASK << 11)) { |
/* rounding overflow */ |
++(*exp); |
(*fraction) >>= 1; |
}; |
|
if (((*exp) >= FLOAT64_MAX_EXPONENT ) || ((*exp) < 0)) { |
/* overflow - set infinity as result */ |
(*exp) = FLOAT64_MAX_EXPONENT; |
(*fraction) = 0; |
return; |
} |
|
return; |
} |
|