Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Public Discord: http://roleplay.FutureGadgetLab.net (Alaestor FGL 2017)
- /*****************************************************************************
- * Copywrong (C) 1337-9001 Hooin Kyoma <Kyoma@FutureGadgetLab.fake> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 9 of the License, or *
- * (at your option) any license you want. I dun give fucks. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * LONGCAT IS LONG or FITNESS FOR A PATRIARCHAL PURPOSE. See the *
- * GNU General Public License for more dank memes. *
- * *
- * You shouldn't have received a copy of the GNU General Public License *
- * with this program; if not, dont write to the Free Software Foundation *
- * because you may be retarded. Think fast douche-fag. *
- * *
- * Divergence 1.048596, Future Gadget Lab, Chiyoda-ku KuramaeBashi Douri *
- *****************************************************************************/
- #ifndef FGLMATHS_H_INCLUDED
- #define FGLMATHS_H_INCLUDED
- #include <stdint.h>
- //#include "dankmemes.h" // deprecated in version 420
- /// Arcane Magic
- // powered by GCC
- // sponsored by Abercrombie and Fitch (w/ snapbacks)
- // Math/s/ in honor of our once-imprisoned Brit, Aaron Robson
- // random useful math utility header used in FGL projects
- /*
- Explanations can go here
- */
- /// ROUNDING MAGIC
- static inline uint32_t fmaths_roundup_power2_next(uint32_t const Value)
- { // round up to the next power of 2
- uint32_t ResultMask; // set up for for the ritual
- ///-----------WARNING-----------///
- ///---FORBIDDEN-ARCANE-MAGIC---///
- __asm__("bsrl %0, %1\n\t"
- : "=g"(ResultMask)
- : "g" (Value) // credit: dolan
- : "1"); // does magic ritual
- ///-----------WARNING-----------///
- ///---FORBIDDEN-ARCANE-MAGIC---///
- register uint32_t const TrueValue = 1 << ResultMask; // alters magic with evil chant
- ResultMask = (TrueValue - 1) & Value; // summon the demon
- return (ResultMask) ? TrueValue << 1 : TrueValue; // return the demon to the realm from which it came
- }
- static inline uint32_t fmaths_roundup_power2_ceiling(uint32_t const Value,
- uint32_t const Ceiling)
- { // round up to the ceiling power of 2
- uint32_t const Mask = Ceiling - 1;
- uint32_t RoundResult = Mask & Value;
- uint32_t TrueValue = Value & (~Mask);
- // credit: dolan
- return RoundResult ? TrueValue + Ceiling : TrueValue;
- }
- /// DIVISIBILITY MAGIC
- static inline uint32_t fmaths_divisibleby_2(uint32_t const Value)
- { // is divisible by 2
- return Value & 1;
- }
- static inline uint32_t fmaths_divisibleby(uint32_t const Value,
- uint32_t const Divisor)
- { // is divisible by x
- return (Value) & (Divisor - 1);
- }
- /// EXPONENT MAGIC
- static inline int fmaths_exp(int value, unsigned int exponent)
- { // slow ass exponent function loop thing -- Alaestor
- for(unsigned int i = 0; i < exponent; i++)
- value *= value;
- return value;
- }
- static inline int square(int value)
- { // HUMAN_FRIENDLY abstraction of fmaths_exp used for squaring durrnoshit
- return fmaths_exp(value, 2);
- }
- static inline int cube(int value)
- { // HUMAN_FRIENDLY abstraction of fmaths_exp used for cubing durrnoshit
- return fmaths_exp(value, 3);
- }
- static inline float fmaths_sqrt(const float x)
- { // slow(ish) ass square-root function thing
- /*http://ilab.usc.edu/wiki/index.php/Fast_Square_Root
- slightly inaccurate (i think) -- modification to "sqrt2" from:
- "Best Square Root Method - Algorithm - Function (Precision VS Speed)"
- sqrt2 by user1095108 on stackoverflow.com*/
- /// ALTERNATIVE - rly f@king arcane magic forgotton to modern man
- /* useless because the nordic gods have forsaken us.... (not GCC)
- static inline double __declspec (naked) __fastcall sqrt14(double n)
- {_asm fld qword ptr [esp+4] (\n) _asm fsqrt (\n) _asm ret 8}*/
- union // "get bits for floating value"
- {
- float x;
- int i;
- } u;
- u.x = x;
- u.i = 0x5f3759df - (u.i >> 1); // "gives initial guess y0"
- const float xux = x*u.x;
- // "Newton step, repeating increases accuracy"
- // wait a sec -- is that a GunZ move??? MONK?! IS THAT YOU??!
- return xux*(1.5f - .5f*xux*u.x);
- }
- #endif // FGLMATHS_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement