| 1 |
pub mod CFloatFPU { |
| 2 |
// Maximum allowed argument for SmallRound |
| 3 |
// const sc_uSmallMax: u32 = 0xFFFFF; |
| 4 |
|
| 5 |
// Binary representation of static_cast<float>(sc_uSmallMax) |
| 6 |
const sc_uBinaryFloatSmallMax: u32 = 0x497ffff0; |
| 7 |
|
| 8 |
fn LargeRound(x: f32) -> i32 { |
| 9 |
//XXX: the SSE2 version is probably slower than a naive SSE4 implementation that can use roundss |
| 10 |
#[cfg(target_feature = "sse2")] |
| 11 |
unsafe { |
| 12 |
#[cfg(target_arch = "x86")] |
| 13 |
use std::arch::x86::{__m128, _mm_set_ss, _mm_cvtss_si32, _mm_cvtsi32_ss, _mm_sub_ss, _mm_cmple_ss, _mm_store_ss, _mm_setzero_ps}; |
| 14 |
#[cfg(target_arch = "x86_64")] |
| 15 |
use std::arch::x86_64::{__m128, _mm_set_ss, _mm_cvtss_si32, _mm_cvtsi32_ss, _mm_sub_ss, _mm_cmple_ss, _mm_store_ss, _mm_setzero_ps}; |
| 16 |
|
| 17 |
let given: __m128 = _mm_set_ss(x); // load given value |
| 18 |
let result = _mm_cvtss_si32(given); |
| 19 |
let rounded: __m128 = _mm_setzero_ps(); // convert it to integer (rounding mode doesn't matter) |
| 20 |
let rounded = _mm_cvtsi32_ss(rounded, result); // convert back to float |
| 21 |
let diff = _mm_sub_ss(rounded, given); // diff = (rounded - given) |
| 22 |
let negHalf = _mm_set_ss(-0.5); // load -0.5f |
| 23 |
let mask = _mm_cmple_ss(diff, negHalf); // get all-ones if (rounded - given) < -0.5f |
| 24 |
let mut correction: i32 = 0; |
| 25 |
_mm_store_ss((&mut correction) as *mut _ as *mut _, mask); // get comparison result as integer |
| 26 |
return result - correction; // correct the result of rounding |
| 27 |
} |
| 28 |
#[cfg(not(target_feature = "sse2"))] |
| 29 |
return (x + 0.5).floor() as i32; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
//+------------------------------------------------------------------------ |
| 34 |
// |
| 35 |
// Function: CFloatFPU::SmallRound |
| 36 |
// |
| 37 |
// Synopsis: Convert given floating point value to nearest integer. |
| 38 |
// Half-integers are rounded up. |
| 39 |
// |
| 40 |
// Important: this routine is fast but restricted: |
| 41 |
// given x should be within (-(0x100000-.5) < x < (0x100000-.5)) |
| 42 |
// |
| 43 |
// Details: Implementation has abnormal looking that use to confuse |
| 44 |
// many people. However, it indeed works, being tested |
| 45 |
// thoroughly on x86 and ia64 platforms for literally |
| 46 |
// each possible argument values in the given range. |
| 47 |
// |
| 48 |
// More details: |
| 49 |
// Implementation is based on the knowledge of floating point |
| 50 |
// value representation. This 32-bits value consists of three parts: |
| 51 |
// v & 0x80000000 = sign |
| 52 |
// v & 0x7F800000 = exponent |
| 53 |
// v & 0x007FFFFF - mantissa |
| 54 |
// |
| 55 |
// Let N to be a floating point number within -0x400000 <= N <= 0x3FFFFF. |
| 56 |
// The sum (S = 0xC00000 + N) thus will satisfy Ox800000 <= S <= 0xFFFFFF. |
| 57 |
// All the numbers within this range (sometimes referred to as "binade") |
| 58 |
// have same position of most significant bit, i.e. 0x800000. |
| 59 |
// Therefore they are normalized equal way, thus |
| 60 |
// providing the weights on mantissa's bits to be the same |
| 61 |
// as integer numbers have. In other words, to get |
| 62 |
// integer value of floating point S, when Ox800000 <= S <= 0xFFFFFF, |
| 63 |
// we can just throw away the exponent and sign, and add assumed |
| 64 |
// most significant bit (that is always 1 and therefore is not stored |
| 65 |
// in floating point value): |
| 66 |
// (int)S = (<float S as int> & 0x7FFFFF | 0x800000); |
| 67 |
// To get given N in as integer, we need to subtract back |
| 68 |
// the value 0xC00000 that was added in order to obtain |
| 69 |
// proper normalization: |
| 70 |
// N = (<float S as int> & 0x7FFFFF | 0x800000) - 0xC00000. |
| 71 |
// or |
| 72 |
// N = (<float S as int> & 0x7FFFFF ) - 0x400000. |
| 73 |
// |
| 74 |
// Hopefully, the text above explains how |
| 75 |
// following routine works: |
| 76 |
// int SmallRound1(float x) |
| 77 |
// { |
| 78 |
// union |
| 79 |
// { |
| 80 |
// __int32 i; |
| 81 |
// float f; |
| 82 |
// } u; |
| 83 |
// |
| 84 |
// u.f = x + float(0x00C00000); |
| 85 |
// return ((u.i - (int)0x00400000) << 9) >> 9; |
| 86 |
// } |
| 87 |
// Unfortunatelly it is imperfect, due to the way how FPU |
| 88 |
// use to round intermediate calculation results. |
| 89 |
// By default, rounding mode is set to "nearest". |
| 90 |
// This means that when it calculates N+float(0x00C00000), |
| 91 |
// the 80-bit precise result will not fit in 32-bit float, |
| 92 |
// so some least significant bits will be thrown away. |
| 93 |
// Rounding to nearest means that S consisting of intS + fraction, |
| 94 |
// where 0 <= fraction < 1, will be converted to intS |
| 95 |
// when fraction < 0.5 and to intS+1 if fraction > 0.5. |
| 96 |
// What would happen with fraction exactly equal to 0.5? |
| 97 |
// Smart thing: S will go to intS if intS is even and |
| 98 |
// to intS+1 if intS is odd. In other words, half-integers |
| 99 |
// are rounded to nearest even number. |
| 100 |
// This FPU feature apparently is useful to minimize |
| 101 |
// average rounding error when somebody is, say, |
| 102 |
// digitally simulating electrons' behavior in plasma. |
| 103 |
// However for graphics this is not desired. |
| 104 |
// |
| 105 |
// We want to move half-integers up, therefore |
| 106 |
// define SmallRound(x) as {return SmallRound1(x*2+.5) >> 1;}. |
| 107 |
// This may require more comments. |
| 108 |
// Let given x = i+f, where i is integer and f is fraction, 0 <= f < 1. |
| 109 |
// Let's wee what is y = x*2+.5: |
| 110 |
// y = i*2 + (f*2 + .5) = i*2 + g, where g = f*2 + .5; |
| 111 |
// If "f" is in the range 0 <= f < .5 (so correct rounding result should be "i"), |
| 112 |
// then range for "g" is .5 <= g < 1.5. The very first value, .5 will force |
| 113 |
// SmallRound1 result to be "i*2", due to round-to-even rule; the remaining |
| 114 |
// will lead to "i*2+1". Consequent shift will throw away extra "1" and give |
| 115 |
// us desired "i". |
| 116 |
// When "f" in in the range .5 <= f < 1, then 1.5 <= g < 2.5. |
| 117 |
// All these values will round to 2, so SmallRound1 will return (2*i+2), |
| 118 |
// and the final shift will give desired 1+1. |
| 119 |
// |
| 120 |
// To get final routine looking we need to transform the combines |
| 121 |
// expression for u.f: |
| 122 |
// (x*2) + .5 + float(0x00C00000) == |
| 123 |
// (x + (.25 + double(0x00600000)) )*2 |
| 124 |
// Note that the ratio "2" means nothing for following operations, |
| 125 |
// since it affects only exponent bits that are ignored anyway. |
| 126 |
// So we can save some processor cycles avoiding this multiplication. |
| 127 |
// |
| 128 |
// And, the very final beautification: |
| 129 |
// to avoid subtracting 0x00400000 let's ignore this bit. |
| 130 |
// This mean that we effectively decrease available range by 1 bit, |
| 131 |
// but we're chasing for performance and found it acceptable. |
| 132 |
// So |
| 133 |
// return ((u.i - (int)0x00400000) << 9) >> 9; |
| 134 |
// is converted to |
| 135 |
// return ((u.i ) << 10) >> 10; |
| 136 |
// Eventually, will found that final shift by 10 bits may be combined |
| 137 |
// with shift by 1 in the definition {return SmallRound1(x*2+.5) >> 1;}, |
| 138 |
// we'll just shift by 11 bits. That's it. |
| 139 |
// |
| 140 |
//------------------------------------------------------------------------- |
| 141 |
fn SmallRound(x: f32) -> i32 |
| 142 |
{ |
| 143 |
//AssertPrecisionAndRoundingMode(); |
| 144 |
debug_assert!(-(0x100000 as f64 -0.5) < x as f64 && (x as f64) < (0x100000 as f64 -0.5)); |
| 145 |
|
| 146 |
|
| 147 |
let fi = (x as f64 + (0x00600000 as f64 + 0.25)) as f32; |
| 148 |
let result = ((fi.to_bits() as i32) << 10) >> 11; |
| 149 |
|
| 150 |
debug_assert!(x < (result as f32) + 0.5 && x >= (result as f32) - 0.5); |
| 151 |
return result; |
| 152 |
} |
| 153 |
|
| 154 |
pub fn Round(x: f32) -> i32 |
| 155 |
{ |
| 156 |
// cut off sign |
| 157 |
let xAbs: u32 = x.to_bits() & 0x7FFFFFFF; |
| 158 |
|
| 159 |
return if xAbs <= sc_uBinaryFloatSmallMax {SmallRound(x)} else {LargeRound(x)}; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
macro_rules! TOREAL { ($e: expr) => { $e as REAL } } |