1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
/*
Copyright (c) 2010, Warmux Team
Large parts of the code are from the fixed point library of Markus Trenkwalder
Copyright (c) 2007, Markus Trenkwalder
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the library's copyright owner nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FIXEDP_CLASS_H_INCLUDED
#define FIXEDP_CLASS_H_INCLUDED
#ifdef _MSC_VER
#pragma once
#endif
#include "fixed_func.h"
#include <sstream>
#include <stdio.h>
#include <limits.h>
//#define TRACK_MINMAX
namespace fp {
// The template argument p in all of the following functions refers to the
// fixed point precision (e.g. p = 8 gives 24.8 fixed point functions).
template <int p>
struct fixed_point {
fint_t intValue;
#ifdef TRACK_MINMAX
static fint_t min, max;
~fixed_point()
{
if (min > intValue) {
min = intValue;
printf("Min: %"PRIi64"\n", min);
}
if (max < intValue) {
max = intValue;
printf("Max: %"PRIi64"\n", max);
}
}
#endif
fixed_point() {}
/*explicit*/ fixed_point(fint_t i) : intValue(i << p) {}
#if FIXINT_BITS == 64
/*explicit*/ fixed_point(int32_t i) : intValue(((fint_t)i) << p) {}
#endif
/*explicit*/ fixed_point(float f) : intValue(float2fix<p>(f)) {}
/*explicit*/ fixed_point(double d) : intValue(float2fix<p>((float)d)) {}
/*explicit*/ fixed_point(unsigned int u) : intValue(((fint_t)u) << p) {}
/*explicit*/ fixed_point(unsigned long int u) : intValue(((fint_t)u) << p) {}
fixed_point& operator += (const fixed_point& r) { intValue += r.intValue; return *this; }
fixed_point& operator -= (const fixed_point& r) { intValue -= r.intValue; return *this; }
fixed_point& operator *= (const fixed_point& r) { intValue = fixmul<p>(intValue, r.intValue); return *this; }
fixed_point& operator /= (const fixed_point& r) { intValue = fixdiv<p>(intValue, r.intValue); return *this; }
fixed_point& operator *= (int32_t r) { intValue *= r; return *this; }
fixed_point& operator /= (int32_t r) { intValue /= r; return *this; }
fixed_point operator - () const { fixed_point x; x.intValue = -intValue; return x; }
fixed_point operator + (const fixed_point& r) const { fixed_point x = *this; x += r; return x;}
fixed_point operator - (const fixed_point& r) const { fixed_point x = *this; x -= r; return x;}
fixed_point operator * (const fixed_point& r) const { fixed_point x = *this; x *= r; return x;}
fixed_point operator / (const fixed_point& r) const { fixed_point x = *this; x /= r; return x;}
bool operator == (const fixed_point& r) const { return intValue == r.intValue; }
bool operator == (int i) const { return intValue == (((fint_t)i) << p); }
bool operator != (const fixed_point& r) const { return !(*this == r); }
bool operator < (const fixed_point& r) const { return intValue < r.intValue; }
bool operator < (int i) const { return intValue < (((fint_t)i) << p); }
bool operator > (const fixed_point& r) const { return intValue > r.intValue; }
bool operator > (int i) const { return intValue > (((fint_t)i) << p); }
bool operator <= (const fixed_point& r) const { return intValue <= r.intValue; }
bool operator <= (int i) const { return intValue <= (((fint_t)i) << p); }
bool operator >= (const fixed_point& r) const { return intValue >= r.intValue; }
bool operator >= (int i) const { return intValue >= (((fint_t)i) << p); }
fixed_point operator + (int32_t r) const { fixed_point x = *this; x += r; return x;}
fixed_point operator - (int32_t r) const { fixed_point x = *this; x -= r; return x;}
fixed_point operator * (int32_t r) const { fixed_point x = *this; x *= r; return x;}
fixed_point operator / (int32_t r) const { fixed_point x = *this; x /= r; return x;}
fixed_point operator + (unsigned int r) const { fixed_point x = *this; x += r; return x;}
fixed_point operator - (unsigned int r) const { fixed_point x = *this; x -= r; return x;}
fixed_point operator * (unsigned int r) const { fixed_point x = *this; x *= r; return x;}
fixed_point operator / (unsigned int r) const { fixed_point x = *this; x /= r; return x;}
operator int() const
{
fuint_t sign = ((fuint_t)intValue)>>(FIXINT_BITS-1);
return int(((fint_t)(intValue+(sign<<p)-sign))>>p);
}
// Must be used explicily as we don't want to calculate with doubles!
double toDouble() const
{
static const double factor = 1.0 / (double)(1 << p);
return intValue * factor;
}
float tofloat() const
{
static const float factor = 1.0f / (float)(1 << p);
return intValue * factor;
}
// Warning, this tests strict equality!
bool IsNotZero() const { return intValue; }
};
// Specializations for use with plain integers
template <int p>
inline fixed_point<p> operator + (int32_t a, fixed_point<p> b)
{ return b + a; }
template <int p>
inline fixed_point<p> operator - (int32_t a, fixed_point<p> b)
{ return -b + a; }
template <int p>
inline fixed_point<p> operator * (int32_t a, fixed_point<p> b)
{ return b * a; }
template <int p>
inline fixed_point<p> operator / (int32_t a, const fixed_point<p>& b)
{ fixed_point<p> r(a); r /= b; return r; }
#ifdef TRACK_MINMAX
template <int p>
fint_t fixed_point<p>::min = LLONG_MAX;
template <int p>
fint_t fixed_point<p>::max = LLONG_MIN;
#endif
template <int p>
inline fixed_point<p> operator + (unsigned int a, const fixed_point<p>& b)
{ return b + a; }
template <int p>
inline fixed_point<p> operator - (unsigned int a, const fixed_point<p>& b)
{ return -b + a; }
template <int p>
inline fixed_point<p> operator * (unsigned int a, const fixed_point<p>& b)
{ return b * a; }
template <int p>
inline fixed_point<p> operator / (unsigned int a, const fixed_point<p>& b)
{ fixed_point<p> r(a); r /= b; return r; }
#ifdef SIZE_T_fp_METHODS
template <int p>
inline fixed_point<p> operator + (size_t a, fixed_point<p> b)
{ return b + a; }
template <int p>
inline fixed_point<p> operator - (size_t a, fixed_point<p> b)
{ return -b + a; }
template <int p>
inline fixed_point<p> operator * (size_t a, fixed_point<p> b)
{ return b * a; }
template <int p>
inline fixed_point<p> operator / (size_t a, fixed_point<p> b)
{ fixed_point<p> r(a); r /= b; return r; }
#endif
template<int p>
inline fixed_point<p> round(fixed_point<p> r);
template<>
inline fixed_point<16> round(fixed_point<16> r)
{
// Very important to have it this way: 0 would be rounded to -1 otherwise
r.intValue += (r.intValue<0) ? -32768 : 32768; // (1<<(16-1))
r.intValue &= 0xFFFFFFFFFFFF0000LL;
return r;
}
template<int p>
inline int uround(const fixed_point<p>& r);
template<>
inline int uround(const fixed_point<16>& r)
{
return int((r.intValue + 32768)>>16);
}
namespace detail {
static bool isDigit(int c) {
return c >= '0' && c <= '9';
}
}
template <int p>
std::istream & operator>> (std::istream & is, fixed_point<p> & r)
{
std::stringstream buffer;
int next = is.peek();
bool minus = (next == '-');
if (minus) {
buffer << "-";
is.get();// skip one character
}
buffer << '0'; // assuming a leading zero makes it possible to parse numbers starting with a dot and parses "" as 0.
next = is.peek();
while (next != (int)is.eof() && detail::isDigit(next)) {
char c = is.get();
buffer << c;
next = is.peek();
}
int number;
buffer >> number;
r = number;
next = is.peek();
if (next == '.') {
is.get();// skip point character
next = is.peek();
fixed_point<p> factor = 1;
fixed_point<p> ten = 10;
while (next != (int)is.eof() && detail::isDigit(next)) {
factor /= ten;
char c;
is >> c;
int digit = c - '0';
if (minus)
r -= digit * factor;
else
r += digit * factor;
buffer << c;
next = is.peek();
}
}
return is;
}
template <int p>
void printTo(std::ostream & os, const fixed_point<p> & r, int digits = -1)
{
if (digits == -1)
digits = p>>2;
fixed_point<p> rounding_correction = 0.5;
fixed_point<p> ten = 10;
for (int k = 0; k < digits; k++) {
rounding_correction = rounding_correction / ten;
}
if (r < 0)
rounding_correction = - rounding_correction;
fixed_point<p> to_print = r + rounding_correction;
fint_t left_of_dot = (int) r;
os << left_of_dot;
if (digits == 0) {
return;
}
os << ".";
to_print -= left_of_dot;
fixed_point<p> zero = 0;
int zeros = 0;
if (to_print < 0) {
to_print = - to_print;
}
for (int i=0; i<digits; i++) {
to_print *= 10;
int digit = (int)to_print;
to_print = to_print - digit;
if (digit == 0) {
zeros++;
} else {
for (int j=0; j<zeros; j++) {
os << "0";
}
zeros = 0;
os << digit;
}
}
// show at least one digit after the point:
if (zeros == digits) {
os << "0";
}
}
template <int p>
std::ostream & operator<< (std::ostream & os, const fixed_point<p> & r)
{
printTo(os, r);
return os;
}
// math functions
// no default implementation
template <int p>
inline fixed_point<p> sin(fixed_point<p> a);
template <int p>
inline fixed_point<p> cos(fixed_point<p> a);
template <int p>
inline fixed_point<p> acos(fixed_point<p> a);
template <int p>
inline fixed_point<p> asin(fixed_point<p> a);
template <int p>
inline fixed_point<p> atan(fixed_point<p> a);
template <int p>
inline fixed_point<p> sqrt(fixed_point<p> a);
template <int p>
inline fixed_point<p> sqrt_approx(fixed_point<p> a);
template <int p>
inline fixed_point<p> rsqrt(fixed_point<p> a);
template <int p>
inline fixed_point<p> inv(fixed_point<p> a);
template <int p>
inline fixed_point<p> abs(fixed_point<p> a)
{
fint_t sign = a.intValue >> (FIXINT_BITS-1);
fixed_point<p> r;
r.intValue = a.intValue^sign;
r.intValue -= sign;
return r;
}
template <int p>
inline fixed_point<p> fmod(fixed_point<p> numerator, fixed_point<p> denominator)
{
return numerator - denominator * floor(numerator / denominator) ;
}
template <int p>
inline fixed_point<p> floor(fixed_point<p> a)
{
fixed_point<p> r = a.intValue >> p;
return r;
}
// specializations for 16.16 format
template <>
inline fixed_point<16> sin(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixsin16(a.intValue);
return r;
}
template <>
inline fixed_point<16> cos(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixcos16(a.intValue);
return r;
}
template <>
inline fixed_point<16> acos(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixacos16(a.intValue);
return r;
}
template <>
inline fixed_point<16> asin(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixasin16(a.intValue);
return r;
}
template <>
inline fixed_point<16> atan(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixatan16(a.intValue);
return r;
}
template <>
inline fixed_point<16> sqrt(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixsqrt16(a.intValue);
return r;
}
template <>
inline fixed_point<16> sqrt_approx(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixsqrt16_approx(a.intValue);
return r;
}
template <>
inline fixed_point<16> rsqrt(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixrsqrt16(a.intValue);
return r;
}
template <>
inline fixed_point<16> inv(fixed_point<16> a)
{
fixed_point<16> r;
r.intValue = fixinv<16>(a.intValue);
return r;
}
} // end namespace fp
#endif
|