ppsspp/math/math_util.h

82 lines
1.9 KiB
C
Raw Normal View History

2012-03-24 22:39:19 +00:00
#ifndef _MATHUTILS_H
#define _MATHUTILS_H
2012-07-26 11:47:15 +00:00
#include <cmath>
#include <cstring>
2012-03-24 22:39:19 +00:00
2012-10-30 12:20:55 +00:00
inline float sqr(float f) {return f*f;}
2012-07-18 10:03:51 +00:00
inline float sqr_signed(float f) {return f<0 ? -f*f : f*f;}
2012-03-24 22:39:19 +00:00
typedef unsigned short float16;
2012-03-31 09:16:13 +00:00
// This ain't a 1.5.10 float16, it's a stupid hack format where we chop 16 bits off a float.
// This choice is subject to change. Don't think I'm using this for anything at all now anyway.
2012-05-08 20:04:24 +00:00
// DEPRECATED
2012-03-24 22:39:19 +00:00
inline float16 FloatToFloat16(float x) {
2012-10-30 12:20:55 +00:00
int ix;
memcpy(&ix, &x, sizeof(float));
return ix >> 16;
2012-03-24 22:39:19 +00:00
}
inline float Float16ToFloat(float16 ix) {
2012-10-30 12:20:55 +00:00
float x;
memcpy(&x, &ix, sizeof(float));
return x;
2012-03-24 22:39:19 +00:00
}
#define PI 3.141592653589793f
2012-07-26 11:47:15 +00:00
#ifndef M_PI
#define M_PI 3.141592653589793f
#endif
2012-03-24 22:39:19 +00:00
// The stuff in this file is from all over the web, esp. dspmusic.org. I think it's all public domain.
// In any case, very little of it is used anywhere at the moment.
// Calculate pseudo-random 32 bit number based on linear congruential method.
void SetSeed(unsigned int seed);
unsigned int GenerateRandomNumber();
inline float GenerateRandomFloat01() {
2012-10-30 12:20:55 +00:00
return (float)((double)GenerateRandomNumber() / 0xFFFFFFFF);
2012-03-24 22:39:19 +00:00
}
inline float GenerateRandomSignedFloat() {
2012-10-30 12:20:55 +00:00
return (float)((double)GenerateRandomNumber() / 0x80000000) - 1.0f;
2012-03-24 22:39:19 +00:00
}
inline float GaussRand()
{
2012-10-30 12:20:55 +00:00
float R1 = GenerateRandomFloat01();
float R2 = GenerateRandomFloat01();
2012-03-24 22:39:19 +00:00
2012-10-30 12:20:55 +00:00
float X = sqrtf(-2.0f * logf(R1)) * cosf(2.0f * PI * R2);
if (X > 4.0f) X = 4.0f;
if (X < -4.0f) X = -4.0f;
return X;
2012-03-24 22:39:19 +00:00
}
// Accuracy unknown
inline double atan_fast(double x) {
2012-10-30 12:20:55 +00:00
return (x / (1.0 + 0.28 * (x * x)));
2012-03-24 22:39:19 +00:00
}
// linear -> dB conversion
inline float lin2dB(float lin) {
2012-10-30 12:20:55 +00:00
const float LOG_2_DB = 8.6858896380650365530225783783321f; // 20 / ln( 10 )
return logf(lin) * LOG_2_DB;
2012-03-24 22:39:19 +00:00
}
// dB -> linear conversion
inline float dB2lin(float dB) {
2012-10-30 12:20:55 +00:00
const float DB_2_LOG = 0.11512925464970228420089957273422f; // ln( 10 ) / 20
return expf(dB * DB_2_LOG);
2012-03-24 22:39:19 +00:00
}
2012-05-08 22:33:43 +00:00
// FPU control.
void EnableFZ();
void DisableFZ();
2012-03-24 22:39:19 +00:00
#endif