Newer
Older
Import / research / 3d-octrees / fixed.h
@John John on 29 Dec 2020 1010 bytes bulk import from macbookpro checkouts
/*
 *       Filename:  fixed.h
 *         Author:  John Ryland (jryland), jryland@xiaofrog.com
 *        Company:  InvertedLogic
 */


typedef signed int  Fixed;


/*
   Q Number Format:
   ---------------
   http://en.wikipedia.org/wiki/Q_(number_format)
*/
#define Q                   16.15
#define QIntegerBits        16 // (int(Q))   // AKA  'm'
#define QFractionalBits     15               // AKA  'n'
#define K                   (1 << (QFractional-1))


inline Fixed Fixed_add(Fixed a, Fixed b)
{
    return a + b;
}

inline Fixed Fixed_subtract(Fixed a, Fixed b)
{
    return a - b;
}

inline Fixed Fixed_multiply(Fixed a, Fixed b)
{
    Fixed result;
    unsigned long long temp;
    temp = (long long)a * (long long)b;
    temp += K;
    result = temp >> QFractionalBits;
    return result;
}

inline Fixed Fixed_division(Fixed a, Fixed b)
{
    Fixed result;
    unsigned long long temp;
    temp = (long long)a << QFractionalBits;
    temp += b / 2;
    result = temp  / b;
    return result;
}