aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/arithmetic.h
blob: 0069df654fc30382c8329bff8f8bd6299e0c4ab4 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifndef ARITHMETIC_H_
#define ARITHMETIC_H_

#include "util.h"


#define ARITHMETIC_ASM_AVR8	1


/* Saturated unsigned addition (32 bit). */
static inline uint32_t add_sat_u32(uint32_t a, uint32_t b)
{
#if ARITHMETIC_ASM_AVR8
	__asm__ __volatile__(
		"add %A0, %A1 \n"
		"adc %B0, %B1 \n"
		"adc %C0, %C1 \n"
		"adc %D0, %D1 \n"
		"brcc 1f \n"
		"ldi %A0, 0xFF \n"
		"ldi %B0, 0xFF \n"
		"ldi %C0, 0xFF \n"
		"ldi %D0, 0xFF \n"
		"1: \n"
		: "+d" (a)
		: "d" (b)
	);
	return a;
#else
	uint32_t x = (uint32_t)(a + b);
	if (x < a)
		return UINT32_MAX;
	return x;
#endif
}

#ifdef uint24_t
/* Saturated unsigned addition (24 bit). */
static inline uint24_t add_sat_u24(uint24_t a, uint24_t b)
{
#if ARITHMETIC_ASM_AVR8
	__asm__ __volatile__(
		"add %A0, %A1 \n"
		"adc %B0, %B1 \n"
		"adc %C0, %C1 \n"
		"brcc 1f \n"
		"ldi %A0, 0xFF \n"
		"ldi %B0, 0xFF \n"
		"ldi %C0, 0xFF \n"
		"1: \n"
		: "+d" (a)
		: "d" (b)
	);
	return a;
#else
	uint24_t x = (uint24_t)(a + b);
	if (x < a)
		return (uint24_t)0xFFFFFFlu;
	return x;
#endif
}
#endif /* uint24_t */

/* Saturated unsigned addition (16 bit). */
static inline uint16_t add_sat_u16(uint16_t a, uint16_t b)
{
#if ARITHMETIC_ASM_AVR8
	__asm__ __volatile__(
		"add %A0, %A1 \n"
		"adc %B0, %B1 \n"
		"brcc 1f \n"
		"ldi %A0, 0xFF \n"
		"ldi %B0, 0xFF \n"
		"1: \n"
		: "+d" (a)
		: "d" (b)
	);
	return a;
#else
	uint16_t x = (uint16_t)(a + b);
	if (x < a)
		return UINT16_MAX;
	return x;
#endif
}

/* Saturated unsigned addition (8 bit). */
static inline uint8_t add_sat_u8(uint8_t a, uint8_t b)
{
#if ARITHMETIC_ASM_AVR8
	if (__builtin_constant_p(b) && (b == 1u)) {
		__asm__ __volatile__(
			"inc %0 \n"
			"brne 1f \n"
			"com %0 \n"
			"1: \n"
			: "+r" (a)
			: /* none */
		);
	} else {
		__asm__ __volatile__(
			"add %0, %1 \n"
			"brcc 1f \n"
			"ldi %0, 0xFF \n"
			"1: \n"
			: "+d" (a)
			: "d" (b)
		);
	}
	return a;
#else
	uint8_t x = (uint8_t)(a + b);
	if (x < a)
		return UINT8_MAX;
	return x;
#endif
}

/* Saturated unsigned subtraction (32 bit). */
static inline uint32_t sub_sat_u32(uint32_t a, uint32_t b)
{
#if ARITHMETIC_ASM_AVR8
	__asm__ __volatile__(
		"sub %A0, %A1 \n"
		"sbc %B0, %B1 \n"
		"sbc %C0, %C1 \n"
		"sbc %D0, %D1 \n"
		"brcc 1f \n"
		"clr %A0 \n"
		"clr %B0 \n"
		"clr %C0 \n"
		"clr %D0 \n"
		"1: \n"
		: "+r" (a)
		: "r" (b)
	);
	return a;
#else
	uint32_t x = (uint32_t)(a - b);
	if (x > a)
		return 0;
	return x;
#endif
}

#ifdef uint24_t
/* Saturated unsigned subtraction (24 bit). */
static inline uint24_t sub_sat_u24(uint24_t a, uint24_t b)
{
#if ARITHMETIC_ASM_AVR8
	__asm__ __volatile__(
		"sub %A0, %A1 \n"
		"sbc %B0, %B1 \n"
		"sbc %C0, %C1 \n"
		"brcc 1f \n"
		"clr %A0 \n"
		"clr %B0 \n"
		"clr %C0 \n"
		"1: \n"
		: "+r" (a)
		: "r" (b)
	);
	return a;
#else
	uint24_t x = (uint24_t)(a - b);
	if (x > a)
		return 0;
	return x;
#endif
}
#endif /* uint24_t */

/* Saturated unsigned subtraction (16 bit). */
static inline uint16_t sub_sat_u16(uint16_t a, uint16_t b)
{
#if ARITHMETIC_ASM_AVR8
	if (__builtin_constant_p(b)) {
		__asm__ __volatile__(
			"sbiw %A0, %A1 \n"
			"brcc 1f \n"
			"clr %A0 \n"
			"clr %B0 \n"
			"1: \n"
			: "+w" (a)
			: "I" (b)
		);
	} else {
		__asm__ __volatile__(
			"sub %A0, %A1 \n"
			"sbc %B0, %B1 \n"
			"brcc 1f \n"
			"clr %A0 \n"
			"clr %B0 \n"
			"1: \n"
			: "+r" (a)
			: "r" (b)
		);
	}
	return a;
#else
	uint16_t x = (uint16_t)(a - b);
	if (x > a)
		return 0;
	return x;
#endif
}

/* Saturated unsigned subtraction (8 bit). */
static inline uint8_t sub_sat_u8(uint8_t a, uint8_t b)
{
#if ARITHMETIC_ASM_AVR8
	if (__builtin_constant_p(b)) {
		__asm__ __volatile__(
			"subi %0, %1 \n"
			"brcc 1f \n"
			"clr %0 \n"
			"1: \n"
			: "+d" (a)
			: "M" (b)
		);
	} else {
		__asm__ __volatile__(
			"sub %0, %1 \n"
			"brcc 1f \n"
			"clr %0 \n"
			"1: \n"
			: "+r" (a)
			: "r" (b)
		);
	}
	return a;
#else
	uint8_t x = (uint8_t)(a - b);
	if (x > a)
		return 0;
	return x;
#endif
}


#endif /* ARITHMETIC_H_ */
bues.ch cgit interface