// Define our target (MC6809):
#define USE_MUL

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
//-------------------------------------------- N2.14 FIXED POINT PACKAGE

//
// Original fp14 code written by: Robert C. Pendleton
// 
// Copyright 1993 by Robert C. Pendleton, all right reserved
//
// Non-commercial use by individuals is permitted.
//
//

typedef int16_t fp14; // Signed fixed point [-2.0:2.0)

//----------------------------------------------
//
// Add and subtract fixed point numbers.
//

#define fp14_add(a1, a2) ((a1) + (a2))
#define fp14_sub(s1, s2) ((s1) - (s2))

//----------------------------------------------
//
// Convert an integer to n N.14 fixed point 
// number and back.
//

#define int16_to_fp14(x) ((x) << (int16_t)14)
#define fp14_to_int16(x) ((x) >> (int16_t)14)

//----------------------------------------------
//
// Get the fractional part of an N.14 fixed point
// number.

#define fp14_fractpt(x) ((x) & ((1<<(int16_t)14)-1))
#define fp14_intpt(x) ((x) & ~((1<<(int16_t)14)-1))

#define fp14_ONE (1<<(int16_t)14)
#define fp14_MINUS_ONE ((-1)<<(int16_t)14)

//----------------------------------------------
//
// Table based fixed point sine and cosine 
// functions.
//

const fp14 qsine[65] = {
     0,     402,    803,   1205,   1605,   2005,   2404,   2801,
  3196,    3589,   3980,   4369,   4756,   5139,   5519,   5896,
  6269,    6639,   7005,   7366,   7723,   8075,   8423,   8765,
  9102,    9434,   9759,  10079,  10393,  10701,  11002,  11297,
  11585,  11866,  12139,  12406,  12665,  12916,  13159,  13395,
  13622,  13842,  14053,  14255,  14449,  14634,  14810,  14978,
  15136,  15286,  15426,  15557,  15678,  15790,  15892,  15985,
  16069,  16142,  16206,  16260,  16305,  16339,  16364,  16379,
  16384,
};

static inline fp14 hsine(uint8_t x) {
  return x>=64 ? qsine[128-x] : qsine[x];
}

static inline fp14 sine(uint8_t x) {
  return x&0x80 ? -hsine(x&0x7f) : hsine(x);
}

static inline fp14 fpSin(uint8_t x) {
  return sine((uint8_t)(x));
}

static inline fp14 fpCos(uint8_t x) {
  return sine((uint8_t)((x)+64));
}

static inline int8_t sin127(int8_t x) {
  return (int8_t)(sine((uint8_t)x&255U)>>7);
}

static inline int8_t cos127(int8_t x) {
  return (int8_t)(sine(((uint8_t)x+64U)&255U)>>7);
}

static inline fp14 mult_sin(fp14 x, uint8_t angle) {
  int32_t product;
  product = x * sine(angle);
  product >>= 14L;
  return (fp14)product;
}

static inline fp14 mult_cos(fp14 x, uint8_t angle) {
  int32_t product;
  angle += 64;
  product = x * sine(angle);
  product >>= 14L;
  return (fp14)product;
}

fp14 fp14_divi(fp14 d1, fp14 d2) {
  // tested on LCC on windows and GCC on 64 bit linux, but still may not be portable.  Signedness is an issue.
  return ((fp14)((((int32_t)d1<<(int32_t)14) / (int32_t)d2)<<(int32_t)14));
}

fp14 fp14_muli(fp14 m1, fp14 m2) {
  return ((fp14)(((int32_t)m1 * (int32_t)m2)>>(int32_t)14));
}

#define FP14_MIN 0x0001

static inline int16_t sdiv10_16_with_remainder(int16_t A, int16_t *Remainder) {
  int16_t Q; /* the quotient */
  int16_t R; /* the remainder */

  // Avoid using divide:
  Q = ((A >> 1) + A) >> 1; /* Q = A*0.11 */
  Q = ((Q >> 4) + Q)     ; /* Q = A*0.110011 */
  Q = ((Q >> 8) + Q) >> 3; /* Q = A*0.00011001100110011 */
  /* either Q = A/10 or Q+1 = A/10 for -87,381 < A < 534,890 */

#ifdef USE_MUL  // Need to time both cases on 6809 to see which is faster.
  R = Q * 10;
#else
  R = ((Q << 2) + Q) << 1;
#endif
  R = -R + A; /* R = A - 10*Q */
  if (R >= 10) { R -= 10; Q += 1; }   /* Q = A/10 for -87,381 < A < 534,890 */
  if ((Q < 0) && (R != 0)) { R -= 10; Q += 1; }
  *Remainder = R;
  return Q;
}

static inline void fp14s(char *s, fp14 Fixed) {
  int16_t i, rem;
  if (Fixed < 0) {
    *s++ = '-';
    *s = abs(Fixed>>(int16_t)14)+'0'; // [-2:1]
    if (fp14_fractpt(Fixed) != 0) *s -= 1;
    s++;
  } else {
    *s++ = ' ';
    *s++ = abs(Fixed>>(int16_t)14)+'0'; // [-2:1]
  }
  *s++ = '.';
  if (Fixed < 0) Fixed = fp14_fractpt(-Fixed); else Fixed = fp14_fractpt(Fixed);
  i = (int16_t)(((int32_t)Fixed*(int32_t)10000)>>(int32_t)14); // 0000 : 9999
  s += 4;
  *s-- = '\0';
  i = sdiv10_16_with_remainder(i, &rem); *s-- = rem+'0';
  i = sdiv10_16_with_remainder(i, &rem); *s-- = rem+'0';
  i = sdiv10_16_with_remainder(i, &rem); *s-- = rem+'0';
  *s = i+'0';
}

char *print_fp(fp14 Fixed) {
#define MAX_FP14STR (1/*sign or ' '*/+1/*integer digit*/+1/*decimal point*/+5/*decimal digits*/+1/*terminating NUL*/)
#define MAX_HACK (1<<5)
  static char buff[MAX_FP14STR][MAX_HACK]; // (in case of multiple calls in a printf...)
  static int next_buff = 0;
  int this_buff = next_buff; next_buff = (next_buff+1) & (MAX_HACK-1);
  fp14s(&buff[0][this_buff], Fixed);
  return &buff[0][this_buff];
}

fp14 Sin(fp14 Angle) {  // INTERPOLATED Sin.  Accurate to 0.0003
  Angle = fp14_fractpt(Angle);
  uint8_t angle256 = Angle >> (14-8); // reduce to 256 values
  fp14 bigsin1 = fpSin(angle256);
  fp14 bigsin2 = fpSin((angle256+1)&255);
  fp14 small = Angle & ((1<<6)-1); // remaining low 6 bits...
  fp14 diff = bigsin2-bigsin1;
  diff = (diff * small ) >> 6;  // (diff * small) / (1<<6)
  return bigsin1 + diff;
}

fp14 Cos(fp14 Angle) {  // Angle is an N2.14 fixed-point number >= 0.0 and < 1.0
  return Sin(Angle + (fp14_ONE>>2));
}

#ifdef UNIT_TEST
int main(int argc, char **argv) {
  int i;
  fp14 testfp;

  // The smallest quantity we can express in fp14 is 0.0000610388817677
  // so we will try to obtain an accuracy of +/- 0.0001 if we can.
  // (We didn't acheive that but 0.0003 is quite close.)
  
  testfp = fp14_ONE/10; fprintf(stderr, "0.1 = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));
  testfp = -fp14_ONE/10; fprintf(stderr, "-0.1 = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));

  fprintf(stderr, "\n");

  testfp = 0x3FFF | (1<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 01
  testfp = 0x2000 | (1<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 01
  testfp = 0x1FFF | (1<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 01
  testfp = 0x0FFF | (1<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 01  1
  testfp = 0x0000 | (1<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 01  1
  testfp = 0x3FFF;           fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 00
  testfp = 0x0000;           fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 00  0
  testfp = 0x3FFF | (3<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 11
  testfp = 0x0000 | (3<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 11 -1
  testfp = 0x3FFF | (2<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 10
  testfp = 0x0000 | (2<<14); fprintf(stderr, "testfp = 0x%04X = %s;\n", 0xFFFF&(int16_t)testfp, print_fp(testfp));  // 10 -2

  fprintf(stderr, "\n");
  
  testfp = Sin(0x0000) ; /* 0 degrees */ fprintf(stderr, "Sin(0) = %s;\n", print_fp(testfp));
  testfp = Sin(0x1000/3) ; /* 0 degrees */ fprintf(stderr, "Sin(30) = %s;\n", print_fp(testfp));
  testfp = Sin(0x0800) ; /* 0 degrees */ fprintf(stderr, "Sin(45) = %s;\n", print_fp(testfp));
  testfp = Sin(0x2000/3) ; /* 0 degrees */ fprintf(stderr, "Sin(60) = %s;\n", print_fp(testfp));
  testfp = Sin(0x1000) ; /* 0 degrees */ fprintf(stderr, "Sin(90) = %s;\n", print_fp(testfp));
  testfp = Sin(0x1800) ; /* 0 degrees */ fprintf(stderr, "Sin(135) = %s;\n", print_fp(testfp));
  testfp = Sin(0x2000) ; /* 0 degrees */ fprintf(stderr, "Sin(180) = %s;\n", print_fp(testfp));
  testfp = Sin(0x2800) ; /* 0 degrees */ fprintf(stderr, "Sin(225) = %s;\n", print_fp(testfp));
  testfp = Sin(0x3000) ; /* 0 degrees */ fprintf(stderr, "Sin(270) = %s;\n", print_fp(testfp));
  testfp = Sin(0x3800) ; /* 0 degrees */ fprintf(stderr, "Sin(315) = %s;\n", print_fp(testfp));

  fprintf(stderr, "\n");
  
  testfp = Cos(0x0000) ; /* 0 degrees */ fprintf(stderr, "Cos(0) = %s;\n", print_fp(testfp));
  testfp = Cos(0x1000/3) ; /* 0 degrees */ fprintf(stderr, "Cos(30) = %s;\n", print_fp(testfp));
  testfp = Cos(0x0800) ; /* 0 degrees */ fprintf(stderr, "Cos(45) = %s;\n", print_fp(testfp));
  testfp = Cos(0x2000/3) ; /* 0 degrees */ fprintf(stderr, "Cos(60) = %s;\n", print_fp(testfp));
  testfp = Cos(0x1000) ; /* 0 degrees */ fprintf(stderr, "Cos(90) = %s;\n", print_fp(testfp));
  testfp = Cos(0x1800) ; /* 0 degrees */ fprintf(stderr, "Cos(135) = %s;\n", print_fp(testfp));
  testfp = Cos(0x2000) ; /* 0 degrees */ fprintf(stderr, "Cos(180) = %s;\n", print_fp(testfp));
  testfp = Cos(0x2800) ; /* 0 degrees */ fprintf(stderr, "Cos(225) = %s;\n", print_fp(testfp));
  testfp = Cos(0x3000) ; /* 0 degrees */ fprintf(stderr, "Cos(270) = %s;\n", print_fp(testfp));
  testfp = Cos(0x3800) ; /* 0 degrees */ fprintf(stderr, "Cos(315) = %s;\n", print_fp(testfp));
  exit(0);
}
#endif // UNIT_TEST
