sierrahotel/pc/include/fmath.h
2024-05-26 20:08:09 -05:00

86 lines
2.3 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FMATH_H
#define FMATH_H
#define FAUX_FIXED
#ifdef FAUX_FIXED
#include <math.h>
// Fake fixed point for debugging while porting.
typedef double fixT;
#define fixAdd(x, y) ((x) + (y))
#define fixAtan(x) atan(x)
#define fixCos(x) cos(x)
#define fixDegs(x) ((x) * 180 / M_PI)
#define fixDiv(x, y) ((x) / (y))
#define fixMul(x, y) ((x) * (y))
#define fixSin(x) sin(x)
#define fixSub(x, y) ((x) - (y))
#define fixToF(x) (x)
#define fixToI(x) ((int)(x))
#define fToFix(x) (x)
#define iToFix(x) ((fixT)(x))
#else // FAUX_FIXED
#include <stdint.h>
// Ratios for converting between radians and fixed point angles.
#define fixToRad 1608 // 2pi/256
#define radToFix 2670177 // 256/2pi
typedef int32_t fixT;
fixT fixAdd(fixT x, fixT y);
fixT fixAtan(fixT x);
fixT fixCos(fixT x);
//fixT fixDegs(fixT x);
fixT fixDiv(fixT x, fixT y);
fixT fixMul(fixT x, fixT y);
fixT fixSin(fixT x);
fixT fixSub(fixT x, fixT y);
double fixToF(fixT x);
int16_t fixToI(fixT x);
fixT fToFix(double x);
fixT iToFix(int16_t x);
#endif // FAUX_FIXED
#endif // FMATH_H