aboutsummaryrefslogtreecommitdiff
path: root/include/byte_math.h
blob: 9be4b67e972c62ad540149fdb8807beb350daf04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma once

#include <stdint.h>

/* Library for doing fast math functions using just uint8_t. */

extern uint8_t sintable[256];

/* Returns ((sin(n) + 1) / 2) * 255 */
static inline uint8_t byte_sin(uint8_t n)
{
  return sintable[n];
}

uint8_t calc_w(uint8_t n);

static inline uint8_t byte_scale(uint8_t v, uint8_t scale)
{
  uint16_t acc = v;
  return (acc * scale) >> 8;
}

static inline uint8_t clip(int x)
{
  if (x > 240) {
    return 240;
  }

  if (x < 0) {
    return 0;
  }

  return (uint8_t)x;
}

#define AS_BYTE(n) ((n) * 256)