Debian Codegraph @

libgcc/config/avr/libf7/libf7-asm.sx

raw
1 ;; Copyright (C) 2019-2025 Free Software Foundation, Inc.
2 ;;
3 ;; This file is part of LIBF7, which is part of GCC.
4 ;;
5 ;; GCC is free software; you can redistribute it and/or modify it under
6 ;; the terms of the GNU General Public License as published by the Free
7 ;; Software Foundation; either version 3, or (at your option) any later
8 ;; version.
9 ;;
10 ;; GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 ;; WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 ;; 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 #ifndef __AVR_TINY__
25
26 #define ASM_DEFS_HAVE_DEFUN
27
28 #include "asm-defs.h"
29 #include "libf7.h"
30
31 #define ZERO __zero_reg__
32 #define TMP __tmp_reg__
33
34 #define F7(name) F7_(name##_asm)
35
36 .macro F7call name
37 .global F7(\name\())
38 XCALL F7(\name\())
39 .endm
40
41 .macro F7jmp name
42 .global F7(\name\())
43 XJMP F7(\name\())
44 .endm
45
46 ;; Just for visibility in disassembly.
47 .macro LLL name
48 .global LLL.\name
49 LLL.\name:
50 nop
51 .endm
52
53 .macro DEFUN name
54 .section .text.libf7.asm.\name, "ax", @progbits
55 .global F7(\name\())
56 .func F7(\name\())
57 F7(\name\()) :
58 .endm
59
60 .macro ENDF name
61 .size F7(\name\()), . - F7(\name\())
62 .endfunc
63 .endm
64
65 .macro LABEL name
66 .global F7(\name\())
67 F7(\name\()) :
68 .endm
69
70 .macro _DEFUN name
71 .section .text.libf7.asm.\name, "ax", @progbits
72 .weak \name
73 .type \name, @function
74 \name :
75 .endm
76
77 .macro _ENDF name
78 .size \name, . - \name
79 .endm
80
81 .macro _LABEL name
82 .weak \name
83 .type \name, @function
84 \name :
85 .endm
86
87 #define F7_NAME(X) F7_(X)
88
89 ;; Make a weak alias.
90 .macro ALIAS sym
91 .weak \sym
92 .type \sym, @function
93 \sym:
94 .endm
95
96 ;; Make a weak alias if double is 64 bits wide.
97 .macro DALIAS sym
98 #if defined (WITH_LIBF7_MATH_SYMBOLS) && __SIZEOF_DOUBLE__ == 8
99 ALIAS \sym
100 #endif
101 .endm
102
103 ;; Make a weak alias if long double is 64 bits wide.
104 .macro LALIAS sym
105 #if defined (WITH_LIBF7_MATH_SYMBOLS) && __SIZEOF_LONG_DOUBLE__ == 8
106 ALIAS \sym
107 #endif
108 .endm
109
110 #define Off 1
111 #define Expo (Off + F7_MANT_BYTES)
112
113 #ifdef F7MOD_classify_
114 ;; r24 = classify (*Z)
115 ;; NaN -> F7_FLAG_nan
116 ;; INF -> F7_FLAG_inf [ | F7_FLAG_sign ]
117 ;; ==0 -> F7_FLAG_zero
118 ;; ... -> 0 [ | F7_FLAG_sign ]
119
120 ;; Clobbers: None (no TMP, no T).
121 DEFUN classify
122
123 ld r24, Z
124 lsr r24
125 brne .Lnan_or_inf
126
127 ldd r24, Z+6+Off
128 tst r24
129 brpl 0f
130 sbc r24, r24
131 andi r24, F7_FLAG_sign
132 ret
133
134 0: ldi r24, F7_FLAG_zero
135 ret
136
137 .Lnan_or_inf:
138 rol r24
139 ret
140
141 ENDF classify
142 #endif /* F7MOD_classify_ */
143
144 #ifdef F7MOD_clr_
145 DEFUN clr
146 std Z+0, ZERO
147 std Z+0+Off, ZERO
148 std Z+1+Off, ZERO
149 std Z+2+Off, ZERO
150 std Z+3+Off, ZERO
151 std Z+4+Off, ZERO
152 std Z+5+Off, ZERO
153 std Z+6+Off, ZERO
154 std Z+0+Expo, ZERO
155 std Z+1+Expo, ZERO
156 ret
157 ENDF clr
158
159 #endif /* F7MOD_clr_ */
160
161 #ifdef F7MOD_clz_
162 ;; The libcc CLZ implementations like __clzsi2 aka. __builtin_clzl are
163 ;; not very well suited for out purpose, so implement our own.
164
165 #define ZBITS r26
166 .macro .test.byte reg
167 or ZERO, \reg
168 brne .Loop_bit
169 subi ZBITS, -8
170 .endm
171
172 ;; R26 = CLZ (uint64_t R18); CLZ (0) = 64.
173 ;; Unchanged: T
174 DEFUN clzdi2
175 clr ZBITS
176 ;; Catch the common case of normalized .mant for speed-up.
177 tst r25
178 brmi 9f
179 .test.byte r25
180 .test.byte r24
181 .test.byte r23
182 .test.byte r22
183 .test.byte r21
184 .test.byte r20
185 .test.byte r19
186 .test.byte r18
187 .Ldone:
188 clr ZERO
189 9: ret
190
191 .Loop_bit:
192 lsl ZERO
193 brcs .Ldone
194 inc ZBITS
195 rjmp .Loop_bit
196
197 ENDF clzdi2
198 #undef ZBITS
199 #endif /* F7MOD_clz_ */
200
201 #ifdef F7MOD_cmp_mant_
202 DEFUN cmp_mant
203
204 adiw X, 6 + Off
205 ld r24, X $ ldd TMP, Z+6+Off $ SUB r24, TMP
206 brne .Lunequal
207
208 sbiw X, 6
209 ld r24, X+ $ ldd TMP, Z+0+Off $ SUB r24, TMP
210 ld r24, X+ $ ldd TMP, Z+1+Off $ sbc r24, TMP
211 ld r24, X+ $ ldd TMP, Z+2+Off $ sbc r24, TMP
212 ld r24, X+ $ ldd TMP, Z+3+Off $ sbc r24, TMP
213 ld r24, X+ $ ldd TMP, Z+4+Off $ sbc r24, TMP
214 ld r24, X+ $ ldd TMP, Z+5+Off $ sbc r24, TMP
215 ;; MSBs are already known to be equal
216 breq 9f
217 .Lunequal:
218 sbc r24, r24
219 sbci r24, -1
220 9: sbiw X, 6 + Off
221 ret
222 ENDF cmp_mant
223 #endif /* F7MOD_cmp_mant_ */
224
225 #define CA 18
226 #define C0 CA+1
227 #define C1 C0+1
228 #define C2 C0+2
229 #define C3 C0+3
230 #define C4 C0+4
231 #define C5 C0+5
232 #define C6 C0+6
233 #define Carry r16
234 #define Flags 18
235
236 #ifdef F7MOD_store_
237 ;; Z->flags = CA.
238 ;; Z->mant = C[7].
239 DEFUN store_mant.with_flags
240 st Z, CA
241
242 ;; Z->mant = C[7].
243 LABEL store_mant
244 std Z+0+Off, C0
245 std Z+1+Off, C1
246 std Z+2+Off, C2
247 std Z+3+Off, C3
248 std Z+4+Off, C4
249 std Z+5+Off, C5
250 std Z+6+Off, C6
251 ret
252 ENDF store_mant.with_flags
253 #endif /* F7MOD_store_ */
254
255 #ifdef F7MOD_load_
256 ;; CA = Z->flags
257 ;; C[7] = Z->mant
258 DEFUN load_mant.with_flags
259 ld CA, Z
260 skipnext
261
262 ;; CA = 0
263 ;; C[7] = Z->mant
264 LABEL load_mant.clr_CA
265 LABEL load_mant.clr_flags
266 clr CA ; May be skipped
267
268 ;; C[7] = Z->mant
269 LABEL load_mant
270 ldd C0, Z+0+Off
271 ldd C1, Z+1+Off
272 ldd C2, Z+2+Off
273 ldd C3, Z+3+Off
274 ldd C4, Z+4+Off
275 ldd C5, Z+5+Off
276 ldd C6, Z+6+Off
277 ret
278 ENDF load_mant.with_flags
279 #endif /* F7MOD_load_ */
280
281 #ifdef F7MOD_copy_
282 DEFUN copy
283 cp XL, ZL
284 cpc XH, ZH
285 breq 9f
286 adiw XL, F7_SIZEOF
287 adiw ZL, F7_SIZEOF
288 set
289 bld ZERO, 1
290 bld ZERO, 3 ; ZERO = 0b1010 = 10.
291 .Loop:
292 ld TMP, -X
293 st -Z, TMP
294 dec ZERO
295 brne .Loop
296 9: ret
297 ENDF copy
298 #endif /* F7MOD_copy_ */
299
300 #ifdef F7MOD_copy_P_
301 DEFUN copy_P
302 set
303 bld ZERO, 1
304 bld ZERO, 3 ; ZERO = 0b1010 = 10.
305 .Loop:
306 #ifdef __AVR_HAVE_LPMX__
307 lpm TMP, Z+
308 #else
309 lpm
310 adiw Z, 1
311 #endif /* Have LPMx */
312 st X+, TMP
313 dec ZERO
314 brne .Loop
315 sbiw X, F7_SIZEOF
316 sbiw Z, F7_SIZEOF
317 ret
318 ENDF copy_P
319 #endif /* F7MOD_copy_P_ */
320
321 #ifdef F7MOD_copy_mant_
322 DEFUN copy_mant
323 cp XL, ZL
324 cpc XH, ZH
325 breq 9f
326 adiw XL, 1
327 adiw ZL, 1
328 set
329 bld ZERO, 3
330 dec ZERO ; ZERO = 7
331 .Loop:
332 ld TMP, X+
333 st Z+, TMP
334 dec ZERO
335 brne .Loop
336 sbiw XL, 8
337 sbiw ZL, 8
338 9: ret
339 ENDF copy_mant
340 #endif /* F7MOD_copy_mant_ */
341
342
343 #ifdef F7MOD_clr_mant_lsbs_
344 DEFUN clr_mant_lsbs
345 push r16
346 mov r16, r20
347 wmov XL, r24
348
349 wmov ZL, r22
350 F7call load_mant
351
352 F7call lshrdi3
353
354 clr CA
355
356 F7call ashldi3
357
358 pop r16
359
360 wmov ZL, XL
361 F7jmp store_mant
362
363 ENDF clr_mant_lsbs
364 #endif /* F7MOD_clr_mant_lsbs_ */
365
366
367 #ifdef F7MOD_normalize_with_carry_
368 ;; Z = &f7_t
369 ;; C[] = .mant may be not normalized
370 ;; Carry === r16 = Addend to Z->expo in [-64, 128).
371 ;; Normalize C[], set Flags, and adjust Z->expo.
372 ;; Return CA (after normalization) in TMP.
373 ;; Unchanged: T
374 #define Addend r17
375 #define Zbits r26
376 #define expL r26
377 #define expH r27
378 DEFUN normalize_with_carry
379 mov Addend, Carry
380 tst C6
381 brmi .Lshift.0
382 ;; r26 = CLZ (uint64_t R18)
383 F7call clzdi2
384 cpi Zbits, 64
385 breq .Lclr
386 sub Addend, Zbits
387 mov r16, Zbits
388
389 F7call ashldi3
390 ;; Assert (R25.7 == 1)
391 .Lshift.0:
392 mov TMP, CA
393 ld Flags, Z
394
395 ;; .expo += Addend
396 ldd expL, Z+0+Expo
397 ldd expH, Z+1+Expo
398 ;; Sign-extend Addend
399 clr r16
400 sbrc Addend, 7
401 com r16
402
403 ;; exp += (int8_t) Addend, i.e. sign-extend Addend.
404 add expL, Addend
405 adc expH, r16
406 brvc .Lnormal
407 tst r16
408 brmi .Lclr
409 ;; Overflow
410 #if F7_HAVE_Inf == 1
411 ori Flags, F7_FLAG_inf
412 #else
413 ldi Flags, F7_FLAG_nan
414 #endif /* Have Inf */
415 ret
416
417 .Lnormal:
418 std Z+0+Expo, expL
419 std Z+1+Expo, expH
420 ret
421
422 .Lclr:
423 ;; Underflow or Zero.
424 clr TMP
425 .global __clr_8
426 XJMP __clr_8
427
428 LABEL normalize.store_with_flags
429 ;; no rounding
430 set
431 skipnext
432 LABEL normalize.round.store_with_flags
433 ;; with rounding
434 clt ; skipped ?
435 LABEL normalize.maybe_round.store_with_flags
436 F7call normalize_with_carry
437 ;; We have:
438 ;; Z = &f7_t
439 ;; X = .expo
440 ;; C[] = .mant
441 ;; R18 = .flags
442 ;; TMP = byte below .mant after normalization
443 ;; T = 1 => no rounding.
444 brts .Lstore
445 lsl TMP
446 adc C0, ZERO
447 brcc .Lstore
448 adc C1, ZERO
449 adc C2, ZERO
450 adc C3, ZERO
451 adc C4, ZERO
452 adc C5, ZERO
453 adc C6, ZERO
454 brcc .Lstore
455 ;; We only come here if C6 overflowed, i.e. C[] is 0 now.
456 ;; .mant = 1.0 by restoring the MSbit.
457 ror C6
458 ;; .expo += 1 and override the .expo stored during normalize.
459 adiw expL, 1
460 std Z+0+Expo, expL
461 std Z+1+Expo, expH
462
463 .Lstore:
464 F7call store_mant.with_flags
465
466 ;; Return the byte below .mant after normalization.
467 ;; This is only useful without rounding; the caller will know.
468 mov R24, TMP
469 ret
470 ENDF normalize_with_carry
471 #endif /* F7MOD_normalize_with_carry_ */
472
473
474 #ifdef F7MOD_normalize_
475 ;; Using above functionality from C.
476 ;; f7_t* normalize (f7_t *cc)
477 ;; Adjusts cc->expo
478 ;; Clears cc->flags
479 DEFUN normalize
480 push r17
481 push r16
482 wmov ZL, r24
483 F7call load_mant.clr_CA
484 clr Carry
485 st Z, ZERO
486 F7call normalize.store_with_flags
487 wmov r24, Z
488 pop r16
489 pop r17
490 ret
491 ENDF normalize
492 #endif /* F7MOD_normalize_ */
493
494
495 #ifdef F7MOD_store_expo_
496 #define Done r24
497 #define expLO r24
498 #define expHI r25
499 ;; expo == INT16_MAX => *Z = Inf, return Done = true.
500 ;; expo == INT16_MIN => *Z = 0x0, return Done = true.
501 ;; else => Z->expo = expo, return Done = false.
502 DEFUN store_expo
503 cpi expHI, 0x80
504 cpc expLO, ZERO
505 breq .Ltiny
506 adiw expLO, 1
507 brvs .Lhuge
508 sbiw expLO, 1
509 std Z+0+Expo, expLO
510 std Z+1+Expo, expHI
511 ldi Done, 0
512 ret
513
514 .Lhuge:
515 #if F7_HAVE_Inf == 1
516 ld Done, Z
517 andi Done, F7_FLAG_sign
518 ori Done, F7_FLAG_inf
519 #else
520 ldi Done, F7_FLAG_nan
521 #endif /* Have Inf */
522 st Z, Done
523 ldi Done, 1
524 ret
525
526 .Ltiny:
527 ldi Done, 1
528 F7jmp clr
529 ENDF store_expo
530 #endif /* F7MOD_store_expo_ */
531
532
533 #ifdef F7MOD_set_u64_
534 DEFUN set_s64
535 set
536 skipnext
537 ;; ...
538 LABEL set_u64
539 clt ; Skipped?
540 wmov Zl, r16
541 ;; TMP holds .flags.
542 clr TMP
543 brtc .Lnot.negative
544
545 bst C6, 7
546 brtc .Lnot.negative
547 bld TMP, F7_FLAGNO_sign
548 .global __negdi2
549 XCALL __negdi2
550
551 .Lnot.negative:
552 st Z, TMP
553 std Z+0+Expo, ZERO
554 std Z+1+Expo, ZERO
555 ldi Carry, 63
556 F7call normalize.round.store_with_flags
557 wmov r24, Z
558 wmov r16, Z ; Unclobber r16.
559 ret
560 ENDF set_s64
561 #endif /* F7MOD_set_u64_ */
562
563
564 #ifdef F7MOD_to_integer_
565 #define Mask r26
566 DEFUN to_integer
567 wmov ZL, r24
568 mov Mask, r22
569
570 F7call load_mant.with_flags
571
572 sbrc Flags, F7_FLAGNO_nan
573 rjmp .Lset_0x8000
574
575 sbrc Flags, F7_FLAGNO_inf
576 rjmp .Lsaturate
577
578 sbrs C6, 7
579 rjmp .Lset_0x0000
580
581 bst Flags, F7_FLAGNO_sign
582 ldd r27, Z+0+Expo
583 ;; Does .expo have bits outside Mask? ...
584 mov TMP, Mask
585 com TMP
586 and TMP, r27
587 ldd r27, Z+1+Expo
588 tst r27
589 brmi .Lset_0x0000 ; ...yes: .expo is < 0 => return 0
590 or TMP, r27
591 brne .Lsaturate.T ; ...yes: .expo > Mask => saturate
592
593 ;; ...no: Shift right to meet .expo = 0.
594 PUSH r16
595 ldd r16, Z+0+Expo
596 eor r16, Mask
597 and r16, Mask
598 clr CA
599 F7call lshrdi3
600 POP r16
601 tst C6
602 brmi .Lsaturate.T ; > INTxx_MAX => saturate
603
604 brtc 9f ; >= 0 => return
605 sbrc Mask, 5
606 .global __negdi2
607 XJMP __negdi2
608 sbrc Mask, 4
609 .global __negsi2
610 XJMP __negsi2
611 neg C6
612 neg C5
613 sbci C6, 0
614 9: ret
615
616 .Lsaturate:
617 bst Flags, F7_FLAGNO_sign
618 .Lsaturate.T:
619
620 #if F7_HAVE_Inf
621 brts .Lset_0x8000
622
623 .Lset_0x7fff:
624 ;; +Inf => return INTxx_MAX
625 sec
626 .global __sbc_8
627 XCALL __sbc_8
628 ldi C6, 0x7f
629 ret
630 #endif /* F7_HAVE_Inf */
631
632 .Lset_0x8000:
633 ;; NaN or -Inf => return INTxx_MIN
634 .global __clr_8
635 XCALL __clr_8
636 ldi C6, 0x80
637 ret
638
639 .Lset_0x0000:
640 ;; Small value => return 0x0
641 .global __clr_8
642 XJMP __clr_8
643
644 ENDF to_integer
645 #endif /* F7MOD_to_integer_ */
646
647
648 #ifdef F7MOD_to_unsigned_
649 #define Mask r26
650 DEFUN to_unsigned
651 wmov ZL, r24
652 mov Mask, r22
653
654 F7call load_mant.with_flags
655
656 sbrc Flags, F7_FLAGNO_nan
657 rjmp .Lset_0xffff
658
659 sbrc Flags, F7_FLAGNO_sign
660 rjmp .Lset_0x0000
661
662 sbrc Flags, F7_FLAGNO_inf
663 rjmp .Lset_0xffff
664
665 sbrs C6, 7
666 rjmp .Lset_0x0000
667
668 ldd r27, Z+0+Expo
669 ;; Does .expo have bits outside Mask? ...
670 mov TMP, Mask
671 com TMP
672 and TMP, r27
673 ldd r27, Z+1+Expo
674 tst r27
675 brmi .Lset_0x0000 ; ...yes: .expo is < 0 => return 0
676 or TMP, r27
677 brne .Lset_0xffff ; ...yes: .expo > Mask => saturate
678
679 ;; ...no: Shift right to meet .expo = 0.
680 PUSH r16
681 ldd r16, Z+0+Expo
682 eor r16, Mask
683 and r16, Mask
684 clr CA
685 F7call lshrdi3
686 POP r16
687 ret
688
689 .Lset_0xffff:
690 ;; return UINTxx_MAX
691 sec
692 .global __sbc_8
693 XJMP __sbc_8
694
695 .Lset_0x0000:
696 ;; Small value => return 0x0
697 .global __clr_8
698 XJMP __clr_8
699
700 ENDF to_unsigned
701 #endif /* F7MOD_to_unsigned_ */
702
703
704 #ifdef F7MOD_addsub_mant_scaled_
705 ;; int8_t f7_addsub_mant_scaled_asm (f7_t *r24, const f7_t *r22, const f7_t 20*,
706 ;; uint8_t r18);
707 ;; R18.0 = 1 : ADD
708 ;; R18.0 = 0 : SUB
709 ;; R18[7..1] : Scale
710 ;; Compute *R24 = *R22 + *R20 >> R18[7..1].
711
712 #define BA 10
713 #define B0 BA+1
714 #define B1 B0+1
715 #define B2 B0+2
716 #define B3 B0+3
717 #define B4 B0+4
718 #define B5 B0+5
719 #define B6 B0+6
720
721 DEFUN addsub_mant_scaled
722 do_prologue_saves 10
723
724 bst r18, 0 ;; ADD ?
725 lsr r18
726 mov r16, r18
727
728 wmov ZL, r20
729 wmov YL, r22
730 ;; C[] = bb >> shift
731 wmov XL, r24
732
733 F7call load_mant.clr_CA
734 F7call lshrdi3
735
736 wmov BA, CA
737 wmov B1, C1
738 wmov B3, C3
739 wmov B5, C5
740 wmov ZL, YL
741 F7call load_mant.clr_CA
742
743 wmov ZL, XL
744
745 brts .Ladd
746
747 .global __subdi3
748 XCALL __subdi3
749
750 breq .Lzero
751 brcc .Lround
752 ;; C = 1: Can underflow happen at all ?
753 .Lzero:
754 F7call clr
755 rjmp .Lepilogue
756
757 .Ladd:
758 .global __adddi3
759 XCALL __adddi3
760 brcc .Lround
761 ldi Carry, 1
762 .global __lshrdi3
763 XCALL __lshrdi3
764 ori C6, 1 << 7
765 skipnext
766 .Lround:
767 clr Carry ; skipped?
768 F7call normalize.round.store_with_flags
769
770 .Lepilogue:
771 do_epilogue_restores 10
772
773 ENDF addsub_mant_scaled
774
775 #if !defined (__AVR_HAVE_MOVW__) || !defined (__AVR_HAVE_JMP_CALL__)
776 DEFUN lshrdi3
777 .global __lshrdi3
778 XJMP __lshrdi3
779 ENDF lshrdi3
780 DEFUN ashldi3
781 .global __ashldi3
782 XJMP __ashldi3
783 ENDF ashldi3
784 #else
785
786 # Basically just a wrapper around libgcc's __lshrdi3.
787 DEFUN lshrdi3
788 ;; Handle bit 5 of shift offset.
789 sbrs r16, 5
790 rjmp 4f
791 wmov CA, C3
792 wmov C1, C5
793 clr C6 $ clr C5 $ wmov C3, C5
794 4:
795 ;; Handle bit 4 of shift offset.
796 sbrs r16, 4
797 rjmp 3f
798 wmov CA, C1
799 wmov C1, C3
800 wmov C3, C5
801 clr C6 $ clr C5
802 3:
803 ;; Handle bits 3...0 of shift offset.
804 push r16
805 andi r16, 0xf
806 breq 0f
807
808 .global __lshrdi3
809 XCALL __lshrdi3
810 0:
811 pop r16
812 ret
813 ENDF lshrdi3
814
815 # Basically just a wrapper around libgcc's __ashldi3.
816 DEFUN ashldi3
817 ;; Handle bit 5 of shift offset.
818 sbrs r16, 5
819 rjmp 4f
820 wmov C5, C1
821 wmov C3, CA
822 clr C2 $ clr C1 $ wmov CA, C1
823 4:
824 ;; Handle bit 4 of shift offset.
825 sbrs r16, 4
826 rjmp 3f
827 wmov C5, C3
828 wmov C3, C1
829 wmov C1, CA
830 clr CA $ clr C0
831 3:
832 ;; Handle bits 3...0 of shift offset.
833 push r16
834 andi r16, 0xf
835 breq 0f
836
837 .global __ashldi3
838 XCALL __ashldi3
839 0:
840 pop r16
841 ret
842 ENDF ashldi3
843 #endif /* Small device */
844
845 #endif /* F7MOD_addsub_mant_scaled_ */
846
847 #if defined F7MOD_mul_mant_ && defined (__AVR_HAVE_MUL__)
848 #define A0 11
849 #define A1 A0+1
850 #define A2 A0+2
851 #define A3 A0+3
852 #define A4 A0+4
853 #define A5 A0+5
854 #define A6 A0+6
855
856 #define TT0 26
857 #define TT1 TT0+1
858 #define TT2 28
859 #define TT3 TT2+1
860
861 #define BB 10
862
863 ;; R18.0 = 1: No rounding.
864
865 DEFUN mul_mant
866 ;; 10 = Y, R17...R10
867 do_prologue_saves 10
868 ;; T = R18.0: Skip rounding?
869 bst r18, 0
870 ;; Save result address for later.
871 push r25
872 push r24
873 ;; Load A's mantissa.
874 movw ZL, r22
875 LDD A0, Z+0+Off
876 LDD A1, Z+1+Off
877 LDD A2, Z+2+Off
878 LDD A3, Z+3+Off
879 LDD A4, Z+4+Off
880 LDD A5, Z+5+Off
881 LDD A6, Z+6+Off
882 movw ZL, r20
883
884 ;; 6 * 6 -> 6:5
885 ;; 4 * 6 -> 4:3
886 ;; 2 * 6 -> 2:1
887 ;; 0 * 6 -> 0:a
888 ldd BB, Z+6+Off
889 mul A6, BB $ movw C5, r0
890 mul A4, BB $ movw C3, r0
891 mul A2, BB $ movw C1, r0
892 mul A0, BB $ movw CA, r0
893
894 ;; 5 * 6 -> 5:4
895 ;; 3 * 6 -> 3:2
896 ;; 1 * 6 -> 1:0
897 mul A5, BB $ movw TT2, r0
898 mul A3, BB $ movw TT0, r0
899 mul A1, BB
900 ADD C0, r0 $ adc C1, r1
901 adc C2, TT0 $ adc C3, TT1
902 adc C4, TT2 $ adc C5, TT3 $ clr ZERO
903 adc C6, ZERO
904 ;; Done B6
905
906 ;; 6 * 5 -> 5:4
907 ;; 4 * 5 -> 3:2
908 ;; 2 * 5 -> 1:0
909 ;; 0 * 5 -> a:-
910 ldd BB, Z+5+Off
911 mul A0, BB
912 ;; Done A0
913 #define Atmp A0
914 #define Null A0
915
916 mov Atmp, r1
917 mul A6, BB $ movw TT2, r0
918 mul A4, BB $ movw TT0, r0
919 mul A2, BB
920
921 ADD CA, Atmp
922 adc C0, r0 $ adc C1, r1
923 adc C2, TT0 $ adc C3, TT1
924 adc C4, TT2 $ adc C5, TT3 $ clr Null
925 adc C6, Null
926
927 ;; 1 * 5 -> 0:a
928 ;; 3 * 5 -> 2:1
929 ;; 5 * 5 -> 4:3
930 mul A1, BB $ movw TT0, r0
931 mul A3, BB $ movw TT2, r0
932 mul A5, BB
933
934 ADD CA, TT0 $ adc C0, TT1
935 adc C1, TT2 $ adc C2, TT3
936 adc C3, r0 $ adc C4, r1
937 adc C5, Null $ adc C6, Null
938 ;; Done B5
939
940 ;; 2 * 4 -> 0:a
941 ;; 4 * 4 -> 2:1
942 ;; 6 * 4 -> 4:3
943 ldd BB, Z+4+Off
944 mul A2, BB $ movw TT0, r0
945 mul A4, BB $ movw TT2, r0
946 mul A6, BB
947
948 ADD CA, TT0 $ adc C0, TT1
949 adc C1, TT2 $ adc C2, TT3
950 adc C3, r0 $ adc C4, r1
951 adc C5, Null $ adc C6, Null
952
953 ;; 1 * 4 -> a:-
954 ;; 3 * 4 -> 1:0
955 ;; 5 * 4 -> 3:2
956 mul A1, BB $ mov TT1, r1
957 mul A3, BB $ movw TT2, r0
958 mul A5, BB
959 ;; Done A1
960 ;; Done B4
961 ADD CA, TT1
962 adc C0, TT2 $ adc C1, TT3
963 adc C2, r0 $ adc C3, r1
964 ;; Accumulate carry for C3 in TT1.
965 ;; Accumulate carry for C4 in A1.
966 #define Cry3 TT1
967 #define Cry4 A1
968 clr Cry3
969 clr Cry4
970 rol Cry4
971
972 ;; 6 * 2 -> 2:1
973 ;; 6 * 3 -> 3:2
974 ;; 5 * 3 -> 2:1
975 ldd BB, Z+2+Off
976 mul A6, BB
977 add C1, r0
978 adc C2, r1
979 adc Cry3, Null
980
981 ldd BB, Z+3+Off
982 mul A6, BB
983 add C2, r0
984 adc C3, r1
985 adc Cry4, Null
986
987 mul A5, BB
988 add C1, r0
989 adc C2, r1
990 adc Cry3, Null
991
992 ;; Perform the remaining 11 multiplications in 4 loopings:
993 ;; 4 * 3 -> 1:0
994 ;; 3 * 3 -> 0:a
995 ;; 2 * 3 -> a:-
996 ;;
997 ;; 5 * 2 -> 1:0
998 ;; 4 * 2 -> 0:a
999 ;; 3 * 2 -> a:-
1000 ;;
1001 ;; 6 * 1 -> 1:0
1002 ;; 5 * 1 -> 0:a
1003 ;; 4 * 1 -> a:-
1004 ;;
1005 ;; . * 0 -> 1:0 (=0)
1006 ;; 6 * 0 -> 0:a
1007 ;; 5 * 0 -> a:-
1008
1009 ;; BB already contains B3, hence let Z point one past B2 so that
1010 ;; the LD *, -Z below will pick up B2, B1, B0.
1011 adiw r30, 1 + Off+2
1012
1013 ;; Accumulate carry for C2 in TT2.
1014 #define Cry2 TT2
1015 clr Cry2
1016
1017 ;; TT3 is the loop counter, iterate over B3...B0.
1018 ldi TT3, 4
1019 rjmp .Loop_start
1020
1021 .Loop:
1022 ;; We use A2...A4 below; so shift bytes of A into place.
1023 mov A2, A3
1024 mov A3, A4
1025 mov A4, A5
1026 mov A5, A6
1027 clr A6
1028 ld BB, -Z
1029 .Loop_start:
1030 mul A3, BB
1031 ADD CA, r0 $ adc C0, r1 $ adc C1, Null $ adc Cry2, Null
1032 MUL A2, BB
1033 mov TT0, r1
1034 MUL A4, BB
1035 ADD CA, TT0 $ adc C0, r0 $ adc C1, r1 $ adc Cry2, Null
1036 dec TT3
1037 brne .Loop
1038
1039 clr ZERO
1040 ADD C2, Cry2
1041 adc C3, Cry3
1042 adc C4, Cry4
1043 adc C5, ZERO
1044 adc C6, ZERO
1045
1046 ;; Finally...
1047
1048 pop ZL
1049 pop ZH
1050 ;; The high byte is at least 0x40 and at most 0xfe.
1051 ;; The result has to be left-shifted by one in order to scale it
1052 ;; correctly.
1053
1054 ldi Carry, 1
1055 F7call normalize.maybe_round.store_with_flags
1056
1057 do_epilogue_restores 10
1058
1059 ENDF mul_mant
1060 #endif /* F7MOD_mul_mant_ && MUL */
1061
1062 #if defined F7MOD_mul_mant_ && ! defined (__AVR_HAVE_MUL__)
1063 #define AA TMP
1064 #define A0 13
1065 #define A1 A0+1
1066 #define A2 A0+2
1067 #define A3 A0+3
1068 #define A4 A0+4
1069 #define A5 r26
1070 #define A6 r27
1071 #define BB ZERO
1072 #define Bits r29
1073 #define Bytes r28
1074
1075 DEFUN mul_mant
1076 do_prologue_saves 7
1077 bst r18, 0 ; T = 1: Don't round.
1078 ;; Save result address for later.
1079 push r25
1080 push r24
1081 ;; Load 1st operand mantissa.
1082 wmov r30, r22
1083 clr AA
1084 LDD A0, Z+0+Off
1085 LDD A1, Z+1+Off
1086 LDD A2, Z+2+Off
1087 LDD A3, Z+3+Off
1088 LDD A4, Z+4+Off
1089 LDD A5, Z+5+Off
1090 LDD A6, Z+6+Off
1091 ;; Let Z point one past .mant of the 2nd input operand.
1092 wmov r30, r20
1093 adiw r30, Expo
1094
1095 ;; Clear the result mantissa.
1096 .global __clr_8
1097 XCALL __clr_8
1098
1099 ;; Loop over the bytes of B's mantissa from highest to lowest.
1100 ;; "+1" because we jump into the loop.
1101 ldi Bytes, 1 + F7_MANT_BYTES
1102
1103 ;; Divide one operand by 2 so that the result mantissa won't overflow.
1104 ;; This is accounted for by "Carry = 1" below.
1105 ldi Bits, 1
1106 rjmp .Loop_entry
1107
1108 .Loop_bytes:
1109 ld BB, -Z
1110 ;; Loop over the bits of B's mantissa from highest to lowest.
1111 ldi Bits, 8
1112 .Loop_bits:
1113 lsl BB
1114 brcc .Lnext_bit
1115
1116 ADD CA, AA
1117 adc C0, A0
1118 adc C1, A1
1119 adc C2, A2
1120 adc C3, A3
1121 adc C4, A4
1122 adc C5, A5
1123 adc C6, A6
1124
1125 .Lnext_bit:
1126 .Loop_entry:
1127 LSR A6
1128 ror A5
1129 ror A4
1130 ror A3
1131 ror A2
1132 ror A1
1133 ror A0
1134 ror AA
1135
1136 dec Bits
1137 brne .Loop_bits
1138
1139 dec Bytes
1140 brne .Loop_bytes
1141
1142 ;; Finally...
1143
1144 pop ZL
1145 pop ZH
1146
1147 ;; The result has to be left-shifted by one (multiplied by 2) in order
1148 ;; to undo the division by 2 of the 1st operand.
1149 ldi Carry, 1
1150 F7call normalize.maybe_round.store_with_flags
1151
1152 do_epilogue_restores 7
1153 ENDF mul_mant
1154 #endif /* F7MOD_mul_mant_ && ! MUL */
1155
1156
1157 #if defined (F7MOD_div_)
1158
1159 ;; Dividend is C[]
1160
1161 ;; Divisor
1162 #define A0 9
1163 #define A1 10
1164 #define A2 11
1165 #define A3 12
1166 #define A4 13
1167 #define A5 14
1168 #define A6 15
1169
1170 ;; Quotient
1171 #define Q0 0 /* === TMP */
1172 #define Q1 Q0+1 /* === ZERO */
1173 #define Q2 26
1174 #define Q3 Q2+1
1175 #define Q4 28
1176 #define Q5 Q4+1
1177 #define Q6 16
1178 #define Q7 Q6+1
1179
1180 #define Cnt CA
1181 #define QBits r8
1182
1183 DEFUN div
1184 do_prologue_saves 12
1185
1186 ;; Number of bits requested for the quotient.
1187 ;; This is usually 2 + F7_MANT_BITS.
1188 mov QBits, r20
1189 wmov ZL, r22
1190 LDD A0, Z+0+Off
1191 LDD A1, Z+1+Off
1192 LDD A2, Z+2+Off
1193 LDD A3, Z+3+Off
1194 LDD A4, Z+4+Off
1195 LDD A5, Z+5+Off
1196 LDD A6, Z+6+Off
1197 wmov ZL, r24
1198 F7call load_mant
1199
1200 ;; Clear quotient Q[].
1201 clr Q0 ; === TMP
1202 ;clr Q1 ; === ZERO
1203 wmov Q2, Q0
1204 wmov Q4, Q0
1205 wmov Q6, Q0
1206
1207 ;; C[] and A[] are valid mantissae, i.e. their MSBit is set. Therefore,
1208 ;; quotient Q[] will be in [0x0.ff..., 0x0.40...] and to adjust Q[] we
1209 ;; need at most 1 left-shift. Compute F7_MANT_BITS + 2 bits of the
1210 ;; quotient: One bit is used for rounding, and one bit might be consumed
1211 ;; by the mentioned left-shift.
1212 mov Cnt, QBits
1213 rjmp .Loop_start
1214
1215 .Loop:
1216 ;; Shift dividend.
1217 LSL C0
1218 rol C1
1219 rol C2
1220 rol C3
1221 rol C4
1222 rol C5
1223 rol C6
1224 brcs .Lfits
1225 ;; Compare dividend against divisor.
1226 .Loop_start:
1227 CP C0, A0
1228 cpc C1, A1
1229 cpc C2, A2
1230 cpc C3, A3
1231 cpc C4, A4
1232 cpc C5, A5
1233 cpc C6, A6
1234 ;; Shift 0 into quotient.
1235 brlo 1f
1236 .Lfits:
1237 ;; Divisor fits into dividend.
1238 SUB C0, A0
1239 sbc C1, A1
1240 sbc C2, A2
1241 sbc C3, A3
1242 sbc C4, A4
1243 sbc C5, A5
1244 sbc C6, A6
1245 ;; Shift 1 into quotient.
1246 sec
1247 rol Q0
1248 skipnext
1249 1: lsl Q0
1250 rol Q1
1251 rol Q2
1252 rol Q3
1253 rol Q4
1254 rol Q5
1255 rol Q6
1256 rol Q7
1257 dec Cnt
1258 brne .Loop
1259
1260 wmov CA, Q0
1261 wmov C1, Q2
1262 wmov C3, Q4
1263 wmov C5, Q6
1264 clr ZERO
1265
1266 ldi Carry, 64
1267 sub Carry, QBits
1268 F7call normalize.round.store_with_flags
1269
1270 do_epilogue_restores 12
1271 ENDF div
1272
1273 #endif /* F7MOD_div_ */
1274
1275
1276 #ifdef F7MOD_sqrt_approx_
1277 ;; ReMainder
1278 #define MX 16
1279 #define M0 17
1280 #define M1 26
1281 #define M2 27
1282 #define M3 14
1283 #define M4 15
1284 #define M5 TMP
1285 #define M6 r29
1286
1287 #define AA ZERO
1288 #define One r13
1289 #define Bits r28
1290 #define Bytes M6
1291
1292 ;; Extend C[] by 0b01 at the low end.
1293 #define CX (0b01 << 6)
1294
1295 ;;; Compute square-root of const f7_t *R22 for a positive number.
1296 DEFUN sqrt_approx
1297 ;; 7 = Y, R17...R13
1298 do_prologue_saves 7
1299
1300 wmov ZL, r22 ; Input const f7_t*
1301 wmov YL, r24 ; Output f7_t*
1302 F7call load_mant
1303 ldi CA, 0xff
1304
1305 ;; The paper-pencil method for the mantissa consumes bits in pairs and
1306 ;; expects the input as Q-format 2.*, but mant is in 1.*. This means
1307 ;; we have to shift one to the right. If expo is odd, then we shift
1308 ;; one to the left and subtract one from expo in order to compensate
1309 ;; and to get an even exponent.
1310
1311 ;; Divide expo by 2 because we are doing sqrt.
1312 ldd XH, Z+Expo+1
1313 ldd XL, Z+Expo+0
1314 asr XH
1315 ror XL
1316 ;; Store expo to result.
1317 wmov ZL, YL
1318 std Z+Expo+0, XL
1319 std Z+Expo+1, XH
1320
1321 brcs 1f
1322 ;; Expo was even. Do >>=1 in order to get Q2.* as explained above.
1323 LSR C6 $ ror C5 $ ror C4 $ ror C3
1324 ror C2 $ ror C1 $ ror C0 $ ror CA
1325 1:
1326 ;; For odd expo, >>=1 to get Q2.* and <<=1 to get an even expo cancel out.
1327 ;; And the right-shift of the exponent implicitly subtracted 1 from it
1328 ;; as needed.
1329 F7call store_mant.with_flags
1330
1331 ;; Let Z point one past the mantissa's MSB.
1332 adiw ZL, Off + F7_MANT_BYTES
1333
1334 ;; Clear the result mantissa.
1335 .global __clr_8
1336 XCALL __clr_8
1337 ;; Clear ReMainder. M6 === Bytes will be zero when Bytes is down to 0.
1338 clr M5
1339 wmov M3, C3
1340 wmov M1, C1
1341 wmov MX, CA
1342
1343 clr One
1344 inc One
1345
1346 ;; "+1" because .flags extends the mantissa at the low end.
1347 ldi Bytes, 1 + F7_MANT_BYTES
1348 .Loop_bytes:
1349 ld AA, -Z
1350 ldi Bits, 8
1351 .Loop_bits:
1352 ;; Shift top 2 bits of MX into M[].
1353 LSL MX $ rol M0 $ rol M1 $ rol M2 $ rol M3
1354 LSL MX $ rol M0 $ rol M1 $ rol M2 $ rol M3
1355
1356 ;; "Take down" 2 bits from A[] to MX.7 and MX.6
1357 mov MX, AA
1358 andi MX, 0xc0
1359 lsl AA
1360 lsl AA
1361
1362 ;; Compare remainder against current result extended by 0b01.
1363 CPI MX, CX
1364 cpc M0, C0
1365 cpc M1, C1
1366 cpc M2, C2
1367 cpc M3, C3
1368 brcs 1f
1369 ;; If the extended result fits, subtract it from M and set the
1370 ;; next result bit to 1.
1371 SUBI MX, CX
1372 sbc M0, C0
1373 sbc M1, C1
1374 sbc M2, C2
1375 sbc M3, C3
1376 1:
1377 ;; If it doesn't fit, set the next result bit to 0 (and don't subtract).
1378 rol C0
1379 eor C0, One
1380 rol C1
1381 rol C2
1382 rol C3
1383
1384 subi Bits, 2
1385 brne .Loop_bits
1386 ;; AA (=== ZERO) is zero again.
1387
1388 dec Bytes
1389 brne .Loop_bytes
1390 ;; B6 (=== Bytes) is zero now.
1391
1392 ;; Now we consumed all the 64 bits of the extended mantissa, but we
1393 ;; only expanded 64 / 2 = 32 bits of the result, which is currently
1394 ;; held in C3 ... C0. Do the same like above, but on all bytes.
1395 ;; Shift in 00's because the mantissa is exhausted.
1396
1397 ;; "-1" because flags is part of the mantissa, which is already consumed.
1398 ldi Bits, 8 * (F7_MANT_BYTES - 1)
1399 .Loop2_bits:
1400 ;; Shift top 2 bits of MX into M[].
1401 .Ltwice:
1402 LSL MX
1403 rol M0
1404 rol M1
1405 rol M2
1406 rol M3
1407 rol M4
1408 rol M5
1409 rol M6
1410 subi Bits, 0x80
1411 brmi .Ltwice
1412
1413 ;; "Take down" two 0's to MX.7 and MX.6
1414 ; clr MX ;; MX is already zero.
1415
1416 ;; Compare remainder against current result extended by 0b01.
1417 CPI MX, CX
1418 cpc M0, C0
1419 cpc M1, C1
1420 cpc M2, C2
1421 cpc M3, C3
1422 cpc M4, C4
1423 cpc M5, C5
1424 cpc M6, C6
1425 brcs 1f
1426 ;; If the extended result fits, subtract it from M and set the
1427 ;; next result bit to 1.
1428 SUBI MX, CX
1429 sbc M0, C0
1430 sbc M1, C1
1431 sbc M2, C2
1432 sbc M3, C3
1433 sbc M4, C4
1434 sbc M5, C5
1435 sbc M6, C6
1436 1:
1437 ;; If it doesn't fit, set the next result bit to 0 (and don't subtract).
1438 rol C0
1439 eor C0, One
1440 rol C1
1441 rol C2
1442 rol C3
1443 rol C4
1444 rol C5
1445 rol C6
1446
1447 subi Bits, 2
1448 brne .Loop2_bits
1449
1450 ;; Set flags.
1451 st Z, ZERO
1452 F7call store_mant
1453
1454 do_epilogue_restores 7
1455 ENDF sqrt_approx
1456 #endif /* F7MOD_sqrt_approx_ */
1457
1458
1459 #if defined (F7MOD_sqrt16_) && defined (__AVR_HAVE_MUL__)
1460
1461 #define Mask C6
1462 #define Q0 C3 /* = R22 */
1463 #define Q1 C4 /* = R23 */
1464
1465 ;; uint16_t R24 = sqrt16_XXX (uint16_t R24);
1466 ;; Clobbers: R22, R23, TMP.
1467 ;;
1468 ;; XXX = floor: Return integral part of square-root of R25:R24 with R25 = 0.
1469 ;; Error is in [0, -1 LSB).
1470 ;; XXX = round: Return quare-root of R25:R24 rounded to nearest integer.
1471 ;; R25 = (Q[] >= 65281) = (Q > 0xff00), i.e. if Q[] is not
1472 ;; bigger than 0xff00, then the result fits in 8 bits.
1473 ;; Return C = 0 if the result is the same as for XXX = floor,
1474 ;; error in [0, -1/2 LSB)
1475 ;; Return C = 1 if the result is one higher than for XXX = floor,
1476 ;; error in [1/2 LSB, 0).
1477 DEFUN sqrt16_round
1478 set
1479 skipnext
1480 ;; ...
1481 LABEL sqrt16_floor
1482 clt ; Skipped?
1483 movw Q0, r24
1484 clr C5
1485 ldi Mask, 1 << 7
1486
1487 .Loop_mask:
1488 add C5, Mask
1489 mul C5, C5
1490 cp Q0, R0
1491 cpc Q1, R1
1492 brsh 1f
1493 sub C5, Mask
1494 1: lsr Mask
1495 brne .Loop_mask
1496
1497 brtc .Ldone ; No rounding => C6 will be 0.
1498
1499 ;; Rounding: (X + 1/2)^2 = X^2 + X + 1/4, thus probing
1500 ;; for bit -1 is testing Q[] against C5^2 + C5.
1501 mul C5, C5
1502 add R0, C5
1503 adc R1, C6 ; Exploit C6 === Mask = 0.
1504 cp R0, Q0
1505 cpc R1, Q1
1506 brcc .Ldone
1507 ;; If C5^2 + C5 + 1/4 fits into Q[], then round up and C = 1.
1508 adiw C5, 1 ; Exploit C6 === Mask = 0.
1509 sec
1510
1511 .Ldone:
1512 clr __zero_reg__
1513 ret
1514 ENDF sqrt16_round
1515 #undef Mask
1516 #undef Q0
1517 #undef Q1
1518 #endif /* F7MOD_sqrt16_ && MUL */
1519
1520
1521 #undef CA
1522 #undef C0
1523 #undef C1
1524 #undef C2
1525 #undef C3
1526 #undef C4
1527 #undef C5
1528 #undef C6
1529 #undef Carry
1530
1531
1532 #ifdef F7MOD_D_fma_
1533 _DEFUN __fma
1534 DALIAS fma
1535 LALIAS fmal
1536
1537 #define n_pushed 4
1538 #define n_frame (2 * F7_SIZEOF)
1539
1540 do_prologue_saves n_pushed, n_frame
1541 ;; Y = FramePointer + 1
1542 adiw Y, 1
1543
1544 ;; FP + 1 = (f7_t) arg1
1545 wmov r16, Y
1546 ;; The double argument arg1 is already in R18[].
1547 XCALL F7_NAME (set_double_impl)
1548
1549 ;; The double argument arg2 is in R10[]. Move it to R18[].
1550 wmov r18, r10
1551 wmov r20, r12
1552 wmov r22, r14
1553 ;; R16, R17 are clobbered. Fetch them from where prologue_saves put them.
1554 ldd r24, Y + n_frame + 3 ; Saved R16
1555 ldd r25, Y + n_frame + 2 ; Saved R17
1556 ;; FP + 1 + 10 = (f7_t) arg2
1557 subi r16, lo8 (-F7_SIZEOF)
1558 sbci r17, hi8 (-F7_SIZEOF)
1559 XCALL F7_NAME (set_double_impl)
1560
1561 wmov r24, Y ; &arg1
1562 wmov r22, r16 ; &arg2
1563 XCALL F7_NAME (Imul) ; arg1 *= arg2
1564
1565 ;; The 3rd double argument arg3 was passed on the stack. Move it to R18[],
1566 ;; Don't use f7_set_pdouble() because that function is unused (for now).
1567 .irp n, 0, 1, 2, 3, 4, 5, 6, 7
1568 ldd 18+\n, Y + n_frame + n_pushed + PC_SIZE + \n
1569 .endr
1570 XCALL F7_NAME (set_double_impl)
1571
1572 wmov r24, Y ; &arg1
1573 wmov r22, r16 ; &arg2
1574 XCALL F7_NAME (Iadd) ; arg1 += arg2
1575
1576 wmov r24, Y ; &arg1
1577 XCALL F7_NAME (get_double)
1578
1579 do_epilogue_restores n_pushed, n_frame
1580 _ENDF __fma
1581 #endif /* F7MOD_D_fma_ */
1582
1583
1584 #ifdef F7MOD_D_fabs_
1585 _DEFUN __fabs
1586 DALIAS fabs
1587 LALIAS fabsl
1588 andi R25, 0b01111111
1589 ret
1590 _ENDF __fabs
1591 #endif /* F7MOD_D_fabs_ */
1592
1593
1594 #ifdef F7MOD_D_neg_
1595 _DEFUN __neg
1596 _LABEL __negdf2
1597 subi R25, 0b10000000
1598 ret
1599 _ENDF __neg
1600 #endif /* F7MOD_D_neg_ */
1601
1602
1603 #ifdef F7MOD_D_signbit_
1604 _DEFUN __signbit
1605 DALIAS signbit
1606 LALIAS signbitl
1607 bst R25, 7
1608 clr R25
1609 clr R24
1610 bld R24, 0
1611 ret
1612 _ENDF __signbit
1613 #endif /* F7MOD_D_signbit_ */
1614
1615
1616 #ifdef F7MOD_D_copysign_
1617 _DEFUN __copysign
1618 DALIAS copysign
1619 LALIAS copysignl
1620 bst R17, 7
1621 bld R25, 7
1622 ret
1623 _ENDF __copysign
1624 #endif /* F7MOD_D_copysign_ */
1625
1626
1627 #ifdef F7MOD_D_isinf_
1628 ;;; +Inf -> +1
1629 ;;; -Inf -> -1
1630 _DEFUN __isinf
1631 DALIAS isinf
1632 LALIAS isinfl
1633 ;; Save sign for later
1634 push R25
1635 F7call class_D
1636 pop TMP
1637 ldi R24, 0
1638 ldi R25, 0
1639 ;; Inf: T = Z = 1.
1640 brtc 0f ; ordinary number
1641 brne 0f ; Nan
1642 ldi R24, 1
1643 sbrc TMP, 7
1644 sbiw R24, 2
1645 0: ret
1646 _ENDF __isinf
1647 #endif /* F7MOD_D_isinf_ */
1648
1649
1650 #ifdef F7MOD_D_isnan_
1651 _DEFUN __isnan
1652 DALIAS isnan
1653 LALIAS isnanl
1654 F7call class_D
1655 ;; NaN: T = 1, Z = 0.
1656 brtc 0f
1657 ldi R24, 1
1658 brne 1f
1659 0:
1660 clr R24
1661 1:
1662 clr R25
1663 ret
1664 _ENDF __isnan
1665 #endif /* F7MOD_D_isnan_ */
1666
1667
1668 #ifdef F7MOD_D_isfinite_
1669 _DEFUN __isfinite
1670 DALIAS isfinite
1671 LALIAS isfinitel
1672 F7call class_D
1673 ;; Number <=> T = 0.
1674 bld R24, 0
1675 com R24
1676 andi R24, 1
1677 clr R25
1678 ret
1679 _ENDF __isfinite
1680 #endif /* F7MOD_D_isfinite_ */
1681
1682
1683 #ifdef F7MOD_D_class_
1684 ;; The encoded exponent has 11 Bits.
1685 #define MAX_BIASED_EXPO 0b0111111111110000
1686
1687 ;; Classify a double in R18[]
1688 ;; Number: T-Flag = 0.
1689 ;; +-Inf : T-Flag = 1, Z-Flag = 1.
1690 ;; NaN : T-Flag = 1, Z-Flag = 0.
1691 DEFUN class_D
1692 wmov R26, R24
1693 andi R26, lo8 (MAX_BIASED_EXPO)
1694 andi R27, hi8 (MAX_BIASED_EXPO)
1695 subi R26, lo8 (MAX_BIASED_EXPO)
1696 sbci R27, hi8 (MAX_BIASED_EXPO)
1697 clt
1698 brne .L.number
1699 set
1700 ;; Set sign and expo to 0.
1701 clr R25
1702 andi R24, lo8 (~MAX_BIASED_EXPO)
1703 ;; What remains is the mantissa.
1704 ;; Mantissa == 0 => +/-Inf.
1705 ;; Mantissa != 0 => NaN.
1706 ;; Compare R18[] against sign_extend(R26) with R26 = 0.
1707 .global __cmpdi2_s8
1708 XJMP __cmpdi2_s8
1709 .L.number:
1710 ret
1711
1712 ENDF class_D
1713 #endif /* F7MOD_D_class_ */
1714
1715
1716 #ifdef F7MOD_D_cmp_
1717
1718 #define A0 18
1719 #define A1 A0 + 1
1720 #define A2 A0 + 2
1721 #define A3 A0 + 3
1722 #define A4 A0 + 4
1723 #define A5 A0 + 5
1724 #define A6 A0 + 6
1725 #define A7 A0 + 7
1726
1727 #define B0 10
1728 #define B1 B0 + 1
1729 #define B2 B0 + 2
1730 #define B3 B0 + 3
1731 #define B4 B0 + 4
1732 #define B5 B0 + 5
1733 #define B6 B0 + 6
1734 #define B7 B0 + 7
1735
1736 #define AA5 XH
1737 #define AA6 ZL
1738 #define AA7 ZH
1739
1740 #define BB0 A0
1741 #define BB1 A1
1742 #define BB2 A2
1743 #define BB3 A3
1744 #define BB4 A4
1745 #define BB5 A5
1746 #define BB6 A6
1747 #define BB7 A7
1748
1749 ;;; Helper for __<cmp>df2 and __unorddf2.
1750 ;;; T = 1: Comparison is unordered.
1751 ;;; T = 0: Comparison is ordered, and Z, N, C, S flags are set according
1752 ;;; to compare (double A, double B) as if set by a signed int comparison.
1753 ;;; Note that f(+0) = f(-0) = 0.
1754 ;;; In any case:
1755 ;;; - return R24 = 1.
1756 ;;; - return R25.0 = isNaN (A)
1757 ;;; - return R25.1 = isNaN (B)
1758 DEFUN D_cmp
1759 rcall D_cmp.map_i64
1760 bld __tmp_reg__, 0
1761 push __tmp_reg__
1762 ;; Save A somewhere else...
1763 wmov AA6, A6
1764 mov AA5, A5
1765 push A4
1766 push A3
1767 push A2
1768 push A1
1769 mov r0, A0
1770 ;; ... so that we can use D_cmp.map_i64 on B.
1771 wmov BB6, B6
1772 wmov BB4, B4
1773 wmov BB2, B2
1774 wmov BB0, B0
1775 rcall D_cmp.map_i64
1776 ;; Run the following code even when B is NaN (T=1) so as to pop the regs.
1777 ;; In the non-NaN case, AA and BB can be compared like int64_t for the
1778 ;; sake of comparing A and B as double.
1779 CP r0, BB0 $ pop r0
1780 cpc r0, BB1 $ pop r0
1781 cpc r0, BB2 $ pop r0
1782 cpc r0, BB3 $ pop r0
1783 cpc r0, BB4
1784 cpc AA5, BB5
1785 cpc AA6, BB6
1786 cpc AA7, BB7
1787 pop r25
1788 ;; R25.0 <=> A is NaN
1789 ;; R25.1 <=> B is NaN
1790 ;; T <=> comparison is unordered
1791 bld r25, 1
1792 sbrc r25, 0
1793 set
1794 ldi r24, 1
1795 ret
1796
1797 ;;; A is NaN: Set T=1.
1798 ;;; A is not a NaN: Set T=0, and map double A to int64_t such that
1799 ;;; f(A) <cmp> f(B) iff A <cmp> B, i.e. we can treat the result
1800 ;;; as int64_t for the matter of double comparison.
1801 ;;; Clobbers: XL.
1802 D_cmp.map_i64:
1803 bst A7, 7
1804 cbr A7, 0x80
1805 ;; If Inf < |A|, then we have a NaN.
1806 CP __zero_reg__, A0
1807 cpc __zero_reg__, A1
1808 cpc __zero_reg__, A2
1809 cpc __zero_reg__, A3
1810 cpc __zero_reg__, A4
1811 cpc __zero_reg__, A5
1812 ldi XL, lo8(0x7ff0) $ cpc XL, A6
1813 ldi XL, hi8(0x7ff0) $ cpc XL, A7
1814 brlo .Lunord
1815 brtc 9f
1816 clt
1817 .global __negdi2
1818 XJMP __negdi2
1819 .Lunord:
1820 set
1821 9: ret
1822
1823 ENDF D_cmp
1824 #endif /* F7MOD_D_cmp_ */
1825
1826
1827 ;; bool __ledf2 (double, double);
1828 #ifdef F7MOD_D_le_
1829 _DEFUN __ledf2
1830 F7call D_cmp
1831 brts 0f
1832 breq 1f
1833 brlt 1f
1834 0: ldi r24, 0
1835 1: ret
1836 _ENDF __ledf2
1837 #endif /* F7MOD_D_le_ */
1838
1839 ;; bool __ltdf2 (double, double);
1840 #ifdef F7MOD_D_lt_
1841 _DEFUN __ltdf2
1842 F7call D_cmp
1843 brts 0f
1844 brlt 1f
1845 0: ldi r24, 0
1846 1: ret
1847 _ENDF __ltdf2
1848 #endif /* F7MOD_D_lt_ */
1849
1850 ;; bool __gedf2 (double, double);
1851 #ifdef F7MOD_D_ge_
1852 _DEFUN __gedf2
1853 F7call D_cmp
1854 brts 0f
1855 brge 1f
1856 0: ldi r24, 0
1857 1: ret
1858 _ENDF __gedf2
1859 #endif /* F7MOD_D_ge_ */
1860
1861 ;; bool __gtdf2 (double, double);
1862 #ifdef F7MOD_D_gt_
1863 _DEFUN __gtdf2
1864 F7call D_cmp
1865 brts 0f
1866 breq 0f
1867 brge 1f
1868 0: ldi r24, 0
1869 1: ret
1870 _ENDF __gtdf2
1871 #endif /* F7MOD_D_gt_ */
1872
1873 ;; bool __nedf2 (double, double);
1874 #ifdef F7MOD_D_ne_
1875 _DEFUN __nedf2
1876 F7call D_cmp
1877 brts 0f
1878 brne 1f
1879 0: ldi r24, 0
1880 1: ret
1881 _ENDF __nedf2
1882 #endif /* F7MOD_D_ne_ */
1883
1884 ;; bool __eqdf2 (double, double);
1885 #ifdef F7MOD_D_eq_
1886 _DEFUN __eqdf2
1887 F7call D_cmp
1888 brts 0f
1889 breq 1f
1890 0: ldi r24, 0
1891 1: ret
1892 _ENDF __eqdf2
1893 #endif /* F7MOD_D_eq_ */
1894
1895 ;; bool __unorddf2 (double, double);
1896 #ifdef F7MOD_D_unord_
1897 _DEFUN __unorddf2
1898 F7call D_cmp
1899 bld r24, 0
1900 ret
1901 _ENDF __unorddf2
1902 #endif /* F7MOD_D_unord_ */
1903
1904 #ifdef F7MOD_D_fminfmax_
1905 _DEFUN __fmin
1906 DALIAS fmin
1907 LALIAS fminl
1908 inc __zero_reg__
1909
1910 _LABEL __fmax
1911 DALIAS fmax
1912 LALIAS fmaxl
1913 ;; Push A[].
1914 push r25
1915 push r24
1916 push r23
1917 push r22
1918 push r21
1919 push r20
1920 push r19
1921 push r18
1922 ;; fmin or fmax
1923 push __zero_reg__
1924 clr __zero_reg__
1925
1926 XCALL __gedf2
1927
1928 pop __tmp_reg__
1929 andi r25, 0x3 ; NaNs?
1930 brne .Lnan
1931 ;; No NaNs involved.
1932 eor __tmp_reg__, r24 ; (f == fmin) ^ (A >= B)
1933 brne 1f
1934 2:
1935 ;; Return B since the cases are:
1936 ;; fmax && A < B
1937 ;; fmin && A >= B
1938 #ifdef __AVR_XMEGA__
1939 in XL, __SP_L__
1940 in XH, __SP_H__
1941 adiw XL, 8
1942 out __SP_L__, XL
1943 out __SP_H__, XH
1944 #else
1945 pop r0 $ pop r0 $ pop r0 $ pop r0
1946 pop r0 $ pop r0 $ pop r0 $ pop r0
1947 #endif
1948 wmov r24, r16
1949 wmov r22, r14
1950 wmov r20, r12
1951 wmov r18, r10
1952 ret
1953 1:
1954 ;; Return A since the cases are:
1955 ;; fmax && A >= B
1956 ;; fmin && A < B
1957 pop r18
1958 pop r19
1959 pop r20
1960 pop r21
1961 pop r22
1962 pop r23
1963 pop r24
1964 pop r25
1965 ret
1966
1967 .Lnan:
1968 ;; There are NaNs.
1969 ;; When only the 1st argument is a NaN, then return the 2nd argument
1970 cpi r25, 0x1
1971 breq 2b
1972 ;; When the 2nd argument is a NaN, then return the 1st argument.
1973 ;; When both arguments are NaNs, then return NaN (e.g. the 1st argument).
1974 rjmp 1b
1975 _ENDF __fmax
1976 #endif /* F7MOD_D_fminfmax_ */
1977
1978
1979 #ifdef F7MOD_D_sincos_
1980 ;;; void sincos (double R18, double *R16, double *R14);
1981 _DEFUN __sincos
1982 DALIAS sincos
1983 LALIAS sincosl
1984
1985 #define n_pushed 4
1986 #define n_frame (2 * F7_SIZEOF)
1987 do_prologue_saves n_pushed, n_frame
1988 ;; Y = FramePointer + 1
1989 adiw Y, 1
1990 ;; R16 = frame-arg 1
1991 wmov r16, Y
1992 ;; The double argument is in R18[].
1993 XCALL F7_NAME (set_double_impl)
1994 ;; void f7_sincos (f7_t *ss, f7_t *cc, const f7_t *aa)
1995 ;; Note that aa may equal ss or cc.
1996 wmov r20, r16 ; aa
1997 wmov r24, r16 ; ss = FP + 1
1998 subi r16, lo8(-F7_SIZEOF)
1999 sbci r17, hi8(-F7_SIZEOF)
2000 wmov r22, r16 ; cc = FP + 1 + F7_SIZEOF
2001 XCALL F7_NAME (sincos)
2002
2003 ;; double R18 = get_double (cc)
2004 wmov r24, r16
2005 XCALL F7_NAME (get_double)
2006 wmov XL, r14 ; double *pcos
2007 rcall store.r18.X ; *pcos = R18
2008
2009 ;; double R18 = get_double (ss)
2010 wmov r24, Y
2011 XCALL F7_NAME (get_double)
2012 ldd XL, Y + n_frame + 3 ; Saved R16
2013 ldd XH, Y + n_frame + 2 ; Saved R17
2014 rcall store.r18.X ; *psin = R18
2015
2016 do_epilogue_restores n_pushed, n_frame
2017
2018 store.r18.X:
2019 st X+, r18
2020 st X+, r19
2021 st X+, r20
2022 st X+, r21
2023 st X+, r22
2024 st X+, r23
2025 st X+, r24
2026 st X+, r25
2027 ret
2028 _ENDF __sincos
2029 #endif /* F7MOD_D_sincos_ */
2030
2031 #ifdef F7MOD_call_dd_
2032
2033 ;; Provide double wrappers for functions that operate on f7_t and get f7_t*.
2034 ;;
2035 ;; We set up a frame of sizeof(f7_t), convert the input double in R18[] to
2036 ;; f7_t in that frame location, then call *Z and finally convert the result f7_t
2037 ;; to double R18[] if that's requested.
2038 ;;
2039 ;; call_dd: double func (double A)
2040 ;; void (*Z) (f7_t *aa, const f7_t *aa)
2041 ;;
2042 ;; call_dx: double func (type_t A) , sizeof(type_t) <= 4
2043 ;; void (*Z) (f7_t *aa, type_t)
2044 ;;
2045 ;; call_xd: type_t func (double A)
2046 ;; type_t (*Z) (const f7_t *aa)
2047 ;;
2048 ;; call_ddx: double func (double A, word_t) , sizeof (word_t) <= 2
2049 ;; void (*Z) (f7_t *aa, const f7_t *aa, word_t)
2050
2051 #define WHAT R13
2052
2053 DEFUN call_dd ; WHAT = R13 = 3
2054 inc ZERO
2055 LABEL call_xd ; WHAT = R13 = 2
2056 inc ZERO
2057 LABEL call_ddx ; WHAT = R13 = 1
2058 inc ZERO
2059 LABEL call_dx ; WHAT = R13 = 0
2060 push WHAT
2061 mov WHAT, ZERO
2062 clr ZERO
2063 ;; R14/R15 hold Z, the address of the f7_worker function, until we need it.
2064 push r14
2065 push r15
2066 wmov r14, Z
2067
2068 #define n_pushed 4
2069 #define n_frame F7_SIZEOF
2070
2071 do_prologue_saves n_pushed, n_frame
2072 ;; Y = FramePointer + 1
2073 adiw Y, 1
2074 dec WHAT
2075 brmi .Ldx ; WHAT was initially 0.
2076 ;; FP + 1 = (f7_t) arg1
2077 wmov r16, Y
2078 ;; The double argument is in R18[].
2079 XCALL F7_NAME (set_double_impl)
2080 tst WHAT
2081 brne .Lno.ddx ; WHAT was initially != 1.
2082 ;; call_ddx: Set R20/21 to the 2-byte scalar / pointer argument.
2083 ;; Fetch it from where prologue_saves put it.
2084 ldd r20, Y + n_frame + 3 ; Saved R16
2085 ldd r21, Y + n_frame + 2 ; Saved R17
2086 .Lno.ddx:
2087 wmov r22, Y ; &arg1 (input)
2088 .Ldo.dx:
2089 wmov r24, Y ; &arg1 (output)
2090 wmov Z, r14
2091 XICALL
2092 dec WHAT
2093 breq .Lepilogue ; WHAT was initially 2: Return non-double.
2094 wmov r24, Y ; &arg1
2095 XCALL F7_NAME (get_double)
2096 .Lepilogue:
2097 ;; + 3 to account for R13...R15 pushed prior to do_prologue_saves.
2098 do_epilogue_restores n_pushed + 3, n_frame
2099
2100 .Ldx:
2101 ;; call_dx: Copy the 4-byte input scalar from R22[4] to R20[4].
2102 wmov r20, r22
2103 wmov r22, r24
2104 rjmp .Ldo.dx
2105
2106 ENDF call_dd
2107 #endif /* F7MOD_call_dd_ */
2108
2109
2110 #ifdef F7MOD_call_ddd_
2111
2112 ;; Provide double wrappers for functions that operate on f7_t and get f7_t*.
2113 ;;
2114 ;; We set up a frame of 2 * sizeof(f7_t), convert the input doubles in R18[]
2115 ;; and R10[] to f7_t in these frame locations, then call *Z and finally
2116 ;; convert the result f7_t to double R18[] if that's requested.
2117 ;;
2118 ;; call_ddd: double func (double A, double B)
2119 ;; void (*Z) (f7_t *aa, const f7_t *aa, const f7_t *bb)
2120 ;;
2121 ;; call_xdd: type_t func (double A, double B)
2122 ;; type_t (*Z) (const f7_t *aa, const f7_t *bb)
2123
2124 DEFUN call_ddd
2125 inc ZERO
2126 LABEL call_xdd
2127 ;; R8/R9 hold Z, the address of the f7_worker function, until we need it.
2128 push r9
2129 push r8
2130 wmov r8, Z
2131 ;; This is an argument to call.2 and will be accessed by the arg pointer.
2132 push ZERO
2133 clr ZERO
2134 rcall call.2
2135 pop TMP
2136 pop r8
2137 pop r9
2138 ret
2139
2140 #define n_pushed 4
2141 #define n_frame (2 * F7_SIZEOF)
2142
2143 call.2:
2144 do_prologue_saves n_pushed, n_frame
2145 ;; Y = FramePointer + 1
2146 adiw Y, 1
2147 ;; FP + 1 = (f7_t) arg1
2148 wmov r16, Y
2149 ;; First double argument is already in R18[].
2150 XCALL F7_NAME (set_double_impl)
2151 ;; FP + 11 = (f7_t) arg2
2152 subi r16, lo8 (-F7_SIZEOF)
2153 sbci r17, hi8 (-F7_SIZEOF)
2154 ;; Move second double argument to R18[].
2155 wmov r18, r10
2156 wmov r20, r12
2157 wmov r22, r14
2158 ;; Get high word of arg2 from where prologue_saves put it.
2159 ldd r24, Y + n_frame + 3 ; Saved R16
2160 ldd r25, Y + n_frame + 2 ; Saved R17
2161 XCALL F7_NAME (set_double_impl)
2162 ;; Z (f7_t *arg1, const f7_t *arg1, const f7_t *arg2)
2163 wmov Z, r8
2164 wmov r24, Y ; &arg1
2165 ;; WHAT == 0 => call_xdd
2166 ;; WHAT != 0 => call_ddd
2167 ldd TMP, Y + n_frame + n_pushed + PC_SIZE
2168 tst TMP
2169 breq .Lxdd
2170 wmov r22, Y ; &arg1
2171 wmov r20, r16 ; &arg2
2172 XICALL
2173 wmov r24, Y ; &arg1
2174 XCALL F7_NAME (get_double)
2175 .Lepilogue:
2176 do_epilogue_restores n_pushed, n_frame
2177 .Lxdd:
2178 wmov r22, r16 ; &arg2
2179 XICALL
2180 rjmp .Lepilogue
2181 ENDF call_ddd
2182 #endif /* F7MOD_call_ddd_ */
2183
2184 #include "f7-wraps.h"
2185
2186 ;;; Some additional, singular wraps that don't match any pattern.
2187
2188 ;; double __powidf2 (double, int) ; __builtin_powi
2189 #ifdef F7MOD_D_powi_
2190 _DEFUN __powidf2
2191 .global F7_NAME(powi)
2192 ldi ZH, hi8(gs(F7_NAME(powi)))
2193 ldi ZL, lo8(gs(F7_NAME(powi)))
2194 F7jmp call_ddx
2195 _ENDF __powidf2
2196 #endif /* F7MOD_D_powi_ */
2197
2198
2199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2200 ;;; Fixed-point -> double conversions.
2201
2202 ;;; The double exponent starts at bit 52 since the encoded mantissa has 52 bits.
2203 ;;; Note that when X is a multiple of 16, then dex_lo(x) evaluates to 0.
2204 #define DEX16(x) (x) << (52 - 48)
2205 #define dex_lo(x) lo8 (DEX16 (x))
2206 #define dex_hi(x) hi8 (DEX16 (x))
2207
2208 #ifdef F7MOD_usa2D_
2209 _DEFUN __fractusadf
2210 ;; Convert USI to DF.
2211 XCALL __floatunsidf
2212 ;; The MSB indicates a value of 0.
2213 cpse r25, __zero_reg__
2214 ;; Divide non-zero values by 2^16 in order to adjust for FBIT = 16.
2215 subi r25, dex_hi (16)
2216 ret
2217 _ENDF __fractusadf
2218 #endif /* F7MOD_usa2D_ */
2219
2220 #ifdef F7MOD_sa2D_
2221 _DEFUN __fractsadf
2222 ;; Convert SI to DF.
2223 XCALL __floatsidf
2224 ;; The MSB indicates a value of 0.
2225 tst r25
2226 breq 0f
2227 ;; Divide non-zero values by 2^15 in order to adjust for FBIT = 15.
2228 subi r24, dex_lo (15)
2229 sbci r25, dex_hi (15)
2230 0: ret
2231 _ENDF __fractsadf
2232 #endif /* F7MOD_sa2D_ */
2233
2234 #ifdef F7MOD_uha2D_
2235 _DEFUN __fractuhadf
2236 ;; Extend UHA to USA.
2237 clr r22
2238 mov r23, r24
2239 mov r24, r25
2240 clr r25
2241 XJMP __fractusadf
2242 _ENDF __fractuhadf
2243 #endif /* F7MOD_uha2D_ */
2244
2245 #ifdef F7MOD_ha2D_
2246 _DEFUN __fracthadf
2247 ;; Extend HA to SA.
2248 clr r22
2249 mov r23, r24
2250 mov r24, r25
2251 lsl r25
2252 sbc r25, r25
2253 XJMP __fractsadf
2254 _ENDF __fracthadf
2255 #endif /* F7MOD_ha2D_ */
2256
2257
2258 #ifdef F7MOD_usq2D_
2259 _DEFUN __fractusqdf
2260 ;; Convert USI to DF.
2261 XCALL __floatunsidf
2262 ;; The MSB indicates a value of 0.
2263 cpse r25, __zero_reg__
2264 ;; Divide non-zero values by 2^32 in order to adjust for FBIT = 32.
2265 subi r25, dex_hi (32)
2266 ret
2267 _ENDF __fractusqdf
2268 #endif /* F7MOD_usq2D_ */
2269
2270 #ifdef F7MOD_sq2D_
2271 _DEFUN __fractsqdf
2272 ;; Convert SI to DF.
2273 XCALL __floatsidf
2274 ;; The MSB indicates a value of 0.
2275 tst r25
2276 breq 0f
2277 ;; Divide non-zero values by 2^31 in order to adjust for FBIT = 31.
2278 subi r24, dex_lo (31)
2279 sbci r25, dex_hi (31)
2280 0: ret
2281 _ENDF __fractsqdf
2282 #endif /* F7MOD_sq2D_ */
2283
2284 #ifdef F7MOD_uqq2D_
2285 _DEFUN __fractuqqdf
2286 ;; Extend UQQ to UHQ.
2287 mov r25, r24
2288 clr r24
2289 _LABEL __fractuhqdf
2290 ;; Extend UHQ to USQ.
2291 clr r23
2292 clr r22
2293 XJMP __fractusqdf
2294 _ENDF __fractuqqdf
2295 #endif /* F7MOD_uqq2D_ */
2296
2297 #ifdef F7MOD_qq2D_
2298 _DEFUN __fractqqdf
2299 ;; Extend QQ to HQ.
2300 mov r25, r24
2301 clr r24
2302 _LABEL __fracthqdf
2303 ;; Extend HQ to SQ.
2304 clr r23
2305 clr r22
2306 XJMP __fractsqdf
2307 _ENDF __fractqqdf
2308 #endif /* F7MOD_qq2D_ */
2309
2310
2311 #ifdef F7MOD_fx2D_
2312 ;;; Convert an [un]signed 64-bit fixed point value R18[] to double.
2313 ;;; double ufx64_to_d (ufx64_t R18, uint8_t fbit R30);
2314 ;;; double sfx64_to_d (sfx64_t R18, uint8_t fbit R30);
2315 ;;; FBIT accounts for the fractional bits of the fixed point type,
2316 ;;; e.g. pass __DA_FBIT__ for long accum etc.
2317 DEFUN ufx64_to_d
2318 push r30
2319 XCALL __floatundidf
2320 rjmp 1f
2321
2322 LABEL sfx64_to_d
2323 push r30
2324 XCALL __floatdidf
2325 1: pop r30
2326
2327 ;; The MSB indicates a value of 0. Notice that at this point,
2328 ;; we have a normal floating point number with a biased exponent
2329 ;; that won't underflow below.
2330 tst r25
2331 breq 0f
2332
2333 ;; Subtract fbit from the double exponent. R30[] = fbit << 4.
2334 swap r30
2335 mov r31, r30
2336 andi r30, lo8 (0x0ff0)
2337 andi r31, hi8 (0x0ff0)
2338 sub r24, r30
2339 sbc r25, r31
2340
2341 0: ret
2342 ENDF ufx64_to_d
2343 #endif /* F7MOD_fx2D_ */
2344
2345 #ifdef F7MOD_da2D_
2346 _DEFUN __fractdadf
2347 ldi r30, __DA_FBIT__
2348 F7jmp sfx64_to_d
2349 _ENDF __fractdadf
2350 #endif /* F7MOD_da2D_ */
2351
2352 #ifdef F7MOD_uda2D_
2353 _DEFUN __fractudadf
2354 ldi r30, __UDA_FBIT__
2355 F7jmp ufx64_to_d
2356 _ENDF __fractudadf
2357 #endif /* F7MOD_uda2D_ */
2358
2359 #ifdef F7MOD_ta2D_
2360 _DEFUN __fracttadf
2361 ldi r30, __TA_FBIT__
2362 F7jmp sfx64_to_d
2363 _ENDF __fracttadf
2364 #endif /* F7MOD_ta2D_ */
2365
2366 #ifdef F7MOD_uta2D_
2367 _DEFUN __fractutadf
2368 ldi r30, __UTA_FBIT__
2369 F7jmp ufx64_to_d
2370 _ENDF __fractutadf
2371 #endif /* F7MOD_uta2D_ */
2372
2373 #ifdef F7MOD_dq2D_
2374 _DEFUN __fractdqdf
2375 ldi r30, __DQ_FBIT__
2376 F7jmp sfx64_to_d
2377 _ENDF __fractdqdf
2378 #endif /* F7MOD_dq2D_ */
2379
2380 #ifdef F7MOD_udq2D_
2381 _DEFUN __fractudqdf
2382 ldi r30, __UDQ_FBIT__
2383 F7jmp ufx64_to_d
2384 _ENDF __fractudqdf
2385 #endif /* F7MOD_udq2D_ */
2386
2387
2388 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2389 ;;; Double -> fixed-point conversions.
2390
2391 #ifdef F7MOD_D2qq_
2392 _DEFUN __fractdfqq
2393 ;; Multiply with 2^{24+7} to get a QQ result in r25.
2394 subi r24, dex_lo (-31)
2395 sbci r25, dex_hi (-31)
2396 XCALL __fixdfsi
2397 mov r24, r25
2398 ret
2399 _ENDF __fractdfqq
2400 #endif /* F7MOD_D2qq_ */
2401
2402 #ifdef F7MOD_D2uqq_
2403 _DEFUN __fractdfuqq
2404 ;; Multiply with 2^{24+8} to get a UQQ result in r25.
2405 subi r25, dex_hi (-32)
2406 XCALL __fixunsdfsi
2407 mov r24, r25
2408 ret
2409 _ENDF __fractdfuqq
2410 #endif /* F7MOD_D2uqq_ */
2411
2412 #ifdef F7MOD_D2ha_
2413 _DEFUN __fractdfha
2414 ;; Multiply with 2^{16+7} to get a HA result in r25:r24
2415 subi r24, dex_lo (-23)
2416 sbci r25, dex_hi (-23)
2417 XJMP __fixdfsi
2418 _ENDF __fractdfha
2419 #endif /* F7MOD_D2ha_ */
2420
2421 #ifdef F7MOD_D2uha_
2422 _DEFUN __fractdfuha
2423 ;; Multiply with 2^24 to get a UHA result in r25:r24.
2424 subi r24, dex_lo (-24)
2425 sbci r25, dex_hi (-24)
2426 XJMP __fixunsdfsi
2427 _ENDF __fractdfuha
2428 #endif /* F7MOD_D2uha_ */
2429
2430 #ifdef F7MOD_D2hq_
2431 _DEFUN __fractdfhq
2432 ;; Multiply with 2^{16+15} to get a HQ result in r25:r24
2433 ;; resp. with 2^31 to get a SQ result in r25:r22.
2434 _LABEL __fractdfsq
2435 subi r24, dex_lo (-31)
2436 sbci r25, dex_hi (-31)
2437 XJMP __fixdfsi
2438 _ENDF __fractdfhq
2439 #endif /* F7MOD_D2hq_ */
2440
2441 #ifdef F7MOD_D2uhq_
2442 _DEFUN __fractdfuhq
2443 ;; Multiply with 2^{16+16} to get a UHQ result in r25:r24
2444 ;; resp. with 2^32 to get a USQ result in r25:r22.
2445 _LABEL __fractdfusq
2446 subi r25, dex_hi (-32)
2447 XJMP __fixunsdfsi
2448 _ENDF __fractdfuhq
2449 #endif /* F7MOD_D2uhq_ */
2450
2451 #ifdef F7MOD_D2sa_
2452 _DEFUN __fractdfsa
2453 ;; Multiply with 2^15 to get a SA result in r25:r22.
2454 subi r24, dex_lo (-15)
2455 sbci r25, dex_hi (-15)
2456 XJMP __fixdfsi
2457 _ENDF __fractdfsa
2458 #endif /* F7MOD_D2sa_ */
2459
2460 #ifdef F7MOD_D2usa_
2461 _DEFUN __fractdfusa
2462 ;; Multiply with 2^16 to get a USA result in r25:r22.
2463 subi r25, dex_hi (-16)
2464 XJMP __fixunsdfsi
2465 _ENDF __fractdfusa
2466 #endif /* F7MOD_D2usa_ */
2467
2468
2469 #ifdef F7MOD_D2fx_
2470 ;;; Convert double R18 to a 64-bit fixed point value.
2471 ;;; R18 d_to_fx64 (double R18, int8_t fbit R16);
2472 ;;; |FBIT| accounts for the extra fbits compared to [u]int64_t.
2473 ;;; FBIT < 0 indicates a signed conversion.
2474 ;;; FBIT > 0 indicates an unsigned conversion.
2475 DEFUN d_to_fx64
2476 do_prologue_saves 4, F7_SIZEOF
2477
2478 ;; Y = FramePointer + 1
2479 adiw Y, 1
2480
2481 ;; FP + 1 = (f7_t) arg
2482 wmov r16, Y
2483
2484 ;; Convert double R18 to f7_t *R16.
2485 XCALL F7_NAME (set_double_impl)
2486
2487 ;; Prepare for *R24 = ldexp (*R24, R22). ldexp() accounts for the
2488 ;; fractional bits of the fixed point value, i.e. we have to <<= FBIT.
2489 wmov r24, r16
2490 ;; Zero-extend FBIT.
2491 clr r23
2492 ;; R16 has been clobbered. Fetch it from where prologue_saves put it.
2493 ldd r22, Y + F7_SIZEOF + 3 ; Saved R16
2494 tst r22
2495 brmi 1f
2496
2497 ;; Positive FBIT indicates an unsigned conversion.
2498 XCALL F7_NAME (Ildexp)
2499 XCALL F7_NAME (get_u64)
2500 rjmp 9f
2501
2502 1: ;; Negative FBIT indicates a signed conversion.
2503 neg r22 ; FBIT := |FBIT|
2504 XCALL F7_NAME (Ildexp)
2505 XCALL F7_NAME (get_s64)
2506
2507 9: do_epilogue_restores 4, F7_SIZEOF
2508 ENDF d_to_fx64
2509 #endif /* F7MOD_D2fx_ */
2510
2511
2512 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2513 ;;; [u]int32_t -> double conversions.
2514
2515 ;; double __floatsidf (int32_t);
2516 #ifdef F7MOD_D_floatsidf_
2517 _DEFUN __floatsidf
2518 bst r25, 7
2519 brtc 0f
2520 XCALL __negsi2
2521 0: XJMP __floatunsidf.ge0
2522 _ENDF __floatsidf
2523 #endif /* F7MOD_D_floatsidf_ */
2524
2525 ;; double __floatunsidf (uint32_t);
2526 #ifdef F7MOD_D_floatunsidf_
2527 _DEFUN __floatunsidf
2528 clt
2529 _LABEL __floatunsidf.ge0
2530 ;; Zero-extend SI at the low end.
2531 clr r18
2532 clr r19
2533 wmov r20, r18
2534 ;; Input is zero?
2535 sbiw r24, 0
2536 sbci r23, 0
2537 sbci r22, 0
2538 breq 9f
2539 ;; No: The double exponent of 0x80000000 is 31 plus a bias of 1023.
2540 ;; Align the SI value such that the MSBit is as R25.4.
2541 ;; For each << we have to subtract 1 from the exponent, and for
2542 ;; each >> we have to add 1. Since we want the MSB in R25.4 and
2543 ;; not in R25.7, the initial exponent must be reduced by 3.
2544 ldi Xl, dex_lo (31 + 1023 - 3)
2545 ldi Xh, dex_hi (31 + 1023 - 3)
2546 ;; Move the MSByte to R25.
2547 1: tst r25
2548 brne 2f
2549 subi Xl, dex_lo (8)
2550 sbci Xh, dex_hi (8)
2551 mov r25, r24
2552 mov r24, r23
2553 mov r23, r22
2554 clr r22
2555 rjmp 1b
2556 2: ;; Now we have R25 != 0.
2557 cpi r25, 0x20
2558 brlo 3f
2559 adiw Xl, DEX16 (1)
2560 lsr r25
2561 ror r24
2562 ror r23
2563 ror r22
2564 ror r21
2565 rjmp 2b
2566 3: cpi r25, 0x10
2567 brsh 4f
2568 sbiw Xl, DEX16 (1)
2569 lsl r22
2570 rol r23
2571 rol r24
2572 rol r25
2573 rjmp 3b
2574 4: ;; Move the mantissa into place and clear the redundant leading 1.
2575 cbr r25, 0x10
2576 mov r20, r21
2577 mov r21, r22
2578 mov r22, r23
2579 mov r23, r24
2580 mov r24, r25
2581 ;; Insert the biased exponent.
2582 or r24, Xl
2583 mov r25, Xh
2584 ;; Insert the sign.
2585 bld r25, 7
2586 9: ret
2587 _ENDF __floatunsidf
2588 #endif /* F7MOD_D_floatunsidf_ */
2589
2590
2591 #endif /* !AVR_TINY */