/
SatS8.cpp
177 lines (153 loc) · 3 KB
/
SatS8.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "SatS8.h"
#include <stdint.h>
SatS8::operator int8_t(void) const
{
return this->value;
}
SatS8 & SatS8::operator++(void)
{
asm volatile
(
"INC %A[result] \n"
"BRPL end%= \n"
"LDI %A[result], 0x7F \n"
"end%=: \n"
:
[result] "+d" (this->value)
);
return *this;
}
SatS8 & SatS8::operator--(void)
{
asm volatile
(
"SBCI %A[result], 1 \n"
"BRMI end%= \n"
"LDI %A[result], 0x80 \n"
"end%=: \n"
:
[result] "+d" (this->value)
);
return *this;
}
SatS8 operator+(const SatS8 & left, const SatS8 & right)
{
SatS8 result;
asm volatile
(
"MOV %A[result], %A[left] \n"
"CLV \n"
"ADC %A[result], %A[right] \n"
"BRVC end%= \n"
"LDI %A[result], 0x7F \n"
"end%=: \n"
:
[result] "+d" (result)
:
[left] "r" (left),
[right] "r" (right)
);
return result;
}
SatS8 operator-(const SatS8 & left, const SatS8 & right)
{
SatS8 result;
asm volatile
(
"MOV %A[result], %A[left] \n"
"CLV \n"
"SBC %A[result], %A[right] \n"
"BRVC end%= \n"
"LDI %A[result], 0x80 \n"
"end%=: \n"
:
[result] "+d" (result)
:
[left] "r" (left),
[right] "r" (right)
);
return result;
}
SatS8 operator*(const SatS8 & left, const SatS8 & right)
{
SatS8 result;
int8_t temp;
int8_t high;
int8_t low;
asm volatile
(
"MULS %A[left], %A[right] \n"
"MOV %A[high], r1 \n"
"MOV %A[low], r0 \n"
"TST %A[high] \n"
"BREQ highIsZero%= \n"
"CPI %A[high], 0xFF \n"
"BREQ highIsFull%= \n"
"else%=: \n"
// Return low
"MOV %A[result], %A[low] \n"
"RJMP end%= \n"
"highIsZero%=: \n"
"TST %A[low] \n"
"BRMI lowIsNeg1%= \n"
// If low not negative
// Return low
"MOV %A[result], %A[low] \n"
"RJMP end%= \n"
// If low negative
"lowIsNeg1%=: \n"
// Return 127
"LDI %A[result], 0x7F \n"
"RJMP end%= \n"
"highIsFull%=: \n"
"TST %A[low] \n"
"BRMI lowIsNeg2%= \n"
// If low not negative
// Return -128
"LDI %A[result], 0x80 \n"
"RJMP end%= \n"
// If low negative
"lowIsNeg2%=: \n"
// Return low
"MOV %A[result], %A[low] \n"
"RJMP end%= \n"
"end%=: \n"
"CLR r1 \n"
/*"CPI %A[temp], 0 \n"
"BRNE overflowed%= \n"
"MOV %A[result], r0 \n"
"RJMP end%= \n"
"overflowed%=: \n"
"SBR %A[result], 0xFF \n"*/
:
[result] "+r" (result),
[temp] "+d" (temp),
[high] "+d" (high),
[low] "+d" (low)
:
[left] "r" (left),
[right] "r" (right)
:
"r0", "r1"
);
return result;
}
#define SatS8Op_SatS8_int8_t(op) \
SatS8 operator op(const SatS8 & left, const int8_t & right) \
{ \
return left op SatS8(right); \
}
#define SatS8Op_int8_t_SatS8(op) \
SatS8 operator op(const int8_t & left, const SatS8 & right) \
{ \
return SatS8(left) op right; \
}
#define SatS8Op(op) \
SatS8Op_SatS8_int8_t(op) \
SatS8Op_int8_t_SatS8(op)
SatS8Op( + )
SatS8Op( - )
SatS8Op( * )
#undef SatS8Op
#undef SatS8Op_int8_t_SatS8
#undef SatS8Op_SatS8_int8_t