ManaPlus
Functions | Variables
mathutils.h File Reference

(642df39)

#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 209 of file mathutils.h.

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

References x.

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

◆ fastSqrt()

float fastSqrt ( const float  x)
inline

Definition at line 220 of file mathutils.h.

221 {
222  return 1.0F / fastInvSqrt(x);
223 }

References fastInvSqrt(), and x.

◆ fastSqrtInt()

int fastSqrtInt ( const unsigned int  n)
inline

Definition at line 244 of file mathutils.h.

245 {
246  if (n < 1000)
247  return CAST_S32(square_roots[n]);
248  return CAST_S32(sqrt(n));
249 }

References CAST_S32, and square_roots.

Referenced by ActorManager::findMostDamagedPlayer().

◆ getCrc16()

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

Definition at line 185 of file mathutils.h.

186 {
187  size_t f = str.size();
188  uint16_t crc16 = 0xffff;
189 
190  while (f != 0)
191  {
192  f --;
193  crc16 = CAST_U16(
194  crc_table[(crc16 ^ (str[f])) & 0xff] ^ (crc16 >> 8));
195  }
196 
197  return crc16;
198 }

References CAST_U16, and crc_table.

◆ powerOfTwo()

int powerOfTwo ( const unsigned int  input)
inline

Definition at line 236 of file mathutils.h.

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

Referenced by AtlasManager::createSDLAtlas().

◆ roundDouble()

int roundDouble ( const double  v)
inline

Definition at line 231 of file mathutils.h.

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

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 225 of file mathutils.h.

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

Variable Documentation

◆ crc_table

const uint16_t crc_table[256]
static

Definition at line 36 of file mathutils.h.

Referenced by getCrc16().

◆ square_roots

const uint8_t square_roots[1000]
static

Definition at line 72 of file mathutils.h.

Referenced by fastSqrtInt().

square_roots
static const uint8_t square_roots[1000]
Definition: mathutils.h:72
crc_table
static const uint16_t crc_table[256]
Definition: mathutils.h:36
x
x
Definition: graphics_calcImageRect.hpp:72
CAST_U16
#define CAST_U16
Definition: cast.h:28
CAST_S32
#define CAST_S32
Definition: cast.h:29
fastInvSqrt
float fastInvSqrt(float x)
Definition: mathutils.h:209