#ifndef __basicmaths_h__
#define __basicmaths_h__
#include <stdlib.h>
#include <stdint.h>
#include "heap.h"
#define _USE_MATH_DEFINES
#ifdef ARM_CORTEX
#include "arm_math.h"
#endif
#ifdef __cplusplus
#include <cmath>
#include <algorithm>
using std::min;
using std::max;
using std::abs;
using std::clamp;
#else
#include <math.h>
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
#ifndef max
#define max(a,b) ((a)>(b)?(a):(b))
#endif
#ifndef abs
#define abs(x) ((x)>0?(x):-(x))
#endif
#ifndef clamp
#define clamp(x, lo, hi) ((x)>(hi)?(hi):((x)<(lo)?(lo):(x)))
#endif
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880
#endif
#ifdef __cplusplus
extern "C" {
#endif
float arm_sqrtf(float in);
void arm_srand32(uint32_t s);
uint32_t arm_rand32();
float fast_powf(float x, float y);
float fast_expf(float x);
float fast_exp2f(float x);
float fast_exp10f(float x);
void fast_pow_set_table(const uint32_t* table, int size);
float fast_logf(float x);
float fast_log2f(float x);
float fast_log10f(float x);
uint32_t fast_log2i(uint32_t x);
void fast_log_set_table(const float* table, int size);
float fast_atan2f(float a, float b);
float randf();
float fast_fmodf(float x, float y);
#ifdef ARM_CORTEX
#define malloc(x) pvPortMalloc(x)
#define calloc(x, y) pvPortCalloc(x, y)
#define free(x) vPortFree(x)
#define realloc(x, y) pvPortRealloc(x, y);
void* pvPortCalloc(size_t nmemb, size_t size);
void* pvPortRealloc(void *pv, size_t xWantedSize);
#endif
#ifdef __cplusplus
}
#endif
#ifdef ARM_CORTEX
#define sin(x) arm_sin_f32(x)
#define sinf(x) arm_sin_f32(x)
#define cos(x) arm_cos_f32(x)
#define cosf(x) arm_cos_f32(x)
#define sqrt(x) sqrtf(x)
#define rand() arm_rand32()
#ifdef __FAST_MATH__
#define pow(x, y) fast_powf(x, y)
#define powf(x, y) fast_powf(x, y)
#define exp(x) fast_expf(x)
#define expf(x) fast_expf(x)
#define exp2(x) fast_exp2f(x)
#define exp2f(x) fast_exp2f(x)
#define exp10(x) fast_exp10f(x)
#define exp10f(x) fast_exp10f(x)
#ifdef log2
#undef log2
#endif
#define log(x) fast_logf(x)
#define logf(x) fast_logf(x)
#define log2(x) fast_log2f(x)
#define log2f(x) fast_log2f(x)
#define log10(x) fast_log10f(x)
#define log10f(x) fast_log10f(x)
#else
#define exp10(x) powf(10, x)
#define exp10f(x) powf(10, x)
#endif
#undef RAND_MAX
#define RAND_MAX UINT32_MAX
#endif
#endif