File: rmath.h

package info (click to toggle)
sofa-framework 1.0~beta4-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 88,624 kB
  • sloc: cpp: 151,120; ansic: 2,387; xml: 581; sh: 436; makefile: 67
file content (115 lines) | stat: -rw-r--r-- 3,227 bytes parent folder | download | duplicates (5)
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
/******* COPYRIGHT ************************************************
*                                                                 *
*                             FlowVR                              *
*                       Template Library                          *
*                                                                 *
*-----------------------------------------------------------------*
* COPYRIGHT (C) 20054 by                                          *
* Laboratoire Informatique et Distribution (UMR5132) and          *
* INRIA Project MOVI. ALL RIGHTS RESERVED.                        *
*                                                                 *
* This source is covered by the GNU LGPL, please refer to the     *
* COPYING file for further information.                           *
*                                                                 *
*-----------------------------------------------------------------*
*                                                                 *
*  Original Contributors:                                         *
*    Jeremie Allard,                                              *
*    Clement Menier.                                              *
*                                                                 * 
*******************************************************************
*                                                                 *
* File: include/ftl/vec.h                                         *
*                                                                 *
* Contacts: 06/22/2005 Jeremie Allard <Jeremie.Allard@imag.fr>    *
*                                                                 *
******************************************************************/
#ifndef FTL_RMATH_H
#define FTL_RMATH_H

#include <math.h>

namespace ftl
{

/*
// Most portable version, but also the slowest
inline int rfloor(double r)
{
  return ((int)(r+1000000))-1000000;
}

inline int rnear(double r)
{
  return ((int)(r+1000000.5))-1000000;
}
*/

/*
// Does not work on gcc 4.x due to more aggressive optimizations
inline int rfloor(double r)
{
  static const double FLOATTOINTCONST=(1.5*(1LL<<(52-16)));
  r+=FLOATTOINTCONST;
  return ((((int*)&r)[0])>>16);
}

inline int rnear(double r)
{
  static const double FLOATTOINTCONST_0_5=(1.5*(1LL<<(52-16)))+0.5;
  r+=FLOATTOINTCONST_0_5;
  return ((((int*)&r)[0])>>16);
}
*/

// Works on gcc 3.x and 4.x
inline int rfloor(double r)
{
  static const double FLOATTOINTCONST=(1.5*(1LL<<(52-16)));
  union { double d; int i; } temp;
  temp.d = r+FLOATTOINTCONST;
  return ((temp.i)>>16);
}

inline int rnear(double r)
{
  static const double FLOATTOINTCONST_0_5=(1.5*(1LL<<(52-16)))+0.5;
  union { double d; int i; } temp;
  temp.d = r+FLOATTOINTCONST_0_5;
  return ((temp.i)>>16);
}



inline int rceil(double r)
{
  return -rfloor(-r);
}

template<class real>
inline real rabs(real r)
{
  return (r>=0)?r:-r;
}

template<class real>
inline real rmin(real r, real s)
{
  return (r<=s)?r:s;
}

template<class real>
inline real rmax(real r, real s)
{
  return (r>s)?r:s;
}

template<class T>
inline T rlerp(const T& a, const T& b, float f)
{
  return a+(b-a)*f;
}

} // namespace ftl

#endif