Debian Codegraph @

libgcc/config/avr/lib1funcs.S

raw
1 /* -*- Mode: Asm -*- */
2 /* Copyright (C) 1998-2025 Free Software Foundation, Inc.
3 Contributed by Denis Chertykov <[email protected]>
4
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
9
10 This file is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
18
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
23
24 #if defined (__AVR_TINY__)
25 #define __zero_reg__ r17
26 #define __tmp_reg__ r16
27 #else
28 #define __zero_reg__ r1
29 #define __tmp_reg__ r0
30 #endif
31 #define __SREG__ 0x3f
32 #if defined (__AVR_HAVE_SPH__)
33 #define __SP_H__ 0x3e
34 #endif
35 #define __SP_L__ 0x3d
36 #define __RAMPZ__ 0x3B
37 #define __EIND__ 0x3C
38
39 /* Most of the functions here are called directly from avr.md
40 patterns, instead of using the standard libcall mechanisms.
41 This can make better code because GCC knows exactly which
42 of the call-used registers (not all of them) are clobbered. */
43
44 /* FIXME: At present, there is no SORT directive in the linker
45 script so that we must not assume that different modules
46 in the same input section like .libgcc.text.mul will be
47 located close together. Therefore, we cannot use
48 RCALL/RJMP to call a function like __udivmodhi4 from
49 __divmodhi4 and have to use lengthy XCALL/XJMP even
50 though they are in the same input section and all same
51 input sections together are small enough to reach every
52 location with a RCALL/RJMP instruction. */
53
54 #if defined (__AVR_HAVE_EIJMP_EICALL__) && !defined (__AVR_HAVE_ELPMX__)
55 #error device not supported
56 #endif
57
58 .macro mov_l r_dest, r_src
59 #if defined (__AVR_HAVE_MOVW__)
60 movw \r_dest, \r_src
61 #else
62 mov \r_dest, \r_src
63 #endif
64 .endm
65
66 .macro mov_h r_dest, r_src
67 #if defined (__AVR_HAVE_MOVW__)
68 ; empty
69 #else
70 mov \r_dest, \r_src
71 #endif
72 .endm
73
74 .macro wmov r_dest, r_src
75 #if defined (__AVR_HAVE_MOVW__)
76 movw \r_dest, \r_src
77 #else
78 mov \r_dest, \r_src
79 mov \r_dest+1, \r_src+1
80 #endif
81 .endm
82
83 .macro mov4 r_dest, r_src
84 wmov \r_dest, \r_src
85 wmov \r_dest+2, \r_src+2
86 .endm
87
88 #if defined (__AVR_HAVE_JMP_CALL__)
89 #define XCALL call
90 #define XJMP jmp
91 #else
92 #define XCALL rcall
93 #define XJMP rjmp
94 #endif
95
96 #if defined (__AVR_HAVE_EIJMP_EICALL__)
97 #define XICALL eicall
98 #define XIJMP eijmp
99 #else
100 #define XICALL icall
101 #define XIJMP ijmp
102 #endif
103
104 ;;; [R]JMP to label \labl when \reg is positive (\reg.7 = 0).
105 ;;; Otherwise, fallthrough.
106 .macro .branch_plus reg, labl
107 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
108 ;; Some cores have a problem skipping 2-word instructions
109 tst \reg
110 brmi .L..\@
111 #else
112 sbrs \reg, 7
113 #endif /* skip erratum */
114 XJMP \labl
115 .L..\@:
116 .endm ; .branch_plus
117
118 ;; Prologue stuff
119
120 .macro do_prologue_saves n_pushed n_frame=0
121 ldi r26, lo8(\n_frame)
122 ldi r27, hi8(\n_frame)
123 ldi r30, lo8(gs(.L_prologue_saves.\@))
124 ldi r31, hi8(gs(.L_prologue_saves.\@))
125 XJMP __prologue_saves__ + ((18 - (\n_pushed)) * 2)
126 .L_prologue_saves.\@:
127 .endm
128
129 ;; Epilogue stuff
130
131 .macro do_epilogue_restores n_pushed n_frame=0
132 in r28, __SP_L__
133 #ifdef __AVR_HAVE_SPH__
134 in r29, __SP_H__
135 .if \n_frame > 63
136 subi r28, lo8(-\n_frame)
137 sbci r29, hi8(-\n_frame)
138 .elseif \n_frame > 0
139 adiw r28, \n_frame
140 .endif
141 #else
142 clr r29
143 .if \n_frame > 0
144 subi r28, lo8(-\n_frame)
145 .endif
146 #endif /* HAVE SPH */
147 ldi r30, \n_pushed
148 XJMP __epilogue_restores__ + ((18 - (\n_pushed)) * 2)
149 .endm
150
151 ;; Support function entry and exit for convenience
152
153 .macro wsubi r_arg1, i_arg2
154 #if defined (__AVR_TINY__)
155 subi \r_arg1, lo8(\i_arg2)
156 sbci \r_arg1+1, hi8(\i_arg2)
157 #else
158 sbiw \r_arg1, \i_arg2
159 #endif
160 .endm
161
162 .macro waddi r_arg1, i_arg2
163 #if defined (__AVR_TINY__)
164 subi \r_arg1, lo8(-\i_arg2)
165 sbci \r_arg1+1, hi8(-\i_arg2)
166 #else
167 adiw \r_arg1, \i_arg2
168 #endif
169 .endm
170
171 .macro DEFUN name
172 .global \name
173 .func \name
174 \name:
175 .endm
176
177 .macro ENDF name
178 .size \name, .-\name
179 .endfunc
180 .endm
181
182 .macro FALIAS name
183 .global \name
184 .func \name
185 \name:
186 .size \name, .-\name
187 .endfunc
188 .endm
189
190 ;; Skip next instruction, typically a jump target
191 #define skip cpse 16,16
192
193 ;; Negate a 2-byte value held in consecutive registers
194 .macro NEG2 reg
195 com \reg+1
196 neg \reg
197 sbci \reg+1, -1
198 .endm
199
200 ;; Negate a 4-byte value held in consecutive registers
201 ;; Sets the V flag for signed overflow tests if REG >= 16
202 .macro NEG4 reg
203 com \reg+3
204 com \reg+2
205 com \reg+1
206 .if \reg >= 16
207 neg \reg
208 sbci \reg+1, -1
209 sbci \reg+2, -1
210 sbci \reg+3, -1
211 .else
212 com \reg
213 adc \reg, __zero_reg__
214 adc \reg+1, __zero_reg__
215 adc \reg+2, __zero_reg__
216 adc \reg+3, __zero_reg__
217 .endif
218 .endm
219
220 #define exp_lo(N) hlo8 ((N) << 23)
221 #define exp_hi(N) hhi8 ((N) << 23)
222
223
224 .section .text.libgcc.mul, "ax", @progbits
225
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 /* Note: mulqi3, mulhi3 are open-coded on the enhanced core. */
228 #if !defined (__AVR_HAVE_MUL__)
229 /*******************************************************
230 Multiplication 8 x 8 without MUL
231 *******************************************************/
232 #if defined (L_mulqi3)
233
234 #define r_arg2 r22 /* multiplicand */
235 #define r_arg1 r24 /* multiplier */
236 #define r_res __tmp_reg__ /* result */
237
238 DEFUN __mulqi3
239 clr r_res ; clear result
240 __mulqi3_loop:
241 sbrc r_arg1,0
242 add r_res,r_arg2
243 add r_arg2,r_arg2 ; shift multiplicand
244 breq __mulqi3_exit ; while multiplicand != 0
245 lsr r_arg1 ;
246 brne __mulqi3_loop ; exit if multiplier = 0
247 __mulqi3_exit:
248 mov r_arg1,r_res ; result to return register
249 ret
250 ENDF __mulqi3
251
252 #undef r_arg2
253 #undef r_arg1
254 #undef r_res
255
256 #endif /* defined (L_mulqi3) */
257
258
259 /*******************************************************
260 Widening Multiplication 16 = 8 x 8 without MUL
261 Multiplication 16 x 16 without MUL
262 *******************************************************/
263
264 #define A0 22
265 #define A1 23
266 #define B0 24
267 #define BB0 20
268 #define B1 25
269 ;; Output overlaps input, thus expand result in CC0/1
270 #define C0 24
271 #define C1 25
272 #define CC0 __tmp_reg__
273 #define CC1 21
274
275 #if defined (L_umulqihi3)
276 ;;; R25:R24 = (unsigned int) R22 * (unsigned int) R24
277 ;;; (C1:C0) = (unsigned int) A0 * (unsigned int) B0
278 ;;; Clobbers: __tmp_reg__, R21..R23
279 DEFUN __umulqihi3
280 clr A1
281 clr B1
282 XJMP __mulhi3
283 ENDF __umulqihi3
284 #endif /* L_umulqihi3 */
285
286 #if defined (L_mulqihi3)
287 ;;; R25:R24 = (signed int) R22 * (signed int) R24
288 ;;; (C1:C0) = (signed int) A0 * (signed int) B0
289 ;;; Clobbers: __tmp_reg__, R20..R23
290 DEFUN __mulqihi3
291 ;; Sign-extend B0
292 clr B1
293 sbrc B0, 7
294 com B1
295 ;; The multiplication runs twice as fast if A1 is zero, thus:
296 ;; Zero-extend A0
297 clr A1
298 #ifdef __AVR_HAVE_JMP_CALL__
299 ;; Store B0 * sign of A
300 clr BB0
301 sbrc A0, 7
302 mov BB0, B0
303 call __mulhi3
304 #else /* have no CALL */
305 ;; Skip sign-extension of A if A >= 0
306 ;; Same size as with the first alternative but avoids errata skip
307 ;; and is faster if A >= 0
308 sbrs A0, 7
309 rjmp __mulhi3
310 ;; If A < 0 store B
311 mov BB0, B0
312 rcall __mulhi3
313 #endif /* HAVE_JMP_CALL */
314 ;; 1-extend A after the multiplication
315 sub C1, BB0
316 ret
317 ENDF __mulqihi3
318 #endif /* L_mulqihi3 */
319
320 #if defined (L_mulhi3)
321 ;;; R25:R24 = R23:R22 * R25:R24
322 ;;; (C1:C0) = (A1:A0) * (B1:B0)
323 ;;; Clobbers: __tmp_reg__, R21..R23
324 DEFUN __mulhi3
325
326 ;; Clear result
327 clr CC0
328 clr CC1
329 rjmp 3f
330 1:
331 ;; Bit n of A is 1 --> C += B << n
332 add CC0, B0
333 adc CC1, B1
334 2:
335 lsl B0
336 rol B1
337 3:
338 ;; If B == 0 we are ready
339 wsubi B0, 0
340 breq 9f
341
342 ;; Carry = n-th bit of A
343 lsr A1
344 ror A0
345 ;; If bit n of A is set, then go add B * 2^n to C
346 brcs 1b
347
348 ;; Carry = 0 --> The ROR above acts like CP A0, 0
349 ;; Thus, it is sufficient to CPC the high part to test A against 0
350 cpc A1, __zero_reg__
351 ;; Only proceed if A != 0
352 brne 2b
353 9:
354 ;; Move Result into place
355 mov C0, CC0
356 mov C1, CC1
357 ret
358 ENDF __mulhi3
359 #endif /* L_mulhi3 */
360
361 #undef A0
362 #undef A1
363 #undef B0
364 #undef BB0
365 #undef B1
366 #undef C0
367 #undef C1
368 #undef CC0
369 #undef CC1
370
371
372 #define A0 22
373 #define A1 A0+1
374 #define A2 A0+2
375 #define A3 A0+3
376
377 #define B0 18
378 #define B1 B0+1
379 #define B2 B0+2
380 #define B3 B0+3
381
382 #define CC0 26
383 #define CC1 CC0+1
384 #define CC2 30
385 #define CC3 CC2+1
386
387 #define C0 22
388 #define C1 C0+1
389 #define C2 C0+2
390 #define C3 C0+3
391
392 /*******************************************************
393 Widening Multiplication 32 = 16 x 16 without MUL
394 *******************************************************/
395
396 #if defined (L_umulhisi3)
397 DEFUN __umulhisi3
398 #ifdef __AVR_TINY__
399 ;; Save callee saved regs.
400 push B0
401 push B1
402 #endif /* AVR_TINY */
403 wmov B0, 24
404 ;; Zero-extend B
405 clr B2
406 clr B3
407 ;; Zero-extend A
408 wmov A2, B2
409 #ifdef __AVR_TINY__
410 ;; Clear hi16 of the result so we can use __mulsi3_helper.
411 wmov CC2, B2
412 XJMP __mulsi3_helper
413 #else
414 XJMP __mulsi3
415 #endif /* AVR_TINY? */
416 ENDF __umulhisi3
417 #endif /* L_umulhisi3 */
418
419 #if defined (L_mulhisi3)
420 DEFUN __mulhisi3
421 #ifdef __AVR_TINY__
422 ;; Save callee saved regs.
423 push B0
424 push B1
425 #endif /* AVR_TINY */
426 wmov B0, 24
427 ;; Sign-extend B
428 lsl r25
429 sbc B2, B2
430 mov B3, B2
431 ;; Zero-extend A and __mulsi3 will run at least twice as fast
432 ;; compared to a sign-extended A.
433 clr A2
434 clr A3
435 ;; Clear hi16 of the result so we can use __mulsi3_helper.
436 wmov CC2, A2
437 sbrs A1, 7
438 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
439 rjmp 1f
440 #else
441 XJMP __mulsi3_helper
442 #endif /* ERRATA_SKIP */
443 ;; If A < 0 then perform the B * 0xffff.... before the
444 ;; very multiplication by initializing the high part of the
445 ;; result CC with -B.
446 sub CC2, B0
447 sbc CC3, B1
448 1: XJMP __mulsi3_helper
449 ENDF __mulhisi3
450 #endif /* L_mulhisi3 */
451
452
453 /*******************************************************
454 Multiplication 32 x 32 without MUL
455 *******************************************************/
456
457 #if defined (L_mulsi3)
458 DEFUN __mulsi3
459 #if defined (__AVR_TINY__)
460 in r26, __SP_L__ ; safe to use X, as it is CC0/CC1
461 in r27, __SP_H__
462 subi r26, lo8(-3) ; Add 3 to point past return address
463 sbci r27, hi8(-3)
464 push B0 ; save callee saved regs
465 push B1
466 ld B0, X+ ; load from caller stack
467 ld B1, X+
468 ld B2, X+
469 ld B3, X
470 #endif
471 ;; Clear result
472 clr CC2
473 clr CC3
474 ;; FALLTHRU
475 ENDF __mulsi3
476
477 DEFUN __mulsi3_helper
478 clr CC0
479 clr CC1
480 rjmp 3f
481
482 1: ;; If bit n of A is set, then add B * 2^n to the result in CC
483 ;; CC += B
484 add CC0,B0 $ adc CC1,B1 $ adc CC2,B2 $ adc CC3,B3
485
486 2: ;; B <<= 1
487 lsl B0 $ rol B1 $ rol B2 $ rol B3
488
489 3: ;; A >>= 1: Carry = n-th bit of A
490 lsr A3 $ ror A2 $ ror A1 $ ror A0
491
492 brcs 1b
493 ;; Only continue if A != 0
494 sbci A1, 0
495 brne 2b
496 wsubi A2, 0
497 brne 2b
498
499 ;; All bits of A are consumed: Copy result to return register C
500 wmov C0, CC0
501 wmov C2, CC2
502 #if defined (__AVR_TINY__)
503 pop B1 ; restore callee saved regs
504 pop B0
505 #endif /* defined (__AVR_TINY__) */
506
507 ret
508 ENDF __mulsi3_helper
509 #endif /* L_mulsi3 */
510
511 #undef A0
512 #undef A1
513 #undef A2
514 #undef A3
515 #undef B0
516 #undef B1
517 #undef B2
518 #undef B3
519 #undef C0
520 #undef C1
521 #undef C2
522 #undef C3
523 #undef CC0
524 #undef CC1
525 #undef CC2
526 #undef CC3
527
528 #endif /* !defined (__AVR_HAVE_MUL__) */
529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
530
531 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532 #if defined (__AVR_HAVE_MUL__)
533 #define A0 26
534 #define B0 18
535 #define C0 22
536
537 #define A1 A0+1
538
539 #define B1 B0+1
540 #define B2 B0+2
541 #define B3 B0+3
542
543 #define C1 C0+1
544 #define C2 C0+2
545 #define C3 C0+3
546
547 /*******************************************************
548 Widening Multiplication 32 = 16 x 16 with MUL
549 *******************************************************/
550
551 #if defined (L_mulhisi3)
552 ;;; R25:R22 = (signed long) R27:R26 * (signed long) R19:R18
553 ;;; C3:C0 = (signed long) A1:A0 * (signed long) B1:B0
554 ;;; Clobbers: __tmp_reg__
555 DEFUN __mulhisi3
556 XCALL __umulhisi3
557 ;; Sign-extend B
558 tst B1
559 brpl 1f
560 sub C2, A0
561 sbc C3, A1
562 1: ;; Sign-extend A
563 XJMP __usmulhisi3_tail
564 ENDF __mulhisi3
565 #endif /* L_mulhisi3 */
566
567 #if defined (L_usmulhisi3)
568 ;;; R25:R22 = (signed long) R27:R26 * (unsigned long) R19:R18
569 ;;; C3:C0 = (signed long) A1:A0 * (unsigned long) B1:B0
570 ;;; Clobbers: __tmp_reg__
571 DEFUN __usmulhisi3
572 XCALL __umulhisi3
573 ;; FALLTHRU
574 ENDF __usmulhisi3
575
576 DEFUN __usmulhisi3_tail
577 ;; Sign-extend A
578 sbrs A1, 7
579 ret
580 sub C2, B0
581 sbc C3, B1
582 ret
583 ENDF __usmulhisi3_tail
584 #endif /* L_usmulhisi3 */
585
586 #if defined (L_umulhisi3)
587 ;;; R25:R22 = (unsigned long) R27:R26 * (unsigned long) R19:R18
588 ;;; C3:C0 = (unsigned long) A1:A0 * (unsigned long) B1:B0
589 ;;; Clobbers: __tmp_reg__
590 DEFUN __umulhisi3
591 mul A0, B0
592 movw C0, r0
593 mul A1, B1
594 movw C2, r0
595 mul A0, B1
596 add C1, r0
597 adc C2, r1
598 clr __zero_reg__
599 adc C3, __zero_reg__
600 mul A1, B0
601 add C1, r0
602 adc C2, r1
603 clr __zero_reg__
604 adc C3, __zero_reg__
605 ret
606 ENDF __umulhisi3
607 #endif /* L_umulhisi3 */
608
609 /*******************************************************
610 Widening Multiplication 32 = 16 x 32 with MUL
611 *******************************************************/
612
613 #if defined (L_mulshisi3)
614 ;;; R25:R22 = (signed long) R27:R26 * R21:R18
615 ;;; (C3:C0) = (signed long) A1:A0 * B3:B0
616 ;;; Clobbers: __tmp_reg__
617 DEFUN __mulshisi3
618 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
619 ;; Some cores have problem skipping 2-word instruction
620 tst A1
621 brmi __mulohisi3
622 #else
623 sbrs A1, 7
624 #endif /* __AVR_HAVE_JMP_CALL__ */
625 XJMP __muluhisi3
626 ;; FALLTHRU
627 ENDF __mulshisi3
628
629 ;;; R25:R22 = (one-extended long) R27:R26 * R21:R18
630 ;;; (C3:C0) = (one-extended long) A1:A0 * B3:B0
631 ;;; Clobbers: __tmp_reg__
632 DEFUN __mulohisi3
633 XCALL __muluhisi3
634 ;; One-extend R27:R26 (A1:A0)
635 sub C2, B0
636 sbc C3, B1
637 ret
638 ENDF __mulohisi3
639 #endif /* L_mulshisi3 */
640
641 #if defined (L_muluhisi3)
642 ;;; R25:R22 = (unsigned long) R27:R26 * R21:R18
643 ;;; (C3:C0) = (unsigned long) A1:A0 * B3:B0
644 ;;; Clobbers: __tmp_reg__
645 DEFUN __muluhisi3
646 XCALL __umulhisi3
647 mul A0, B3
648 add C3, r0
649 mul A1, B2
650 add C3, r0
651 mul A0, B2
652 add C2, r0
653 adc C3, r1
654 clr __zero_reg__
655 ret
656 ENDF __muluhisi3
657 #endif /* L_muluhisi3 */
658
659 /*******************************************************
660 Multiplication 32 x 32 with MUL
661 *******************************************************/
662
663 #if defined (L_mulsi3)
664 ;;; R25:R22 = R25:R22 * R21:R18
665 ;;; (C3:C0) = C3:C0 * B3:B0
666 ;;; Clobbers: R26, R27, __tmp_reg__
667 DEFUN __mulsi3
668 movw A0, C0
669 push C2
670 push C3
671 XCALL __muluhisi3
672 pop A1
673 pop A0
674 ;; A1:A0 now contains the high word of A
675 mul A0, B0
676 add C2, r0
677 adc C3, r1
678 mul A0, B1
679 add C3, r0
680 mul A1, B0
681 add C3, r0
682 clr __zero_reg__
683 ret
684 ENDF __mulsi3
685 #endif /* L_mulsi3 */
686
687 #undef A0
688 #undef A1
689
690 #undef B0
691 #undef B1
692 #undef B2
693 #undef B3
694
695 #undef C0
696 #undef C1
697 #undef C2
698 #undef C3
699
700 #endif /* __AVR_HAVE_MUL__ */
701
702 /*******************************************************
703 Multiplication 24 x 24 with MUL
704 *******************************************************/
705
706 #if defined (L_mulpsi3)
707
708 ;; A[0..2]: In: Multiplicand; Out: Product
709 #define A0 22
710 #define A1 A0+1
711 #define A2 A0+2
712
713 ;; B[0..2]: In: Multiplier
714 #define B0 18
715 #define B1 B0+1
716 #define B2 B0+2
717
718 #if defined (__AVR_HAVE_MUL__)
719
720 ;; C[0..2]: Expand Result
721 #define C0 22
722 #define C1 C0+1
723 #define C2 C0+2
724
725 ;; R24:R22 *= R20:R18
726 ;; Clobbers: r21, r25, r26, r27, __tmp_reg__
727
728 #define AA0 26
729 #define AA2 21
730
731 DEFUN __mulpsi3
732 wmov AA0, A0
733 mov AA2, A2
734 XCALL __umulhisi3
735 mul AA2, B0 $ add C2, r0
736 mul AA0, B2 $ add C2, r0
737 clr __zero_reg__
738 ret
739 ENDF __mulpsi3
740
741 #undef AA2
742 #undef AA0
743
744 #undef C2
745 #undef C1
746 #undef C0
747
748 #else /* !HAVE_MUL */
749 ;; C[0..2]: Expand Result
750 #if defined (__AVR_TINY__)
751 #define C0 16
752 #else
753 #define C0 0
754 #endif /* defined (__AVR_TINY__) */
755 #define C1 C0+1
756 #define C2 21
757
758 ;; R24:R22 *= R20:R18
759 ;; Clobbers: __tmp_reg__, R18, R19, R20, R21
760
761 DEFUN __mulpsi3
762 #if defined (__AVR_TINY__)
763 in r26,__SP_L__
764 in r27,__SP_H__
765 subi r26, lo8(-3) ; Add 3 to point past return address
766 sbci r27, hi8(-3)
767 push B0 ; save callee saved regs
768 push B1
769 ld B0,X+ ; load from caller stack
770 ld B1,X+
771 ld B2,X+
772 #endif /* defined (__AVR_TINY__) */
773
774 ;; C[] = 0
775 clr __tmp_reg__
776 clr C2
777
778 0: ;; Shift N-th Bit of B[] into Carry. N = 24 - Loop
779 LSR B2 $ ror B1 $ ror B0
780
781 ;; If the N-th Bit of B[] was set...
782 brcc 1f
783
784 ;; ...then add A[] * 2^N to the Result C[]
785 ADD C0,A0 $ adc C1,A1 $ adc C2,A2
786
787 1: ;; Multiply A[] by 2
788 LSL A0 $ rol A1 $ rol A2
789
790 ;; Loop until B[] is 0
791 subi B0,0 $ sbci B1,0 $ sbci B2,0
792 brne 0b
793
794 ;; Copy C[] to the return Register A[]
795 wmov A0, C0
796 mov A2, C2
797
798 clr __zero_reg__
799 #if defined (__AVR_TINY__)
800 pop B1
801 pop B0
802 #endif /* (__AVR_TINY__) */
803 ret
804 ENDF __mulpsi3
805
806 #undef C2
807 #undef C1
808 #undef C0
809
810 #endif /* HAVE_MUL */
811
812 #undef B2
813 #undef B1
814 #undef B0
815
816 #undef A2
817 #undef A1
818 #undef A0
819
820 #endif /* L_mulpsi3 */
821
822 #if defined (L_mulsqipsi3) && defined (__AVR_HAVE_MUL__)
823
824 ;; A[0..2]: In: Multiplicand
825 #define A0 22
826 #define A1 A0+1
827 #define A2 A0+2
828
829 ;; BB: In: Multiplier
830 #define BB 25
831
832 ;; C[0..2]: Result
833 #define C0 18
834 #define C1 C0+1
835 #define C2 C0+2
836
837 ;; C[] = A[] * sign_extend (BB)
838 DEFUN __mulsqipsi3
839 mul A0, BB
840 movw C0, r0
841 mul A2, BB
842 mov C2, r0
843 mul A1, BB
844 add C1, r0
845 adc C2, r1
846 clr __zero_reg__
847 sbrs BB, 7
848 ret
849 ;; One-extend BB
850 sub C1, A0
851 sbc C2, A1
852 ret
853 ENDF __mulsqipsi3
854
855 #undef C2
856 #undef C1
857 #undef C0
858
859 #undef BB
860
861 #undef A2
862 #undef A1
863 #undef A0
864
865 #endif /* L_mulsqipsi3 && HAVE_MUL */
866
867 /*******************************************************
868 Multiplication 64 x 64
869 *******************************************************/
870
871 ;; A[] = A[] * B[]
872
873 ;; A[0..7]: In: Multiplicand
874 ;; Out: Product
875 #define A0 18
876 #define A1 A0+1
877 #define A2 A0+2
878 #define A3 A0+3
879 #define A4 A0+4
880 #define A5 A0+5
881 #define A6 A0+6
882 #define A7 A0+7
883
884 ;; B[0..7]: In: Multiplier
885 #define B0 10
886 #define B1 B0+1
887 #define B2 B0+2
888 #define B3 B0+3
889 #define B4 B0+4
890 #define B5 B0+5
891 #define B6 B0+6
892 #define B7 B0+7
893
894 #ifndef __AVR_TINY__
895 #if defined (__AVR_HAVE_MUL__)
896 ;; Define C[] for convenience
897 ;; Notice that parts of C[] overlap A[] respective B[]
898 #define C0 16
899 #define C1 C0+1
900 #define C2 20
901 #define C3 C2+1
902 #define C4 28
903 #define C5 C4+1
904 #define C6 C4+2
905 #define C7 C4+3
906
907 #if defined (L_muldi3)
908
909 ;; A[] *= B[]
910 ;; R25:R18 *= R17:R10
911 ;; Ordinary ABI-Function
912
913 DEFUN __muldi3
914 push r29
915 push r28
916 push r17
917 push r16
918
919 ;; Counting in Words, we have to perform a 4 * 4 Multiplication
920
921 ;; 3 * 0 + 0 * 3
922 mul A7,B0 $ $ mov C7,r0
923 mul A0,B7 $ $ add C7,r0
924 mul A6,B1 $ $ add C7,r0
925 mul A6,B0 $ mov C6,r0 $ add C7,r1
926 mul B6,A1 $ $ add C7,r0
927 mul B6,A0 $ add C6,r0 $ adc C7,r1
928
929 ;; 1 * 2
930 mul A2,B4 $ add C6,r0 $ adc C7,r1
931 mul A3,B4 $ $ add C7,r0
932 mul A2,B5 $ $ add C7,r0
933
934 push A5
935 push A4
936 push B1
937 push B0
938 push A3
939 push A2
940
941 ;; 0 * 0
942 wmov 26, B0
943 XCALL __umulhisi3
944 wmov C0, 22
945 wmov C2, 24
946
947 ;; 0 * 2
948 wmov 26, B4
949 XCALL __umulhisi3 $ wmov C4,22 $ add C6,24 $ adc C7,25
950
951 wmov 26, B2
952 ;; 0 * 1
953 XCALL __muldi3_6
954
955 pop A0
956 pop A1
957 ;; 1 * 1
958 wmov 26, B2
959 XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25
960
961 pop r26
962 pop r27
963 ;; 1 * 0
964 XCALL __muldi3_6
965
966 pop A0
967 pop A1
968 ;; 2 * 0
969 XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25
970
971 ;; 2 * 1
972 wmov 26, B2
973 XCALL __umulhisi3 $ $ $ add C6,22 $ adc C7,23
974
975 ;; A[] = C[]
976 wmov A0, C0
977 ;; A2 = C2 already
978 wmov A4, C4
979 wmov A6, C6
980
981 pop r16
982 pop r17
983 pop r28
984 pop r29
985 ret
986 ENDF __muldi3
987 #endif /* L_muldi3 */
988
989 #if defined (L_muldi3_6)
990 ;; A helper for some 64-bit multiplications with MUL available
991 DEFUN __muldi3_6
992 __muldi3_6:
993 XCALL __umulhisi3
994 add C2, 22
995 adc C3, 23
996 adc C4, 24
997 adc C5, 25
998 brcc 0f
999 adiw C6, 1
1000 0: ret
1001 ENDF __muldi3_6
1002 #endif /* L_muldi3_6 */
1003
1004 #undef C7
1005 #undef C6
1006 #undef C5
1007 #undef C4
1008 #undef C3
1009 #undef C2
1010 #undef C1
1011 #undef C0
1012
1013 #else /* !HAVE_MUL */
1014
1015 #if defined (L_muldi3)
1016
1017 #define C0 26
1018 #define C1 C0+1
1019 #define C2 C0+2
1020 #define C3 C0+3
1021 #define C4 C0+4
1022 #define C5 C0+5
1023 #define C6 0
1024 #define C7 C6+1
1025
1026 #define Loop 9
1027
1028 ;; A[] *= B[]
1029 ;; R25:R18 *= R17:R10
1030 ;; Ordinary ABI-Function
1031
1032 DEFUN __muldi3
1033 push r29
1034 push r28
1035 push Loop
1036
1037 ldi C0, 64
1038 mov Loop, C0
1039
1040 ;; C[] = 0
1041 clr __tmp_reg__
1042 wmov C0, 0
1043 wmov C2, 0
1044 wmov C4, 0
1045
1046 0: ;; Rotate B[] right by 1 and set Carry to the N-th Bit of B[]
1047 ;; where N = 64 - Loop.
1048 ;; Notice that B[] = B[] >>> 64 so after this Routine has finished,
1049 ;; B[] will have its initial Value again.
1050 LSR B7 $ ror B6 $ ror B5 $ ror B4
1051 ror B3 $ ror B2 $ ror B1 $ ror B0
1052
1053 ;; If the N-th Bit of B[] was set then...
1054 brcc 1f
1055 ;; ...finish Rotation...
1056 ori B7, 1 << 7
1057
1058 ;; ...and add A[] * 2^N to the Result C[]
1059 ADD C0,A0 $ adc C1,A1 $ adc C2,A2 $ adc C3,A3
1060 adc C4,A4 $ adc C5,A5 $ adc C6,A6 $ adc C7,A7
1061
1062 1: ;; Multiply A[] by 2
1063 LSL A0 $ rol A1 $ rol A2 $ rol A3
1064 rol A4 $ rol A5 $ rol A6 $ rol A7
1065
1066 dec Loop
1067 brne 0b
1068
1069 ;; We expanded the Result in C[]
1070 ;; Copy Result to the Return Register A[]
1071 wmov A0, C0
1072 wmov A2, C2
1073 wmov A4, C4
1074 wmov A6, C6
1075
1076 clr __zero_reg__
1077 pop Loop
1078 pop r28
1079 pop r29
1080 ret
1081 ENDF __muldi3
1082
1083 #undef Loop
1084
1085 #undef C7
1086 #undef C6
1087 #undef C5
1088 #undef C4
1089 #undef C3
1090 #undef C2
1091 #undef C1
1092 #undef C0
1093
1094 #endif /* L_muldi3 */
1095 #endif /* HAVE_MUL */
1096 #endif /* if not __AVR_TINY__ */
1097
1098 #undef B7
1099 #undef B6
1100 #undef B5
1101 #undef B4
1102 #undef B3
1103 #undef B2
1104 #undef B1
1105 #undef B0
1106
1107 #undef A7
1108 #undef A6
1109 #undef A5
1110 #undef A4
1111 #undef A3
1112 #undef A2
1113 #undef A1
1114 #undef A0
1115
1116 /*******************************************************
1117 Widening Multiplication 64 = 32 x 32 with MUL
1118 *******************************************************/
1119
1120 #if defined (__AVR_HAVE_MUL__)
1121 #define A0 r22
1122 #define A1 r23
1123 #define A2 r24
1124 #define A3 r25
1125
1126 #define B0 r18
1127 #define B1 r19
1128 #define B2 r20
1129 #define B3 r21
1130
1131 #define C0 18
1132 #define C1 C0+1
1133 #define C2 20
1134 #define C3 C2+1
1135 #define C4 28
1136 #define C5 C4+1
1137 #define C6 C4+2
1138 #define C7 C4+3
1139
1140 #if defined (L_umulsidi3)
1141
1142 ;; Unsigned widening 64 = 32 * 32 Multiplication with MUL
1143
1144 ;; R18[8] = R22[4] * R18[4]
1145 ;;
1146 ;; Ordinary ABI Function, but additionally sets
1147 ;; X = R20[2] = B2[2]
1148 ;; Z = R22[2] = A0[2]
1149 DEFUN __umulsidi3
1150 clt
1151 ;; FALLTHRU
1152 ENDF __umulsidi3
1153 ;; T = sign (A)
1154 DEFUN __umulsidi3_helper
1155 push 29 $ push 28 ; Y
1156 wmov 30, A2
1157 ;; Counting in Words, we have to perform 4 Multiplications
1158 ;; 0 * 0
1159 wmov 26, A0
1160 XCALL __umulhisi3
1161 push 23 $ push 22 ; C0
1162 wmov 28, B0
1163 wmov 18, B2
1164 wmov C2, 24
1165 push 27 $ push 26 ; A0
1166 push 19 $ push 18 ; B2
1167 ;;
1168 ;; 18 20 22 24 26 28 30 | B2, B3, A0, A1, C0, C1, Y
1169 ;; B2 C2 -- -- -- B0 A2
1170 ;; 1 * 1
1171 wmov 26, 30 ; A2
1172 XCALL __umulhisi3
1173 ;; Sign-extend A. T holds the sign of A
1174 brtc 0f
1175 ;; Subtract B from the high part of the result
1176 sub 22, 28
1177 sbc 23, 29
1178 sbc 24, 18
1179 sbc 25, 19
1180 0: wmov 18, 28 ;; B0
1181 wmov C4, 22
1182 wmov C6, 24
1183 ;;
1184 ;; 18 20 22 24 26 28 30 | B2, B3, A0, A1, C0, C1, Y
1185 ;; B0 C2 -- -- A2 C4 C6
1186 ;;
1187 ;; 1 * 0
1188 XCALL __muldi3_6
1189 ;; 0 * 1
1190 pop 26 $ pop 27 ;; B2
1191 pop 18 $ pop 19 ;; A0
1192 XCALL __muldi3_6
1193
1194 ;; Move result C into place and save A0 in Z
1195 wmov 22, C4
1196 wmov 24, C6
1197 wmov 30, 18 ; A0
1198 pop C0 $ pop C1
1199
1200 ;; Epilogue
1201 pop 28 $ pop 29 ;; Y
1202 ret
1203 ENDF __umulsidi3_helper
1204 #endif /* L_umulsidi3 */
1205
1206
1207 #if defined (L_mulsidi3)
1208
1209 ;; Signed widening 64 = 32 * 32 Multiplication
1210 ;;
1211 ;; R18[8] = R22[4] * R18[4]
1212 ;; Ordinary ABI Function
1213 DEFUN __mulsidi3
1214 bst A3, 7
1215 sbrs B3, 7 ; Enhanced core has no skip bug
1216 XJMP __umulsidi3_helper
1217
1218 ;; B needs sign-extension
1219 push A3
1220 push A2
1221 XCALL __umulsidi3_helper
1222 ;; A0 survived in Z
1223 sub r22, r30
1224 sbc r23, r31
1225 pop r26
1226 pop r27
1227 sbc r24, r26
1228 sbc r25, r27
1229 ret
1230 ENDF __mulsidi3
1231 #endif /* L_mulsidi3 */
1232
1233 #undef A0
1234 #undef A1
1235 #undef A2
1236 #undef A3
1237 #undef B0
1238 #undef B1
1239 #undef B2
1240 #undef B3
1241 #undef C0
1242 #undef C1
1243 #undef C2
1244 #undef C3
1245 #undef C4
1246 #undef C5
1247 #undef C6
1248 #undef C7
1249 #endif /* HAVE_MUL */
1250
1251 /**********************************************************
1252 Widening Multiplication 64 = 32 x 32 without MUL
1253 **********************************************************/
1254 #ifndef __AVR_TINY__ /* if not __AVR_TINY__ */
1255 #if defined (L_mulsidi3) && !defined (__AVR_HAVE_MUL__)
1256 #define A0 18
1257 #define A1 A0+1
1258 #define A2 A0+2
1259 #define A3 A0+3
1260 #define A4 A0+4
1261 #define A5 A0+5
1262 #define A6 A0+6
1263 #define A7 A0+7
1264
1265 #define B0 10
1266 #define B1 B0+1
1267 #define B2 B0+2
1268 #define B3 B0+3
1269 #define B4 B0+4
1270 #define B5 B0+5
1271 #define B6 B0+6
1272 #define B7 B0+7
1273
1274 #define AA0 22
1275 #define AA1 AA0+1
1276 #define AA2 AA0+2
1277 #define AA3 AA0+3
1278
1279 #define BB0 18
1280 #define BB1 BB0+1
1281 #define BB2 BB0+2
1282 #define BB3 BB0+3
1283
1284 #define Mask r30
1285
1286 ;; Signed / Unsigned widening 64 = 32 * 32 Multiplication without MUL
1287 ;;
1288 ;; R18[8] = R22[4] * R18[4]
1289 ;; Ordinary ABI Function
1290 DEFUN __mulsidi3
1291 set
1292 skip
1293 ;; FALLTHRU
1294 ENDF __mulsidi3
1295
1296 DEFUN __umulsidi3
1297 clt ; skipped
1298 ;; Save 10 Registers: R10..R17, R28, R29
1299 do_prologue_saves 10
1300 ldi Mask, 0xff
1301 bld Mask, 7
1302 ;; Move B into place...
1303 wmov B0, BB0
1304 wmov B2, BB2
1305 ;; ...and extend it
1306 and BB3, Mask
1307 lsl BB3
1308 sbc B4, B4
1309 mov B5, B4
1310 wmov B6, B4
1311 ;; Move A into place...
1312 wmov A0, AA0
1313 wmov A2, AA2
1314 ;; ...and extend it
1315 and AA3, Mask
1316 lsl AA3
1317 sbc A4, A4
1318 mov A5, A4
1319 wmov A6, A4
1320 XCALL __muldi3
1321 do_epilogue_restores 10
1322 ENDF __umulsidi3
1323
1324 #undef A0
1325 #undef A1
1326 #undef A2
1327 #undef A3
1328 #undef A4
1329 #undef A5
1330 #undef A6
1331 #undef A7
1332 #undef B0
1333 #undef B1
1334 #undef B2
1335 #undef B3
1336 #undef B4
1337 #undef B5
1338 #undef B6
1339 #undef B7
1340 #undef AA0
1341 #undef AA1
1342 #undef AA2
1343 #undef AA3
1344 #undef BB0
1345 #undef BB1
1346 #undef BB2
1347 #undef BB3
1348 #undef Mask
1349 #endif /* L_mulsidi3 && !HAVE_MUL */
1350 #endif /* if not __AVR_TINY__ */
1351 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1352
1353
1354 .section .text.libgcc.div, "ax", @progbits
1355
1356 /*******************************************************
1357 Division 8 / 8 => (result + remainder)
1358 *******************************************************/
1359 #define r_rem r25 /* remainder */
1360 #define r_arg1 r24 /* dividend, quotient */
1361 #define r_arg2 r22 /* divisor */
1362 #define r_cnt r23 /* loop count */
1363
1364 #if defined (L_udivmodqi4)
1365 DEFUN __udivmodqi4
1366 clr r_rem ; clear remainder
1367 ldi r_cnt,8 ; init loop counter
1368 lsl r_arg1 ; shift dividend
1369 __udivmodqi4_loop:
1370 rol r_rem ; shift dividend into remainder
1371 cp r_rem,r_arg2 ; compare remainder & divisor
1372 brcs __udivmodqi4_ep ; remainder <= divisor
1373 sub r_rem,r_arg2 ; restore remainder
1374 __udivmodqi4_ep:
1375 rol r_arg1 ; shift dividend (with CARRY)
1376 dec r_cnt ; decrement loop counter
1377 brne __udivmodqi4_loop
1378 com r_arg1 ; complement result
1379 ; because C flag was complemented in loop
1380 ret
1381 ENDF __udivmodqi4
1382 #endif /* defined (L_udivmodqi4) */
1383
1384 #if defined (L_divmodqi4)
1385 DEFUN __divmodqi4
1386 bst r_arg1,7 ; store sign of dividend
1387 mov __tmp_reg__,r_arg1
1388 eor __tmp_reg__,r_arg2; r0.7 is sign of result
1389 sbrc r_arg1,7
1390 neg r_arg1 ; dividend negative : negate
1391 sbrc r_arg2,7
1392 neg r_arg2 ; divisor negative : negate
1393 XCALL __udivmodqi4 ; do the unsigned div/mod
1394 brtc __divmodqi4_1
1395 neg r_rem ; correct remainder sign
1396 __divmodqi4_1:
1397 sbrc __tmp_reg__,7
1398 neg r_arg1 ; correct result sign
1399 __divmodqi4_exit:
1400 ret
1401 ENDF __divmodqi4
1402 #endif /* defined (L_divmodqi4) */
1403
1404 #undef r_rem
1405 #undef r_arg1
1406 #undef r_arg2
1407 #undef r_cnt
1408
1409
1410 /*******************************************************
1411 Division 16 / 16 => (result + remainder)
1412 *******************************************************/
1413 #define r_remL r26 /* remainder Low */
1414 #define r_remH r27 /* remainder High */
1415
1416 /* return: remainder */
1417 #define r_arg1L r24 /* dividend Low */
1418 #define r_arg1H r25 /* dividend High */
1419
1420 /* return: quotient */
1421 #define r_arg2L r22 /* divisor Low */
1422 #define r_arg2H r23 /* divisor High */
1423
1424 #define r_cnt r21 /* loop count */
1425
1426 #if defined (L_udivmodhi4)
1427 DEFUN __udivmodhi4
1428 sub r_remL,r_remL
1429 sub r_remH,r_remH ; clear remainder and carry
1430 ldi r_cnt,17 ; init loop counter
1431 rjmp __udivmodhi4_ep ; jump to entry point
1432 __udivmodhi4_loop:
1433 rol r_remL ; shift dividend into remainder
1434 rol r_remH
1435 cp r_remL,r_arg2L ; compare remainder & divisor
1436 cpc r_remH,r_arg2H
1437 brcs __udivmodhi4_ep ; remainder < divisor
1438 sub r_remL,r_arg2L ; restore remainder
1439 sbc r_remH,r_arg2H
1440 __udivmodhi4_ep:
1441 rol r_arg1L ; shift dividend (with CARRY)
1442 rol r_arg1H
1443 dec r_cnt ; decrement loop counter
1444 brne __udivmodhi4_loop
1445 com r_arg1L
1446 com r_arg1H
1447 ; div/mod results to return registers, as for the div() function
1448 mov_l r_arg2L, r_arg1L ; quotient
1449 mov_h r_arg2H, r_arg1H
1450 mov_l r_arg1L, r_remL ; remainder
1451 mov_h r_arg1H, r_remH
1452 ret
1453 ENDF __udivmodhi4
1454 #endif /* defined (L_udivmodhi4) */
1455
1456 #if defined (L_divmodhi4)
1457 DEFUN __divmodhi4
1458 .global _div
1459 _div:
1460 bst r_arg1H,7 ; store sign of dividend
1461 mov __tmp_reg__,r_arg2H
1462 brtc 0f
1463 com __tmp_reg__ ; r0.7 is sign of result
1464 rcall __divmodhi4_neg1 ; dividend negative: negate
1465 0:
1466 sbrc r_arg2H,7
1467 rcall __divmodhi4_neg2 ; divisor negative: negate
1468 XCALL __udivmodhi4 ; do the unsigned div/mod
1469 sbrc __tmp_reg__,7
1470 rcall __divmodhi4_neg2 ; correct remainder sign
1471 brtc __divmodhi4_exit
1472 __divmodhi4_neg1:
1473 ;; correct dividend/remainder sign
1474 com r_arg1H
1475 neg r_arg1L
1476 sbci r_arg1H,0xff
1477 ret
1478 __divmodhi4_neg2:
1479 ;; correct divisor/result sign
1480 com r_arg2H
1481 neg r_arg2L
1482 sbci r_arg2H,0xff
1483 __divmodhi4_exit:
1484 ret
1485 ENDF __divmodhi4
1486 #endif /* defined (L_divmodhi4) */
1487
1488 #undef r_remH
1489 #undef r_remL
1490
1491 #undef r_arg1H
1492 #undef r_arg1L
1493
1494 #undef r_arg2H
1495 #undef r_arg2L
1496
1497 #undef r_cnt
1498
1499 /*******************************************************
1500 Division 24 / 24 => (result + remainder)
1501 *******************************************************/
1502
1503 ;; A[0..2]: In: Dividend; Out: Quotient
1504 #define A0 22
1505 #define A1 A0+1
1506 #define A2 A0+2
1507
1508 ;; B[0..2]: In: Divisor; Out: Remainder
1509 #define B0 18
1510 #define B1 B0+1
1511 #define B2 B0+2
1512
1513 ;; C[0..2]: Expand remainder
1514 #define C0 __zero_reg__
1515 #define C1 26
1516 #define C2 25
1517
1518 ;; Loop counter
1519 #define r_cnt 21
1520
1521 #if defined (L_udivmodpsi4)
1522 ;; R24:R22 = R24:R24 udiv R20:R18
1523 ;; R20:R18 = R24:R22 umod R20:R18
1524 ;; Clobbers: R21, R25, R26
1525
1526 DEFUN __udivmodpsi4
1527 ; init loop counter
1528 ldi r_cnt, 24+1
1529 ; Clear remainder and carry. C0 is already 0
1530 clr C1
1531 sub C2, C2
1532 ; jump to entry point
1533 rjmp __udivmodpsi4_start
1534 __udivmodpsi4_loop:
1535 ; shift dividend into remainder
1536 rol C0
1537 rol C1
1538 rol C2
1539 ; compare remainder & divisor
1540 cp C0, B0
1541 cpc C1, B1
1542 cpc C2, B2
1543 brcs __udivmodpsi4_start ; remainder <= divisor
1544 sub C0, B0 ; restore remainder
1545 sbc C1, B1
1546 sbc C2, B2
1547 __udivmodpsi4_start:
1548 ; shift dividend (with CARRY)
1549 rol A0
1550 rol A1
1551 rol A2
1552 ; decrement loop counter
1553 dec r_cnt
1554 brne __udivmodpsi4_loop
1555 com A0
1556 com A1
1557 com A2
1558 ; div/mod results to return registers
1559 ; remainder
1560 mov B0, C0
1561 mov B1, C1
1562 mov B2, C2
1563 clr __zero_reg__ ; C0
1564 ret
1565 ENDF __udivmodpsi4
1566 #endif /* defined (L_udivmodpsi4) */
1567
1568 #if defined (L_divmodpsi4)
1569 ;; R24:R22 = R24:R22 div R20:R18
1570 ;; R20:R18 = R24:R22 mod R20:R18
1571 ;; Clobbers: T, __tmp_reg__, R21, R25, R26
1572
1573 DEFUN __divmodpsi4
1574 ; R0.7 will contain the sign of the result:
1575 ; R0.7 = A.sign ^ B.sign
1576 mov __tmp_reg__, B2
1577 ; T-flag = sign of dividend
1578 bst A2, 7
1579 brtc 0f
1580 com __tmp_reg__
1581 ; Adjust dividend's sign
1582 rcall __divmodpsi4_negA
1583 0:
1584 ; Adjust divisor's sign
1585 sbrc B2, 7
1586 rcall __divmodpsi4_negB
1587
1588 ; Do the unsigned div/mod
1589 XCALL __udivmodpsi4
1590
1591 ; Adjust quotient's sign
1592 sbrc __tmp_reg__, 7
1593 rcall __divmodpsi4_negA
1594
1595 ; Adjust remainder's sign
1596 brtc __divmodpsi4_end
1597
1598 __divmodpsi4_negB:
1599 ; Correct divisor/remainder sign
1600 com B2
1601 com B1
1602 neg B0
1603 sbci B1, -1
1604 sbci B2, -1
1605 ret
1606
1607 ; Correct dividend/quotient sign
1608 __divmodpsi4_negA:
1609 com A2
1610 com A1
1611 neg A0
1612 sbci A1, -1
1613 sbci A2, -1
1614 __divmodpsi4_end:
1615 ret
1616
1617 ENDF __divmodpsi4
1618 #endif /* defined (L_divmodpsi4) */
1619
1620 #undef A0
1621 #undef A1
1622 #undef A2
1623
1624 #undef B0
1625 #undef B1
1626 #undef B2
1627
1628 #undef C0
1629 #undef C1
1630 #undef C2
1631
1632 #undef r_cnt
1633
1634 /*******************************************************
1635 Division 32 / 32 => (result + remainder)
1636 *******************************************************/
1637 #define r_remHH r31 /* remainder High */
1638 #define r_remHL r30
1639 #define r_remH r27
1640 #define r_remL r26 /* remainder Low */
1641
1642 /* return: remainder */
1643 #define r_arg1HH r25 /* dividend High */
1644 #define r_arg1HL r24
1645 #define r_arg1H r23
1646 #define r_arg1L r22 /* dividend Low */
1647
1648 /* return: quotient */
1649 #define r_arg2HH r21 /* divisor High */
1650 #define r_arg2HL r20
1651 #define r_arg2H r19
1652 #define r_arg2L r18 /* divisor Low */
1653
1654 #define r_cnt __zero_reg__ /* loop count (0 after the loop!) */
1655
1656 #if defined (L_udivmodsi4)
1657 DEFUN __udivmodsi4
1658 ldi r_remL, 33 ; init loop counter
1659 mov r_cnt, r_remL
1660 sub r_remL,r_remL
1661 sub r_remH,r_remH ; clear remainder and carry
1662 mov_l r_remHL, r_remL
1663 mov_h r_remHH, r_remH
1664 rjmp __udivmodsi4_ep ; jump to entry point
1665 __udivmodsi4_loop:
1666 rol r_remL ; shift dividend into remainder
1667 rol r_remH
1668 rol r_remHL
1669 rol r_remHH
1670 cp r_remL,r_arg2L ; compare remainder & divisor
1671 cpc r_remH,r_arg2H
1672 cpc r_remHL,r_arg2HL
1673 cpc r_remHH,r_arg2HH
1674 brcs __udivmodsi4_ep ; remainder <= divisor
1675 sub r_remL,r_arg2L ; restore remainder
1676 sbc r_remH,r_arg2H
1677 sbc r_remHL,r_arg2HL
1678 sbc r_remHH,r_arg2HH
1679 __udivmodsi4_ep:
1680 rol r_arg1L ; shift dividend (with CARRY)
1681 rol r_arg1H
1682 rol r_arg1HL
1683 rol r_arg1HH
1684 dec r_cnt ; decrement loop counter
1685 brne __udivmodsi4_loop
1686 ; __zero_reg__ now restored (r_cnt == 0)
1687 com r_arg1L
1688 com r_arg1H
1689 com r_arg1HL
1690 com r_arg1HH
1691 ; div/mod results to return registers, as for the ldiv() function
1692 mov_l r_arg2L, r_arg1L ; quotient
1693 mov_h r_arg2H, r_arg1H
1694 mov_l r_arg2HL, r_arg1HL
1695 mov_h r_arg2HH, r_arg1HH
1696 mov_l r_arg1L, r_remL ; remainder
1697 mov_h r_arg1H, r_remH
1698 mov_l r_arg1HL, r_remHL
1699 mov_h r_arg1HH, r_remHH
1700 ret
1701 ENDF __udivmodsi4
1702 #endif /* defined (L_udivmodsi4) */
1703
1704 #if defined (L_divmodsi4)
1705 DEFUN __divmodsi4
1706 mov __tmp_reg__,r_arg2HH
1707 bst r_arg1HH,7 ; store sign of dividend
1708 brtc 0f
1709 com __tmp_reg__ ; r0.7 is sign of result
1710 XCALL __negsi2 ; dividend negative: negate
1711 0:
1712 sbrc r_arg2HH,7
1713 rcall __divmodsi4_neg2 ; divisor negative: negate
1714 XCALL __udivmodsi4 ; do the unsigned div/mod
1715 sbrc __tmp_reg__, 7 ; correct quotient sign
1716 rcall __divmodsi4_neg2
1717 brtc __divmodsi4_exit ; correct remainder sign
1718 XJMP __negsi2
1719 __divmodsi4_neg2:
1720 ;; correct divisor/quotient sign
1721 com r_arg2HH
1722 com r_arg2HL
1723 com r_arg2H
1724 neg r_arg2L
1725 sbci r_arg2H,0xff
1726 sbci r_arg2HL,0xff
1727 sbci r_arg2HH,0xff
1728 __divmodsi4_exit:
1729 ret
1730 ENDF __divmodsi4
1731 #endif /* defined (L_divmodsi4) */
1732
1733 #if defined (L_negsi2)
1734 ;; (set (reg:SI 22)
1735 ;; (neg:SI (reg:SI 22)))
1736 ;; Sets the V flag for signed overflow tests
1737 DEFUN __negsi2
1738 NEG4 22
1739 ret
1740 ENDF __negsi2
1741 #endif /* L_negsi2 */
1742
1743 #undef r_remHH
1744 #undef r_remHL
1745 #undef r_remH
1746 #undef r_remL
1747 #undef r_arg1HH
1748 #undef r_arg1HL
1749 #undef r_arg1H
1750 #undef r_arg1L
1751 #undef r_arg2HH
1752 #undef r_arg2HL
1753 #undef r_arg2H
1754 #undef r_arg2L
1755 #undef r_cnt
1756
1757 /* *di routines use registers below R19 and won't work with tiny arch
1758 right now. */
1759
1760 #if !defined (__AVR_TINY__)
1761 /*******************************************************
1762 Division 64 / 64
1763 Modulo 64 % 64
1764 *******************************************************/
1765
1766 ;; Use Speed-optimized Version on "big" Devices, i.e. Devices with
1767 ;; at least 16k of Program Memory. For smaller Devices, depend
1768 ;; on MOVW and SP Size. There is a Connexion between SP Size and
1769 ;; Flash Size so that SP Size can be used to test for Flash Size.
1770
1771 #if defined (__AVR_HAVE_JMP_CALL__)
1772 # define SPEED_DIV 8
1773 #elif defined (__AVR_HAVE_MOVW__) && defined (__AVR_HAVE_SPH__)
1774 # define SPEED_DIV 16
1775 #else
1776 # define SPEED_DIV 0
1777 #endif
1778
1779 ;; A[0..7]: In: Dividend;
1780 ;; Out: Quotient (T = 0)
1781 ;; Out: Remainder (T = 1)
1782 #define A0 18
1783 #define A1 A0+1
1784 #define A2 A0+2
1785 #define A3 A0+3
1786 #define A4 A0+4
1787 #define A5 A0+5
1788 #define A6 A0+6
1789 #define A7 A0+7
1790
1791 ;; B[0..7]: In: Divisor; Out: Clobber
1792 #define B0 10
1793 #define B1 B0+1
1794 #define B2 B0+2
1795 #define B3 B0+3
1796 #define B4 B0+4
1797 #define B5 B0+5
1798 #define B6 B0+6
1799 #define B7 B0+7
1800
1801 ;; C[0..7]: Expand remainder; Out: Remainder (unused)
1802 #define C0 8
1803 #define C1 C0+1
1804 #define C2 30
1805 #define C3 C2+1
1806 #define C4 28
1807 #define C5 C4+1
1808 #define C6 26
1809 #define C7 C6+1
1810
1811 ;; Holds Signs during Division Routine
1812 #define SS __tmp_reg__
1813
1814 ;; Bit-Counter in Division Routine
1815 #define R_cnt __zero_reg__
1816
1817 ;; Scratch Register for Negation
1818 #define NN r31
1819
1820 #if defined (L_udivdi3)
1821
1822 ;; R25:R18 = R24:R18 umod R17:R10
1823 ;; Ordinary ABI-Function
1824
1825 DEFUN __umoddi3
1826 set
1827 rjmp __udivdi3_umoddi3
1828 ENDF __umoddi3
1829
1830 ;; R25:R18 = R24:R18 udiv R17:R10
1831 ;; Ordinary ABI-Function
1832
1833 DEFUN __udivdi3
1834 clt
1835 ENDF __udivdi3
1836
1837 DEFUN __udivdi3_umoddi3
1838 push C0
1839 push C1
1840 push C4
1841 push C5
1842 XCALL __udivmod64
1843 pop C5
1844 pop C4
1845 pop C1
1846 pop C0
1847 ret
1848 ENDF __udivdi3_umoddi3
1849 #endif /* L_udivdi3 */
1850
1851 #if defined (L_udivmod64)
1852
1853 ;; Worker Routine for 64-Bit unsigned Quotient and Remainder Computation
1854 ;; No Registers saved/restored; the Callers will take Care.
1855 ;; Preserves B[] and T-flag
1856 ;; T = 0: Compute Quotient in A[]
1857 ;; T = 1: Compute Remainder in A[] and shift SS one Bit left
1858
1859 DEFUN __udivmod64
1860
1861 ;; Clear Remainder (C6, C7 will follow)
1862 clr C0
1863 clr C1
1864 wmov C2, C0
1865 wmov C4, C0
1866 ldi C7, 64
1867
1868 #if SPEED_DIV == 0 || SPEED_DIV == 16
1869 ;; Initialize Loop-Counter
1870 mov R_cnt, C7
1871 wmov C6, C0
1872 #endif /* SPEED_DIV */
1873
1874 #if SPEED_DIV == 8
1875
1876 push A7
1877 clr C6
1878
1879 1: ;; Compare shifted Devidend against Divisor
1880 ;; If -- even after Shifting -- it is smaller...
1881 CP A7,B0 $ cpc C0,B1 $ cpc C1,B2 $ cpc C2,B3
1882 cpc C3,B4 $ cpc C4,B5 $ cpc C5,B6 $ cpc C6,B7
1883 brcc 2f
1884
1885 ;; ...then we can subtract it. Thus, it is legal to shift left
1886 $ mov C6,C5 $ mov C5,C4 $ mov C4,C3
1887 mov C3,C2 $ mov C2,C1 $ mov C1,C0 $ mov C0,A7
1888 mov A7,A6 $ mov A6,A5 $ mov A5,A4 $ mov A4,A3
1889 mov A3,A2 $ mov A2,A1 $ mov A1,A0 $ clr A0
1890
1891 ;; 8 Bits are done
1892 subi C7, 8
1893 brne 1b
1894
1895 ;; Shifted 64 Bits: A7 has traveled to C7
1896 pop C7
1897 ;; Divisor is greater than Dividend. We have:
1898 ;; A[] % B[] = A[]
1899 ;; A[] / B[] = 0
1900 ;; Thus, we can return immediately
1901 rjmp 5f
1902
1903 2: ;; Initialze Bit-Counter with Number of Bits still to be performed
1904 mov R_cnt, C7
1905
1906 ;; Push of A7 is not needed because C7 is still 0
1907 pop C7
1908 clr C7
1909
1910 #elif SPEED_DIV == 16
1911
1912 ;; Compare shifted Dividend against Divisor
1913 cp A7, B3
1914 cpc C0, B4
1915 cpc C1, B5
1916 cpc C2, B6
1917 cpc C3, B7
1918 brcc 2f
1919
1920 ;; Divisor is greater than shifted Dividen: We can shift the Dividend
1921 ;; and it is still smaller than the Divisor --> Shift one 32-Bit Chunk
1922 wmov C2,A6 $ wmov C0,A4
1923 wmov A6,A2 $ wmov A4,A0
1924 wmov A2,C6 $ wmov A0,C4
1925
1926 ;; Set Bit Counter to 32
1927 lsr R_cnt
1928 2:
1929 #elif SPEED_DIV
1930 #error SPEED_DIV = ?
1931 #endif /* SPEED_DIV */
1932
1933 ;; The very Division + Remainder Routine
1934
1935 3: ;; Left-shift Dividend...
1936 lsl A0 $ rol A1 $ rol A2 $ rol A3
1937 rol A4 $ rol A5 $ rol A6 $ rol A7
1938
1939 ;; ...into Remainder
1940 rol C0 $ rol C1 $ rol C2 $ rol C3
1941 rol C4 $ rol C5 $ rol C6 $ rol C7
1942
1943 ;; Compare Remainder and Divisor
1944 CP C0,B0 $ cpc C1,B1 $ cpc C2,B2 $ cpc C3,B3
1945 cpc C4,B4 $ cpc C5,B5 $ cpc C6,B6 $ cpc C7,B7
1946
1947 brcs 4f
1948
1949 ;; Divisor fits into Remainder: Subtract it from Remainder...
1950 SUB C0,B0 $ sbc C1,B1 $ sbc C2,B2 $ sbc C3,B3
1951 sbc C4,B4 $ sbc C5,B5 $ sbc C6,B6 $ sbc C7,B7
1952
1953 ;; ...and set according Bit in the upcoming Quotient
1954 ;; The Bit will travel to its final Position
1955 ori A0, 1
1956
1957 4: ;; This Bit is done
1958 dec R_cnt
1959 brne 3b
1960 ;; __zero_reg__ is 0 again
1961
1962 ;; T = 0: We are fine with the Quotient in A[]
1963 ;; T = 1: Copy Remainder to A[]
1964 5: brtc 6f
1965 wmov A0, C0
1966 wmov A2, C2
1967 wmov A4, C4
1968 wmov A6, C6
1969 ;; Move the Sign of the Result to SS.7
1970 lsl SS
1971
1972 6: ret
1973
1974 ENDF __udivmod64
1975 #endif /* L_udivmod64 */
1976
1977
1978 #if defined (L_divdi3)
1979
1980 ;; R25:R18 = R24:R18 mod R17:R10
1981 ;; Ordinary ABI-Function
1982
1983 DEFUN __moddi3
1984 set
1985 rjmp __divdi3_moddi3
1986 ENDF __moddi3
1987
1988 ;; R25:R18 = R24:R18 div R17:R10
1989 ;; Ordinary ABI-Function
1990
1991 DEFUN __divdi3
1992 clt
1993 ENDF __divdi3
1994
1995 DEFUN __divdi3_moddi3
1996 #if SPEED_DIV
1997 mov r31, A7
1998 or r31, B7
1999 brmi 0f
2000 ;; Both Signs are 0: the following Complexitiy is not needed
2001 XJMP __udivdi3_umoddi3
2002 #endif /* SPEED_DIV */
2003
2004 0: ;; The Prologue
2005 ;; Save 12 Registers: Y, 17...8
2006 ;; No Frame needed
2007 do_prologue_saves 12
2008
2009 ;; SS.7 will contain the Sign of the Quotient (A.sign * B.sign)
2010 ;; SS.6 will contain the Sign of the Remainder (A.sign)
2011 mov SS, A7
2012 asr SS
2013 ;; Adjust Dividend's Sign as needed
2014 #if SPEED_DIV
2015 ;; Compiling for Speed we know that at least one Sign must be < 0
2016 ;; Thus, if A[] >= 0 then we know B[] < 0
2017 brpl 22f
2018 #else
2019 brpl 21f
2020 #endif /* SPEED_DIV */
2021
2022 XCALL __negdi2
2023
2024 ;; Adjust Divisor's Sign and SS.7 as needed
2025 21: tst B7
2026 brpl 3f
2027 22: ldi NN, 1 << 7
2028 eor SS, NN
2029
2030 ldi NN, -1
2031 com B4 $ com B5 $ com B6 $ com B7
2032 $ com B1 $ com B2 $ com B3
2033 NEG B0
2034 $ sbc B1,NN $ sbc B2,NN $ sbc B3,NN
2035 sbc B4,NN $ sbc B5,NN $ sbc B6,NN $ sbc B7,NN
2036
2037 3: ;; Do the unsigned 64-Bit Division/Modulo (depending on T-flag)
2038 XCALL __udivmod64
2039
2040 ;; Adjust Result's Sign
2041 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
2042 tst SS
2043 brpl 4f
2044 #else
2045 sbrc SS, 7
2046 #endif /* __AVR_HAVE_JMP_CALL__ */
2047 XCALL __negdi2
2048
2049 4: ;; Epilogue: Restore 12 Registers and return
2050 do_epilogue_restores 12
2051
2052 ENDF __divdi3_moddi3
2053
2054 #endif /* L_divdi3 */
2055
2056 #undef R_cnt
2057 #undef SS
2058 #undef NN
2059
2060 .section .text.libgcc, "ax", @progbits
2061
2062 #define TT __tmp_reg__
2063
2064 #if defined (L_adddi3)
2065 ;; (set (reg:DI 18)
2066 ;; (plus:DI (reg:DI 18)
2067 ;; (reg:DI 10)))
2068 ;; Sets the V flag for signed overflow tests
2069 ;; Sets the C flag for unsigned overflow tests
2070 DEFUN __adddi3
2071 ADD A0,B0 $ adc A1,B1 $ adc A2,B2 $ adc A3,B3
2072 adc A4,B4 $ adc A5,B5 $ adc A6,B6 $ adc A7,B7
2073 ret
2074 ENDF __adddi3
2075 #endif /* L_adddi3 */
2076
2077 #if defined (L_adddi3_s8)
2078 ;; (set (reg:DI 18)
2079 ;; (plus:DI (reg:DI 18)
2080 ;; (sign_extend:SI (reg:QI 26))))
2081 ;; Sets the V flag for signed overflow tests
2082 ;; Sets the C flag for unsigned overflow tests provided 0 <= R26 < 128
2083 DEFUN __adddi3_s8
2084 clr TT
2085 sbrc r26, 7
2086 com TT
2087 ADD A0,r26 $ adc A1,TT $ adc A2,TT $ adc A3,TT
2088 adc A4,TT $ adc A5,TT $ adc A6,TT $ adc A7,TT
2089 ret
2090 ENDF __adddi3_s8
2091 #endif /* L_adddi3_s8 */
2092
2093 #if defined (L_subdi3)
2094 ;; (set (reg:DI 18)
2095 ;; (minus:DI (reg:DI 18)
2096 ;; (reg:DI 10)))
2097 ;; Sets the V flag for signed overflow tests
2098 ;; Sets the C flag for unsigned overflow tests
2099 DEFUN __subdi3
2100 SUB A0,B0 $ sbc A1,B1 $ sbc A2,B2 $ sbc A3,B3
2101 sbc A4,B4 $ sbc A5,B5 $ sbc A6,B6 $ sbc A7,B7
2102 ret
2103 ENDF __subdi3
2104 #endif /* L_subdi3 */
2105
2106 #if defined (L_cmpdi2)
2107 ;; (set (cc0)
2108 ;; (compare (reg:DI 18)
2109 ;; (reg:DI 10)))
2110 DEFUN __cmpdi2
2111 CP A0,B0 $ cpc A1,B1 $ cpc A2,B2 $ cpc A3,B3
2112 cpc A4,B4 $ cpc A5,B5 $ cpc A6,B6 $ cpc A7,B7
2113 ret
2114 ENDF __cmpdi2
2115 #endif /* L_cmpdi2 */
2116
2117 #if defined (L_cmpdi2_s8)
2118 ;; (set (cc0)
2119 ;; (compare (reg:DI 18)
2120 ;; (sign_extend:SI (reg:QI 26))))
2121 DEFUN __cmpdi2_s8
2122 clr TT
2123 sbrc r26, 7
2124 com TT
2125 CP A0,r26 $ cpc A1,TT $ cpc A2,TT $ cpc A3,TT
2126 cpc A4,TT $ cpc A5,TT $ cpc A6,TT $ cpc A7,TT
2127 ret
2128 ENDF __cmpdi2_s8
2129 #endif /* L_cmpdi2_s8 */
2130
2131 #if defined (L_negdi2)
2132 ;; (set (reg:DI 18)
2133 ;; (neg:DI (reg:DI 18)))
2134 ;; Sets the V flag for signed overflow tests
2135 DEFUN __negdi2
2136
2137 com A4 $ com A5 $ com A6 $ com A7
2138 $ com A1 $ com A2 $ com A3
2139 NEG A0
2140 $ sbci A1,-1 $ sbci A2,-1 $ sbci A3,-1
2141 sbci A4,-1 $ sbci A5,-1 $ sbci A6,-1 $ sbci A7,-1
2142 ret
2143
2144 ENDF __negdi2
2145 #endif /* L_negdi2 */
2146
2147 #undef TT
2148
2149 #undef C7
2150 #undef C6
2151 #undef C5
2152 #undef C4
2153 #undef C3
2154 #undef C2
2155 #undef C1
2156 #undef C0
2157
2158 #undef B7
2159 #undef B6
2160 #undef B5
2161 #undef B4
2162 #undef B3
2163 #undef B2
2164 #undef B1
2165 #undef B0
2166
2167 #undef A7
2168 #undef A6
2169 #undef A5
2170 #undef A4
2171 #undef A3
2172 #undef A2
2173 #undef A1
2174 #undef A0
2175
2176 #endif /* !defined (__AVR_TINY__) */
2177
2178
2179 .section .text.libgcc.prologue, "ax", @progbits
2180
2181 /**********************************
2182 * This is a prologue subroutine
2183 **********************************/
2184 #if !defined (__AVR_TINY__)
2185 #if defined (L_prologue)
2186
2187 ;; This function does not clobber T-flag; 64-bit division relies on it
2188 DEFUN __prologue_saves__
2189 push r2
2190 push r3
2191 push r4
2192 push r5
2193 push r6
2194 push r7
2195 push r8
2196 push r9
2197 push r10
2198 push r11
2199 push r12
2200 push r13
2201 push r14
2202 push r15
2203 push r16
2204 push r17
2205 push r28
2206 push r29
2207 #if !defined (__AVR_HAVE_SPH__)
2208 in r28,__SP_L__
2209 sub r28,r26
2210 out __SP_L__,r28
2211 clr r29
2212 #elif defined (__AVR_XMEGA__)
2213 in r28,__SP_L__
2214 in r29,__SP_H__
2215 sub r28,r26
2216 sbc r29,r27
2217 out __SP_L__,r28
2218 out __SP_H__,r29
2219 #else
2220 in r28,__SP_L__
2221 in r29,__SP_H__
2222 sub r28,r26
2223 sbc r29,r27
2224 in __tmp_reg__,__SREG__
2225 cli
2226 out __SP_H__,r29
2227 out __SREG__,__tmp_reg__
2228 out __SP_L__,r28
2229 #endif /* #SP = 8/16 */
2230
2231 XIJMP
2232
2233 ENDF __prologue_saves__
2234 #endif /* defined (L_prologue) */
2235
2236 /*
2237 * This is an epilogue subroutine
2238 */
2239 #if defined (L_epilogue)
2240
2241 DEFUN __epilogue_restores__
2242 ldd r2,Y+18
2243 ldd r3,Y+17
2244 ldd r4,Y+16
2245 ldd r5,Y+15
2246 ldd r6,Y+14
2247 ldd r7,Y+13
2248 ldd r8,Y+12
2249 ldd r9,Y+11
2250 ldd r10,Y+10
2251 ldd r11,Y+9
2252 ldd r12,Y+8
2253 ldd r13,Y+7
2254 ldd r14,Y+6
2255 ldd r15,Y+5
2256 ldd r16,Y+4
2257 ldd r17,Y+3
2258 ldd r26,Y+2
2259 #if !defined (__AVR_HAVE_SPH__)
2260 ldd r29,Y+1
2261 add r28,r30
2262 out __SP_L__,r28
2263 mov r28, r26
2264 #elif defined (__AVR_XMEGA__)
2265 ldd r27,Y+1
2266 add r28,r30
2267 adc r29,__zero_reg__
2268 out __SP_L__,r28
2269 out __SP_H__,r29
2270 wmov 28, 26
2271 #else
2272 ldd r27,Y+1
2273 add r28,r30
2274 adc r29,__zero_reg__
2275 in __tmp_reg__,__SREG__
2276 cli
2277 out __SP_H__,r29
2278 out __SREG__,__tmp_reg__
2279 out __SP_L__,r28
2280 mov_l r28, r26
2281 mov_h r29, r27
2282 #endif /* #SP = 8/16 */
2283 ret
2284 ENDF __epilogue_restores__
2285 #endif /* defined (L_epilogue) */
2286 #endif /* !defined (__AVR_TINY__) */
2287
2288 #ifdef L_exit
2289 .section .fini9,"ax",@progbits
2290 DEFUN _exit
2291 .weak exit
2292 exit:
2293 ENDF _exit
2294
2295 /* Code from .fini8 ... .fini1 sections inserted by ld script. */
2296
2297 .section .fini0,"ax",@progbits
2298 cli
2299 __stop_program:
2300 rjmp __stop_program
2301 #endif /* defined (L_exit) */
2302
2303 #ifdef L_cleanup
2304 .weak _cleanup
2305 .func _cleanup
2306 _cleanup:
2307 ret
2308 .endfunc
2309 #endif /* defined (L_cleanup) */
2310
2311
2312 .section .text.libgcc, "ax", @progbits
2313
2314 #ifdef L_tablejump2
2315 DEFUN __tablejump2__
2316 lsl r30
2317 rol r31
2318 #if defined (__AVR_HAVE_EIJMP_EICALL__)
2319 ;; Word address of gs() jumptable entry in R24:Z
2320 rol r24
2321 out __RAMPZ__, r24
2322 #elif defined (__AVR_HAVE_ELPM__)
2323 ;; Word address of jumptable entry in Z
2324 clr __tmp_reg__
2325 rol __tmp_reg__
2326 out __RAMPZ__, __tmp_reg__
2327 #endif
2328
2329 ;; Read word address from jumptable and jump
2330
2331 #if defined (__AVR_HAVE_ELPMX__)
2332 elpm __tmp_reg__, Z+
2333 elpm r31, Z
2334 mov r30, __tmp_reg__
2335 #ifdef __AVR_HAVE_RAMPD__
2336 ;; Reset RAMPZ to 0 so that EBI devices don't read garbage from RAM
2337 out __RAMPZ__, __zero_reg__
2338 #endif /* RAMPD */
2339 XIJMP
2340 #elif defined (__AVR_HAVE_ELPM__)
2341 elpm
2342 push r0
2343 adiw r30, 1
2344 elpm
2345 push r0
2346 ret
2347 #elif defined (__AVR_HAVE_LPMX__)
2348 lpm __tmp_reg__, Z+
2349 lpm r31, Z
2350 mov r30, __tmp_reg__
2351 ijmp
2352 #elif defined (__AVR_TINY__)
2353 wsubi 30, -(__AVR_TINY_PM_BASE_ADDRESS__) ; Add PM offset to Z
2354 ld __tmp_reg__, Z+
2355 ld r31, Z ; Use ld instead of lpm to load Z
2356 mov r30, __tmp_reg__
2357 ijmp
2358 #else
2359 lpm
2360 push r0
2361 adiw r30, 1
2362 lpm
2363 push r0
2364 ret
2365 #endif
2366 ENDF __tablejump2__
2367 #endif /* L_tablejump2 */
2368
2369 #if defined(__AVR_TINY__)
2370 #ifdef L_copy_data
2371 .section .init4,"ax",@progbits
2372 .global __do_copy_data
2373 __do_copy_data:
2374 ldi r18, hi8(__data_end)
2375 ldi r26, lo8(__data_start)
2376 ldi r27, hi8(__data_start)
2377 ldi r30, lo8(__data_load_start + __AVR_TINY_PM_BASE_ADDRESS__)
2378 ldi r31, hi8(__data_load_start + __AVR_TINY_PM_BASE_ADDRESS__)
2379 rjmp .L__do_copy_data_start
2380 .L__do_copy_data_loop:
2381 ld r19, z+
2382 st X+, r19
2383 .L__do_copy_data_start:
2384 cpi r26, lo8(__data_end)
2385 cpc r27, r18
2386 brne .L__do_copy_data_loop
2387 #endif
2388 #else
2389 #ifdef L_copy_data
2390 .section .init4,"ax",@progbits
2391 DEFUN __do_copy_data
2392 #if defined(__AVR_HAVE_ELPMX__)
2393 ldi r17, hi8(__data_end)
2394 ldi r26, lo8(__data_start)
2395 ldi r27, hi8(__data_start)
2396 ldi r30, lo8(__data_load_start)
2397 ldi r31, hi8(__data_load_start)
2398 ldi r16, hh8(__data_load_start)
2399 out __RAMPZ__, r16
2400 rjmp .L__do_copy_data_start
2401 .L__do_copy_data_loop:
2402 elpm r0, Z+
2403 st X+, r0
2404 .L__do_copy_data_start:
2405 cpi r26, lo8(__data_end)
2406 cpc r27, r17
2407 brne .L__do_copy_data_loop
2408 #elif !defined(__AVR_HAVE_ELPMX__) && defined(__AVR_HAVE_ELPM__)
2409 ldi r17, hi8(__data_end)
2410 ldi r26, lo8(__data_start)
2411 ldi r27, hi8(__data_start)
2412 ldi r30, lo8(__data_load_start)
2413 ldi r31, hi8(__data_load_start)
2414 ldi r16, hh8(__data_load_start - 0x10000)
2415 .L__do_copy_data_carry:
2416 inc r16
2417 out __RAMPZ__, r16
2418 rjmp .L__do_copy_data_start
2419 .L__do_copy_data_loop:
2420 elpm
2421 st X+, r0
2422 adiw r30, 1
2423 brcs .L__do_copy_data_carry
2424 .L__do_copy_data_start:
2425 cpi r26, lo8(__data_end)
2426 cpc r27, r17
2427 brne .L__do_copy_data_loop
2428 #elif !defined(__AVR_HAVE_ELPMX__) && !defined(__AVR_HAVE_ELPM__)
2429 ldi r17, hi8(__data_end)
2430 ldi r26, lo8(__data_start)
2431 ldi r27, hi8(__data_start)
2432 ldi r30, lo8(__data_load_start)
2433 ldi r31, hi8(__data_load_start)
2434 rjmp .L__do_copy_data_start
2435 .L__do_copy_data_loop:
2436 #if defined (__AVR_HAVE_LPMX__)
2437 lpm r0, Z+
2438 #else
2439 lpm
2440 adiw r30, 1
2441 #endif
2442 st X+, r0
2443 .L__do_copy_data_start:
2444 cpi r26, lo8(__data_end)
2445 cpc r27, r17
2446 brne .L__do_copy_data_loop
2447 #endif /* !defined(__AVR_HAVE_ELPMX__) && !defined(__AVR_HAVE_ELPM__) */
2448 #if defined (__AVR_HAVE_ELPM__) && defined (__AVR_HAVE_RAMPD__)
2449 ;; Reset RAMPZ to 0 so that EBI devices don't read garbage from RAM
2450 out __RAMPZ__, __zero_reg__
2451 #endif /* ELPM && RAMPD */
2452 ENDF __do_copy_data
2453 #endif /* L_copy_data */
2454 #endif /* !defined (__AVR_TINY__) */
2455
2456 /* __do_clear_bss is only necessary if there is anything in .bss section. */
2457
2458 #ifdef L_clear_bss
2459 .section .init4,"ax",@progbits
2460 DEFUN __do_clear_bss
2461 ldi r18, hi8(__bss_end)
2462 ldi r26, lo8(__bss_start)
2463 ldi r27, hi8(__bss_start)
2464 rjmp .do_clear_bss_start
2465 .do_clear_bss_loop:
2466 st X+, __zero_reg__
2467 .do_clear_bss_start:
2468 cpi r26, lo8(__bss_end)
2469 cpc r27, r18
2470 brne .do_clear_bss_loop
2471 ENDF __do_clear_bss
2472 #endif /* L_clear_bss */
2473
2474 /* __do_global_ctors and __do_global_dtors are only necessary
2475 if there are any constructors/destructors. */
2476
2477 #if defined(__AVR_TINY__)
2478 #define cdtors_tst_reg r18
2479 #else
2480 #define cdtors_tst_reg r17
2481 #endif
2482
2483 #ifdef L_ctors
2484 .section .init6,"ax",@progbits
2485 DEFUN __do_global_ctors
2486 ldi cdtors_tst_reg, pm_hi8(__ctors_start)
2487 ldi r28, pm_lo8(__ctors_end)
2488 ldi r29, pm_hi8(__ctors_end)
2489 #ifdef __AVR_HAVE_EIJMP_EICALL__
2490 ldi r16, pm_hh8(__ctors_end)
2491 #endif /* HAVE_EIJMP */
2492 rjmp .L__do_global_ctors_start
2493 .L__do_global_ctors_loop:
2494 wsubi 28, 1
2495 #ifdef __AVR_HAVE_EIJMP_EICALL__
2496 sbc r16, __zero_reg__
2497 mov r24, r16
2498 #endif /* HAVE_EIJMP */
2499 mov_h r31, r29
2500 mov_l r30, r28
2501 XCALL __tablejump2__
2502 .L__do_global_ctors_start:
2503 cpi r28, pm_lo8(__ctors_start)
2504 cpc r29, cdtors_tst_reg
2505 #ifdef __AVR_HAVE_EIJMP_EICALL__
2506 ldi r24, pm_hh8(__ctors_start)
2507 cpc r16, r24
2508 #endif /* HAVE_EIJMP */
2509 brne .L__do_global_ctors_loop
2510 ENDF __do_global_ctors
2511 #endif /* L_ctors */
2512
2513 #ifdef L_dtors
2514 .section .fini6,"ax",@progbits
2515 DEFUN __do_global_dtors
2516 ldi cdtors_tst_reg, pm_hi8(__dtors_end)
2517 ldi r28, pm_lo8(__dtors_start)
2518 ldi r29, pm_hi8(__dtors_start)
2519 #ifdef __AVR_HAVE_EIJMP_EICALL__
2520 ldi r16, pm_hh8(__dtors_start)
2521 #endif /* HAVE_EIJMP */
2522 rjmp .L__do_global_dtors_start
2523 .L__do_global_dtors_loop:
2524 #ifdef __AVR_HAVE_EIJMP_EICALL__
2525 mov r24, r16
2526 #endif /* HAVE_EIJMP */
2527 mov_h r31, r29
2528 mov_l r30, r28
2529 XCALL __tablejump2__
2530 waddi 28, 1
2531 #ifdef __AVR_HAVE_EIJMP_EICALL__
2532 adc r16, __zero_reg__
2533 #endif /* HAVE_EIJMP */
2534 .L__do_global_dtors_start:
2535 cpi r28, pm_lo8(__dtors_end)
2536 cpc r29, cdtors_tst_reg
2537 #ifdef __AVR_HAVE_EIJMP_EICALL__
2538 ldi r24, pm_hh8(__dtors_end)
2539 cpc r16, r24
2540 #endif /* HAVE_EIJMP */
2541 brne .L__do_global_dtors_loop
2542 ENDF __do_global_dtors
2543 #endif /* L_dtors */
2544
2545 #undef cdtors_tst_reg
2546
2547 .section .text.libgcc, "ax", @progbits
2548
2549 #if !defined (__AVR_TINY__)
2550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2551 ;; Loading n bytes from Flash; n = 3,4
2552 ;; R22... = Flash[Z]
2553 ;; Clobbers: __tmp_reg__
2554
2555 #if (defined (L_load_3) \
2556 || defined (L_load_4)) \
2557 && !defined (__AVR_HAVE_LPMX__)
2558
2559 ;; Destination
2560 #define D0 22
2561 #define D1 D0+1
2562 #define D2 D0+2
2563 #define D3 D0+3
2564
2565 .macro .load dest, n
2566 lpm
2567 mov \dest, r0
2568 .if \dest != D0+\n-1
2569 adiw r30, 1
2570 .else
2571 sbiw r30, \n-1
2572 .endif
2573 .endm
2574
2575 #if defined (L_load_3)
2576 DEFUN __load_3
2577 push D3
2578 XCALL __load_4
2579 pop D3
2580 ret
2581 ENDF __load_3
2582 #endif /* L_load_3 */
2583
2584 #if defined (L_load_4)
2585 DEFUN __load_4
2586 .load D0, 4
2587 .load D1, 4
2588 .load D2, 4
2589 .load D3, 4
2590 ret
2591 ENDF __load_4
2592 #endif /* L_load_4 */
2593
2594 #endif /* L_load_3 || L_load_3 */
2595 #endif /* !defined (__AVR_TINY__) */
2596
2597 #if !defined (__AVR_TINY__)
2598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2599 ;; Loading n bytes from Flash or RAM; n = 1,2,3,4
2600 ;; R22... = Flash[R21:Z] or RAM[Z] depending on R21.7
2601 ;; Clobbers: __tmp_reg__, R21, R30, R31
2602
2603 #if (defined (L_xload_1) \
2604 || defined (L_xload_2) \
2605 || defined (L_xload_3) \
2606 || defined (L_xload_4))
2607
2608 ;; Destination
2609 #define D0 22
2610 #define D1 D0+1
2611 #define D2 D0+2
2612 #define D3 D0+3
2613
2614 ;; Register containing bits 16+ of the address
2615
2616 #define HHI8 21
2617
2618 #if defined (L_xload_1)
2619 DEFUN __xload_1
2620 #if defined (__AVR_HAVE_LPMX__) && !defined (__AVR_HAVE_ELPM__)
2621 sbrc HHI8, 7
2622 ld D0, Z
2623 sbrs HHI8, 7
2624 lpm D0, Z
2625 ret
2626 #else
2627 .branch_plus HHI8, __fload_1
2628 ld D0, Z
2629 ret
2630 #endif /* LPMx && ! ELPM */
2631 ENDF __xload_1
2632 #endif /* L_xload_1 */
2633
2634 #if defined (L_xload_2)
2635 DEFUN __xload_2
2636 .branch_plus HHI8, __fload_2
2637 ld D0, Z+
2638 ld D1, Z+
2639 ret
2640 ENDF __xload_2
2641 #endif /* L_xload_2 */
2642
2643 #if defined (L_xload_3)
2644 DEFUN __xload_3
2645 .branch_plus HHI8, __fload_3
2646 ld D0, Z+
2647 ld D1, Z+
2648 ld D2, Z+
2649 ret
2650 ENDF __xload_3
2651 #endif /* L_xload_3 */
2652
2653 #if defined (L_xload_4)
2654 DEFUN __xload_4
2655 .branch_plus HHI8, __fload_4
2656 ld D0, Z+
2657 ld D1, Z+
2658 ld D2, Z+
2659 ld D3, Z+
2660 ret
2661 ENDF __xload_4
2662 #endif /* L_xload_4 */
2663
2664 #endif /* L_xload_{1|2|3|4} */
2665 #endif /* if !defined (__AVR_TINY__) */
2666
2667 #if !defined (__AVR_TINY__)
2668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2669 ;; Loading n bytes from Flash; n = 1,2,3,4
2670 ;; R22... = Flash[R21:Z]
2671 ;; Clobbers: __tmp_reg__, R21, R30, R31
2672
2673 #if (defined (L_fload_1) \
2674 || defined (L_fload_2) \
2675 || defined (L_fload_3) \
2676 || defined (L_fload_4))
2677
2678 ;; Destination
2679 #define D0 22
2680 #define D1 D0+1
2681 #define D2 D0+2
2682 #define D3 D0+3
2683
2684 ;; Register containing bits 16+ of the address
2685
2686 #define HHI8 21
2687
2688 .macro .fload dest, n
2689 #if defined (__AVR_HAVE_ELPM__)
2690 .if \dest == D0
2691 out __RAMPZ__, HHI8
2692 .endif
2693 #endif /* __AVR_HAVE_ELPM__ */
2694 #if defined (__AVR_HAVE_ELPMX__)
2695 elpm \dest, Z+
2696 #elif defined (__AVR_HAVE_ELPM__)
2697 elpm
2698 mov \dest, r0
2699 .if \dest != D0+\n-1
2700 adiw r30, 1
2701 adc HHI8, __zero_reg__
2702 out __RAMPZ__, HHI8
2703 .endif
2704 #elif defined (__AVR_HAVE_LPMX__)
2705 lpm \dest, Z+
2706 #else
2707 lpm
2708 mov \dest, r0
2709 .if \dest != D0+\n-1
2710 adiw r30, 1
2711 .endif
2712 #endif
2713 #if defined (__AVR_HAVE_ELPM__) && defined (__AVR_HAVE_RAMPD__)
2714 .if \dest == D0+\n-1
2715 ;; Reset RAMPZ to 0 so that EBI devices don't read garbage from RAM
2716 out __RAMPZ__, __zero_reg__
2717 .endif
2718 #endif
2719 .endm ; .fload
2720
2721 #if defined (L_fload_1)
2722 DEFUN __fload_1
2723 #if defined (__AVR_HAVE_LPMX__) && !defined (__AVR_HAVE_ELPM__)
2724 lpm D0, Z
2725 ret
2726 #else
2727 .fload D0, 1
2728 ret
2729 #endif /* LPMx && ! ELPM */
2730 ENDF __fload_1
2731 #endif /* L_fload_1 */
2732
2733 #if defined (L_fload_2)
2734 DEFUN __fload_2
2735 .fload D0, 2
2736 .fload D1, 2
2737 ret
2738 ENDF __fload_2
2739 #endif /* L_fload_2 */
2740
2741 #if defined (L_fload_3)
2742 DEFUN __fload_3
2743 .fload D0, 3
2744 .fload D1, 3
2745 .fload D2, 3
2746 ret
2747 ENDF __fload_3
2748 #endif /* L_fload_3 */
2749
2750 #if defined (L_fload_4)
2751 DEFUN __fload_4
2752 .fload D0, 4
2753 .fload D1, 4
2754 .fload D2, 4
2755 .fload D3, 4
2756 ret
2757 ENDF __fload_4
2758 #endif /* L_fload_4 */
2759
2760 #endif /* L_fload_{1|2|3|4} */
2761 #endif /* if !defined (__AVR_TINY__) */
2762
2763
2764 #if !defined (__AVR_TINY__)
2765 #if defined (L_strlen_memx)
2766 DEFUN __strlen_memx
2767 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
2768 tst r24
2769 brmi 1f
2770 #else
2771 sbrs r24, 7
2772 #endif
2773 XJMP strlen_PF ; AVR-LibC
2774 1: wmov 24, 22
2775 XJMP strlen ; AVR-LibC
2776 ENDF __strlen_memx
2777 #endif /* L_strlen_memx */
2778 #endif /* if !defined (__AVR_TINY__) */
2779
2780
2781 #if !defined (__AVR_TINY__)
2782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2783 ;; memcopy from Address Space __memx to RAM
2784 ;; R23:Z = Source Address
2785 ;; X = Destination Address
2786 ;; Clobbers: __tmp_reg__, R23, R24, R25, X, Z
2787
2788 #if defined (L_movmemx)
2789
2790 #define HHI8 23
2791 #define LOOP 24
2792
2793 DEFUN __movmemx_qi
2794 ;; #Bytes to copy fits in 8 Bits (1..255)
2795 ;; Zero-extend Loop Counter
2796 clr LOOP+1
2797 ;; FALLTHRU
2798 ENDF __movmemx_qi
2799
2800 DEFUN __movmemx_hi
2801
2802 .branch_plus HHI8, __movmemf_hi
2803
2804 ;; Read 1 Byte from RAM...
2805 1: ld r0, Z+
2806 ;; and store that Byte to RAM Destination
2807 st X+, r0
2808 sbiw LOOP, 1
2809 brne 1b
2810 ret
2811 ENDF __movmemx_hi
2812
2813 #undef HHI8
2814 #undef LOOP
2815
2816 #endif /* L_movmemx */
2817
2818 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2819 ;; memcopy from Address Space __flashx to RAM
2820 ;; R23:Z = Source Address
2821 ;; X = Destination Address
2822 ;; Clobbers: __tmp_reg__, R23, R24, R25, X, Z
2823
2824 #if defined (L_movmemf)
2825
2826 #define HHI8 23
2827 #define LOOP 24
2828
2829 DEFUN __movmemf_qi
2830 ;; #Bytes to copy fits in 8 Bits (1..255)
2831 ;; Zero-extend Loop Counter
2832 clr LOOP+1
2833 ;; FALLTHRU
2834 ENDF __movmemf_qi
2835
2836 DEFUN __movmemf_hi
2837
2838 ;; Read from Flash
2839
2840 #if defined (__AVR_HAVE_ELPM__)
2841 out __RAMPZ__, HHI8
2842 #endif
2843
2844 0: ;; Load 1 Byte from Flash...
2845
2846 #if defined (__AVR_HAVE_ELPMX__)
2847 elpm r0, Z+
2848 #elif defined (__AVR_HAVE_ELPM__)
2849 elpm
2850 adiw r30, 1
2851 adc HHI8, __zero_reg__
2852 out __RAMPZ__, HHI8
2853 #elif defined (__AVR_HAVE_LPMX__)
2854 lpm r0, Z+
2855 #else
2856 lpm
2857 adiw r30, 1
2858 #endif
2859
2860 ;; ...and store that Byte to RAM Destination
2861 st X+, r0
2862 sbiw LOOP, 1
2863 brne 0b
2864 #if defined (__AVR_HAVE_ELPM__) && defined (__AVR_HAVE_RAMPD__)
2865 ;; Reset RAMPZ to 0 so that EBI devices don't read garbage from RAM
2866 out __RAMPZ__, __zero_reg__
2867 #endif /* ELPM && RAMPD */
2868 ret
2869 ENDF __movmemf_hi
2870
2871 #undef HHI8
2872 #undef LOOP
2873
2874 #endif /* L_movmemf */
2875 #endif /* !defined (__AVR_TINY__) */
2876
2877
2878 .section .text.libgcc.builtins, "ax", @progbits
2879
2880 /**********************************
2881 * Find first set Bit (ffs)
2882 **********************************/
2883
2884 #if defined (L_ffssi2)
2885 ;; find first set bit
2886 ;; r25:r24 = ffs32 (r25:r22)
2887 ;; clobbers: r22, r26
2888 DEFUN __ffssi2
2889 clr r26
2890 tst r22
2891 brne 1f
2892 subi r26, -8
2893 or r22, r23
2894 brne 1f
2895 subi r26, -8
2896 or r22, r24
2897 brne 1f
2898 subi r26, -8
2899 or r22, r25
2900 brne 1f
2901 ret
2902 1: mov r24, r22
2903 XJMP __loop_ffsqi2
2904 ENDF __ffssi2
2905 #endif /* defined (L_ffssi2) */
2906
2907 #if defined (L_ffshi2)
2908 ;; find first set bit
2909 ;; r25:r24 = ffs16 (r25:r24)
2910 ;; clobbers: r26
2911 DEFUN __ffshi2
2912 clr r26
2913 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
2914 ;; Some cores have problem skipping 2-word instruction
2915 tst r24
2916 breq 2f
2917 #else
2918 cpse r24, __zero_reg__
2919 #endif /* __AVR_HAVE_JMP_CALL__ */
2920 1: XJMP __loop_ffsqi2
2921 2: ldi r26, 8
2922 or r24, r25
2923 brne 1b
2924 ret
2925 ENDF __ffshi2
2926 #endif /* defined (L_ffshi2) */
2927
2928 #if defined (L_loop_ffsqi2)
2929 ;; Helper for ffshi2, ffssi2
2930 ;; r25:r24 = r26 + zero_extend16 (ffs8(r24))
2931 ;; r24 must be != 0
2932 ;; clobbers: r26
2933 DEFUN __loop_ffsqi2
2934 inc r26
2935 lsr r24
2936 brcc __loop_ffsqi2
2937 mov r24, r26
2938 clr r25
2939 ret
2940 ENDF __loop_ffsqi2
2941 #endif /* defined (L_loop_ffsqi2) */
2942
2943
2944 /**********************************
2945 * Count trailing Zeros (ctz)
2946 **********************************/
2947
2948 #if defined (L_ctzsi2)
2949 ;; count trailing zeros
2950 ;; r25:r24 = ctz32 (r25:r22)
2951 ;; clobbers: r26, r22
2952 ;; ctz(0) = 255
2953 ;; Note that ctz(0) in undefined for GCC
2954 DEFUN __ctzsi2
2955 XCALL __ffssi2
2956 dec r24
2957 ret
2958 ENDF __ctzsi2
2959 #endif /* defined (L_ctzsi2) */
2960
2961 #if defined (L_ctzhi2)
2962 ;; count trailing zeros
2963 ;; r25:r24 = ctz16 (r25:r24)
2964 ;; clobbers: r26
2965 ;; ctz(0) = 255
2966 ;; Note that ctz(0) in undefined for GCC
2967 DEFUN __ctzhi2
2968 XCALL __ffshi2
2969 dec r24
2970 ret
2971 ENDF __ctzhi2
2972 #endif /* defined (L_ctzhi2) */
2973
2974
2975 /**********************************
2976 * Count leading Zeros (clz)
2977 **********************************/
2978
2979 #if defined (L_clzdi2)
2980 ;; count leading zeros
2981 ;; r25:r24 = clz64 (r25:r18)
2982 ;; clobbers: r22, r23, r26
2983 DEFUN __clzdi2
2984 XCALL __clzsi2
2985 sbrs r24, 5
2986 ret
2987 mov_l r22, r18
2988 mov_h r23, r19
2989 mov_l r24, r20
2990 mov_h r25, r21
2991 XCALL __clzsi2
2992 subi r24, -32
2993 ret
2994 ENDF __clzdi2
2995 #endif /* defined (L_clzdi2) */
2996
2997 #if defined (L_clzsi2)
2998 ;; count leading zeros
2999 ;; r25:r24 = clz32 (r25:r22)
3000 ;; clobbers: r26
3001 DEFUN __clzsi2
3002 XCALL __clzhi2
3003 sbrs r24, 4
3004 ret
3005 mov_l r24, r22
3006 mov_h r25, r23
3007 XCALL __clzhi2
3008 subi r24, -16
3009 ret
3010 ENDF __clzsi2
3011 #endif /* defined (L_clzsi2) */
3012
3013 #if defined (L_clzhi2)
3014 ;; count leading zeros
3015 ;; r25:r24 = clz16 (r25:r24)
3016 ;; clobbers: r26
3017 DEFUN __clzhi2
3018 clr r26
3019 tst r25
3020 brne 1f
3021 or r25, r24
3022 breq 0f
3023 subi r26, -8
3024 1: cpi r25, 16
3025 brsh 3f
3026 subi r26, -3
3027 swap r25
3028 2: inc r26
3029 3: lsl r25
3030 brcc 2b
3031 mov r24, r26
3032 clr r25
3033 ret
3034 0: ldi r24, 16
3035 ret
3036 ENDF __clzhi2
3037 #endif /* defined (L_clzhi2) */
3038
3039
3040 /**********************************
3041 * Parity
3042 **********************************/
3043
3044 #if defined (L_paritydi2)
3045 ;; r25:r24 = parity64 (r25:r18)
3046 ;; clobbers: __tmp_reg__
3047 DEFUN __paritydi2
3048 eor r24, r18
3049 eor r24, r19
3050 eor r24, r20
3051 eor r24, r21
3052 XJMP __paritysi2
3053 ENDF __paritydi2
3054 #endif /* defined (L_paritydi2) */
3055
3056 #if defined (L_paritysi2)
3057 ;; r25:r24 = parity32 (r25:r22)
3058 ;; clobbers: __tmp_reg__
3059 DEFUN __paritysi2
3060 eor r24, r22
3061 eor r24, r23
3062 XJMP __parityhi2
3063 ENDF __paritysi2
3064 #endif /* defined (L_paritysi2) */
3065
3066 #if defined (L_parityhi2)
3067 ;; r25:r24 = parity16 (r25:r24)
3068 ;; clobbers: __tmp_reg__
3069 DEFUN __parityhi2
3070 eor r24, r25
3071 ;; FALLTHRU
3072 ENDF __parityhi2
3073
3074 ;; r25:r24 = parity8 (r24)
3075 ;; clobbers: __tmp_reg__
3076 DEFUN __parityqi2
3077 ;; parity is in r24[0..7]
3078 mov __tmp_reg__, r24
3079 swap __tmp_reg__
3080 eor r24, __tmp_reg__
3081 ;; parity is in r24[0..3]
3082 subi r24, -4
3083 andi r24, -5
3084 subi r24, -6
3085 ;; parity is in r24[0,3]
3086 sbrc r24, 3
3087 inc r24
3088 ;; parity is in r24[0]
3089 andi r24, 1
3090 clr r25
3091 ret
3092 ENDF __parityqi2
3093 #endif /* defined (L_parityhi2) */
3094
3095
3096 /**********************************
3097 * Population Count
3098 **********************************/
3099
3100 #if defined (L_popcounthi2)
3101 ;; population count
3102 ;; r25:r24 = popcount16 (r25:r24)
3103 ;; clobbers: __tmp_reg__
3104 DEFUN __popcounthi2
3105 XCALL __popcountqi2
3106 push r24
3107 mov r24, r25
3108 XCALL __popcountqi2
3109 clr r25
3110 ;; FALLTHRU
3111 ENDF __popcounthi2
3112
3113 DEFUN __popcounthi2_tail
3114 pop __tmp_reg__
3115 add r24, __tmp_reg__
3116 ret
3117 ENDF __popcounthi2_tail
3118 #endif /* defined (L_popcounthi2) */
3119
3120 #if defined (L_popcountsi2)
3121 ;; population count
3122 ;; r25:r24 = popcount32 (r25:r22)
3123 ;; clobbers: __tmp_reg__
3124 DEFUN __popcountsi2
3125 XCALL __popcounthi2
3126 push r24
3127 mov_l r24, r22
3128 mov_h r25, r23
3129 XCALL __popcounthi2
3130 XJMP __popcounthi2_tail
3131 ENDF __popcountsi2
3132 #endif /* defined (L_popcountsi2) */
3133
3134 #if defined (L_popcountdi2)
3135 ;; population count
3136 ;; r25:r24 = popcount64 (r25:r18)
3137 ;; clobbers: r22, r23, __tmp_reg__
3138 DEFUN __popcountdi2
3139 XCALL __popcountsi2
3140 push r24
3141 mov_l r22, r18
3142 mov_h r23, r19
3143 mov_l r24, r20
3144 mov_h r25, r21
3145 XCALL __popcountsi2
3146 XJMP __popcounthi2_tail
3147 ENDF __popcountdi2
3148 #endif /* defined (L_popcountdi2) */
3149
3150 #if defined (L_popcountqi2)
3151 ;; population count
3152 ;; r24 = popcount8 (r24)
3153 ;; clobbers: __tmp_reg__
3154 DEFUN __popcountqi2
3155 mov __tmp_reg__, r24 ; oeoeoeoe
3156 andi r24, 0xAA ; o0o0o0o0
3157 lsr r24 ; 0o0o0o0o
3158 ;; Four values 0, 1 or 2: # bits set o+e
3159 sub __tmp_reg__, r24 ; 44332211
3160 mov r24, __tmp_reg__ ; 44332211
3161 andi r24, 0x33 ; 00330011
3162 eor __tmp_reg__, r24 ; 44002200
3163 lsr __tmp_reg__ ; 04400220
3164 lsr __tmp_reg__ ; 00440022
3165 add r24, __tmp_reg__ ; 04210421
3166 mov __tmp_reg__, r24 ; h421l421
3167 swap __tmp_reg__ ; l421h421
3168 add r24, __tmp_reg__ ; 84218421
3169 andi r24, 0xf ; 00008421 /17
3170 ret
3171 ENDF __popcountqi2
3172 #endif /* defined (L_popcountqi2) */
3173
3174
3175 /**********************************
3176 * Swap bytes
3177 **********************************/
3178
3179 ;; swap two registers with different register number
3180 .macro bswap a, b
3181 eor \a, \b
3182 eor \b, \a
3183 eor \a, \b
3184 .endm
3185
3186 #if defined (L_bswapsi2)
3187 ;; swap bytes
3188 ;; r25:r22 = bswap32 (r25:r22)
3189 DEFUN __bswapsi2
3190 bswap r22, r25
3191 bswap r23, r24
3192 ret
3193 ENDF __bswapsi2
3194 #endif /* defined (L_bswapsi2) */
3195
3196 #if defined (L_bswapdi2)
3197 ;; swap bytes
3198 ;; r25:r18 = bswap64 (r25:r18)
3199 DEFUN __bswapdi2
3200 bswap r18, r25
3201 bswap r19, r24
3202 bswap r20, r23
3203 bswap r21, r22
3204 ret
3205 ENDF __bswapdi2
3206 #endif /* defined (L_bswapdi2) */
3207
3208
3209 /**********************************
3210 * 64-bit shifts
3211 **********************************/
3212
3213 #if defined (L_ashrdi3)
3214
3215 #define SS __zero_reg__
3216
3217 ;; Arithmetic shift right
3218 ;; r25:r18 = ashr64 (r25:r18, r17:r16)
3219 DEFUN __ashrdi3
3220 sbrc r25, 7
3221 com SS
3222 ;; FALLTHRU
3223 ENDF __ashrdi3
3224
3225 ;; Logic shift right
3226 ;; r25:r18 = lshr64 (r25:r18, r17:r16)
3227 DEFUN __lshrdi3
3228 ;; Signs are in SS (zero_reg)
3229 mov __tmp_reg__, r16
3230 0: cpi r16, 8
3231 brlo 2f
3232 subi r16, 8
3233 mov r18, r19
3234 mov r19, r20
3235 mov r20, r21
3236 mov r21, r22
3237 mov r22, r23
3238 mov r23, r24
3239 mov r24, r25
3240 mov r25, SS
3241 rjmp 0b
3242 1: asr SS
3243 ror r25
3244 ror r24
3245 ror r23
3246 ror r22
3247 ror r21
3248 ror r20
3249 ror r19
3250 ror r18
3251 2: dec r16
3252 brpl 1b
3253 clr __zero_reg__
3254 mov r16, __tmp_reg__
3255 ret
3256 ENDF __lshrdi3
3257
3258 #undef SS
3259
3260 #endif /* defined (L_ashrdi3) */
3261
3262 #if defined (L_ashldi3)
3263 ;; Shift left
3264 ;; r25:r18 = ashl64 (r25:r18, r17:r16)
3265 ;; This function does not clobber T.
3266 DEFUN __ashldi3
3267 mov __tmp_reg__, r16
3268 0: cpi r16, 8
3269 brlo 2f
3270 mov r25, r24
3271 mov r24, r23
3272 mov r23, r22
3273 mov r22, r21
3274 mov r21, r20
3275 mov r20, r19
3276 mov r19, r18
3277 clr r18
3278 subi r16, 8
3279 rjmp 0b
3280 1: lsl r18
3281 rol r19
3282 rol r20
3283 rol r21
3284 rol r22
3285 rol r23
3286 rol r24
3287 rol r25
3288 2: dec r16
3289 brpl 1b
3290 mov r16, __tmp_reg__
3291 ret
3292 ENDF __ashldi3
3293 #endif /* defined (L_ashldi3) */
3294
3295 #if defined (L_rotldi3)
3296 ;; Rotate left
3297 ;; r25:r18 = rotl64 (r25:r18, r17:r16)
3298 DEFUN __rotldi3
3299 push r16
3300 0: cpi r16, 8
3301 brlo 2f
3302 subi r16, 8
3303 mov __tmp_reg__, r25
3304 mov r25, r24
3305 mov r24, r23
3306 mov r23, r22
3307 mov r22, r21
3308 mov r21, r20
3309 mov r20, r19
3310 mov r19, r18
3311 mov r18, __tmp_reg__
3312 rjmp 0b
3313 1: lsl r18
3314 rol r19
3315 rol r20
3316 rol r21
3317 rol r22
3318 rol r23
3319 rol r24
3320 rol r25
3321 adc r18, __zero_reg__
3322 2: dec r16
3323 brpl 1b
3324 pop r16
3325 ret
3326 ENDF __rotldi3
3327 #endif /* defined (L_rotldi3) */
3328
3329
3330 .section .text.libgcc.fmul, "ax", @progbits
3331
3332 /***********************************************************/
3333 ;;; Softmul versions of FMUL, FMULS and FMULSU to implement
3334 ;;; __builtin_avr_fmul* if !AVR_HAVE_MUL
3335 /***********************************************************/
3336
3337 #define A1 24
3338 #define B1 25
3339 #define C0 22
3340 #define C1 23
3341 #define A0 __tmp_reg__
3342
3343 #ifdef L_fmuls
3344 ;;; r23:r22 = fmuls (r24, r25) like in FMULS instruction
3345 ;;; Clobbers: r24, r25, __tmp_reg__
3346 DEFUN __fmuls
3347 ;; A0.7 = negate result?
3348 mov A0, A1
3349 eor A0, B1
3350 ;; B1 = |B1|
3351 sbrc B1, 7
3352 neg B1
3353 XJMP __fmulsu_exit
3354 ENDF __fmuls
3355 #endif /* L_fmuls */
3356
3357 #ifdef L_fmulsu
3358 ;;; r23:r22 = fmulsu (r24, r25) like in FMULSU instruction
3359 ;;; Clobbers: r24, r25, __tmp_reg__
3360 DEFUN __fmulsu
3361 ;; A0.7 = negate result?
3362 mov A0, A1
3363 ;; FALLTHRU
3364 ENDF __fmulsu
3365
3366 ;; Helper for __fmuls and __fmulsu
3367 DEFUN __fmulsu_exit
3368 ;; A1 = |A1|
3369 sbrc A1, 7
3370 neg A1
3371 #ifdef __AVR_ERRATA_SKIP_JMP_CALL__
3372 ;; Some cores have problem skipping 2-word instruction
3373 tst A0
3374 brmi 1f
3375 #else
3376 sbrs A0, 7
3377 #endif /* __AVR_HAVE_JMP_CALL__ */
3378 XJMP __fmul
3379 1: XCALL __fmul
3380 ;; C = -C iff A0.7 = 1
3381 NEG2 C0
3382 ret
3383 ENDF __fmulsu_exit
3384 #endif /* L_fmulsu */
3385
3386
3387 #ifdef L_fmul
3388 ;;; r22:r23 = fmul (r24, r25) like in FMUL instruction
3389 ;;; Clobbers: r24, r25, __tmp_reg__
3390 DEFUN __fmul
3391 ; clear result
3392 clr C0
3393 clr C1
3394 clr A0
3395 1: tst B1
3396 ;; 1.0 = 0x80, so test for bit 7 of B to see if A must to be added to C.
3397 2: brpl 3f
3398 ;; C += A
3399 add C0, A0
3400 adc C1, A1
3401 3: ;; A >>= 1
3402 lsr A1
3403 ror A0
3404 ;; B <<= 1
3405 lsl B1
3406 brne 2b
3407 ret
3408 ENDF __fmul
3409 #endif /* L_fmul */
3410
3411 #undef A0
3412 #undef A1
3413 #undef B1
3414 #undef C0
3415 #undef C1
3416
3417
3418
3419 /**********************************
3420 * Floating-Point
3421 **********************************/
3422
3423 #if defined (L_powif)
3424 #ifndef __AVR_TINY__
3425
3426 ;; float output and arg #1
3427 #define A0 22
3428 #define A1 A0 + 1
3429 #define A2 A0 + 2
3430 #define A3 A0 + 3
3431
3432 ;; float arg #2
3433 #define B0 18
3434 #define B1 B0 + 1
3435 #define B2 B0 + 2
3436 #define B3 B0 + 3
3437
3438 ;; float X: input and iterated squares
3439 #define X0 10
3440 #define X1 X0 + 1
3441 #define X2 X0 + 2
3442 #define X3 X0 + 3
3443
3444 ;; float Y: expand result
3445 #define Y0 14
3446 #define Y1 Y0 + 1
3447 #define Y2 Y0 + 2
3448 #define Y3 Y0 + 3
3449
3450 ;; .7 = Sign of I.
3451 ;; .0 == 0 => Y = 1.0f implicitly.
3452 #define Flags R9
3453 #define Y_set 0
3454
3455 ;;; Integer exponent input.
3456 #define I0 28
3457 #define I1 I0+1
3458
3459 #define ONE 0x3f800000
3460
3461 DEFUN __powisf2
3462 ;; Save 11 Registers: R9...R17, R28, R29
3463 do_prologue_saves 11
3464
3465 ;; Fill local vars with input parameters.
3466 wmov I0, 20
3467 mov4 X0, A0
3468 ;; Save sign of exponent for later.
3469 mov Flags, I1
3470 ;; I := abs (I)
3471 tst I1
3472 brpl 1f
3473 NEG2 I0
3474 1:
3475 ;; Y := (I % 2) ? X : 1.0f
3476 ;; (When we come from below, this is like SET, i.e. Flags.Y_set := 1).
3477 bst I0, 0
3478 ;; Flags.Y_set = false means that we have to assume Y = 1.0f below.
3479 bld Flags, Y_set
3480 2: ;; We have A == X when we come from above.
3481 mov4 Y0, A0
3482
3483 .Loop:
3484 ;; while (I >>= 1)
3485 lsr I1
3486 ror I0
3487 sbiw I0, 0
3488 breq .Loop_done
3489
3490 ;; X := X * X
3491 mov4 A0, X0
3492 #ifdef __WITH_AVRLIBC__
3493 XCALL squaref
3494 #else
3495 mov4 B0, X0
3496 XCALL __mulsf3
3497 #endif /* Have AVR-LibC? */
3498 mov4 X0, A0
3499
3500 ;; if (I % 2 == 1) Y := Y * X
3501 bst I0, 0
3502 brtc .Loop
3503 bst Flags, Y_set
3504 ;; When Y is not set => Y := Y * X = 1.0f * X (= A)
3505 ;; Plus, we have to set Y_set = 1 (= I0.0)
3506 brtc 1b
3507 ;; Y is already set: Y := X * Y (= A * Y)
3508 mov4 B0, Y0
3509 XCALL __mulsf3
3510 rjmp 2b
3511
3512 ;; End while
3513 .Loop_done:
3514
3515 ;; A := 1.0f
3516 ldi A3, hhi8(ONE)
3517 ldi A2, hlo8(ONE)
3518 ldi A1, hi8(ONE)
3519 ldi A0, lo8(ONE)
3520
3521 ;; When Y is still not set, the result is 1.0f (= A).
3522 bst Flags, Y_set
3523 brtc .Lret
3524
3525 ;; if (I was < 0) Y = 1.0f / Y
3526 tst Flags
3527 brmi 1f
3528 ;; A := Y
3529 mov4 A0, Y0
3530 rjmp .Lret
3531 1: ;; A := 1 / Y = A / Y
3532 mov4 B0, Y0
3533 XCALL __divsf3
3534
3535 .Lret:
3536 do_epilogue_restores 11
3537 ENDF __powisf2
3538
3539 #undef A0
3540 #undef A1
3541 #undef A2
3542 #undef A3
3543
3544 #undef B0
3545 #undef B1
3546 #undef B2
3547 #undef B3
3548
3549 #undef X0
3550 #undef X1
3551 #undef X2
3552 #undef X3
3553
3554 #undef Y0
3555 #undef Y1
3556 #undef Y2
3557 #undef Y3
3558
3559 #undef I0
3560 #undef I1
3561 #undef ONE
3562
3563 #endif /* __AVR_TINY__ */
3564 #endif /* L_powif */
3565
3566 #include "lib1funcs-fixed.S"