ManaPlus
Functions | Variables
mathutils.h File Reference

(986a3bf)

#include "utils/cast.h"
#include <string>
#include <cmath>
#include "localconsts.h"

Go to the source code of this file.

Functions

uint16_t getCrc16 (const std::string &str)
 
float fastInvSqrt (float x)
 
float fastSqrt (const float x)
 
float weightedAverage (const float n1, const float n2, const float w)
 
int roundDouble (const double v)
 
int powerOfTwo (const unsigned int input)
 
int fastSqrtInt (const unsigned int n)
 

Variables

static const uint16_t crc_table [256]
 
static const uint8_t square_roots [1000]
 

Function Documentation

◆ fastInvSqrt()

float fastInvSqrt ( float  x)
inline

Definition at line 210 of file mathutils.h.

211 {
212  union { int i; float x; } tmp = {0U};
213  const float xhalf = 0.5F * x;
214  tmp.x = x;
215  tmp.i = 0x5f375a86 - (tmp.i >> 1);
216  x = tmp.x;
217  x = x * (1.5F - xhalf * x * x);
218  return x;
219 }

References x.

Referenced by fastSqrt(), and Particle::updateSelf().

◆ fastSqrt()

float fastSqrt ( const float  x)
inline

Definition at line 221 of file mathutils.h.

222 {
223  return 1.0F / fastInvSqrt(x);
224 }
float fastInvSqrt(float x)
Definition: mathutils.h:210

References fastInvSqrt(), and x.

◆ fastSqrtInt()

int fastSqrtInt ( const unsigned int  n)
inline

Definition at line 245 of file mathutils.h.

246 {
247  if (n < 1000)
248  return CAST_S32(square_roots[n]);
249  return CAST_S32(sqrt(n));
250 }
#define CAST_S32
Definition: cast.h:30
static const uint8_t square_roots[1000]
Definition: mathutils.h:73

References CAST_S32, and square_roots.

Referenced by ActorManager::findMostDamagedPlayer().

◆ getCrc16()

uint16_t getCrc16 ( const std::string &  str)
inline

Definition at line 186 of file mathutils.h.

187 {
188  size_t f = str.size();
189  uint16_t crc16 = 0xffff;
190 
191  while (f != 0)
192  {
193  f --;
194  crc16 = CAST_U16(
195  crc_table[(crc16 ^ (str[f])) & 0xff] ^ (crc16 >> 8));
196  }
197 
198  return crc16;
199 }
#define CAST_U16
Definition: cast.h:29
static const uint16_t crc_table[256]
Definition: mathutils.h:37

References CAST_U16, and crc_table.

◆ powerOfTwo()

int powerOfTwo ( const unsigned int  input)
inline

Definition at line 237 of file mathutils.h.

238 {
239  unsigned int value = 1;
240  while (value < input)
241  value <<= 1;
242  return value;
243 }

Referenced by AtlasManager::createSDLAtlas().

◆ roundDouble()

int roundDouble ( const double  v)
inline

Definition at line 232 of file mathutils.h.

233 {
234  return (v > 0.0) ? CAST_S32(v + 0.5) : CAST_S32(v - 0.5);
235 }

References CAST_S32.

Referenced by SetupItemSlider2::fromWidget(), and SetupItemSlider2::toWidget().

◆ weightedAverage()

float weightedAverage ( const float  n1,
const float  n2,
const float  w 
)
inline

Definition at line 226 of file mathutils.h.

228 {
229  return w < 0.0F ? n1 : (w > 1.0F ? n2 : w * n2 + (1.0F - w) * n1);
230 }

Variable Documentation

◆ crc_table

const uint16_t crc_table[256]
static

Definition at line 37 of file mathutils.h.

Referenced by getCrc16().

◆ square_roots

const uint8_t square_roots[1000]
static

Definition at line 73 of file mathutils.h.

Referenced by fastSqrtInt().