21#ifndef __CALF_FIXED_POINT_H
22#define __CALF_FIXED_POINT_H
28inline uint64_t
shr(uint64_t
v,
int bits = 1) {
return v>>bits; };
29inline int64_t
shr(int64_t
v,
int bits = 1) {
return v>>bits; };
30inline float shr(
float v,
int bits = 1) {
return v*(1.0/(1<<bits)); };
31inline double shr(
double v,
int bits = 1) {
return v*(1.0/(1<<bits)); };
32template<
class T,
int FracBits>
33inline T
shr(T
v,
int bits = 1) {
40 enum {
IntBits = (
sizeof(T)*8) - FracBits };
49 if (FracBits == FracBits2)
value =
v.get();
50 else if (FracBits > FracBits2)
value =
v.get() <<
abs(FracBits - FracBits2);
51 else value =
v.get() >>
abs(FracBits - FracBits2);
71 inline static T
one() {
72 return (T)(1) << FracBits;
79 inline T
get()
const {
83 inline operator double()
const {
93 if (FracBits == FracBits2)
95 if (FracBits > FracBits2)
96 return v.get() <<
abs(FracBits - FracBits2);
97 return v.get() >>
abs(FracBits2 - FracBits);
130 tmp.
set(((int64_t)
value) *
v.get() >> FracBits2);
136 value = (T)(((BigType)
value) *
v.get() >> FracBits2);
175 return value >> FracBits;
180 return ((
unsigned)
value) >> FracBits;
185 return ((uint64_t)
value) >> FracBits;
190 return value & ((1 << FracBits)-1);
196 int fbits =
value & ((1 << FracBits)-1);
197 if (
Bits == FracBits)
return fbits;
198 int shift =
abs(
Bits-FracBits);
199 return (
Bits < FracBits) ? (fbits >> shift) : (fbits << shift);
204 return (
value & ((1 << FracBits)-1)) * (1.0 / (1 << FracBits));
210 template<
class U,
int UseBits,
class MulType>
213 assert ( fp >=0 && fp <= (1LL<<UseBits));
214 return v1 +
shr(((MulType)(
v2-v1) * fp), UseBits);
217 template<
class U,
int UseBits>
219 unsigned int pos =
uipart();
225 template<
class U,
int UseBits>
227 unsigned int pos = (
uipart() + shift) & ((1u <<
IntBits) - 1);
233 unsigned int pos =
uipart();
244 template<
class U,
int UseBits,
class MulType>
257template<
class T,
int FractBits>
float abs(const fft_t *freqs, off_t x)
Definition OscilGen.cpp:52
static const unsigned long mask[]
Definition bitwise.c:31
Definition fixed_point.h:38
unsigned long long int value
Definition fixed_point.h:39
fixed_point operator-(int v) const
Definition fixed_point.h:146
void set(T value)
Definition fixed_point.h:75
fixed_point & operator+=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:105
fixed_point operator*(int v) const
Definition fixed_point.h:152
fixed_point()
default constructor, does not initialize the value, just like - say - float doesn't
Definition fixed_point.h:44
fixed_point operator+(int v) const
Definition fixed_point.h:140
U lerp_table_lookup_float_mask(U *data, unsigned int mask) const
Definition fixed_point.h:238
static T rebase(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:92
U lerp_ptr_lookup_float(U *data) const
Definition fixed_point.h:251
U lerp_table_lookup_int_shift(U *data, unsigned int shift)
Definition fixed_point.h:226
unsigned int uipart() const
return integer part as unsigned int
Definition fixed_point.h:179
fixed_point operator*(const fixed_point< U, FracBits2 > &v) const
multiply two fixed point values, using long long int to store the temporary multiplication result
Definition fixed_point.h:128
unsigned int ui64part() const
return integer part as unsigned int
Definition fixed_point.h:184
fixed_point & operator-=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:110
fixed_point(const fixed_point< U, FracBits2 > &v)
copy constructor from any other fixed_point value
Definition fixed_point.h:48
@ IntBits
Definition fixed_point.h:40
fixed_point operator+(const fixed_point< U, FracBits2 > &v) const
Definition fixed_point.h:115
T get() const
Definition fixed_point.h:79
fixed_point & operator+=(int v)
Definition fixed_point.h:158
fixed_point & operator-=(int v)
Definition fixed_point.h:163
T fpart() const
return fractional part as 0..(2^Bits-1)
Definition fixed_point.h:195
static T one()
Definition fixed_point.h:71
U lerp_table_lookup_float(U *data) const
Definition fixed_point.h:232
T fpart() const
return fractional part as 0..(2^FracBits-1)
Definition fixed_point.h:189
fixed_point & operator*=(const fixed_point< U, FracBits2 > &v)
multiply two fixed point values, using BigType (usually 64-bit int) to store the temporary multiplica...
Definition fixed_point.h:135
U lerp_table_lookup_int(U *data) const
Definition fixed_point.h:218
static fixed_point from_base(const T &v)
Makes an instance from a representation value (ie. same type of value as is used for internal storage...
Definition fixed_point.h:64
fixed_point & operator*=(int v)
Definition fixed_point.h:168
double fpart_as_double() const
return fractional part as 0..1
Definition fixed_point.h:203
T ipart() const
return integer part
Definition fixed_point.h:174
U lerp_ptr_lookup_int(U *data) const
Definition fixed_point.h:245
U lerp_by_fract_int(U v1, U v2) const
Definition fixed_point.h:211
fixed_point(double v)
Definition fixed_point.h:59
fixed_point & operator=(const fixed_point< U, FracBits2 > &v)
Definition fixed_point.h:100
fixed_point & operator=(double v)
Definition fixed_point.h:87
fixed_point operator-(const fixed_point< U, FracBits2 > &v) const
Definition fixed_point.h:121
unsigned v[N_MAX]
Definition inflate.c:1584
static void v2(register WDL_FFT_REAL *a)
Definition fft.c:1099
#define U(x)
Definition fmopl.c:132
int int32_t
Definition mid.cpp:97
unsigned int uint32_t
Definition mid.cpp:100
fixed_point< T, FractBits > operator*(int v, fixed_point< T, FractBits > v2)
Definition fixed_point.h:258
T sine_table< T, N, Multiplier >::data[N+1]
Definition primitives.h:442
uint32_t shr(uint32_t v, int bits=1)
Definition fixed_point.h:26
fixed_point< unsigned long long int, 24 > wpos
wave position (unsigned 64-bit int including 24-bit fractional part)
Definition fixed_point.h:264
intptr_t Bits
Definition nukedopl.h:39
int result
Definition process.c:1455