/**
 * qmath.h
 *
 * Fixed point math functions / defines.
 * 
 * Copyright (c) 2022 Bigscreen, Inc.
 */


#ifndef QMATH_H_
#define QMATH_H_

/************************************************************************ 
 * Helper library for fixed point math.
 ************************************************************************/

// Type definition for 16-bit fractional value.
// Expressed as a s.15 bit number. First bit is
// sign, next 15 bits are fractions.
// So...regular int16 goes from -32768 to 32767
// This is mapped to -1 to +(1 - 1e-15)

typedef int16_t		q16_t;
#define Q_BITS		(15)
#define Q16_MAX		(32767) // 0x7FFF, maximum value = 0.999969482
#define Q16_MIN		(-32768) // 0x8000, minimum value = -1

inline q16_t qmult(q16_t x, q16_t y) {
	return ((int32_t)x * (int32_t)y) >> Q_BITS;
}


#endif /* QMATH_H_ */
