mirrored from git://gcc.gnu.org/git/gcc.git
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathlib1funcs.S
3469 lines (3023 loc) · 72.3 KB
/
lib1funcs.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: Asm -*- */
/* Copyright (C) 1998-2024 Free Software Foundation, Inc.
Contributed by Denis Chertykov <chertykov@gmail.com>
This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#if defined (__AVR_TINY__)
#define __zero_reg__ r17
#define __tmp_reg__ r16
#else
#define __zero_reg__ r1
#define __tmp_reg__ r0
#endif
#define __SREG__ 0x3f
#if defined (__AVR_HAVE_SPH__)
#define __SP_H__ 0x3e
#endif
#define __SP_L__ 0x3d
#define __RAMPZ__ 0x3B
#define __EIND__ 0x3C
/* Most of the functions here are called directly from avr.md
patterns, instead of using the standard libcall mechanisms.
This can make better code because GCC knows exactly which
of the call-used registers (not all of them) are clobbered. */
/* FIXME: At present, there is no SORT directive in the linker
script so that we must not assume that different modules
in the same input section like .libgcc.text.mul will be
located close together. Therefore, we cannot use
RCALL/RJMP to call a function like __udivmodhi4 from
__divmodhi4 and have to use lengthy XCALL/XJMP even
though they are in the same input section and all same
input sections together are small enough to reach every
location with a RCALL/RJMP instruction. */
#if defined (__AVR_HAVE_EIJMP_EICALL__) && !defined (__AVR_HAVE_ELPMX__)
#error device not supported
#endif
.macro mov_l r_dest, r_src
#if defined (__AVR_HAVE_MOVW__)
movw \r_dest, \r_src
#else
mov \r_dest, \r_src
#endif
.endm
.macro mov_h r_dest, r_src
#if defined (__AVR_HAVE_MOVW__)
; empty
#else
mov \r_dest, \r_src
#endif
.endm
.macro wmov r_dest, r_src
#if defined (__AVR_HAVE_MOVW__)
movw \r_dest, \r_src
#else
mov \r_dest, \r_src
mov \r_dest+1, \r_src+1
#endif
.endm
.macro mov4 r_dest, r_src
wmov \r_dest, \r_src
wmov \r_dest+2, \r_src+2
.endm
#if defined (__AVR_HAVE_JMP_CALL__)
#define XCALL call
#define XJMP jmp
#else
#define XCALL rcall
#define XJMP rjmp
#endif
#if defined (__AVR_HAVE_EIJMP_EICALL__)
#define XICALL eicall
#define XIJMP eijmp
#else
#define XICALL icall
#define XIJMP ijmp
#endif
;; Prologue stuff
.macro do_prologue_saves n_pushed n_frame=0
ldi r26, lo8(\n_frame)
ldi r27, hi8(\n_frame)
ldi r30, lo8(gs(.L_prologue_saves.\@))
ldi r31, hi8(gs(.L_prologue_saves.\@))
XJMP __prologue_saves__ + ((18 - (\n_pushed)) * 2)
.L_prologue_saves.\@:
.endm
;; Epilogue stuff
.macro do_epilogue_restores n_pushed n_frame=0
in r28, __SP_L__
#ifdef __AVR_HAVE_SPH__
in r29, __SP_H__
.if \n_frame > 63
subi r28, lo8(-\n_frame)
sbci r29, hi8(-\n_frame)
.elseif \n_frame > 0
adiw r28, \n_frame
.endif
#else
clr r29
.if \n_frame > 0
subi r28, lo8(-\n_frame)
.endif
#endif /* HAVE SPH */
ldi r30, \n_pushed
XJMP __epilogue_restores__ + ((18 - (\n_pushed)) * 2)
.endm
;; Support function entry and exit for convenience
.macro wsubi r_arg1, i_arg2
#if defined (__AVR_TINY__)
subi \r_arg1, lo8(\i_arg2)
sbci \r_arg1+1, hi8(\i_arg2)
#else
sbiw \r_arg1, \i_arg2
#endif
.endm
.macro waddi r_arg1, i_arg2
#if defined (__AVR_TINY__)
subi \r_arg1, lo8(-\i_arg2)
sbci \r_arg1+1, hi8(-\i_arg2)
#else
adiw \r_arg1, \i_arg2
#endif
.endm
.macro DEFUN name
.global \name
.func \name
\name:
.endm
.macro ENDF name
.size \name, .-\name
.endfunc
.endm
.macro FALIAS name
.global \name
.func \name
\name:
.size \name, .-\name
.endfunc
.endm
;; Skip next instruction, typically a jump target
#define skip cpse 16,16
;; Negate a 2-byte value held in consecutive registers
.macro NEG2 reg
com \reg+1
neg \reg
sbci \reg+1, -1
.endm
;; Negate a 4-byte value held in consecutive registers
;; Sets the V flag for signed overflow tests if REG >= 16
.macro NEG4 reg
com \reg+3
com \reg+2
com \reg+1
.if \reg >= 16
neg \reg
sbci \reg+1, -1
sbci \reg+2, -1
sbci \reg+3, -1
.else
com \reg
adc \reg, __zero_reg__
adc \reg+1, __zero_reg__
adc \reg+2, __zero_reg__
adc \reg+3, __zero_reg__
.endif
.endm
#define exp_lo(N) hlo8 ((N) << 23)
#define exp_hi(N) hhi8 ((N) << 23)
.section .text.libgcc.mul, "ax", @progbits
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
/* Note: mulqi3, mulhi3 are open-coded on the enhanced core. */
#if !defined (__AVR_HAVE_MUL__)
/*******************************************************
Multiplication 8 x 8 without MUL
*******************************************************/
#if defined (L_mulqi3)
#define r_arg2 r22 /* multiplicand */
#define r_arg1 r24 /* multiplier */
#define r_res __tmp_reg__ /* result */
DEFUN __mulqi3
clr r_res ; clear result
__mulqi3_loop:
sbrc r_arg1,0
add r_res,r_arg2
add r_arg2,r_arg2 ; shift multiplicand
breq __mulqi3_exit ; while multiplicand != 0
lsr r_arg1 ;
brne __mulqi3_loop ; exit if multiplier = 0
__mulqi3_exit:
mov r_arg1,r_res ; result to return register
ret
ENDF __mulqi3
#undef r_arg2
#undef r_arg1
#undef r_res
#endif /* defined (L_mulqi3) */
/*******************************************************
Widening Multiplication 16 = 8 x 8 without MUL
Multiplication 16 x 16 without MUL
*******************************************************/
#define A0 22
#define A1 23
#define B0 24
#define BB0 20
#define B1 25
;; Output overlaps input, thus expand result in CC0/1
#define C0 24
#define C1 25
#define CC0 __tmp_reg__
#define CC1 21
#if defined (L_umulqihi3)
;;; R25:R24 = (unsigned int) R22 * (unsigned int) R24
;;; (C1:C0) = (unsigned int) A0 * (unsigned int) B0
;;; Clobbers: __tmp_reg__, R21..R23
DEFUN __umulqihi3
clr A1
clr B1
XJMP __mulhi3
ENDF __umulqihi3
#endif /* L_umulqihi3 */
#if defined (L_mulqihi3)
;;; R25:R24 = (signed int) R22 * (signed int) R24
;;; (C1:C0) = (signed int) A0 * (signed int) B0
;;; Clobbers: __tmp_reg__, R20..R23
DEFUN __mulqihi3
;; Sign-extend B0
clr B1
sbrc B0, 7
com B1
;; The multiplication runs twice as fast if A1 is zero, thus:
;; Zero-extend A0
clr A1
#ifdef __AVR_HAVE_JMP_CALL__
;; Store B0 * sign of A
clr BB0
sbrc A0, 7
mov BB0, B0
call __mulhi3
#else /* have no CALL */
;; Skip sign-extension of A if A >= 0
;; Same size as with the first alternative but avoids errata skip
;; and is faster if A >= 0
sbrs A0, 7
rjmp __mulhi3
;; If A < 0 store B
mov BB0, B0
rcall __mulhi3
#endif /* HAVE_JMP_CALL */
;; 1-extend A after the multiplication
sub C1, BB0
ret
ENDF __mulqihi3
#endif /* L_mulqihi3 */
#if defined (L_mulhi3)
;;; R25:R24 = R23:R22 * R25:R24
;;; (C1:C0) = (A1:A0) * (B1:B0)
;;; Clobbers: __tmp_reg__, R21..R23
DEFUN __mulhi3
;; Clear result
clr CC0
clr CC1
rjmp 3f
1:
;; Bit n of A is 1 --> C += B << n
add CC0, B0
adc CC1, B1
2:
lsl B0
rol B1
3:
;; If B == 0 we are ready
wsubi B0, 0
breq 9f
;; Carry = n-th bit of A
lsr A1
ror A0
;; If bit n of A is set, then go add B * 2^n to C
brcs 1b
;; Carry = 0 --> The ROR above acts like CP A0, 0
;; Thus, it is sufficient to CPC the high part to test A against 0
cpc A1, __zero_reg__
;; Only proceed if A != 0
brne 2b
9:
;; Move Result into place
mov C0, CC0
mov C1, CC1
ret
ENDF __mulhi3
#endif /* L_mulhi3 */
#undef A0
#undef A1
#undef B0
#undef BB0
#undef B1
#undef C0
#undef C1
#undef CC0
#undef CC1
#define A0 22
#define A1 A0+1
#define A2 A0+2
#define A3 A0+3
#define B0 18
#define B1 B0+1
#define B2 B0+2
#define B3 B0+3
#define CC0 26
#define CC1 CC0+1
#define CC2 30
#define CC3 CC2+1
#define C0 22
#define C1 C0+1
#define C2 C0+2
#define C3 C0+3
/*******************************************************
Widening Multiplication 32 = 16 x 16 without MUL
*******************************************************/
#if defined (L_umulhisi3)
DEFUN __umulhisi3
wmov B0, 24
;; Zero-extend B
clr B2
clr B3
;; Zero-extend A
wmov A2, B2
XJMP __mulsi3
ENDF __umulhisi3
#endif /* L_umulhisi3 */
#if defined (L_mulhisi3)
DEFUN __mulhisi3
wmov B0, 24
;; Sign-extend B
lsl r25
sbc B2, B2
mov B3, B2
#ifdef __AVR_ERRATA_SKIP_JMP_CALL__
;; Sign-extend A
clr A2
sbrc A1, 7
com A2
mov A3, A2
XJMP __mulsi3
#else /* no __AVR_ERRATA_SKIP_JMP_CALL__ */
;; Zero-extend A and __mulsi3 will run at least twice as fast
;; compared to a sign-extended A.
clr A2
clr A3
sbrs A1, 7
XJMP __mulsi3
;; If A < 0 then perform the B * 0xffff.... before the
;; very multiplication by initializing the high part of the
;; result CC with -B.
wmov CC2, A2
sub CC2, B0
sbc CC3, B1
XJMP __mulsi3_helper
#endif /* __AVR_ERRATA_SKIP_JMP_CALL__ */
ENDF __mulhisi3
#endif /* L_mulhisi3 */
/*******************************************************
Multiplication 32 x 32 without MUL
*******************************************************/
#if defined (L_mulsi3)
DEFUN __mulsi3
#if defined (__AVR_TINY__)
in r26, __SP_L__ ; safe to use X, as it is CC0/CC1
in r27, __SP_H__
subi r26, lo8(-3) ; Add 3 to point past return address
sbci r27, hi8(-3)
push B0 ; save callee saved regs
push B1
ld B0, X+ ; load from caller stack
ld B1, X+
ld B2, X+
ld B3, X
#endif
;; Clear result
clr CC2
clr CC3
;; FALLTHRU
ENDF __mulsi3
DEFUN __mulsi3_helper
clr CC0
clr CC1
rjmp 3f
1: ;; If bit n of A is set, then add B * 2^n to the result in CC
;; CC += B
add CC0,B0 $ adc CC1,B1 $ adc CC2,B2 $ adc CC3,B3
2: ;; B <<= 1
lsl B0 $ rol B1 $ rol B2 $ rol B3
3: ;; A >>= 1: Carry = n-th bit of A
lsr A3 $ ror A2 $ ror A1 $ ror A0
brcs 1b
;; Only continue if A != 0
sbci A1, 0
brne 2b
wsubi A2, 0
brne 2b
;; All bits of A are consumed: Copy result to return register C
wmov C0, CC0
wmov C2, CC2
#if defined (__AVR_TINY__)
pop B1 ; restore callee saved regs
pop B0
#endif /* defined (__AVR_TINY__) */
ret
ENDF __mulsi3_helper
#endif /* L_mulsi3 */
#undef A0
#undef A1
#undef A2
#undef A3
#undef B0
#undef B1
#undef B2
#undef B3
#undef C0
#undef C1
#undef C2
#undef C3
#undef CC0
#undef CC1
#undef CC2
#undef CC3
#endif /* !defined (__AVR_HAVE_MUL__) */
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#if defined (__AVR_HAVE_MUL__)
#define A0 26
#define B0 18
#define C0 22
#define A1 A0+1
#define B1 B0+1
#define B2 B0+2
#define B3 B0+3
#define C1 C0+1
#define C2 C0+2
#define C3 C0+3
/*******************************************************
Widening Multiplication 32 = 16 x 16 with MUL
*******************************************************/
#if defined (L_mulhisi3)
;;; R25:R22 = (signed long) R27:R26 * (signed long) R19:R18
;;; C3:C0 = (signed long) A1:A0 * (signed long) B1:B0
;;; Clobbers: __tmp_reg__
DEFUN __mulhisi3
XCALL __umulhisi3
;; Sign-extend B
tst B1
brpl 1f
sub C2, A0
sbc C3, A1
1: ;; Sign-extend A
XJMP __usmulhisi3_tail
ENDF __mulhisi3
#endif /* L_mulhisi3 */
#if defined (L_usmulhisi3)
;;; R25:R22 = (signed long) R27:R26 * (unsigned long) R19:R18
;;; C3:C0 = (signed long) A1:A0 * (unsigned long) B1:B0
;;; Clobbers: __tmp_reg__
DEFUN __usmulhisi3
XCALL __umulhisi3
;; FALLTHRU
ENDF __usmulhisi3
DEFUN __usmulhisi3_tail
;; Sign-extend A
sbrs A1, 7
ret
sub C2, B0
sbc C3, B1
ret
ENDF __usmulhisi3_tail
#endif /* L_usmulhisi3 */
#if defined (L_umulhisi3)
;;; R25:R22 = (unsigned long) R27:R26 * (unsigned long) R19:R18
;;; C3:C0 = (unsigned long) A1:A0 * (unsigned long) B1:B0
;;; Clobbers: __tmp_reg__
DEFUN __umulhisi3
mul A0, B0
movw C0, r0
mul A1, B1
movw C2, r0
mul A0, B1
#ifdef __AVR_HAVE_JMP_CALL__
;; This function is used by many other routines, often multiple times.
;; Therefore, if the flash size is not too limited, avoid the RCALL
;; and inverst 6 Bytes to speed things up.
add C1, r0
adc C2, r1
clr __zero_reg__
adc C3, __zero_reg__
#else
rcall 1f
#endif
mul A1, B0
1: add C1, r0
adc C2, r1
clr __zero_reg__
adc C3, __zero_reg__
ret
ENDF __umulhisi3
#endif /* L_umulhisi3 */
/*******************************************************
Widening Multiplication 32 = 16 x 32 with MUL
*******************************************************/
#if defined (L_mulshisi3)
;;; R25:R22 = (signed long) R27:R26 * R21:R18
;;; (C3:C0) = (signed long) A1:A0 * B3:B0
;;; Clobbers: __tmp_reg__
DEFUN __mulshisi3
#ifdef __AVR_ERRATA_SKIP_JMP_CALL__
;; Some cores have problem skipping 2-word instruction
tst A1
brmi __mulohisi3
#else
sbrs A1, 7
#endif /* __AVR_HAVE_JMP_CALL__ */
XJMP __muluhisi3
;; FALLTHRU
ENDF __mulshisi3
;;; R25:R22 = (one-extended long) R27:R26 * R21:R18
;;; (C3:C0) = (one-extended long) A1:A0 * B3:B0
;;; Clobbers: __tmp_reg__
DEFUN __mulohisi3
XCALL __muluhisi3
;; One-extend R27:R26 (A1:A0)
sub C2, B0
sbc C3, B1
ret
ENDF __mulohisi3
#endif /* L_mulshisi3 */
#if defined (L_muluhisi3)
;;; R25:R22 = (unsigned long) R27:R26 * R21:R18
;;; (C3:C0) = (unsigned long) A1:A0 * B3:B0
;;; Clobbers: __tmp_reg__
DEFUN __muluhisi3
XCALL __umulhisi3
mul A0, B3
add C3, r0
mul A1, B2
add C3, r0
mul A0, B2
add C2, r0
adc C3, r1
clr __zero_reg__
ret
ENDF __muluhisi3
#endif /* L_muluhisi3 */
/*******************************************************
Multiplication 32 x 32 with MUL
*******************************************************/
#if defined (L_mulsi3)
;;; R25:R22 = R25:R22 * R21:R18
;;; (C3:C0) = C3:C0 * B3:B0
;;; Clobbers: R26, R27, __tmp_reg__
DEFUN __mulsi3
movw A0, C0
push C2
push C3
XCALL __muluhisi3
pop A1
pop A0
;; A1:A0 now contains the high word of A
mul A0, B0
add C2, r0
adc C3, r1
mul A0, B1
add C3, r0
mul A1, B0
add C3, r0
clr __zero_reg__
ret
ENDF __mulsi3
#endif /* L_mulsi3 */
#undef A0
#undef A1
#undef B0
#undef B1
#undef B2
#undef B3
#undef C0
#undef C1
#undef C2
#undef C3
#endif /* __AVR_HAVE_MUL__ */
/*******************************************************
Multiplication 24 x 24 with MUL
*******************************************************/
#if defined (L_mulpsi3)
;; A[0..2]: In: Multiplicand; Out: Product
#define A0 22
#define A1 A0+1
#define A2 A0+2
;; B[0..2]: In: Multiplier
#define B0 18
#define B1 B0+1
#define B2 B0+2
#if defined (__AVR_HAVE_MUL__)
;; C[0..2]: Expand Result
#define C0 22
#define C1 C0+1
#define C2 C0+2
;; R24:R22 *= R20:R18
;; Clobbers: r21, r25, r26, r27, __tmp_reg__
#define AA0 26
#define AA2 21
DEFUN __mulpsi3
wmov AA0, A0
mov AA2, A2
XCALL __umulhisi3
mul AA2, B0 $ add C2, r0
mul AA0, B2 $ add C2, r0
clr __zero_reg__
ret
ENDF __mulpsi3
#undef AA2
#undef AA0
#undef C2
#undef C1
#undef C0
#else /* !HAVE_MUL */
;; C[0..2]: Expand Result
#if defined (__AVR_TINY__)
#define C0 16
#else
#define C0 0
#endif /* defined (__AVR_TINY__) */
#define C1 C0+1
#define C2 21
;; R24:R22 *= R20:R18
;; Clobbers: __tmp_reg__, R18, R19, R20, R21
DEFUN __mulpsi3
#if defined (__AVR_TINY__)
in r26,__SP_L__
in r27,__SP_H__
subi r26, lo8(-3) ; Add 3 to point past return address
sbci r27, hi8(-3)
push B0 ; save callee saved regs
push B1
ld B0,X+ ; load from caller stack
ld B1,X+
ld B2,X+
#endif /* defined (__AVR_TINY__) */
;; C[] = 0
clr __tmp_reg__
clr C2
0: ;; Shift N-th Bit of B[] into Carry. N = 24 - Loop
LSR B2 $ ror B1 $ ror B0
;; If the N-th Bit of B[] was set...
brcc 1f
;; ...then add A[] * 2^N to the Result C[]
ADD C0,A0 $ adc C1,A1 $ adc C2,A2
1: ;; Multiply A[] by 2
LSL A0 $ rol A1 $ rol A2
;; Loop until B[] is 0
subi B0,0 $ sbci B1,0 $ sbci B2,0
brne 0b
;; Copy C[] to the return Register A[]
wmov A0, C0
mov A2, C2
clr __zero_reg__
#if defined (__AVR_TINY__)
pop B1
pop B0
#endif /* (__AVR_TINY__) */
ret
ENDF __mulpsi3
#undef C2
#undef C1
#undef C0
#endif /* HAVE_MUL */
#undef B2
#undef B1
#undef B0
#undef A2
#undef A1
#undef A0
#endif /* L_mulpsi3 */
#if defined (L_mulsqipsi3) && defined (__AVR_HAVE_MUL__)
;; A[0..2]: In: Multiplicand
#define A0 22
#define A1 A0+1
#define A2 A0+2
;; BB: In: Multiplier
#define BB 25
;; C[0..2]: Result
#define C0 18
#define C1 C0+1
#define C2 C0+2
;; C[] = A[] * sign_extend (BB)
DEFUN __mulsqipsi3
mul A0, BB
movw C0, r0
mul A2, BB
mov C2, r0
mul A1, BB
add C1, r0
adc C2, r1
clr __zero_reg__
sbrs BB, 7
ret
;; One-extend BB
sub C1, A0
sbc C2, A1
ret
ENDF __mulsqipsi3
#undef C2
#undef C1
#undef C0
#undef BB
#undef A2
#undef A1
#undef A0
#endif /* L_mulsqipsi3 && HAVE_MUL */
/*******************************************************
Multiplication 64 x 64
*******************************************************/
;; A[] = A[] * B[]
;; A[0..7]: In: Multiplicand
;; Out: Product
#define A0 18
#define A1 A0+1
#define A2 A0+2
#define A3 A0+3
#define A4 A0+4
#define A5 A0+5
#define A6 A0+6
#define A7 A0+7
;; B[0..7]: In: Multiplier
#define B0 10
#define B1 B0+1
#define B2 B0+2
#define B3 B0+3
#define B4 B0+4
#define B5 B0+5
#define B6 B0+6
#define B7 B0+7
#ifndef __AVR_TINY__
#if defined (__AVR_HAVE_MUL__)
;; Define C[] for convenience
;; Notice that parts of C[] overlap A[] respective B[]
#define C0 16
#define C1 C0+1
#define C2 20
#define C3 C2+1
#define C4 28
#define C5 C4+1
#define C6 C4+2
#define C7 C4+3
#if defined (L_muldi3)
;; A[] *= B[]
;; R25:R18 *= R17:R10
;; Ordinary ABI-Function
DEFUN __muldi3
push r29
push r28
push r17
push r16
;; Counting in Words, we have to perform a 4 * 4 Multiplication
;; 3 * 0 + 0 * 3
mul A7,B0 $ $ mov C7,r0
mul A0,B7 $ $ add C7,r0
mul A6,B1 $ $ add C7,r0
mul A6,B0 $ mov C6,r0 $ add C7,r1
mul B6,A1 $ $ add C7,r0
mul B6,A0 $ add C6,r0 $ adc C7,r1
;; 1 * 2
mul A2,B4 $ add C6,r0 $ adc C7,r1
mul A3,B4 $ $ add C7,r0
mul A2,B5 $ $ add C7,r0
push A5
push A4
push B1
push B0
push A3
push A2
;; 0 * 0
wmov 26, B0
XCALL __umulhisi3
wmov C0, 22
wmov C2, 24
;; 0 * 2
wmov 26, B4
XCALL __umulhisi3 $ wmov C4,22 $ add C6,24 $ adc C7,25
wmov 26, B2
;; 0 * 1
XCALL __muldi3_6
pop A0
pop A1
;; 1 * 1
wmov 26, B2
XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25
pop r26
pop r27
;; 1 * 0
XCALL __muldi3_6
pop A0
pop A1
;; 2 * 0
XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25
;; 2 * 1
wmov 26, B2
XCALL __umulhisi3 $ $ $ add C6,22 $ adc C7,23
;; A[] = C[]
wmov A0, C0
;; A2 = C2 already
wmov A4, C4
wmov A6, C6
pop r16
pop r17
pop r28
pop r29
ret
ENDF __muldi3
#endif /* L_muldi3 */
#if defined (L_muldi3_6)
;; A helper for some 64-bit multiplications with MUL available
DEFUN __muldi3_6
__muldi3_6:
XCALL __umulhisi3
add C2, 22
adc C3, 23
adc C4, 24
adc C5, 25
brcc 0f
adiw C6, 1
0: ret
ENDF __muldi3_6
#endif /* L_muldi3_6 */
#undef C7
#undef C6
#undef C5
#undef C4
#undef C3
#undef C2
#undef C1
#undef C0
#else /* !HAVE_MUL */
#if defined (L_muldi3)
#define C0 26
#define C1 C0+1
#define C2 C0+2