Advertisement
alaestor

[FGL Utility] Maths Header

Mar 28th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. // Public Discord: http://roleplay.FutureGadgetLab.net (Alaestor FGL 2017)
  2.  
  3. /*****************************************************************************
  4.  *   Copywrong (C) 1337-9001 Hooin Kyoma <Kyoma@FutureGadgetLab.fake>        *
  5.  *                                                                           *
  6.  *   This program is free software; you can redistribute it and/or modify    *
  7.  *   it under the terms of the GNU General Public License as published by    *
  8.  *   the Free Software Foundation; either version 9 of the License, or       *
  9.  *   (at your option) any license you want. I dun give fucks.                *
  10.  *                                                                           *
  11.  *   This program is distributed in the hope that it will be useful,         *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
  13.  *   LONGCAT IS LONG or FITNESS FOR A PATRIARCHAL PURPOSE.  See the          *
  14.  *   GNU General Public License for more dank memes.                         *
  15.  *                                                                           *
  16.  *   You shouldn't have received a copy of the GNU General Public License    *
  17.  *   with this program; if not, dont write to the Free Software Foundation   *
  18.  *   because you may be retarded. Think fast douche-fag.                     *
  19.  *                                                                           *
  20.  *   Divergence 1.048596, Future Gadget Lab, Chiyoda-ku KuramaeBashi Douri   *
  21.  *****************************************************************************/
  22.  
  23. #ifndef FGLMATHS_H_INCLUDED
  24. #define FGLMATHS_H_INCLUDED
  25.  
  26. #include <stdint.h>
  27. //#include "dankmemes.h" // deprecated in version 420
  28.  
  29. /// Arcane Magic
  30. // powered by GCC
  31. // sponsored by Abercrombie and Fitch (w/ snapbacks)
  32. // Math/s/ in honor of our once-imprisoned Brit, Aaron Robson
  33. // random useful math utility header used in FGL projects
  34.  
  35. /*
  36.     Explanations can go here
  37. */
  38.  
  39. /// ROUNDING MAGIC
  40. static inline uint32_t fmaths_roundup_power2_next(uint32_t const Value)
  41. { // round up to the next power of 2
  42.     uint32_t ResultMask; // set up for for the ritual
  43.  
  44. ///-----------WARNING-----------///
  45. ///---FORBIDDEN-ARCANE-MAGIC---///
  46.     __asm__("bsrl %0, %1\n\t"
  47.         : "=g"(ResultMask)
  48.         : "g" (Value) // credit: dolan
  49.         : "1"); // does magic ritual
  50. ///-----------WARNING-----------///
  51. ///---FORBIDDEN-ARCANE-MAGIC---///
  52.  
  53.     register uint32_t const TrueValue = 1 << ResultMask; // alters magic with evil chant
  54.  
  55.     ResultMask = (TrueValue - 1) & Value; // summon the demon
  56.  
  57.     return (ResultMask) ? TrueValue << 1 : TrueValue; // return the demon to the realm from which it came
  58. }
  59.  
  60. static inline uint32_t fmaths_roundup_power2_ceiling(uint32_t const Value,
  61.                                                      uint32_t const Ceiling)
  62. { // round up to the ceiling power of 2
  63.     uint32_t const Mask = Ceiling - 1;
  64.     uint32_t RoundResult = Mask & Value;
  65.     uint32_t TrueValue = Value & (~Mask);
  66.     // credit: dolan
  67.     return RoundResult ? TrueValue + Ceiling : TrueValue;
  68. }
  69.  
  70.  
  71. /// DIVISIBILITY MAGIC
  72. static inline uint32_t fmaths_divisibleby_2(uint32_t const Value)
  73. { // is divisible by 2
  74.     return Value & 1;
  75. }
  76.  
  77. static inline uint32_t fmaths_divisibleby(uint32_t const Value,
  78.                                           uint32_t const Divisor)
  79. { // is divisible by x
  80.     return (Value) & (Divisor - 1);
  81. }
  82.  
  83.  
  84. /// EXPONENT MAGIC
  85. static inline int fmaths_exp(int value, unsigned int exponent)
  86. { // slow ass exponent function loop thing -- Alaestor
  87.     for(unsigned int i = 0; i < exponent; i++)
  88.         value *= value;
  89.  
  90.     return value;
  91. }
  92.  
  93. static inline int square(int value)
  94. { // HUMAN_FRIENDLY abstraction of fmaths_exp used for squaring durrnoshit
  95.     return fmaths_exp(value, 2);
  96. }
  97.  
  98. static inline int cube(int value)
  99. { // HUMAN_FRIENDLY abstraction of fmaths_exp used for cubing durrnoshit
  100.     return fmaths_exp(value, 3);
  101. }
  102.  
  103. static inline float fmaths_sqrt(const float x)
  104. { // slow(ish) ass square-root function thing
  105.     /*http://ilab.usc.edu/wiki/index.php/Fast_Square_Root
  106.     slightly inaccurate (i think) -- modification to "sqrt2" from:
  107.     "Best Square Root Method - Algorithm - Function (Precision VS Speed)"
  108.     sqrt2 by user1095108 on stackoverflow.com*/
  109.  
  110.     /// ALTERNATIVE - rly f@king arcane magic forgotton to modern man
  111.     /* useless because the nordic gods have forsaken us.... (not GCC)
  112.     static inline double __declspec (naked) __fastcall sqrt14(double n)
  113.     {_asm fld qword ptr [esp+4] (\n) _asm fsqrt (\n) _asm ret 8}*/
  114.  
  115.     union // "get bits for floating value"
  116.     {
  117.     float x;
  118.     int i;
  119.     } u;
  120.  
  121.     u.x = x;
  122.     u.i = 0x5f3759df - (u.i >> 1);  // "gives initial guess y0"
  123.  
  124.     const float xux = x*u.x;
  125.  
  126.     // "Newton step, repeating increases accuracy"
  127.     // wait a sec -- is that a GunZ move??? MONK?! IS THAT YOU??!
  128.     return xux*(1.5f - .5f*xux*u.x);
  129. }
  130.  
  131. #endif // FGLMATHS_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement