Debian Codegraph @

libgcc/config/avr/lib1funcs-fixed.S

raw
1 /* -*- Mode: Asm -*- */
2 ;; Copyright (C) 2012-2025 Free Software Foundation, Inc.
3 ;; Contributed by Sean D'Epagnier ([email protected])
4 ;; Georg-Johann Lay ([email protected])
5
6 ;; This file is free software; you can redistribute it and/or modify it
7 ;; under the terms of the GNU General Public License as published by the
8 ;; Free Software Foundation; either version 3, or (at your option) any
9 ;; later version.
10
11 ;; In addition to the permissions in the GNU General Public License, the
12 ;; Free Software Foundation gives you unlimited permission to link the
13 ;; compiled version of this file into combinations with other programs,
14 ;; and to distribute those combinations without any restriction coming
15 ;; from the use of this file. (The General Public License restrictions
16 ;; do apply in other respects; for example, they cover modification of
17 ;; the file, and distribution when not linked into a combine
18 ;; executable.)
19
20 ;; This file is distributed in the hope that it will be useful, but
21 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 ;; General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program; see the file COPYING. If not, write to
27 ;; the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
29
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 ;; Fixed point library routines for AVR
32 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33
34 .section .text.libgcc.fixed, "ax", @progbits
35
36 #ifndef __AVR_TINY__
37
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Conversions to float
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
42 #if defined (L_fractqqsf)
43 DEFUN __fractqqsf
44 ;; Move in place for SA -> SF conversion
45 clr r22
46 mov r23, r24
47 ;; Sign-extend
48 lsl r24
49 sbc r24, r24
50 mov r25, r24
51 XJMP __fractsasf
52 ENDF __fractqqsf
53 #endif /* L_fractqqsf */
54
55 #if defined (L_fractuqqsf)
56 DEFUN __fractuqqsf
57 ;; Move in place for USA -> SF conversion
58 clr r22
59 mov r23, r24
60 ;; Zero-extend
61 clr r24
62 clr r25
63 XJMP __fractusasf
64 ENDF __fractuqqsf
65 #endif /* L_fractuqqsf */
66
67 #if defined (L_fracthqsf)
68 DEFUN __fracthqsf
69 ;; Move in place for SA -> SF conversion
70 wmov 22, 24
71 ;; Sign-extend
72 lsl r25
73 sbc r24, r24
74 mov r25, r24
75 XJMP __fractsasf
76 ENDF __fracthqsf
77 #endif /* L_fracthqsf */
78
79 #if defined (L_fractuhqsf)
80 DEFUN __fractuhqsf
81 ;; Move in place for USA -> SF conversion
82 wmov 22, 24
83 ;; Zero-extend
84 clr r24
85 clr r25
86 XJMP __fractusasf
87 ENDF __fractuhqsf
88 #endif /* L_fractuhqsf */
89
90 #if defined (L_fracthasf)
91 DEFUN __fracthasf
92 ;; Move in place for SA -> SF conversion
93 clr r22
94 mov r23, r24
95 mov r24, r25
96 ;; Sign-extend
97 lsl r25
98 sbc r25, r25
99 XJMP __fractsasf
100 ENDF __fracthasf
101 #endif /* L_fracthasf */
102
103 #if defined (L_fractuhasf)
104 DEFUN __fractuhasf
105 ;; Move in place for USA -> SF conversion
106 clr r22
107 mov r23, r24
108 mov r24, r25
109 ;; Zero-extend
110 clr r25
111 XJMP __fractusasf
112 ENDF __fractuhasf
113 #endif /* L_fractuhasf */
114
115
116 #if defined (L_fractsqsf)
117 DEFUN __fractsqsf
118 XCALL __floatsisf
119 ;; Divide non-zero results by 2^31 to move the
120 ;; decimal point into place
121 tst r25
122 breq 0f
123 subi r24, exp_lo (31)
124 sbci r25, exp_hi (31)
125 0: ret
126 ENDF __fractsqsf
127 #endif /* L_fractsqsf */
128
129 #if defined (L_fractusqsf)
130 DEFUN __fractusqsf
131 XCALL __floatunsisf
132 ;; Divide non-zero results by 2^32 to move the
133 ;; decimal point into place
134 cpse r25, __zero_reg__
135 subi r25, exp_hi (32)
136 ret
137 ENDF __fractusqsf
138 #endif /* L_fractusqsf */
139
140 #if defined (L_fractsasf)
141 DEFUN __fractsasf
142 XCALL __floatsisf
143 ;; Divide non-zero results by 2^15 to move the
144 ;; decimal point into place
145 tst r25
146 breq 0f
147 subi r24, exp_lo (15)
148 sbci r25, exp_hi (15)
149 0: ret
150 ENDF __fractsasf
151 #endif /* L_fractsasf */
152
153 #if defined (L_fractusasf)
154 DEFUN __fractusasf
155 XCALL __floatunsisf
156 ;; Divide non-zero results by 2^16 to move the
157 ;; decimal point into place
158 cpse r25, __zero_reg__
159 subi r25, exp_hi (16)
160 ret
161 ENDF __fractusasf
162 #endif /* L_fractusasf */
163
164 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
165 ;; Conversions from float
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167
168 #if defined (L_fractsfqq)
169 DEFUN __fractsfqq
170 ;; Multiply with 2^{24+7} to get a QQ result in r25
171 subi r24, exp_lo (-31)
172 sbci r25, exp_hi (-31)
173 XCALL __fixsfsi
174 mov r24, r25
175 ret
176 ENDF __fractsfqq
177 #endif /* L_fractsfqq */
178
179 #if defined (L_fractsfuqq)
180 DEFUN __fractsfuqq
181 ;; Multiply with 2^{24+8} to get a UQQ result in r25
182 subi r25, exp_hi (-32)
183 XCALL __fixunssfsi
184 mov r24, r25
185 ret
186 ENDF __fractsfuqq
187 #endif /* L_fractsfuqq */
188
189 #if defined (L_fractsfha)
190 DEFUN __fractsfha
191 ;; Multiply with 2^{16+7} to get a HA result in r25:r24
192 subi r24, exp_lo (-23)
193 sbci r25, exp_hi (-23)
194 XJMP __fixsfsi
195 ENDF __fractsfha
196 #endif /* L_fractsfha */
197
198 #if defined (L_fractsfuha)
199 DEFUN __fractsfuha
200 ;; Multiply with 2^24 to get a UHA result in r25:r24
201 subi r25, exp_hi (-24)
202 XJMP __fixunssfsi
203 ENDF __fractsfuha
204 #endif /* L_fractsfuha */
205
206 #if defined (L_fractsfhq)
207 FALIAS __fractsfsq
208
209 DEFUN __fractsfhq
210 ;; Multiply with 2^{16+15} to get a HQ result in r25:r24
211 ;; resp. with 2^31 to get a SQ result in r25:r22
212 subi r24, exp_lo (-31)
213 sbci r25, exp_hi (-31)
214 XJMP __fixsfsi
215 ENDF __fractsfhq
216 #endif /* L_fractsfhq */
217
218 #if defined (L_fractsfuhq)
219 FALIAS __fractsfusq
220
221 DEFUN __fractsfuhq
222 ;; Multiply with 2^{16+16} to get a UHQ result in r25:r24
223 ;; resp. with 2^32 to get a USQ result in r25:r22
224 subi r25, exp_hi (-32)
225 XJMP __fixunssfsi
226 ENDF __fractsfuhq
227 #endif /* L_fractsfuhq */
228
229 #if defined (L_fractsfsa)
230 DEFUN __fractsfsa
231 ;; Multiply with 2^15 to get a SA result in r25:r22
232 subi r24, exp_lo (-15)
233 sbci r25, exp_hi (-15)
234 XJMP __fixsfsi
235 ENDF __fractsfsa
236 #endif /* L_fractsfsa */
237
238 #if defined (L_fractsfusa)
239 DEFUN __fractsfusa
240 ;; Multiply with 2^16 to get a USA result in r25:r22
241 subi r25, exp_hi (-16)
242 XJMP __fixunssfsi
243 ENDF __fractsfusa
244 #endif /* L_fractsfusa */
245
246 #endif /* ! AVR_TINY */
247
248 ;; For multiplication the functions here are called directly from
249 ;; avr-fixed.md instead of using the standard libcall mechanisms.
250 ;; This can make better code because GCC knows exactly which
251 ;; of the call-used registers (not all of them) are clobbered. */
252
253 /*******************************************************
254 Fractional Multiplication 8 x 8 without MUL
255 *******************************************************/
256
257 #if defined (L_mulqq3) && !defined (__AVR_HAVE_MUL__)
258 ;;; R23 = R24 * R25
259 ;;; Clobbers: __tmp_reg__, R22, R24, R25
260 ;;; Rounding: ???
261 DEFUN __mulqq3
262 XCALL __fmuls
263 ;; TR 18037 requires that (-1) * (-1) does not overflow
264 ;; The only input that can produce -1 is (-1)^2.
265 dec r23
266 brvs 0f
267 inc r23
268 0: ret
269 ENDF __mulqq3
270 #endif /* L_mulqq3 && ! HAVE_MUL */
271
272 /*******************************************************
273 Fractional Multiply .16 x .16 with and without MUL
274 *******************************************************/
275
276 #if defined (L_mulhq3)
277 ;;; Same code with and without MUL, but the interfaces differ:
278 ;;; no MUL: (R25:R24) = (R22:R23) * (R24:R25)
279 ;;; Clobbers: ABI, called by optabs
280 ;;; MUL: (R25:R24) = (R19:R18) * (R27:R26)
281 ;;; Clobbers: __tmp_reg__, R22, R23
282 ;;; Rounding: -0.5 LSB <= error <= 0.5 LSB
283 DEFUN __mulhq3
284 XCALL __mulhisi3
285 ;; Shift result into place
286 lsl r23
287 rol r24
288 rol r25
289 brvs 1f
290 ;; Round
291 #ifndef __AVR_TINY__
292 sbrc r23, 7
293 adiw r24, 1
294 #else
295 lsl r23
296 adc r24, __zero_reg__
297 adc r25, __zero_reg__
298 #endif /* AVR_TINY? */
299 ret
300 1: ;; Overflow. TR 18037 requires (-1)^2 not to overflow
301 ldi r24, lo8 (0x7fff)
302 ldi r25, hi8 (0x7fff)
303 ret
304 ENDF __mulhq3
305 #endif /* defined (L_mulhq3) */
306
307 #if defined (L_muluhq3)
308 ;;; Same code with and without MUL, but the interfaces differ:
309 ;;; no MUL: (R25:R24) *= (R23:R22)
310 ;;; Clobbers: ABI, called by optabs
311 ;;; MUL: (R25:R24) = (R19:R18) * (R27:R26)
312 ;;; Clobbers: __tmp_reg__, R22, R23
313 ;;; Rounding: -0.5 LSB < error <= 0.5 LSB
314 DEFUN __muluhq3
315 XCALL __umulhisi3
316 ;; Round
317 #ifndef __AVR_TINY__
318 sbrc r23, 7
319 adiw r24, 1
320 #else
321 lsl r23
322 adc r24, __zero_reg__
323 adc r25, __zero_reg__
324 #endif /* AVR_TINY? */
325 ret
326 ENDF __muluhq3
327 #endif /* L_muluhq3 */
328
329
330 /*******************************************************
331 Fixed Multiply 8.8 x 8.8 with and without MUL
332 *******************************************************/
333
334 #if defined (L_mulha3)
335 ;;; Same code with and without MUL, but the interfaces differ:
336 ;;; no MUL: (R25:R24) = (R22:R23) * (R24:R25)
337 ;;; Clobbers: ABI, called by optabs
338 ;;; MUL: (R25:R24) = (R19:R18) * (R27:R26)
339 ;;; Clobbers: __tmp_reg__, R22, R23
340 ;;; Rounding: -0.5 LSB <= error <= 0.5 LSB
341 DEFUN __mulha3
342 XCALL __mulhisi3
343 lsl r22
344 rol r23
345 rol r24
346 XJMP __muluha3_round
347 ENDF __mulha3
348 #endif /* L_mulha3 */
349
350 #if defined (L_muluha3)
351 ;;; Same code with and without MUL, but the interfaces differ:
352 ;;; no MUL: (R25:R24) *= (R23:R22)
353 ;;; Clobbers: ABI, called by optabs
354 ;;; MUL: (R25:R24) = (R19:R18) * (R27:R26)
355 ;;; Clobbers: __tmp_reg__, R22, R23
356 ;;; Rounding: -0.5 LSB < error <= 0.5 LSB
357 DEFUN __muluha3
358 XCALL __umulhisi3
359 XJMP __muluha3_round
360 ENDF __muluha3
361 #endif /* L_muluha3 */
362
363 #if defined (L_muluha3_round)
364 DEFUN __muluha3_round
365 ;; Shift result into place
366 mov r25, r24
367 mov r24, r23
368 ;; Round
369 #ifndef __AVR_TINY__
370 sbrc r22, 7
371 adiw r24, 1
372 #else
373 lsl r22
374 adc r24, __zero_reg__
375 adc r25, __zero_reg__
376 #endif /* AVR_TINY? */
377 ret
378 ENDF __muluha3_round
379 #endif /* L_muluha3_round */
380
381
382 /*******************************************************
383 Fixed Multiplication 16.16 x 16.16
384 *******************************************************/
385
386 #ifndef __AVR_TINY__
387
388 ;; Bits outside the result (below LSB), used in the signed version
389 #define GUARD __tmp_reg__
390
391 #if defined (__AVR_HAVE_MUL__)
392
393 ;; Multiplier
394 #define A0 16
395 #define A1 A0+1
396 #define A2 A1+1
397 #define A3 A2+1
398
399 ;; Multiplicand
400 #define B0 20
401 #define B1 B0+1
402 #define B2 B1+1
403 #define B3 B2+1
404
405 ;; Result
406 #define C0 24
407 #define C1 C0+1
408 #define C2 C1+1
409 #define C3 C2+1
410
411 #if defined (L_mulusa3)
412 ;;; (C3:C0) = (A3:A0) * (B3:B0)
413 DEFUN __mulusa3
414 set
415 ;; Fallthru
416 ENDF __mulusa3
417
418 ;;; Round for last digit iff T = 1
419 ;;; Return guard bits in GUARD (__tmp_reg__).
420 ;;; Rounding, T = 0: -1.0 LSB < error <= 0 LSB
421 ;;; Rounding, T = 1: -0.5 LSB < error <= 0.5 LSB
422 DEFUN __mulusa3_round
423 ;; Some of the MUL instructions have LSBs outside the result.
424 ;; Don't ignore these LSBs in order to tame rounding error.
425 ;; Use C2/C3 for these LSBs.
426
427 clr C0
428 clr C1
429 mul A0, B0 $ movw C2, r0
430
431 mul A1, B0 $ add C3, r0 $ adc C0, r1
432 mul A0, B1 $ add C3, r0 $ adc C0, r1 $ rol C1
433
434 ;; Round if T = 1. Store guarding bits outside the result for rounding
435 ;; and left-shift by the signed version (function below).
436 brtc 0f
437 sbrc C3, 7
438 adiw C0, 1
439 0: push C3
440
441 ;; The following MULs don't have LSBs outside the result.
442 ;; C2/C3 is the high part.
443
444 mul A0, B2 $ add C0, r0 $ adc C1, r1 $ sbc C2, C2
445 mul A1, B1 $ add C0, r0 $ adc C1, r1 $ sbci C2, 0
446 mul A2, B0 $ add C0, r0 $ adc C1, r1 $ sbci C2, 0
447 neg C2
448
449 mul A0, B3 $ add C1, r0 $ adc C2, r1 $ sbc C3, C3
450 mul A1, B2 $ add C1, r0 $ adc C2, r1 $ sbci C3, 0
451 mul A2, B1 $ add C1, r0 $ adc C2, r1 $ sbci C3, 0
452 mul A3, B0 $ add C1, r0 $ adc C2, r1 $ sbci C3, 0
453 neg C3
454
455 mul A1, B3 $ add C2, r0 $ adc C3, r1
456 mul A2, B2 $ add C2, r0 $ adc C3, r1
457 mul A3, B1 $ add C2, r0 $ adc C3, r1
458
459 mul A2, B3 $ add C3, r0
460 mul A3, B2 $ add C3, r0
461
462 ;; Guard bits used in the signed version below.
463 pop GUARD
464 clr __zero_reg__
465 ret
466 ENDF __mulusa3_round
467 #endif /* L_mulusa3 */
468
469 #if defined (L_mulsa3)
470 ;;; (C3:C0) = (A3:A0) * (B3:B0)
471 ;;; Clobbers: __tmp_reg__, T
472 ;;; Rounding: -0.5 LSB <= error <= 0.5 LSB
473 DEFUN __mulsa3
474 clt
475 XCALL __mulusa3_round
476 ;; A posteriori sign extension of the operands
477 tst B3
478 brpl 1f
479 sub C2, A0
480 sbc C3, A1
481 1: sbrs A3, 7
482 rjmp 2f
483 sub C2, B0
484 sbc C3, B1
485 2:
486 ;; Shift 1 bit left to adjust for 15 fractional bits
487 lsl GUARD
488 rol C0
489 rol C1
490 rol C2
491 rol C3
492 ;; Round last digit
493 lsl GUARD
494 adc C0, __zero_reg__
495 adc C1, __zero_reg__
496 adc C2, __zero_reg__
497 adc C3, __zero_reg__
498 ret
499 ENDF __mulsa3
500 #endif /* L_mulsa3 */
501
502 #undef A0
503 #undef A1
504 #undef A2
505 #undef A3
506 #undef B0
507 #undef B1
508 #undef B2
509 #undef B3
510 #undef C0
511 #undef C1
512 #undef C2
513 #undef C3
514
515 #else /* __AVR_HAVE_MUL__ */
516
517 #define A0 18
518 #define A1 A0+1
519 #define A2 A0+2
520 #define A3 A0+3
521
522 #define B0 22
523 #define B1 B0+1
524 #define B2 B0+2
525 #define B3 B0+3
526
527 #define C0 22
528 #define C1 C0+1
529 #define C2 C0+2
530 #define C3 C0+3
531
532 ;; __tmp_reg__
533 #define CC0 0
534 ;; __zero_reg__
535 #define CC1 1
536 #define CC2 16
537 #define CC3 17
538
539 #define AA0 26
540 #define AA1 AA0+1
541 #define AA2 30
542 #define AA3 AA2+1
543
544 #if defined (L_mulsa3)
545 ;;; (R25:R22) *= (R21:R18)
546 ;;; Clobbers: ABI, called by optabs
547 ;;; Rounding: -1 LSB <= error <= 1 LSB
548 DEFUN __mulsa3
549 push B0
550 push B1
551 push B3
552 clt
553 XCALL __mulusa3_round
554 pop r30
555 ;; sign-extend B
556 bst r30, 7
557 brtc 1f
558 ;; A1, A0 survived in R27:R26
559 sub C2, AA0
560 sbc C3, AA1
561 1:
562 pop AA1 ;; B1
563 pop AA0 ;; B0
564
565 ;; sign-extend A. A3 survived in R31
566 bst AA3, 7
567 brtc 2f
568 sub C2, AA0
569 sbc C3, AA1
570 2:
571 ;; Shift 1 bit left to adjust for 15 fractional bits
572 lsl GUARD
573 rol C0
574 rol C1
575 rol C2
576 rol C3
577 ;; Round last digit
578 lsl GUARD
579 adc C0, __zero_reg__
580 adc C1, __zero_reg__
581 adc C2, __zero_reg__
582 adc C3, __zero_reg__
583 ret
584 ENDF __mulsa3
585 #endif /* L_mulsa3 */
586
587 #if defined (L_mulusa3)
588 ;;; (R25:R22) *= (R21:R18)
589 ;;; Clobbers: ABI, called by optabs
590 ;;; Rounding: -1 LSB <= error <= 1 LSB
591 DEFUN __mulusa3
592 set
593 ;; Fallthru
594 ENDF __mulusa3
595
596 ;;; A[] survives in 26, 27, 30, 31
597 ;;; Also used by __mulsa3 with T = 0
598 ;;; Round if T = 1
599 ;;; Return Guard bits in GUARD (__tmp_reg__), used by signed version.
600 DEFUN __mulusa3_round
601 push CC2
602 push CC3
603 ; clear result
604 clr __tmp_reg__
605 wmov CC2, CC0
606 ; save multiplicand
607 wmov AA0, A0
608 wmov AA2, A2
609 rjmp 3f
610
611 ;; Loop the integral part
612
613 1: ;; CC += A * 2^n; n >= 0
614 add CC0,A0 $ adc CC1,A1 $ adc CC2,A2 $ adc CC3,A3
615
616 2: ;; A <<= 1
617 lsl A0 $ rol A1 $ rol A2 $ rol A3
618
619 3: ;; IBIT(B) >>= 1
620 ;; Carry = n-th bit of B; n >= 0
621 lsr B3
622 ror B2
623 brcs 1b
624 sbci B3, 0
625 brne 2b
626
627 ;; Loop the fractional part
628 ;; B2/B3 is 0 now, use as guard bits for rounding
629 ;; Restore multiplicand
630 wmov A0, AA0
631 wmov A2, AA2
632 rjmp 5f
633
634 4: ;; CC += A:Guard * 2^n; n < 0
635 add B3,B2 $ adc CC0,A0 $ adc CC1,A1 $ adc CC2,A2 $ adc CC3,A3
636 5:
637 ;; A:Guard >>= 1
638 lsr A3 $ ror A2 $ ror A1 $ ror A0 $ ror B2
639
640 ;; FBIT(B) <<= 1
641 ;; Carry = n-th bit of B; n < 0
642 lsl B0
643 rol B1
644 brcs 4b
645 sbci B0, 0
646 brne 5b
647
648 ;; Save guard bits and set carry for rounding
649 push B3
650 lsl B3
651 ;; Move result into place
652 wmov C2, CC2
653 wmov C0, CC0
654 clr __zero_reg__
655 brtc 6f
656 ;; Round iff T = 1
657 adc C0, __zero_reg__
658 adc C1, __zero_reg__
659 adc C2, __zero_reg__
660 adc C3, __zero_reg__
661 6:
662 pop GUARD
663 ;; Epilogue
664 pop CC3
665 pop CC2
666 ret
667 ENDF __mulusa3_round
668 #endif /* L_mulusa3 */
669
670 #undef A0
671 #undef A1
672 #undef A2
673 #undef A3
674 #undef B0
675 #undef B1
676 #undef B2
677 #undef B3
678 #undef C0
679 #undef C1
680 #undef C2
681 #undef C3
682 #undef AA0
683 #undef AA1
684 #undef AA2
685 #undef AA3
686 #undef CC0
687 #undef CC1
688 #undef CC2
689 #undef CC3
690
691 #endif /* __AVR_HAVE_MUL__ */
692
693 #undef GUARD
694
695 #endif /* ! AVR_TINY */
696
697 /***********************************************************
698 Fixed unsigned saturated Multiplication 8.8 x 8.8
699 ***********************************************************/
700
701 #define C0 22
702 #define C1 C0+1
703 #define C2 C0+2
704 #define C3 C0+3
705 #define SS __tmp_reg__
706
707 #if defined (L_usmuluha3)
708 DEFUN __usmuluha3
709 ;; Widening multiply
710 #ifdef __AVR_HAVE_MUL__
711 ;; Adjust interface
712 movw R26, R22
713 movw R18, R24
714 #endif /* HAVE MUL */
715 XCALL __umulhisi3
716 tst C3
717 brne .Lmax
718 ;; Round, target is in C1..C2
719 lsl C0
720 adc C1, __zero_reg__
721 adc C2, __zero_reg__
722 brcs .Lmax
723 ;; Move result into place
724 mov C3, C2
725 mov C2, C1
726 ret
727 .Lmax:
728 ;; Saturate
729 ldi C2, 0xff
730 ldi C3, 0xff
731 ret
732 ENDF __usmuluha3
733 #endif /* L_usmuluha3 */
734
735 /***********************************************************
736 Fixed signed saturated Multiplication s8.7 x s8.7
737 ***********************************************************/
738
739 #if defined (L_ssmulha3)
740 DEFUN __ssmulha3
741 ;; Widening multiply
742 #ifdef __AVR_HAVE_MUL__
743 ;; Adjust interface
744 movw R26, R22
745 movw R18, R24
746 #endif /* HAVE MUL */
747 XCALL __mulhisi3
748 ;; Adjust decimal point
749 lsl C0
750 rol C1
751 rol C2
752 brvs .LsatC3.3
753 ;; The 9 MSBs must be the same
754 rol C3
755 sbc SS, SS
756 cp C3, SS
757 brne .LsatSS
758 ;; Round
759 lsl C0
760 adc C1, __zero_reg__
761 adc C2, __zero_reg__
762 brvs .Lmax
763 ;; Move result into place
764 mov C3, C2
765 mov C2, C1
766 ret
767 .Lmax:
768 ;; Load 0x7fff
769 clr C3
770 .LsatC3.3:
771 ;; C3 < 0 --> 0x8000
772 ;; C3 >= 0 --> 0x7fff
773 mov SS, C3
774 .LsatSS:
775 ;; Load min / max value:
776 ;; SS = -1 --> 0x8000
777 ;; SS = 0 --> 0x7fff
778 ldi C3, 0x7f
779 ldi C2, 0xff
780 #ifndef __AVR_TINY__
781 sbrc SS, 7
782 adiw C2, 1
783 #else
784 lsl SS
785 adc C2, __zero_reg__
786 adc C3, __zero_reg__
787 #endif /* AVR_TINY? */
788 ret
789 ENDF __ssmulha3
790 #endif /* L_ssmulha3 */
791
792 #undef C0
793 #undef C1
794 #undef C2
795 #undef C3
796 #undef SS
797
798 /***********************************************************
799 Fixed unsigned saturated Multiplication 16.16 x 16.16
800 ***********************************************************/
801
802 #ifndef __AVR_TINY__
803
804 #define C0 18
805 #define C1 C0+1
806 #define C2 C0+2
807 #define C3 C0+3
808 #define C4 C0+4
809 #define C5 C0+5
810 #define C6 C0+6
811 #define C7 C0+7
812 #define SS __tmp_reg__
813
814 #if defined (L_usmulusa3)
815 ;; R22[4] = R22[4] *{ssat} R18[4]
816 ;; Ordinary ABI function
817 DEFUN __usmulusa3
818 ;; Widening multiply
819 XCALL __umulsidi3
820 or C7, C6
821 brne .Lmax
822 ;; Round, target is in C2..C5
823 lsl C1
824 adc C2, __zero_reg__
825 adc C3, __zero_reg__
826 adc C4, __zero_reg__
827 adc C5, __zero_reg__
828 brcs .Lmax
829 ;; Move result into place
830 wmov C6, C4
831 wmov C4, C2
832 ret
833 .Lmax:
834 ;; Saturate
835 ldi C7, 0xff
836 ldi C6, 0xff
837 wmov C4, C6
838 ret
839 ENDF __usmulusa3
840 #endif /* L_usmulusa3 */
841
842 /***********************************************************
843 Fixed signed saturated Multiplication s16.15 x s16.15
844 ***********************************************************/
845
846 #if defined (L_ssmulsa3)
847 ;; R22[4] = R22[4] *{ssat} R18[4]
848 ;; Ordinary ABI function
849 DEFUN __ssmulsa3
850 ;; Widening multiply
851 XCALL __mulsidi3
852 ;; Adjust decimal point
853 lsl C1
854 rol C2
855 rol C3
856 rol C4
857 rol C5
858 brvs .LsatC7.7
859 ;; The 17 MSBs must be the same
860 rol C6
861 rol C7
862 sbc SS, SS
863 cp C6, SS
864 cpc C7, SS
865 brne .LsatSS
866 ;; Round
867 lsl C1
868 adc C2, __zero_reg__
869 adc C3, __zero_reg__
870 adc C4, __zero_reg__
871 adc C5, __zero_reg__
872 brvs .Lmax
873 ;; Move result into place
874 wmov C6, C4
875 wmov C4, C2
876 ret
877
878 .Lmax:
879 ;; Load 0x7fffffff
880 clr C7
881 .LsatC7.7:
882 ;; C7 < 0 --> 0x80000000
883 ;; C7 >= 0 --> 0x7fffffff
884 lsl C7
885 sbc SS, SS
886 .LsatSS:
887 ;; Load min / max value:
888 ;; SS = -1 --> 0x80000000
889 ;; SS = 0 --> 0x7fffffff
890 com SS
891 mov C4, SS
892 mov C5, C4
893 wmov C6, C4
894 subi C7, 0x80
895 ret
896 ENDF __ssmulsa3
897 #endif /* L_ssmulsa3 */
898
899 #undef C0
900 #undef C1
901 #undef C2
902 #undef C3
903 #undef C4
904 #undef C5
905 #undef C6
906 #undef C7
907 #undef SS
908
909 #endif /* ! AVR_TINY */
910
911 /*******************************************************
912 Fractional Division 8 / 8
913 *******************************************************/
914
915 #define r_divd r25 /* dividend */
916 #define r_quo r24 /* quotient */
917 #define r_div r22 /* divisor */
918 #define r_sign __tmp_reg__
919
920 #if defined (L_divqq3)
921 DEFUN __divqq3
922 mov r_sign, r_divd
923 eor r_sign, r_div
924 sbrc r_div, 7
925 neg r_div
926 sbrc r_divd, 7
927 neg r_divd
928 XCALL __divqq_helper
929 lsr r_quo
930 sbrc r_sign, 7 ; negate result if needed
931 neg r_quo
932 ret
933 ENDF __divqq3
934 #endif /* L_divqq3 */
935
936 #if defined (L_udivuqq3)
937 DEFUN __udivuqq3
938 cp r_divd, r_div
939 brsh 0f
940 XJMP __divqq_helper
941 ;; Result is out of [0, 1) ==> Return 1 - eps.
942 0: ldi r_quo, 0xff
943 ret
944 ENDF __udivuqq3
945 #endif /* L_udivuqq3 */
946
947
948 #if defined (L_divqq_helper)
949 DEFUN __divqq_helper
950 clr r_quo ; clear quotient
951 inc __zero_reg__ ; init loop counter, used per shift
952 __udivuqq3_loop:
953 lsl r_divd ; shift dividend
954 brcs 0f ; dividend overflow
955 cp r_divd,r_div ; compare dividend & divisor
956 brcc 0f ; dividend >= divisor
957 rol r_quo ; shift quotient (with CARRY)
958 rjmp __udivuqq3_cont
959 0:
960 sub r_divd,r_div ; restore dividend
961 lsl r_quo ; shift quotient (without CARRY)
962 __udivuqq3_cont:
963 lsl __zero_reg__ ; shift loop-counter bit
964 brne __udivuqq3_loop
965 com r_quo ; complement result
966 ; because C flag was complemented in loop
967 ret
968 ENDF __divqq_helper
969 #endif /* L_divqq_helper */
970
971 #undef r_divd
972 #undef r_quo
973 #undef r_div
974 #undef r_sign
975
976
977 /*******************************************************
978 Fractional Division 16 / 16
979 *******************************************************/
980 #define r_divdL 26 /* dividend Low */
981 #define r_divdH 27 /* dividend Hig */
982 #define r_quoL 24 /* quotient Low */
983 #define r_quoH 25 /* quotient High */
984 #define r_divL 22 /* divisor */
985 #define r_divH 23 /* divisor */
986 #define r_cnt 21
987
988 #if defined (L_divhq3)
989 DEFUN __divhq3
990 mov __tmp_reg__, r_divdH
991 eor __tmp_reg__, r_divH
992 sbrs r_divH, 7
993 rjmp 1f
994 NEG2 r_divL
995 1:
996 sbrs r_divdH, 7
997 rjmp 2f
998 NEG2 r_divdL
999 2:
1000 cp r_divdL, r_divL
1001 cpc r_divdH, r_divH
1002 breq __divhq3_minus1 ; if equal return -1
1003 XCALL __udivuhq3
1004 lsr r_quoH
1005 ror r_quoL
1006 brpl 9f
1007 ;; negate result if needed
1008 NEG2 r_quoL
1009 9:
1010 ret
1011 __divhq3_minus1:
1012 ldi r_quoH, 0x80
1013 clr r_quoL
1014 ret
1015 ENDF __divhq3
1016 #endif /* defined (L_divhq3) */
1017
1018 #if defined (L_udivuhq3)
1019 DEFUN __udivuhq3
1020 sub r_quoH,r_quoH ; clear quotient and carry
1021 ;; FALLTHRU
1022 ENDF __udivuhq3
1023
1024 DEFUN __udivuha3_common
1025 clr r_quoL ; clear quotient
1026 ldi r_cnt,16 ; init loop counter
1027 __udivuhq3_loop:
1028 rol r_divdL ; shift dividend (with CARRY)
1029 rol r_divdH
1030 brcs __udivuhq3_ep ; dividend overflow
1031 cp r_divdL,r_divL ; compare dividend & divisor
1032 cpc r_divdH,r_divH
1033 brcc __udivuhq3_ep ; dividend >= divisor
1034 rol r_quoL ; shift quotient (with CARRY)
1035 rjmp __udivuhq3_cont
1036 __udivuhq3_ep:
1037 sub r_divdL,r_divL ; restore dividend
1038 sbc r_divdH,r_divH
1039 lsl r_quoL ; shift quotient (without CARRY)
1040 __udivuhq3_cont:
1041 rol r_quoH ; shift quotient
1042 dec r_cnt ; decrement loop counter
1043 brne __udivuhq3_loop
1044 com r_quoL ; complement result
1045 com r_quoH ; because C flag was complemented in loop
1046 ret
1047 ENDF __udivuha3_common
1048 #endif /* defined (L_udivuhq3) */
1049
1050 /*******************************************************
1051 Fixed Division 8.8 / 8.8
1052 *******************************************************/
1053 #if defined (L_divha3)
1054 DEFUN __divha3
1055 mov __tmp_reg__, r_divdH
1056 eor __tmp_reg__, r_divH
1057 sbrs r_divH, 7
1058 rjmp 1f
1059 NEG2 r_divL
1060 1:
1061 sbrs r_divdH, 7
1062 rjmp 2f
1063 NEG2 r_divdL
1064 2:
1065 XCALL __udivuha3
1066 lsr r_quoH ; adjust to 7 fractional bits
1067 ror r_quoL
1068 sbrs __tmp_reg__, 7 ; negate result if needed
1069 ret
1070 NEG2 r_quoL
1071 ret
1072 ENDF __divha3
1073 #endif /* defined (L_divha3) */
1074
1075 #if defined (L_udivuha3)
1076 DEFUN __udivuha3
1077 mov r_quoH, r_divdL
1078 mov r_divdL, r_divdH
1079 clr r_divdH
1080 lsl r_quoH ; shift quotient into carry
1081 XJMP __udivuha3_common ; same as fractional after rearrange
1082 ENDF __udivuha3
1083 #endif /* defined (L_udivuha3) */
1084
1085 #undef r_divdL
1086 #undef r_divdH
1087 #undef r_quoL
1088 #undef r_quoH
1089 #undef r_divL
1090 #undef r_divH
1091 #undef r_cnt
1092
1093 /*******************************************************
1094 Fixed Division 16.16 / 16.16
1095 *******************************************************/
1096
1097 #ifndef __AVR_TINY__
1098
1099 #define r_arg1L 24 /* arg1 gets passed already in place */
1100 #define r_arg1H 25
1101 #define r_arg1HL 26
1102 #define r_arg1HH 27
1103 #define r_divdL 26 /* dividend Low */
1104 #define r_divdH 27
1105 #define r_divdHL 30
1106 #define r_divdHH 31 /* dividend High */
1107 #define r_quoL 22 /* quotient Low */
1108 #define r_quoH 23
1109 #define r_quoHL 24
1110 #define r_quoHH 25 /* quotient High */
1111 #define r_divL 18 /* divisor Low */
1112 #define r_divH 19
1113 #define r_divHL 20
1114 #define r_divHH 21 /* divisor High */
1115 #define r_cnt __zero_reg__ /* loop count (0 after the loop!) */
1116
1117 #if defined (L_divsa3)
1118 DEFUN __divsa3
1119 mov r0, r_arg1HH
1120 eor r0, r_divHH
1121 sbrs r_divHH, 7
1122 rjmp 1f
1123 NEG4 r_divL
1124 1:
1125 sbrs r_arg1HH, 7
1126 rjmp 2f
1127 NEG4 r_arg1L
1128 2:
1129 XCALL __udivusa3
1130 lsr r_quoHH ; adjust to 15 fractional bits
1131 ror r_quoHL
1132 ror r_quoH
1133 ror r_quoL
1134 sbrs r0, 7 ; negate result if needed
1135 ret
1136 ;; negate r_quoL
1137 XJMP __negsi2
1138 ENDF __divsa3
1139 #endif /* defined (L_divsa3) */
1140
1141 #if defined (L_udivusa3)
1142 DEFUN __udivusa3
1143 ldi r_divdHL, 32 ; init loop counter
1144 mov r_cnt, r_divdHL
1145 clr r_divdHL
1146 clr r_divdHH
1147 wmov r_quoL, r_divdHL
1148 lsl r_quoHL ; shift quotient into carry
1149 rol r_quoHH
1150 __udivusa3_loop:
1151 rol r_divdL ; shift dividend (with CARRY)
1152 rol r_divdH
1153 rol r_divdHL
1154 rol r_divdHH
1155 brcs __udivusa3_ep ; dividend overflow
1156 cp r_divdL,r_divL ; compare dividend & divisor
1157 cpc r_divdH,r_divH
1158 cpc r_divdHL,r_divHL
1159 cpc r_divdHH,r_divHH
1160 brcc __udivusa3_ep ; dividend >= divisor
1161 rol r_quoL ; shift quotient (with CARRY)
1162 rjmp __udivusa3_cont
1163 __udivusa3_ep:
1164 sub r_divdL,r_divL ; restore dividend
1165 sbc r_divdH,r_divH
1166 sbc r_divdHL,r_divHL
1167 sbc r_divdHH,r_divHH
1168 lsl r_quoL ; shift quotient (without CARRY)
1169 __udivusa3_cont:
1170 rol r_quoH ; shift quotient
1171 rol r_quoHL
1172 rol r_quoHH
1173 dec r_cnt ; decrement loop counter
1174 brne __udivusa3_loop
1175 com r_quoL ; complement result
1176 com r_quoH ; because C flag was complemented in loop
1177 com r_quoHL
1178 com r_quoHH
1179 ret
1180 ENDF __udivusa3
1181 #endif /* defined (L_udivusa3) */
1182
1183 #undef r_arg1L
1184 #undef r_arg1H
1185 #undef r_arg1HL
1186 #undef r_arg1HH
1187 #undef r_divdL
1188 #undef r_divdH
1189 #undef r_divdHL
1190 #undef r_divdHH
1191 #undef r_quoL
1192 #undef r_quoH
1193 #undef r_quoHL
1194 #undef r_quoHH
1195 #undef r_divL
1196 #undef r_divH
1197 #undef r_divHL
1198 #undef r_divHH
1199 #undef r_cnt
1200
1201 #endif /* ! AVR_TINY */
1202
1203
1204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1205 ;; Saturation, 1 Byte
1206 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1207
1208 ;; First Argument and Return Register
1209 #define A0 24
1210
1211 #if defined (L_ssabs_1)
1212 DEFUN __ssabs_1
1213 sbrs A0, 7
1214 ret
1215 neg A0
1216 sbrc A0,7
1217 dec A0
1218 ret
1219 ENDF __ssabs_1
1220 #endif /* L_ssabs_1 */
1221
1222 #undef A0
1223
1224
1225
1226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1227 ;; Saturation, 2 Bytes
1228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1229
1230 ;; First Argument and Return Register
1231 #define A0 24
1232 #define A1 A0+1
1233
1234 #if defined (L_ssneg_2)
1235 DEFUN __ssneg_2
1236 NEG2 A0
1237 brvc 0f
1238 wsubi A0, 1
1239 0: ret
1240 ENDF __ssneg_2
1241 #endif /* L_ssneg_2 */
1242
1243 #if defined (L_ssabs_2)
1244 DEFUN __ssabs_2
1245 sbrs A1, 7
1246 ret
1247 XJMP __ssneg_2
1248 ENDF __ssabs_2
1249 #endif /* L_ssabs_2 */
1250
1251 #undef A0
1252 #undef A1
1253
1254
1255
1256 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1257 ;; Saturation, 4 Bytes
1258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1259
1260 #ifndef __AVR_TINY__
1261
1262 ;; First Argument and Return Register
1263 #define A0 22
1264 #define A1 A0+1
1265 #define A2 A0+2
1266 #define A3 A0+3
1267
1268 #if defined (L_ssneg_4)
1269 DEFUN __ssneg_4
1270 XCALL __negsi2
1271 brvc 0f
1272 ldi A3, 0x7f
1273 ldi A2, 0xff
1274 ldi A1, 0xff
1275 ldi A0, 0xff
1276 0: ret
1277 ENDF __ssneg_4
1278 #endif /* L_ssneg_4 */
1279
1280 #if defined (L_ssabs_4)
1281 DEFUN __ssabs_4
1282 sbrs A3, 7
1283 ret
1284 XJMP __ssneg_4
1285 ENDF __ssabs_4
1286 #endif /* L_ssabs_4 */
1287
1288 #undef A0
1289 #undef A1
1290 #undef A2
1291 #undef A3
1292
1293
1294
1295 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1296 ;; Saturation, 8 Bytes
1297 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1298
1299 ;; First Argument and Return Register
1300 #define A0 18
1301 #define A1 A0+1
1302 #define A2 A0+2
1303 #define A3 A0+3
1304 #define A4 A0+4
1305 #define A5 A0+5
1306 #define A6 A0+6
1307 #define A7 A0+7
1308
1309 #if defined (L_clr_8)
1310 FALIAS __usneguta2
1311 FALIAS __usneguda2
1312 FALIAS __usnegudq2
1313
1314 ;; Clear Carry and all Bytes
1315 DEFUN __clr_8
1316 ;; Clear Carry and set Z
1317 sub A7, A7
1318 ;; FALLTHRU
1319 ENDF __clr_8
1320 ;; Propagate Carry to all Bytes, Carry unaltered
1321 DEFUN __sbc_8
1322 sbc A7, A7
1323 sbc A6, A6
1324 wmov A4, A6
1325 wmov A2, A6
1326 wmov A0, A6
1327 ret
1328 ENDF __sbc_8
1329 #endif /* L_clr_8 */
1330
1331 #if defined (L_ssneg_8)
1332 FALIAS __ssnegta2
1333 FALIAS __ssnegda2
1334 FALIAS __ssnegdq2
1335
1336 DEFUN __ssneg_8
1337 XCALL __negdi2
1338 brvc 0f
1339 ;; A[] = 0x7fffffff
1340 sec
1341 XCALL __sbc_8
1342 ldi A7, 0x7f
1343 0: ret
1344 ENDF __ssneg_8
1345 #endif /* L_ssneg_8 */
1346
1347 #if defined (L_ssabs_8)
1348 FALIAS __ssabsta2
1349 FALIAS __ssabsda2
1350 FALIAS __ssabsdq2
1351
1352 DEFUN __ssabs_8
1353 sbrs A7, 7
1354 ret
1355 XJMP __ssneg_8
1356 ENDF __ssabs_8
1357 #endif /* L_ssabs_8 */
1358
1359 ;; Second Argument
1360 #define B0 10
1361 #define B1 B0+1
1362 #define B2 B0+2
1363 #define B3 B0+3
1364 #define B4 B0+4
1365 #define B5 B0+5
1366 #define B6 B0+6
1367 #define B7 B0+7
1368
1369 #if defined (L_usadd_8)
1370 FALIAS __usadduta3
1371 FALIAS __usadduda3
1372 FALIAS __usaddudq3
1373
1374 DEFUN __usadd_8
1375 XCALL __adddi3
1376 brcs 0f
1377 ret
1378 0: ;; A[] = 0xffffffff
1379 XJMP __sbc_8
1380 ENDF __usadd_8
1381 #endif /* L_usadd_8 */
1382
1383 #if defined (L_ussub_8)
1384 FALIAS __ussubuta3
1385 FALIAS __ussubuda3
1386 FALIAS __ussubudq3
1387
1388 DEFUN __ussub_8
1389 XCALL __subdi3
1390 brcs 0f
1391 ret
1392 0: ;; A[] = 0
1393 XJMP __clr_8
1394 ENDF __ussub_8
1395 #endif /* L_ussub_8 */
1396
1397 #if defined (L_ssadd_8)
1398 FALIAS __ssaddta3
1399 FALIAS __ssaddda3
1400 FALIAS __ssadddq3
1401
1402 DEFUN __ssadd_8
1403 XCALL __adddi3
1404 brvc 0f
1405 ;; A = (B >= 0) ? INT64_MAX : INT64_MIN
1406 cpi B7, 0x80
1407 XCALL __sbc_8
1408 subi A7, 0x80
1409 0: ret
1410 ENDF __ssadd_8
1411 #endif /* L_ssadd_8 */
1412
1413 #if defined (L_sssub_8)
1414 FALIAS __sssubta3
1415 FALIAS __sssubda3
1416 FALIAS __sssubdq3
1417
1418 DEFUN __sssub_8
1419 XCALL __subdi3
1420 brvc 0f
1421 ;; A = (B < 0) ? INT64_MAX : INT64_MIN
1422 ldi A7, 0x7f
1423 cp A7, B7
1424 XCALL __sbc_8
1425 subi A7, 0x80
1426 0: ret
1427 ENDF __sssub_8
1428 #endif /* L_sssub_8 */
1429
1430 #undef A0
1431 #undef A1
1432 #undef A2
1433 #undef A3
1434 #undef A4
1435 #undef A5
1436 #undef A6
1437 #undef A7
1438 #undef B0
1439 #undef B1
1440 #undef B2
1441 #undef B3
1442 #undef B4
1443 #undef B5
1444 #undef B6
1445 #undef B7
1446
1447 #endif /* ! AVR_TINY */
1448
1449
1450 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1451 ;; Rounding Helpers
1452 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1453
1454 #ifdef L_mask1
1455
1456 #define AA 24
1457 #define CC 25
1458
1459 ;; R25 = 1 << (R24 & 7)
1460 ;; CC = 1 << (AA & 7)
1461 ;; Clobbers: None
1462 DEFUN __mask1
1463 ;; CC = 2 ^ AA.1
1464 ldi CC, 1 << 2
1465 sbrs AA, 1
1466 ldi CC, 1 << 0
1467 ;; CC *= 2 ^ AA.0
1468 sbrc AA, 0
1469 lsl CC
1470 ;; CC *= 2 ^ AA.2
1471 sbrc AA, 2
1472 swap CC
1473 ret
1474 ENDF __mask1
1475
1476 #undef AA
1477 #undef CC
1478 #endif /* L_mask1 */
1479
1480 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1481
1482 ;; The rounding point. Any bits smaller than
1483 ;; 2^{-RP} will be cleared.
1484 #define RP R24
1485
1486 #define A0 22
1487 #define A1 A0 + 1
1488
1489 #define C0 24
1490 #define C1 C0 + 1
1491
1492 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1493 ;; Rounding, 1 Byte
1494 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1495
1496 #ifdef L_roundqq3
1497
1498 ;; R24 = round (R22, R24)
1499 ;; Clobbers: R22, __tmp_reg__
1500 DEFUN __roundqq3
1501 mov __tmp_reg__, C1
1502 subi RP, __QQ_FBIT__ - 1
1503 neg RP
1504 ;; R25 = 1 << RP (Total offset is FBIT-1 - RP)
1505 XCALL __mask1
1506 mov C0, C1
1507 ;; Add-Saturate 2^{-RP-1}
1508 add A0, C0
1509 brvc 0f
1510 ldi C0, 0x7f
1511 rjmp 9f
1512 0: ;; Mask out bits beyond RP
1513 lsl C0
1514 neg C0
1515 and C0, A0
1516 9: mov C1, __tmp_reg__
1517 ret
1518 ENDF __roundqq3
1519 #endif /* L_roundqq3 */
1520
1521 #ifdef L_rounduqq3
1522
1523 ;; R24 = round (R22, R24)
1524 ;; Clobbers: R22, __tmp_reg__
1525 DEFUN __rounduqq3
1526 mov __tmp_reg__, C1
1527 subi RP, __UQQ_FBIT__ - 1
1528 neg RP
1529 ;; R25 = 1 << RP (Total offset is FBIT-1 - RP)
1530 XCALL __mask1
1531 mov C0, C1
1532 ;; Add-Saturate 2^{-RP-1}
1533 add A0, C0
1534 brcc 0f
1535 ldi C0, 0xff
1536 rjmp 9f
1537 0: ;; Mask out bits beyond RP
1538 lsl C0
1539 neg C0
1540 and C0, A0
1541 9: mov C1, __tmp_reg__
1542 ret
1543 ENDF __rounduqq3
1544 #endif /* L_rounduqq3 */
1545
1546 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1547 ;; Rounding, 2 Bytes
1548 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1549
1550 #ifdef L_addmask_2
1551
1552 ;; [ R25:R24 = 1 << (R24 & 15)
1553 ;; R23:R22 += 1 << (R24 & 15) ]
1554 ;; SREG is set according to the addition
1555 DEFUN __addmask_2
1556 ;; R25 = 1 << (R24 & 7)
1557 XCALL __mask1
1558 cpi RP, 1 << 3
1559 sbc C0, C0
1560 ;; Swap C0 and C1 if RP.3 was set
1561 and C0, C1
1562 eor C1, C0
1563 ;; Finally, add the power-of-two: A[] += C[]
1564 add A0, C0
1565 adc A1, C1
1566 ret
1567 ENDF __addmask_2
1568 #endif /* L_addmask_2 */
1569
1570 #ifdef L_round_s2
1571
1572 ;; R25:R24 = round (R23:R22, R24)
1573 ;; Clobbers: R23, R22
1574 DEFUN __roundhq3
1575 subi RP, __HQ_FBIT__ - __HA_FBIT__
1576 ENDF __roundhq3
1577 DEFUN __roundha3
1578 subi RP, __HA_FBIT__ - 1
1579 neg RP
1580 ;; [ R25:R24 = 1 << (FBIT-1 - RP)
1581 ;; R23:R22 += 1 << (FBIT-1 - RP) ]
1582 XCALL __addmask_2
1583 XJMP __round_s2_const
1584 ENDF __roundha3
1585
1586 #endif /* L_round_s2 */
1587
1588 #ifdef L_round_u2
1589
1590 ;; R25:R24 = round (R23:R22, R24)
1591 ;; Clobbers: R23, R22
1592 DEFUN __rounduhq3
1593 subi RP, __UHQ_FBIT__ - __UHA_FBIT__
1594 ENDF __rounduhq3
1595 DEFUN __rounduha3
1596 subi RP, __UHA_FBIT__ - 1
1597 neg RP
1598 ;; [ R25:R24 = 1 << (FBIT-1 - RP)
1599 ;; R23:R22 += 1 << (FBIT-1 - RP) ]
1600 XCALL __addmask_2
1601 XJMP __round_u2_const
1602 ENDF __rounduha3
1603
1604 #endif /* L_round_u2 */
1605
1606
1607 #ifdef L_round_2_const
1608
1609 ;; Helpers for 2 byte wide rounding
1610
1611 DEFUN __round_s2_const
1612 brvc 2f
1613 ldi C1, 0x7f
1614 rjmp 1f
1615 ;; FALLTHRU (Barrier)
1616 ENDF __round_s2_const
1617
1618 DEFUN __round_u2_const
1619 brcc 2f
1620 ldi C1, 0xff
1621 1:
1622 ldi C0, 0xff
1623 rjmp 9f
1624 2:
1625 ;; Saturation is performed now.
1626 ;; Currently, we have C[] = 2^{-RP-1}
1627 ;; C[] = 2^{-RP}
1628 lsl C0
1629 rol C1
1630 ;;
1631 NEG2 C0
1632 ;; Clear the bits beyond the rounding point.
1633 and C0, A0
1634 and C1, A1
1635 9: ret
1636 ENDF __round_u2_const
1637
1638 #endif /* L_round_2_const */
1639
1640 #undef A0
1641 #undef A1
1642 #undef C0
1643 #undef C1
1644
1645 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1646 ;; Rounding, 4 Bytes
1647 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1648
1649 #ifndef __AVR_TINY__
1650
1651 #define A0 18
1652 #define A1 A0 + 1
1653 #define A2 A0 + 2
1654 #define A3 A0 + 3
1655
1656 #define C0 22
1657 #define C1 C0 + 1
1658 #define C2 C0 + 2
1659 #define C3 C0 + 3
1660
1661 #ifdef L_addmask_4
1662
1663 ;; [ R25:R22 = 1 << (R24 & 31)
1664 ;; R21:R18 += 1 << (R24 & 31) ]
1665 ;; SREG is set according to the addition
1666 DEFUN __addmask_4
1667 ;; R25 = 1 << (R24 & 7)
1668 XCALL __mask1
1669 cpi RP, 1 << 4
1670 sbc C0, C0
1671 sbc C1, C1
1672 ;; Swap C2 with C3 if RP.3 is not set
1673 cpi RP, 1 << 3
1674 sbc C2, C2
1675 and C2, C3
1676 eor C3, C2
1677 ;; Swap C3:C2 with C1:C0 if RP.4 is not set
1678 and C0, C2 $ eor C2, C0
1679 and C1, C3 $ eor C3, C1
1680 ;; Finally, add the power-of-two: A[] += C[]
1681 add A0, C0
1682 adc A1, C1
1683 adc A2, C2
1684 adc A3, C3
1685 ret
1686 ENDF __addmask_4
1687 #endif /* L_addmask_4 */
1688
1689 #ifdef L_round_s4
1690
1691 ;; R25:R22 = round (R21:R18, R24)
1692 ;; Clobbers: R18...R21
1693 DEFUN __roundsq3
1694 subi RP, __SQ_FBIT__ - __SA_FBIT__
1695 ENDF __roundsq3
1696 DEFUN __roundsa3
1697 subi RP, __SA_FBIT__ - 1
1698 neg RP
1699 ;; [ R25:R22 = 1 << (FBIT-1 - RP)
1700 ;; R21:R18 += 1 << (FBIT-1 - RP) ]
1701 XCALL __addmask_4
1702 XJMP __round_s4_const
1703 ENDF __roundsa3
1704
1705 #endif /* L_round_s4 */
1706
1707 #ifdef L_round_u4
1708
1709 ;; R25:R22 = round (R21:R18, R24)
1710 ;; Clobbers: R18...R21
1711 DEFUN __roundusq3
1712 subi RP, __USQ_FBIT__ - __USA_FBIT__
1713 ENDF __roundusq3
1714 DEFUN __roundusa3
1715 subi RP, __USA_FBIT__ - 1
1716 neg RP
1717 ;; [ R25:R22 = 1 << (FBIT-1 - RP)
1718 ;; R21:R18 += 1 << (FBIT-1 - RP) ]
1719 XCALL __addmask_4
1720 XJMP __round_u4_const
1721 ENDF __roundusa3
1722
1723 #endif /* L_round_u4 */
1724
1725
1726 #ifdef L_round_4_const
1727
1728 ;; Helpers for 4 byte wide rounding
1729
1730 DEFUN __round_s4_const
1731 brvc 2f
1732 ldi C3, 0x7f
1733 rjmp 1f
1734 ;; FALLTHRU (Barrier)
1735 ENDF __round_s4_const
1736
1737 DEFUN __round_u4_const
1738 brcc 2f
1739 ldi C3, 0xff
1740 1:
1741 ldi C2, 0xff
1742 ldi C1, 0xff
1743 ldi C0, 0xff
1744 rjmp 9f
1745 2:
1746 ;; Saturation is performed now.
1747 ;; Currently, we have C[] = 2^{-RP-1}
1748 ;; C[] = 2^{-RP}
1749 lsl C0
1750 rol C1
1751 rol C2
1752 rol C3
1753 XCALL __negsi2
1754 ;; Clear the bits beyond the rounding point.
1755 and C0, A0
1756 and C1, A1
1757 and C2, A2
1758 and C3, A3
1759 9: ret
1760 ENDF __round_u4_const
1761
1762 #endif /* L_round_4_const */
1763
1764 #undef A0
1765 #undef A1
1766 #undef A2
1767 #undef A3
1768 #undef C0
1769 #undef C1
1770 #undef C2
1771 #undef C3
1772
1773 #undef RP
1774
1775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1776 ;; Rounding, 8 Bytes
1777 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1778
1779 #define RP 16
1780 #define FBITm1 31
1781
1782 #define C0 18
1783 #define C1 C0 + 1
1784 #define C2 C0 + 2
1785 #define C3 C0 + 3
1786 #define C4 C0 + 4
1787 #define C5 C0 + 5
1788 #define C6 C0 + 6
1789 #define C7 C0 + 7
1790
1791 #define A0 16
1792 #define A1 17
1793 #define A2 26
1794 #define A3 27
1795 #define A4 28
1796 #define A5 29
1797 #define A6 30
1798 #define A7 31
1799
1800
1801 #ifdef L_rounddq3
1802 ;; R25:R18 = round (R25:R18, R16)
1803 ;; Clobbers: ABI
1804 DEFUN __rounddq3
1805 ldi FBITm1, __DQ_FBIT__ - 1
1806 clt
1807 XJMP __round_x8
1808 ENDF __rounddq3
1809 #endif /* L_rounddq3 */
1810
1811 #ifdef L_roundudq3
1812 ;; R25:R18 = round (R25:R18, R16)
1813 ;; Clobbers: ABI
1814 DEFUN __roundudq3
1815 ldi FBITm1, __UDQ_FBIT__ - 1
1816 set
1817 XJMP __round_x8
1818 ENDF __roundudq3
1819 #endif /* L_roundudq3 */
1820
1821 #ifdef L_roundda3
1822 ;; R25:R18 = round (R25:R18, R16)
1823 ;; Clobbers: ABI
1824 DEFUN __roundda3
1825 ldi FBITm1, __DA_FBIT__ - 1
1826 clt
1827 XJMP __round_x8
1828 ENDF __roundda3
1829 #endif /* L_roundda3 */
1830
1831 #ifdef L_rounduda3
1832 ;; R25:R18 = round (R25:R18, R16)
1833 ;; Clobbers: ABI
1834 DEFUN __rounduda3
1835 ldi FBITm1, __UDA_FBIT__ - 1
1836 set
1837 XJMP __round_x8
1838 ENDF __rounduda3
1839 #endif /* L_rounduda3 */
1840
1841 #ifdef L_roundta3
1842 ;; R25:R18 = round (R25:R18, R16)
1843 ;; Clobbers: ABI
1844 DEFUN __roundta3
1845 ldi FBITm1, __TA_FBIT__ - 1
1846 clt
1847 XJMP __round_x8
1848 ENDF __roundta3
1849 #endif /* L_roundta3 */
1850
1851 #ifdef L_rounduta3
1852 ;; R25:R18 = round (R25:R18, R16)
1853 ;; Clobbers: ABI
1854 DEFUN __rounduta3
1855 ldi FBITm1, __UTA_FBIT__ - 1
1856 set
1857 XJMP __round_x8
1858 ENDF __rounduta3
1859 #endif /* L_rounduta3 */
1860
1861
1862 #ifdef L_round_x8
1863 DEFUN __round_x8
1864 push r16
1865 push r17
1866 push r28
1867 push r29
1868 ;; Compute log2 of addend from rounding point
1869 sub RP, FBITm1
1870 neg RP
1871 ;; Move input to work register A[]
1872 push C0
1873 mov A1, C1
1874 wmov A2, C2
1875 wmov A4, C4
1876 wmov A6, C6
1877 ;; C[] = 1 << (FBIT-1 - RP)
1878 XCALL __clr_8
1879 inc C0
1880 XCALL __ashldi3
1881 pop A0
1882 ;; A[] += C[]
1883 add A0, C0
1884 adc A1, C1
1885 adc A2, C2
1886 adc A3, C3
1887 adc A4, C4
1888 adc A5, C5
1889 adc A6, C6
1890 adc A7, C7
1891 brts 1f
1892 ;; Signed
1893 brvc 3f
1894 ;; Signed overflow: A[] = 0x7f...
1895 brvs 2f
1896 1: ;; Unsigned
1897 brcc 3f
1898 ;; Unsigned overflow: A[] = 0xff...
1899 2: ldi C7, 0xff
1900 ldi C6, 0xff
1901 wmov C0, C6
1902 wmov C2, C6
1903 wmov C4, C6
1904 bld C7, 7
1905 rjmp 9f
1906 3:
1907 ;; C[] = -C[] - C[]
1908 push A0
1909 ldi r16, 1
1910 XCALL __ashldi3
1911 pop A0
1912 XCALL __negdi2
1913 ;; Clear the bits beyond the rounding point.
1914 and C0, A0
1915 and C1, A1
1916 and C2, A2
1917 and C3, A3
1918 and C4, A4
1919 and C5, A5
1920 and C6, A6
1921 and C7, A7
1922 9: ;; Epilogue
1923 pop r29
1924 pop r28
1925 pop r17
1926 pop r16
1927 ret
1928 ENDF __round_x8
1929
1930 #endif /* L_round_x8 */
1931
1932 #undef A0
1933 #undef A1
1934 #undef A2
1935 #undef A3
1936 #undef A4
1937 #undef A5
1938 #undef A6
1939 #undef A7
1940
1941 #undef C0
1942 #undef C1
1943 #undef C2
1944 #undef C3
1945 #undef C4
1946 #undef C5
1947 #undef C6
1948 #undef C7
1949
1950 #undef RP
1951 #undef FBITm1
1952
1953 #endif /* ! AVR_TINY */
1954
1955 ;; Supply implementations / symbols for the bit-banging functions
1956 ;; __builtin_avr_bitsfx and __builtin_avr_fxbits
1957 #ifdef L_ret
1958 DEFUN __ret
1959 ret
1960 ENDF __ret
1961 #endif /* L_ret */