-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_point.cpp
More file actions
43 lines (43 loc) · 2.76 KB
/
Copy pathfixed_point.cpp
File metadata and controls
43 lines (43 loc) · 2.76 KB
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
37
38
39
40
41
42
43
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Project: Patchworks //
// Author: Jeffrey Bednar //
// Copyright (c) Illusion Interactive, 2011 - 2026. //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "Headers/fixed_point.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline fixed16_t toFixed(int32_t value, uint8_t shift) {
return value << shift;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline int32_t fromFixed(fixed16_t value, uint8_t shift) {
return value >> shift;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline fixed16_t fixedMultiply(fixed16_t a, fixed16_t b, uint8_t shift) {
// Upscale to avoid truncating.
return (fixed16_t)(((int64_t)a * b) >> shift);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline fixed16_t fixedDivide(fixed16_t a, fixed16_t b, uint8_t shift) {
// Upscale to avoid truncating.
return (fixed16_t)(((int64_t)a << shift) / b);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline fixed16_t extractComponentAsFixed(fixed16_t value, uint8_t shift) {
return (value >> shift) << shift;
//return value & ~(value - 1); // Subtract for 0xFF... invert and mask.
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline uint32_t extractFractionalAsNatural(fixed16_t value, uint8_t shift) {
return (uint32_t)value & ((1U << shift) - 1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline float fixedToFloat(fixed16_t value, uint8_t shift) {
return (float)value / (1 << shift);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline fixed16_t fixedLog(fixed16_t value, uint8_t shift) {
// Approximate/look up table with interpolation.
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////