URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgcc/] [config/] [avr/] [lib1funcs.S] - Rev 734
Compare with Previous | Blame | View Log
/* -*- Mode: Asm -*- *//* Copyright (C) 1998, 1999, 2000, 2007, 2008, 2009Free Software Foundation, Inc.Contributed by Denis Chertykov <chertykov@gmail.com>This file is free software; you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation; either version 3, or (at your option) anylater version.This file is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.Under Section 7 of GPL version 3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation.You should have received a copy of the GNU General Public License anda 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/>. */#define __zero_reg__ r1#define __tmp_reg__ r0#define __SREG__ 0x3f#define __SP_H__ 0x3e#define __SP_L__ 0x3d#define __RAMPZ__ 0x3B#define __EIND__ 0x3C/* Most of the functions here are called directly from avr.mdpatterns, instead of using the standard libcall mechanisms.This can make better code because GCC knows exactly whichof the call-used registers (not all of them) are clobbered. *//* FIXME: At present, there is no SORT directive in the linkerscript so that we must not assume that different modulesin the same input section like .libgcc.text.mul will belocated close together. Therefore, we cannot useRCALL/RJMP to call a function like __udivmodhi4 from__divmodhi4 and have to use lengthy XCALL/XJMP eventhough they are in the same input section and all sameinput sections together are small enough to reach everylocation with a RCALL/RJMP instruction. */.macro mov_l r_dest, r_src#if defined (__AVR_HAVE_MOVW__)movw \r_dest, \r_src#elsemov \r_dest, \r_src#endif.endm.macro mov_h r_dest, r_src#if defined (__AVR_HAVE_MOVW__); empty#elsemov \r_dest, \r_src#endif.endm.macro wmov r_dest, r_src#if defined (__AVR_HAVE_MOVW__)movw \r_dest, \r_src#elsemov \r_dest, \r_srcmov \r_dest+1, \r_src+1#endif.endm#if defined (__AVR_HAVE_JMP_CALL__)#define XCALL call#define XJMP jmp#else#define XCALL rcall#define XJMP rjmp#endif.macro DEFUN name.global \name.func \name\name:.endm.macro ENDF name.size \name, .-\name.endfunc.endm.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 __mulqi3clr r_res ; clear result__mulqi3_loop:sbrc r_arg1,0add r_res,r_arg2add r_arg2,r_arg2 ; shift multiplicandbreq __mulqi3_exit ; while multiplicand != 0lsr r_arg1 ;brne __mulqi3_loop ; exit if multiplier = 0__mulqi3_exit:mov r_arg1,r_res ; result to return registerretENDF __mulqi3#undef r_arg2#undef r_arg1#undef r_res#endif /* defined (L_mulqi3) */#if defined (L_mulqihi3)DEFUN __mulqihi3clr r25sbrc r24, 7dec r25clr r23sbrc r22, 7dec r22XJMP __mulhi3ENDF __mulqihi3:#endif /* defined (L_mulqihi3) */#if defined (L_umulqihi3)DEFUN __umulqihi3clr r25clr r23XJMP __mulhi3ENDF __umulqihi3#endif /* defined (L_umulqihi3) *//*******************************************************Multiplication 16 x 16 without MUL*******************************************************/#if defined (L_mulhi3)#define r_arg1L r24 /* multiplier Low */#define r_arg1H r25 /* multiplier High */#define r_arg2L r22 /* multiplicand Low */#define r_arg2H r23 /* multiplicand High */#define r_resL __tmp_reg__ /* result Low */#define r_resH r21 /* result High */DEFUN __mulhi3clr r_resH ; clear resultclr r_resL ; clear result__mulhi3_loop:sbrs r_arg1L,0rjmp __mulhi3_skip1add r_resL,r_arg2L ; result + multiplicandadc r_resH,r_arg2H__mulhi3_skip1:add r_arg2L,r_arg2L ; shift multiplicandadc r_arg2H,r_arg2Hcp r_arg2L,__zero_reg__cpc r_arg2H,__zero_reg__breq __mulhi3_exit ; while multiplicand != 0lsr r_arg1H ; gets LSB of multiplierror r_arg1Lsbiw r_arg1L,0brne __mulhi3_loop ; exit if multiplier = 0__mulhi3_exit:mov r_arg1H,r_resH ; result to return registermov r_arg1L,r_resLretENDF __mulhi3#undef r_arg1L#undef r_arg1H#undef r_arg2L#undef r_arg2H#undef r_resL#undef r_resH#endif /* defined (L_mulhi3) *//*******************************************************Widening Multiplication 32 = 16 x 16 without MUL*******************************************************/#if defined (L_mulhisi3)DEFUN __mulhisi3;;; FIXME: This is dead code (noone calls it)mov_l r18, r24mov_h r19, r25clr r24sbrc r23, 7dec r24mov r25, r24clr r20sbrc r19, 7dec r20mov r21, r20XJMP __mulsi3ENDF __mulhisi3#endif /* defined (L_mulhisi3) */#if defined (L_umulhisi3)DEFUN __umulhisi3;;; FIXME: This is dead code (noone calls it)mov_l r18, r24mov_h r19, r25clr r24clr r25mov_l r20, r24mov_h r21, r25XJMP __mulsi3ENDF __umulhisi3#endif /* defined (L_umulhisi3) */#if defined (L_mulsi3)/*******************************************************Multiplication 32 x 32 without MUL*******************************************************/#define r_arg1L r22 /* multiplier Low */#define r_arg1H r23#define r_arg1HL r24#define r_arg1HH r25 /* multiplier High */#define r_arg2L r18 /* multiplicand Low */#define r_arg2H r19#define r_arg2HL r20#define r_arg2HH r21 /* multiplicand High */#define r_resL r26 /* result Low */#define r_resH r27#define r_resHL r30#define r_resHH r31 /* result High */DEFUN __mulsi3clr r_resHH ; clear resultclr r_resHL ; clear resultclr r_resH ; clear resultclr r_resL ; clear result__mulsi3_loop:sbrs r_arg1L,0rjmp __mulsi3_skip1add r_resL,r_arg2L ; result + multiplicandadc r_resH,r_arg2Hadc r_resHL,r_arg2HLadc r_resHH,r_arg2HH__mulsi3_skip1:add r_arg2L,r_arg2L ; shift multiplicandadc r_arg2H,r_arg2Hadc r_arg2HL,r_arg2HLadc r_arg2HH,r_arg2HHlsr r_arg1HH ; gets LSB of multiplierror r_arg1HLror r_arg1Hror r_arg1Lbrne __mulsi3_loopsbiw r_arg1HL,0cpc r_arg1H,r_arg1Lbrne __mulsi3_loop ; exit if multiplier = 0__mulsi3_exit:mov_h r_arg1HH,r_resHH ; result to return registermov_l r_arg1HL,r_resHLmov_h r_arg1H,r_resHmov_l r_arg1L,r_resLretENDF __mulsi3#undef r_arg1L#undef r_arg1H#undef r_arg1HL#undef r_arg1HH#undef r_arg2L#undef r_arg2H#undef r_arg2HL#undef r_arg2HH#undef r_resL#undef r_resH#undef r_resHL#undef r_resHH#endif /* defined (L_mulsi3) */#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*******************************************************/#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 __mulhisi3XCALL __umulhisi3;; Sign-extend Btst B1brpl 1fsub C2, A0sbc C3, A11: ;; Sign-extend AXJMP __usmulhisi3_tailENDF __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 __usmulhisi3XCALL __umulhisi3;; FALLTHRUENDF __usmulhisi3DEFUN __usmulhisi3_tail;; Sign-extend Asbrs A1, 7retsub C2, B0sbc C3, B1retENDF __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 __umulhisi3mul A0, B0movw C0, r0mul A1, B1movw C2, r0mul A0, B1rcall 1fmul A1, B01: add C1, r0adc C2, r1clr __zero_reg__adc C3, __zero_reg__retENDF __umulhisi3#endif /* L_umulhisi3 *//*******************************************************Widening Multiplication 32 = 16 x 32*******************************************************/#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 instructiontst A1brmi __mulohisi3#elsesbrs A1, 7#endif /* __AVR_HAVE_JMP_CALL__ */XJMP __muluhisi3;; FALLTHRUENDF __mulshisi3;;; R25:R22 = (one-extended long) R27:R26 * R21:R18;;; (C3:C0) = (one-extended long) A1:A0 * B3:B0;;; Clobbers: __tmp_reg__DEFUN __mulohisi3XCALL __muluhisi3;; One-extend R27:R26 (A1:A0)sub C2, B0sbc C3, B1retENDF __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 __muluhisi3XCALL __umulhisi3mul A0, B3add C3, r0mul A1, B2add C3, r0mul A0, B2add C2, r0adc C3, r1clr __zero_reg__retENDF __muluhisi3#endif /* L_muluhisi3 *//*******************************************************Multiplication 32 x 32*******************************************************/#if defined (L_mulsi3);;; R25:R22 = R25:R22 * R21:R18;;; (C3:C0) = C3:C0 * B3:B0;;; Clobbers: R26, R27, __tmp_reg__DEFUN __mulsi3movw A0, C0push C2push C3XCALL __muluhisi3pop A1pop A0;; A1:A0 now contains the high word of Amul A0, B0add C2, r0adc C3, r1mul A0, B1add C3, r0mul A1, B0add C3, r0clr __zero_reg__retENDF __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*******************************************************/#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 21DEFUN __mulpsi3wmov AA0, A0mov AA2, A2XCALL __umulhisi3mul AA2, B0 $ add C2, r0mul AA0, B2 $ add C2, r0clr __zero_reg__retENDF __mulpsi3#undef AA2#undef AA0#undef C2#undef C1#undef C0#else /* !HAVE_MUL */;; C[0..2]: Expand Result#define C0 0#define C1 C0+1#define C2 21;; R24:R22 *= R20:R18;; Clobbers: __tmp_reg__, R18, R19, R20, R21DEFUN __mulpsi3;; C[] = 0clr __tmp_reg__clr C20: ;; Shift N-th Bit of B[] into Carry. N = 24 - LoopLSR 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,A21: ;; Multiply A[] by 2LSL A0 $ rol A1 $ rol A2;; Loop until B[] is 0subi B0,0 $ sbci B1,0 $ sbci B2,0brne 0b;; Copy C[] to the return Register A[]wmov A0, C0mov A2, C2clr __zero_reg__retENDF __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 __mulsqipsi3mul A0, BBmovw C0, r0mul A2, BBmov C2, r0mul A1, BBadd C1, r0adc C2, r1clr __zero_reg__sbrs BB, 7ret;; One-extend BBsub C1, A0sbc C2, A1retENDF __mulsqipsi3#undef C2#undef C1#undef C0#undef BB#undef A2#undef A1#undef A0#endif /* L_mulsqipsi3 && HAVE_MUL *//*******************************************************Multiplication 64 x 64*******************************************************/#if defined (L_muldi3);; 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#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;; A[] *= B[];; R25:R18 *= R17:R10;; Ordinary ABI-FunctionDEFUN __muldi3push r29push r28push r17push r16;; Counting in Words, we have to perform a 4 * 4 Multiplication;; 3 * 0 + 0 * 3mul A7,B0 $ $ mov C7,r0mul A0,B7 $ $ add C7,r0mul A6,B1 $ $ add C7,r0mul A6,B0 $ mov C6,r0 $ add C7,r1mul B6,A1 $ $ add C7,r0mul B6,A0 $ add C6,r0 $ adc C7,r1;; 1 * 2mul A2,B4 $ add C6,r0 $ adc C7,r1mul A3,B4 $ $ add C7,r0mul A2,B5 $ $ add C7,r0push A5push A4push B1push B0push A3push A2;; 0 * 0wmov 26, B0XCALL __umulhisi3wmov C0, 22wmov C2, 24;; 0 * 2wmov 26, B4XCALL __umulhisi3 $ wmov C4,22 $ add C6,24 $ adc C7,25wmov 26, B2;; 0 * 1rcall __muldi3_6pop A0pop A1;; 1 * 1wmov 26, B2XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25pop r26pop r27;; 1 * 0rcall __muldi3_6pop A0pop A1;; 2 * 0XCALL __umulhisi3 $ add C4,22 $ adc C5,23 $ adc C6,24 $ adc C7,25;; 2 * 1wmov 26, B2XCALL __umulhisi3 $ $ $ add C6,22 $ adc C7,23;; A[] = C[]wmov A0, C0;; A2 = C2 alreadywmov A4, C4wmov A6, C6clr __zero_reg__pop r16pop r17pop r28pop r29ret__muldi3_6:XCALL __umulhisi3add C2, 22adc C3, 23adc C4, 24adc C5, 25brcc 0fadiw C6, 10: retENDF __muldi3#undef C7#undef C6#undef C5#undef C4#undef C3#undef C2#undef C1#undef C0#else /* !HAVE_MUL */#define C0 26#define C1 C0+1#define C2 C0+2#define C3 C0+3#define C4 C0+4#define C5 C0+5#define C6 0#define C7 C6+1#define Loop 9;; A[] *= B[];; R25:R18 *= R17:R10;; Ordinary ABI-FunctionDEFUN __muldi3push r29push r28push Loopldi C0, 64mov Loop, C0;; C[] = 0clr __tmp_reg__wmov C0, 0wmov C2, 0wmov C4, 00: ;; Rotate B[] right by 1 and set Carry to the N-th Bit of B[];; where N = 64 - Loop.;; Notice that B[] = B[] >>> 64 so after this Routine has finished,;; B[] will have its initial Value again.LSR B7 $ ror B6 $ ror B5 $ ror B4ror B3 $ ror B2 $ ror B1 $ ror B0;; If the N-th Bit of B[] was set then...brcc 1f;; ...finish Rotation...ori B7, 1 << 7;; ...and add A[] * 2^N to the Result C[]ADD C0,A0 $ adc C1,A1 $ adc C2,A2 $ adc C3,A3adc C4,A4 $ adc C5,A5 $ adc C6,A6 $ adc C7,A71: ;; Multiply A[] by 2LSL A0 $ rol A1 $ rol A2 $ rol A3rol A4 $ rol A5 $ rol A6 $ rol A7dec Loopbrne 0b;; We expanded the Result in C[];; Copy Result to the Return Register A[]wmov A0, C0wmov A2, C2wmov A4, C4wmov A6, C6clr __zero_reg__pop Looppop r28pop r29retENDF __muldi3#undef Loop#undef C7#undef C6#undef C5#undef C4#undef C3#undef C2#undef C1#undef C0#endif /* HAVE_MUL */#undef B7#undef B6#undef B5#undef B4#undef B3#undef B2#undef B1#undef B0#undef A7#undef A6#undef A5#undef A4#undef A3#undef A2#undef A1#undef A0#endif /* L_muldi3 */;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;.section .text.libgcc.div, "ax", @progbits/*******************************************************Division 8 / 8 => (result + remainder)*******************************************************/#define r_rem r25 /* remainder */#define r_arg1 r24 /* dividend, quotient */#define r_arg2 r22 /* divisor */#define r_cnt r23 /* loop count */#if defined (L_udivmodqi4)DEFUN __udivmodqi4sub r_rem,r_rem ; clear remainder and carryldi r_cnt,9 ; init loop counterrjmp __udivmodqi4_ep ; jump to entry point__udivmodqi4_loop:rol r_rem ; shift dividend into remaindercp r_rem,r_arg2 ; compare remainder & divisorbrcs __udivmodqi4_ep ; remainder <= divisorsub r_rem,r_arg2 ; restore remainder__udivmodqi4_ep:rol r_arg1 ; shift dividend (with CARRY)dec r_cnt ; decrement loop counterbrne __udivmodqi4_loopcom r_arg1 ; complement result; because C flag was complemented in loopretENDF __udivmodqi4#endif /* defined (L_udivmodqi4) */#if defined (L_divmodqi4)DEFUN __divmodqi4bst r_arg1,7 ; store sign of dividendmov __tmp_reg__,r_arg1eor __tmp_reg__,r_arg2; r0.7 is sign of resultsbrc r_arg1,7neg r_arg1 ; dividend negative : negatesbrc r_arg2,7neg r_arg2 ; divisor negative : negateXCALL __udivmodqi4 ; do the unsigned div/modbrtc __divmodqi4_1neg r_rem ; correct remainder sign__divmodqi4_1:sbrc __tmp_reg__,7neg r_arg1 ; correct result sign__divmodqi4_exit:retENDF __divmodqi4#endif /* defined (L_divmodqi4) */#undef r_rem#undef r_arg1#undef r_arg2#undef r_cnt/*******************************************************Division 16 / 16 => (result + remainder)*******************************************************/#define r_remL r26 /* remainder Low */#define r_remH r27 /* remainder High *//* return: remainder */#define r_arg1L r24 /* dividend Low */#define r_arg1H r25 /* dividend High *//* return: quotient */#define r_arg2L r22 /* divisor Low */#define r_arg2H r23 /* divisor High */#define r_cnt r21 /* loop count */#if defined (L_udivmodhi4)DEFUN __udivmodhi4sub r_remL,r_remLsub r_remH,r_remH ; clear remainder and carryldi r_cnt,17 ; init loop counterrjmp __udivmodhi4_ep ; jump to entry point__udivmodhi4_loop:rol r_remL ; shift dividend into remainderrol r_remHcp r_remL,r_arg2L ; compare remainder & divisorcpc r_remH,r_arg2Hbrcs __udivmodhi4_ep ; remainder < divisorsub r_remL,r_arg2L ; restore remaindersbc r_remH,r_arg2H__udivmodhi4_ep:rol r_arg1L ; shift dividend (with CARRY)rol r_arg1Hdec r_cnt ; decrement loop counterbrne __udivmodhi4_loopcom r_arg1Lcom r_arg1H; div/mod results to return registers, as for the div() functionmov_l r_arg2L, r_arg1L ; quotientmov_h r_arg2H, r_arg1Hmov_l r_arg1L, r_remL ; remaindermov_h r_arg1H, r_remHretENDF __udivmodhi4#endif /* defined (L_udivmodhi4) */#if defined (L_divmodhi4)DEFUN __divmodhi4.global _div_div:bst r_arg1H,7 ; store sign of dividendmov __tmp_reg__,r_arg2Hbrtc 0fcom __tmp_reg__ ; r0.7 is sign of resultrcall __divmodhi4_neg1 ; dividend negative: negate0:sbrc r_arg2H,7rcall __divmodhi4_neg2 ; divisor negative: negateXCALL __udivmodhi4 ; do the unsigned div/modsbrc __tmp_reg__,7rcall __divmodhi4_neg2 ; correct remainder signbrtc __divmodhi4_exit__divmodhi4_neg1:;; correct dividend/remainder signcom r_arg1Hneg r_arg1Lsbci r_arg1H,0xffret__divmodhi4_neg2:;; correct divisor/result signcom r_arg2Hneg r_arg2Lsbci r_arg2H,0xff__divmodhi4_exit:retENDF __divmodhi4#endif /* defined (L_divmodhi4) */#undef r_remH#undef r_remL#undef r_arg1H#undef r_arg1L#undef r_arg2H#undef r_arg2L#undef r_cnt/*******************************************************Division 24 / 24 => (result + remainder)*******************************************************/;; A[0..2]: In: Dividend; Out: Quotient#define A0 22#define A1 A0+1#define A2 A0+2;; B[0..2]: In: Divisor; Out: Remainder#define B0 18#define B1 B0+1#define B2 B0+2;; C[0..2]: Expand remainder#define C0 __zero_reg__#define C1 26#define C2 25;; Loop counter#define r_cnt 21#if defined (L_udivmodpsi4);; R24:R22 = R24:R22 udiv R20:R18;; R20:R18 = R24:R22 umod R20:R18;; Clobbers: R21, R25, R26DEFUN __udivmodpsi4; init loop counterldi r_cnt, 24+1; Clear remainder and carry. C0 is already 0clr C1sub C2, C2; jump to entry pointrjmp __udivmodpsi4_start__udivmodpsi4_loop:; shift dividend into remainderrol C0rol C1rol C2; compare remainder & divisorcp C0, B0cpc C1, B1cpc C2, B2brcs __udivmodpsi4_start ; remainder <= divisorsub C0, B0 ; restore remaindersbc C1, B1sbc C2, B2__udivmodpsi4_start:; shift dividend (with CARRY)rol A0rol A1rol A2; decrement loop counterdec r_cntbrne __udivmodpsi4_loopcom A0com A1com A2; div/mod results to return registers; remaindermov B0, C0mov B1, C1mov B2, C2clr __zero_reg__ ; C0retENDF __udivmodpsi4#endif /* defined (L_udivmodpsi4) */#if defined (L_divmodpsi4);; R24:R22 = R24:R22 div R20:R18;; R20:R18 = R24:R22 mod R20:R18;; Clobbers: T, __tmp_reg__, R21, R25, R26DEFUN __divmodpsi4; R0.7 will contain the sign of the result:; R0.7 = A.sign ^ B.signmov __tmp_reg__, B2; T-flag = sign of dividendbst A2, 7brtc 0fcom __tmp_reg__; Adjust dividend's signrcall __divmodpsi4_negA0:; Adjust divisor's signsbrc B2, 7rcall __divmodpsi4_negB; Do the unsigned div/modXCALL __udivmodpsi4; Adjust quotient's signsbrc __tmp_reg__, 7rcall __divmodpsi4_negA; Adjust remainder's signbrtc __divmodpsi4_end__divmodpsi4_negB:; Correct divisor/remainder signcom B2com B1neg B0sbci B1, -1sbci B2, -1ret; Correct dividend/quotient sign__divmodpsi4_negA:com A2com A1neg A0sbci A1, -1sbci A2, -1__divmodpsi4_end:retENDF __divmodpsi4#endif /* defined (L_divmodpsi4) */#undef A0#undef A1#undef A2#undef B0#undef B1#undef B2#undef C0#undef C1#undef C2#undef r_cnt/*******************************************************Division 32 / 32 => (result + remainder)*******************************************************/#define r_remHH r31 /* remainder High */#define r_remHL r30#define r_remH r27#define r_remL r26 /* remainder Low *//* return: remainder */#define r_arg1HH r25 /* dividend High */#define r_arg1HL r24#define r_arg1H r23#define r_arg1L r22 /* dividend Low *//* return: quotient */#define r_arg2HH r21 /* divisor High */#define r_arg2HL r20#define r_arg2H r19#define r_arg2L r18 /* divisor Low */#define r_cnt __zero_reg__ /* loop count (0 after the loop!) */#if defined (L_udivmodsi4)DEFUN __udivmodsi4ldi r_remL, 33 ; init loop countermov r_cnt, r_remLsub r_remL,r_remLsub r_remH,r_remH ; clear remainder and carrymov_l r_remHL, r_remLmov_h r_remHH, r_remHrjmp __udivmodsi4_ep ; jump to entry point__udivmodsi4_loop:rol r_remL ; shift dividend into remainderrol r_remHrol r_remHLrol r_remHHcp r_remL,r_arg2L ; compare remainder & divisorcpc r_remH,r_arg2Hcpc r_remHL,r_arg2HLcpc r_remHH,r_arg2HHbrcs __udivmodsi4_ep ; remainder <= divisorsub r_remL,r_arg2L ; restore remaindersbc r_remH,r_arg2Hsbc r_remHL,r_arg2HLsbc r_remHH,r_arg2HH__udivmodsi4_ep:rol r_arg1L ; shift dividend (with CARRY)rol r_arg1Hrol r_arg1HLrol r_arg1HHdec r_cnt ; decrement loop counterbrne __udivmodsi4_loop; __zero_reg__ now restored (r_cnt == 0)com r_arg1Lcom r_arg1Hcom r_arg1HLcom r_arg1HH; div/mod results to return registers, as for the ldiv() functionmov_l r_arg2L, r_arg1L ; quotientmov_h r_arg2H, r_arg1Hmov_l r_arg2HL, r_arg1HLmov_h r_arg2HH, r_arg1HHmov_l r_arg1L, r_remL ; remaindermov_h r_arg1H, r_remHmov_l r_arg1HL, r_remHLmov_h r_arg1HH, r_remHHretENDF __udivmodsi4#endif /* defined (L_udivmodsi4) */#if defined (L_divmodsi4)DEFUN __divmodsi4mov __tmp_reg__,r_arg2HHbst r_arg1HH,7 ; store sign of dividendbrtc 0fcom __tmp_reg__ ; r0.7 is sign of resultrcall __divmodsi4_neg1 ; dividend negative: negate0:sbrc r_arg2HH,7rcall __divmodsi4_neg2 ; divisor negative: negateXCALL __udivmodsi4 ; do the unsigned div/modsbrc __tmp_reg__, 7 ; correct quotient signrcall __divmodsi4_neg2brtc __divmodsi4_exit ; correct remainder sign__divmodsi4_neg1:;; correct dividend/remainder signcom r_arg1HHcom r_arg1HLcom r_arg1Hneg r_arg1Lsbci r_arg1H, 0xffsbci r_arg1HL,0xffsbci r_arg1HH,0xffret__divmodsi4_neg2:;; correct divisor/quotient signcom r_arg2HHcom r_arg2HLcom r_arg2Hneg r_arg2Lsbci r_arg2H,0xffsbci r_arg2HL,0xffsbci r_arg2HH,0xff__divmodsi4_exit:retENDF __divmodsi4#endif /* defined (L_divmodsi4) *//*******************************************************Division 64 / 64Modulo 64 % 64*******************************************************/;; Use Speed-optimized Version on "big" Devices, i.e. Devices with;; at least 16k of Program Memory. For smaller Devices, depend;; on MOVW and SP Size. There is a Connexion between SP Size and;; Flash Size so that SP Size can be used to test for Flash Size.#if defined (__AVR_HAVE_JMP_CALL__)# define SPEED_DIV 8#elif defined (__AVR_HAVE_MOVW__) && !defined (__AVR_HAVE_8BIT_SP__)# define SPEED_DIV 16#else# define SPEED_DIV 0#endif;; A[0..7]: In: Dividend;;; Out: Quotient (T = 0);; Out: Remainder (T = 1)#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: Divisor; Out: Clobber#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;; C[0..7]: Expand remainder; Out: Remainder (unused)#define C0 8#define C1 C0+1#define C2 30#define C3 C2+1#define C4 28#define C5 C4+1#define C6 26#define C7 C6+1;; Holds Signs during Division Routine#define SS __tmp_reg__;; Bit-Counter in Division Routine#define R_cnt __zero_reg__;; Scratch Register for Negation#define NN r31#if defined (L_udivdi3);; R25:R18 = R24:R18 umod R17:R10;; Ordinary ABI-FunctionDEFUN __umoddi3setrjmp __udivdi3_umoddi3ENDF __umoddi3;; R25:R18 = R24:R18 udiv R17:R10;; Ordinary ABI-FunctionDEFUN __udivdi3cltENDF __udivdi3DEFUN __udivdi3_umoddi3push C0push C1push C4push C5XCALL __udivmod64pop C5pop C4pop C1pop C0retENDF __udivdi3_umoddi3#endif /* L_udivdi3 */#if defined (L_udivmod64);; Worker Routine for 64-Bit unsigned Quotient and Remainder Computation;; No Registers saved/restored; the Callers will take Care.;; Preserves B[] and T-flag;; T = 0: Compute Quotient in A[];; T = 1: Compute Remainder in A[] and shift SS one Bit leftDEFUN __udivmod64;; Clear Remainder (C6, C7 will follow)clr C0clr C1wmov C2, C0wmov C4, C0ldi C7, 64#if SPEED_DIV == 0 || SPEED_DIV == 16;; Initialize Loop-Countermov R_cnt, C7wmov C6, C0#endif /* SPEED_DIV */#if SPEED_DIV == 8push A7clr C61: ;; Compare shifted Devidend against Divisor;; If -- even after Shifting -- it is smaller...CP A7,B0 $ cpc C0,B1 $ cpc C1,B2 $ cpc C2,B3cpc C3,B4 $ cpc C4,B5 $ cpc C5,B6 $ cpc C6,B7brcc 2f;; ...then we can subtract it. Thus, it is legal to shift left$ mov C6,C5 $ mov C5,C4 $ mov C4,C3mov C3,C2 $ mov C2,C1 $ mov C1,C0 $ mov C0,A7mov A7,A6 $ mov A6,A5 $ mov A5,A4 $ mov A4,A3mov A3,A2 $ mov A2,A1 $ mov A1,A0 $ clr A0;; 8 Bits are donesubi C7, 8brne 1b;; Shifted 64 Bits: A7 has traveled to C7pop C7;; Divisor is greater than Dividend. We have:;; A[] % B[] = A[];; A[] / B[] = 0;; Thus, we can return immediatelyrjmp 5f2: ;; Initialze Bit-Counter with Number of Bits still to be performedmov R_cnt, C7;; Push of A7 is not needed because C7 is still 0pop C7clr C7#elif SPEED_DIV == 16;; Compare shifted Dividend against Divisorcp A7, B3cpc C0, B4cpc C1, B5cpc C2, B6cpc C3, B7brcc 2f;; Divisor is greater than shifted Dividen: We can shift the Dividend;; and it is still smaller than the Divisor --> Shift one 32-Bit Chunkwmov C2,A6 $ wmov C0,A4wmov A6,A2 $ wmov A4,A0wmov A2,C6 $ wmov A0,C4;; Set Bit Counter to 32lsr R_cnt2:#elif SPEED_DIV#error SPEED_DIV = ?#endif /* SPEED_DIV */;; The very Division + Remainder Routine3: ;; Left-shift Dividend...lsl A0 $ rol A1 $ rol A2 $ rol A3rol A4 $ rol A5 $ rol A6 $ rol A7;; ...into Remainderrol C0 $ rol C1 $ rol C2 $ rol C3rol C4 $ rol C5 $ rol C6 $ rol C7;; Compare Remainder and DivisorCP C0,B0 $ cpc C1,B1 $ cpc C2,B2 $ cpc C3,B3cpc C4,B4 $ cpc C5,B5 $ cpc C6,B6 $ cpc C7,B7brcs 4f;; Divisor fits into Remainder: Subtract it from Remainder...SUB C0,B0 $ sbc C1,B1 $ sbc C2,B2 $ sbc C3,B3sbc C4,B4 $ sbc C5,B5 $ sbc C6,B6 $ sbc C7,B7;; ...and set according Bit in the upcoming Quotient;; The Bit will travel to its final Positionori A0, 14: ;; This Bit is donedec R_cntbrne 3b;; __zero_reg__ is 0 again;; T = 0: We are fine with the Quotient in A[];; T = 1: Copy Remainder to A[]5: brtc 6fwmov A0, C0wmov A2, C2wmov A4, C4wmov A6, C6;; Move the Sign of the Result to SS.7lsl SS6: retENDF __udivmod64#endif /* L_udivmod64 */#if defined (L_divdi3);; R25:R18 = R24:R18 mod R17:R10;; Ordinary ABI-FunctionDEFUN __moddi3setrjmp __divdi3_moddi3ENDF __moddi3;; R25:R18 = R24:R18 div R17:R10;; Ordinary ABI-FunctionDEFUN __divdi3cltENDF __divdi3DEFUN __divdi3_moddi3#if SPEED_DIVmov r31, A7or r31, B7brmi 0f;; Both Signs are 0: the following Complexitiy is not neededXJMP __udivdi3_umoddi3#endif /* SPEED_DIV */0: ;; The Prologue;; Save 12 Registers: Y, 17...8;; No Frame needed (X = 0)clr r26clr r27ldi r30, lo8(gs(1f))ldi r31, hi8(gs(1f))XJMP __prologue_saves__ + ((18 - 12) * 2)1: ;; SS.7 will contain the Sign of the Quotient (A.sign * B.sign);; SS.6 will contain the Sign of the Remainder (A.sign)mov SS, A7asr SS;; Adjust Dividend's Sign as needed#if SPEED_DIV;; Compiling for Speed we know that at least one Sign must be < 0;; Thus, if A[] >= 0 then we know B[] < 0brpl 22f#elsebrpl 21f#endif /* SPEED_DIV */XCALL __negdi2;; Adjust Divisor's Sign and SS.7 as needed21: tst B7brpl 3f22: ldi NN, 1 << 7eor SS, NNldi NN, -1com B4 $ com B5 $ com B6 $ com B7$ com B1 $ com B2 $ com B3NEG B0$ sbc B1,NN $ sbc B2,NN $ sbc B3,NNsbc B4,NN $ sbc B5,NN $ sbc B6,NN $ sbc B7,NN3: ;; Do the unsigned 64-Bit Division/Modulo (depending on T-flag)XCALL __udivmod64;; Adjust Result's Sign#ifdef __AVR_ERRATA_SKIP_JMP_CALL__tst SSbrpl 4f#elsesbrc SS, 7#endif /* __AVR_HAVE_JMP_CALL__ */XCALL __negdi24: ;; Epilogue: Restore the Z = 12 Registers and returnin r28, __SP_L__#if defined (__AVR_HAVE_8BIT_SP__)clr r29#elsein r29, __SP_H__#endif /* #SP = 8/16 */ldi r30, 12XJMP __epilogue_restores__ + ((18 - 12) * 2)ENDF __divdi3_moddi3#undef R_cnt#undef SS#undef NN#endif /* L_divdi3 */.section .text.libgcc, "ax", @progbits#define TT __tmp_reg__#if defined (L_adddi3);; (set (reg:DI 18);; (plus:DI (reg:DI 18);; (reg:DI 10)))DEFUN __adddi3ADD A0,B0 $ adc A1,B1 $ adc A2,B2 $ adc A3,B3adc A4,B4 $ adc A5,B5 $ adc A6,B6 $ adc A7,B7retENDF __adddi3#endif /* L_adddi3 */#if defined (L_adddi3_s8);; (set (reg:DI 18);; (plus:DI (reg:DI 18);; (sign_extend:SI (reg:QI 26))))DEFUN __adddi3_s8clr TTsbrc r26, 7com TTADD A0,r26 $ adc A1,TT $ adc A2,TT $ adc A3,TTadc A4,TT $ adc A5,TT $ adc A6,TT $ adc A7,TTretENDF __adddi3_s8#endif /* L_adddi3_s8 */#if defined (L_subdi3);; (set (reg:DI 18);; (minus:DI (reg:DI 18);; (reg:DI 10)))DEFUN __subdi3SUB A0,B0 $ sbc A1,B1 $ sbc A2,B2 $ sbc A3,B3sbc A4,B4 $ sbc A5,B5 $ sbc A6,B6 $ sbc A7,B7retENDF __subdi3#endif /* L_subdi3 */#if defined (L_cmpdi2);; (set (cc0);; (compare (reg:DI 18);; (reg:DI 10)))DEFUN __cmpdi2CP A0,B0 $ cpc A1,B1 $ cpc A2,B2 $ cpc A3,B3cpc A4,B4 $ cpc A5,B5 $ cpc A6,B6 $ cpc A7,B7retENDF __cmpdi2#endif /* L_cmpdi2 */#if defined (L_cmpdi2_s8);; (set (cc0);; (compare (reg:DI 18);; (sign_extend:SI (reg:QI 26))))DEFUN __cmpdi2_s8clr TTsbrc r26, 7com TTCP A0,r26 $ cpc A1,TT $ cpc A2,TT $ cpc A3,TTcpc A4,TT $ cpc A5,TT $ cpc A6,TT $ cpc A7,TTretENDF __cmpdi2_s8#endif /* L_cmpdi2_s8 */#if defined (L_negdi2)DEFUN __negdi2com A4 $ com A5 $ com A6 $ com A7$ com A1 $ com A2 $ com A3NEG A0$ sbci A1,-1 $ sbci A2,-1 $ sbci A3,-1sbci A4,-1 $ sbci A5,-1 $ sbci A6,-1 $ sbci A7,-1retENDF __negdi2#endif /* L_negdi2 */#undef TT#undef C7#undef C6#undef C5#undef C4#undef C3#undef C2#undef C1#undef C0#undef B7#undef B6#undef B5#undef B4#undef B3#undef B2#undef B1#undef B0#undef A7#undef A6#undef A5#undef A4#undef A3#undef A2#undef A1#undef A0.section .text.libgcc.prologue, "ax", @progbits/*********************************** This is a prologue subroutine**********************************/#if defined (L_prologue);; This function does not clobber T-flag; 64-bit division relies on itDEFUN __prologue_saves__push r2push r3push r4push r5push r6push r7push r8push r9push r10push r11push r12push r13push r14push r15push r16push r17push r28push r29#if defined (__AVR_HAVE_8BIT_SP__)in r28,__SP_L__sub r28,r26out __SP_L__,r28clr r29#elif defined (__AVR_XMEGA__)in r28,__SP_L__in r29,__SP_H__sub r28,r26sbc r29,r27out __SP_L__,r28out __SP_H__,r29#elsein r28,__SP_L__in r29,__SP_H__sub r28,r26sbc r29,r27in __tmp_reg__,__SREG__cliout __SP_H__,r29out __SREG__,__tmp_reg__out __SP_L__,r28#endif /* #SP = 8/16 */#if defined (__AVR_HAVE_EIJMP_EICALL__)eijmp#elseijmp#endifENDF __prologue_saves__#endif /* defined (L_prologue) *//** This is an epilogue subroutine*/#if defined (L_epilogue)DEFUN __epilogue_restores__ldd r2,Y+18ldd r3,Y+17ldd r4,Y+16ldd r5,Y+15ldd r6,Y+14ldd r7,Y+13ldd r8,Y+12ldd r9,Y+11ldd r10,Y+10ldd r11,Y+9ldd r12,Y+8ldd r13,Y+7ldd r14,Y+6ldd r15,Y+5ldd r16,Y+4ldd r17,Y+3ldd r26,Y+2#if defined (__AVR_HAVE_8BIT_SP__)ldd r29,Y+1add r28,r30out __SP_L__,r28mov r28, r26#elif defined (__AVR_XMEGA__)ldd r27,Y+1add r28,r30adc r29,__zero_reg__out __SP_L__,r28out __SP_H__,r29wmov 28, 26#elseldd r27,Y+1add r28,r30adc r29,__zero_reg__in __tmp_reg__,__SREG__cliout __SP_H__,r29out __SREG__,__tmp_reg__out __SP_L__,r28mov_l r28, r26mov_h r29, r27#endif /* #SP = 8/16 */retENDF __epilogue_restores__#endif /* defined (L_epilogue) */#ifdef L_exit.section .fini9,"ax",@progbitsDEFUN _exit.weak exitexit:ENDF _exit/* Code from .fini8 ... .fini1 sections inserted by ld script. */.section .fini0,"ax",@progbitscli__stop_program:rjmp __stop_program#endif /* defined (L_exit) */#ifdef L_cleanup.weak _cleanup.func _cleanup_cleanup:ret.endfunc#endif /* defined (L_cleanup) */.section .text.libgcc, "ax", @progbits#ifdef L_tablejumpDEFUN __tablejump2__lsl r30rol r31;; FALLTHRUENDF __tablejump2__DEFUN __tablejump__#if defined (__AVR_HAVE_LPMX__)lpm __tmp_reg__, Z+lpm r31, Zmov r30, __tmp_reg__#if defined (__AVR_HAVE_EIJMP_EICALL__)eijmp#elseijmp#endif#else /* !HAVE_LPMX */lpmadiw r30, 1push r0lpmpush r0#if defined (__AVR_HAVE_EIJMP_EICALL__)in __tmp_reg__, __EIND__push __tmp_reg__#endifret#endif /* !HAVE_LPMX */ENDF __tablejump__#endif /* defined (L_tablejump) */#ifdef L_copy_data.section .init4,"ax",@progbitsDEFUN __do_copy_data#if defined(__AVR_HAVE_ELPMX__)ldi r17, hi8(__data_end)ldi r26, lo8(__data_start)ldi r27, hi8(__data_start)ldi r30, lo8(__data_load_start)ldi r31, hi8(__data_load_start)ldi r16, hh8(__data_load_start)out __RAMPZ__, r16rjmp .L__do_copy_data_start.L__do_copy_data_loop:elpm r0, Z+st X+, r0.L__do_copy_data_start:cpi r26, lo8(__data_end)cpc r27, r17brne .L__do_copy_data_loop#elif !defined(__AVR_HAVE_ELPMX__) && defined(__AVR_HAVE_ELPM__)ldi r17, hi8(__data_end)ldi r26, lo8(__data_start)ldi r27, hi8(__data_start)ldi r30, lo8(__data_load_start)ldi r31, hi8(__data_load_start)ldi r16, hh8(__data_load_start - 0x10000).L__do_copy_data_carry:inc r16out __RAMPZ__, r16rjmp .L__do_copy_data_start.L__do_copy_data_loop:elpmst X+, r0adiw r30, 1brcs .L__do_copy_data_carry.L__do_copy_data_start:cpi r26, lo8(__data_end)cpc r27, r17brne .L__do_copy_data_loop#elif !defined(__AVR_HAVE_ELPMX__) && !defined(__AVR_HAVE_ELPM__)ldi r17, hi8(__data_end)ldi r26, lo8(__data_start)ldi r27, hi8(__data_start)ldi r30, lo8(__data_load_start)ldi r31, hi8(__data_load_start)rjmp .L__do_copy_data_start.L__do_copy_data_loop:#if defined (__AVR_HAVE_LPMX__)lpm r0, Z+#elselpmadiw r30, 1#endifst X+, r0.L__do_copy_data_start:cpi r26, lo8(__data_end)cpc r27, r17brne .L__do_copy_data_loop#endif /* !defined(__AVR_HAVE_ELPMX__) && !defined(__AVR_HAVE_ELPM__) */ENDF __do_copy_data#endif /* L_copy_data *//* __do_clear_bss is only necessary if there is anything in .bss section. */#ifdef L_clear_bss.section .init4,"ax",@progbitsDEFUN __do_clear_bssldi r17, hi8(__bss_end)ldi r26, lo8(__bss_start)ldi r27, hi8(__bss_start)rjmp .do_clear_bss_start.do_clear_bss_loop:st X+, __zero_reg__.do_clear_bss_start:cpi r26, lo8(__bss_end)cpc r27, r17brne .do_clear_bss_loopENDF __do_clear_bss#endif /* L_clear_bss *//* __do_global_ctors and __do_global_dtors are only necessaryif there are any constructors/destructors. */#ifdef L_ctors.section .init6,"ax",@progbitsDEFUN __do_global_ctors#if defined(__AVR_HAVE_RAMPZ__)ldi r17, hi8(__ctors_start)ldi r28, lo8(__ctors_end)ldi r29, hi8(__ctors_end)ldi r16, hh8(__ctors_end)rjmp .L__do_global_ctors_start.L__do_global_ctors_loop:sbiw r28, 2sbc r16, __zero_reg__mov_h r31, r29mov_l r30, r28out __RAMPZ__, r16XCALL __tablejump_elpm__.L__do_global_ctors_start:cpi r28, lo8(__ctors_start)cpc r29, r17ldi r24, hh8(__ctors_start)cpc r16, r24brne .L__do_global_ctors_loop#elseldi r17, hi8(__ctors_start)ldi r28, lo8(__ctors_end)ldi r29, hi8(__ctors_end)rjmp .L__do_global_ctors_start.L__do_global_ctors_loop:sbiw r28, 2mov_h r31, r29mov_l r30, r28XCALL __tablejump__.L__do_global_ctors_start:cpi r28, lo8(__ctors_start)cpc r29, r17brne .L__do_global_ctors_loop#endif /* defined(__AVR_HAVE_RAMPZ__) */ENDF __do_global_ctors#endif /* L_ctors */#ifdef L_dtors.section .fini6,"ax",@progbitsDEFUN __do_global_dtors#if defined(__AVR_HAVE_RAMPZ__)ldi r17, hi8(__dtors_end)ldi r28, lo8(__dtors_start)ldi r29, hi8(__dtors_start)ldi r16, hh8(__dtors_start)rjmp .L__do_global_dtors_start.L__do_global_dtors_loop:sbiw r28, 2sbc r16, __zero_reg__mov_h r31, r29mov_l r30, r28out __RAMPZ__, r16XCALL __tablejump_elpm__.L__do_global_dtors_start:cpi r28, lo8(__dtors_end)cpc r29, r17ldi r24, hh8(__dtors_end)cpc r16, r24brne .L__do_global_dtors_loop#elseldi r17, hi8(__dtors_end)ldi r28, lo8(__dtors_start)ldi r29, hi8(__dtors_start)rjmp .L__do_global_dtors_start.L__do_global_dtors_loop:mov_h r31, r29mov_l r30, r28XCALL __tablejump__adiw r28, 2.L__do_global_dtors_start:cpi r28, lo8(__dtors_end)cpc r29, r17brne .L__do_global_dtors_loop#endif /* defined(__AVR_HAVE_RAMPZ__) */ENDF __do_global_dtors#endif /* L_dtors */.section .text.libgcc, "ax", @progbits#ifdef L_tablejump_elpmDEFUN __tablejump_elpm__#if defined (__AVR_HAVE_ELPM__)#if defined (__AVR_HAVE_LPMX__)elpm __tmp_reg__, Z+elpm r31, Zmov r30, __tmp_reg__#if defined (__AVR_HAVE_EIJMP_EICALL__)eijmp#elseijmp#endif#elseelpmadiw r30, 1push r0elpmpush r0#if defined (__AVR_HAVE_EIJMP_EICALL__)in __tmp_reg__, __EIND__push __tmp_reg__#endifret#endif#endif /* defined (__AVR_HAVE_ELPM__) */ENDF __tablejump_elpm__#endif /* defined (L_tablejump_elpm) */;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Loading n bytes from Flash; n = 3,4;; R22... = Flash[Z];; Clobbers: __tmp_reg__#if (defined (L_load_3) \|| defined (L_load_4)) \&& !defined (__AVR_HAVE_LPMX__);; Destination#define D0 22#define D1 D0+1#define D2 D0+2#define D3 D0+3.macro .load dest, nlpmmov \dest, r0.if \dest != D0+\n-1adiw r30, 1.elsesbiw r30, \n-1.endif.endm#if defined (L_load_3)DEFUN __load_3push D3XCALL __load_4pop D3retENDF __load_3#endif /* L_load_3 */#if defined (L_load_4)DEFUN __load_4.load D0, 4.load D1, 4.load D2, 4.load D3, 4retENDF __load_4#endif /* L_load_4 */#endif /* L_load_3 || L_load_3 */;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Loading n bytes from Flash or RAM; n = 1,2,3,4;; R22... = Flash[R21:Z] or RAM[Z] depending on R21.7;; Clobbers: __tmp_reg__, R21, R30, R31#if (defined (L_xload_1) \|| defined (L_xload_2) \|| defined (L_xload_3) \|| defined (L_xload_4));; Destination#define D0 22#define D1 D0+1#define D2 D0+2#define D3 D0+3;; Register containing bits 16+ of the address#define HHI8 21.macro .xload dest, n#if defined (__AVR_HAVE_ELPMX__)elpm \dest, Z+#elif defined (__AVR_HAVE_ELPM__)elpmmov \dest, r0.if \dest != D0+\n-1adiw r30, 1adc HHI8, __zero_reg__out __RAMPZ__, HHI8.endif#elif defined (__AVR_HAVE_LPMX__)lpm \dest, Z+#elselpmmov \dest, r0.if \dest != D0+\n-1adiw r30, 1.endif#endif.endm ; .xload#if defined (L_xload_1)DEFUN __xload_1#if defined (__AVR_HAVE_LPMX__) && !defined (__AVR_HAVE_RAMPZ__)ld D0, Zsbrs HHI8, 7lpm D0, Zret#elsesbrc HHI8, 7rjmp 1f#if defined (__AVR_HAVE_RAMPZ__)out __RAMPZ__, HHI8#endif /* __AVR_HAVE_RAMPZ__ */.xload D0, 1ret1: ld D0, Zret#endif /* LPMx && ! RAMPZ */ENDF __xload_1#endif /* L_xload_1 */#if defined (L_xload_2)DEFUN __xload_2sbrc HHI8, 7rjmp 1f#if defined (__AVR_HAVE_RAMPZ__)out __RAMPZ__, HHI8#endif /* __AVR_HAVE_RAMPZ__ */.xload D0, 2.xload D1, 2ret1: ld D0, Z+ld D1, Z+retENDF __xload_2#endif /* L_xload_2 */#if defined (L_xload_3)DEFUN __xload_3sbrc HHI8, 7rjmp 1f#if defined (__AVR_HAVE_RAMPZ__)out __RAMPZ__, HHI8#endif /* __AVR_HAVE_RAMPZ__ */.xload D0, 3.xload D1, 3.xload D2, 3ret1: ld D0, Z+ld D1, Z+ld D2, Z+retENDF __xload_3#endif /* L_xload_3 */#if defined (L_xload_4)DEFUN __xload_4sbrc HHI8, 7rjmp 1f#if defined (__AVR_HAVE_RAMPZ__)out __RAMPZ__, HHI8#endif /* __AVR_HAVE_RAMPZ__ */.xload D0, 4.xload D1, 4.xload D2, 4.xload D3, 4ret1: ld D0, Z+ld D1, Z+ld D2, Z+ld D3, Z+retENDF __xload_4#endif /* L_xload_4 */#endif /* L_xload_{1|2|3|4} */;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; memcopy from Address Space __pgmx to RAM;; R23:Z = Source Address;; X = Destination Address;; Clobbers: __tmp_reg__, R23, R24, R25, X, Z#if defined (L_movmemx)#define HHI8 23#define LOOP 24DEFUN __movmemx_qi;; #Bytes to copy fity in 8 Bits (1..255);; Zero-extend Loop Counterclr LOOP+1;; FALLTHRUENDF __movmemx_qiDEFUN __movmemx_hi;; Read from where?sbrc HHI8, 7rjmp 1f;; Read from Flash#if defined (__AVR_HAVE_RAMPZ__)out __RAMPZ__, HHI8#endif0: ;; Load 1 Byte from Flash...#if defined (__AVR_HAVE_ELPMX__)elpm r0, Z+#elif defined (__AVR_HAVE_ELPM__)elpmadiw r30, 1adc HHI8, __zero_reg__out __RAMPZ__, HHI8#elif defined (__AVR_HAVE_LPMX__)lpm r0, Z+#elselpmadiw r30, 1#endif;; ...and store that Byte to RAM Destinationst X+, r0sbiw LOOP, 1brne 0bret;; Read from RAM1: ;; Read 1 Byte from RAM...ld r0, Z+;; and store that Byte to RAM Destinationst X+, r0sbiw LOOP, 1brne 0bretENDF __movmemx_hi#undef HHI8#undef LOOP#endif /* L_movmemx */.section .text.libgcc.builtins, "ax", @progbits/*********************************** Find first set Bit (ffs)**********************************/#if defined (L_ffssi2);; find first set bit;; r25:r24 = ffs32 (r25:r22);; clobbers: r22, r26DEFUN __ffssi2clr r26tst r22brne 1fsubi r26, -8or r22, r23brne 1fsubi r26, -8or r22, r24brne 1fsubi r26, -8or r22, r25brne 1fret1: mov r24, r22XJMP __loop_ffsqi2ENDF __ffssi2#endif /* defined (L_ffssi2) */#if defined (L_ffshi2);; find first set bit;; r25:r24 = ffs16 (r25:r24);; clobbers: r26DEFUN __ffshi2clr r26#ifdef __AVR_ERRATA_SKIP_JMP_CALL__;; Some cores have problem skipping 2-word instructiontst r24breq 2f#elsecpse r24, __zero_reg__#endif /* __AVR_HAVE_JMP_CALL__ */1: XJMP __loop_ffsqi22: ldi r26, 8or r24, r25brne 1bretENDF __ffshi2#endif /* defined (L_ffshi2) */#if defined (L_loop_ffsqi2);; Helper for ffshi2, ffssi2;; r25:r24 = r26 + zero_extend16 (ffs8(r24));; r24 must be != 0;; clobbers: r26DEFUN __loop_ffsqi2inc r26lsr r24brcc __loop_ffsqi2mov r24, r26clr r25retENDF __loop_ffsqi2#endif /* defined (L_loop_ffsqi2) *//*********************************** Count trailing Zeros (ctz)**********************************/#if defined (L_ctzsi2);; count trailing zeros;; r25:r24 = ctz32 (r25:r22);; clobbers: r26, r22;; ctz(0) = 255;; Note that ctz(0) in undefined for GCCDEFUN __ctzsi2XCALL __ffssi2dec r24retENDF __ctzsi2#endif /* defined (L_ctzsi2) */#if defined (L_ctzhi2);; count trailing zeros;; r25:r24 = ctz16 (r25:r24);; clobbers: r26;; ctz(0) = 255;; Note that ctz(0) in undefined for GCCDEFUN __ctzhi2XCALL __ffshi2dec r24retENDF __ctzhi2#endif /* defined (L_ctzhi2) *//*********************************** Count leading Zeros (clz)**********************************/#if defined (L_clzdi2);; count leading zeros;; r25:r24 = clz64 (r25:r18);; clobbers: r22, r23, r26DEFUN __clzdi2XCALL __clzsi2sbrs r24, 5retmov_l r22, r18mov_h r23, r19mov_l r24, r20mov_h r25, r21XCALL __clzsi2subi r24, -32retENDF __clzdi2#endif /* defined (L_clzdi2) */#if defined (L_clzsi2);; count leading zeros;; r25:r24 = clz32 (r25:r22);; clobbers: r26DEFUN __clzsi2XCALL __clzhi2sbrs r24, 4retmov_l r24, r22mov_h r25, r23XCALL __clzhi2subi r24, -16retENDF __clzsi2#endif /* defined (L_clzsi2) */#if defined (L_clzhi2);; count leading zeros;; r25:r24 = clz16 (r25:r24);; clobbers: r26DEFUN __clzhi2clr r26tst r25brne 1fsubi r26, -8or r25, r24brne 1fldi r24, 16ret1: cpi r25, 16brsh 3fsubi r26, -3swap r252: inc r263: lsl r25brcc 2bmov r24, r26clr r25retENDF __clzhi2#endif /* defined (L_clzhi2) *//*********************************** Parity**********************************/#if defined (L_paritydi2);; r25:r24 = parity64 (r25:r18);; clobbers: __tmp_reg__DEFUN __paritydi2eor r24, r18eor r24, r19eor r24, r20eor r24, r21XJMP __paritysi2ENDF __paritydi2#endif /* defined (L_paritydi2) */#if defined (L_paritysi2);; r25:r24 = parity32 (r25:r22);; clobbers: __tmp_reg__DEFUN __paritysi2eor r24, r22eor r24, r23XJMP __parityhi2ENDF __paritysi2#endif /* defined (L_paritysi2) */#if defined (L_parityhi2);; r25:r24 = parity16 (r25:r24);; clobbers: __tmp_reg__DEFUN __parityhi2eor r24, r25;; FALLTHRUENDF __parityhi2;; r25:r24 = parity8 (r24);; clobbers: __tmp_reg__DEFUN __parityqi2;; parity is in r24[0..7]mov __tmp_reg__, r24swap __tmp_reg__eor r24, __tmp_reg__;; parity is in r24[0..3]subi r24, -4andi r24, -5subi r24, -6;; parity is in r24[0,3]sbrc r24, 3inc r24;; parity is in r24[0]andi r24, 1clr r25retENDF __parityqi2#endif /* defined (L_parityhi2) *//*********************************** Population Count**********************************/#if defined (L_popcounthi2);; population count;; r25:r24 = popcount16 (r25:r24);; clobbers: __tmp_reg__DEFUN __popcounthi2XCALL __popcountqi2push r24mov r24, r25XCALL __popcountqi2clr r25;; FALLTHRUENDF __popcounthi2DEFUN __popcounthi2_tailpop __tmp_reg__add r24, __tmp_reg__retENDF __popcounthi2_tail#endif /* defined (L_popcounthi2) */#if defined (L_popcountsi2);; population count;; r25:r24 = popcount32 (r25:r22);; clobbers: __tmp_reg__DEFUN __popcountsi2XCALL __popcounthi2push r24mov_l r24, r22mov_h r25, r23XCALL __popcounthi2XJMP __popcounthi2_tailENDF __popcountsi2#endif /* defined (L_popcountsi2) */#if defined (L_popcountdi2);; population count;; r25:r24 = popcount64 (r25:r18);; clobbers: r22, r23, __tmp_reg__DEFUN __popcountdi2XCALL __popcountsi2push r24mov_l r22, r18mov_h r23, r19mov_l r24, r20mov_h r25, r21XCALL __popcountsi2XJMP __popcounthi2_tailENDF __popcountdi2#endif /* defined (L_popcountdi2) */#if defined (L_popcountqi2);; population count;; r24 = popcount8 (r24);; clobbers: __tmp_reg__DEFUN __popcountqi2mov __tmp_reg__, r24andi r24, 1lsr __tmp_reg__lsr __tmp_reg__adc r24, __zero_reg__lsr __tmp_reg__adc r24, __zero_reg__lsr __tmp_reg__adc r24, __zero_reg__lsr __tmp_reg__adc r24, __zero_reg__lsr __tmp_reg__adc r24, __zero_reg__lsr __tmp_reg__adc r24, __tmp_reg__retENDF __popcountqi2#endif /* defined (L_popcountqi2) *//*********************************** Swap bytes**********************************/;; swap two registers with different register number.macro bswap a, beor \a, \beor \b, \aeor \a, \b.endm#if defined (L_bswapsi2);; swap bytes;; r25:r22 = bswap32 (r25:r22)DEFUN __bswapsi2bswap r22, r25bswap r23, r24retENDF __bswapsi2#endif /* defined (L_bswapsi2) */#if defined (L_bswapdi2);; swap bytes;; r25:r18 = bswap64 (r25:r18)DEFUN __bswapdi2bswap r18, r25bswap r19, r24bswap r20, r23bswap r21, r22retENDF __bswapdi2#endif /* defined (L_bswapdi2) *//*********************************** 64-bit shifts**********************************/#if defined (L_ashrdi3);; Arithmetic shift right;; r25:r18 = ashr64 (r25:r18, r17:r16)DEFUN __ashrdi3push r16andi r16, 63breq 2f1: asr r25ror r24ror r23ror r22ror r21ror r20ror r19ror r18dec r16brne 1b2: pop r16retENDF __ashrdi3#endif /* defined (L_ashrdi3) */#if defined (L_lshrdi3);; Logic shift right;; r25:r18 = lshr64 (r25:r18, r17:r16)DEFUN __lshrdi3push r16andi r16, 63breq 2f1: lsr r25ror r24ror r23ror r22ror r21ror r20ror r19ror r18dec r16brne 1b2: pop r16retENDF __lshrdi3#endif /* defined (L_lshrdi3) */#if defined (L_ashldi3);; Shift left;; r25:r18 = ashl64 (r25:r18, r17:r16)DEFUN __ashldi3push r16andi r16, 63breq 2f1: lsl r18rol r19rol r20rol r21rol r22rol r23rol r24rol r25dec r16brne 1b2: pop r16retENDF __ashldi3#endif /* defined (L_ashldi3) */#if defined (L_rotldi3);; Shift left;; r25:r18 = rotl64 (r25:r18, r17:r16)DEFUN __rotldi3push r16andi r16, 63breq 2f1: lsl r18rol r19rol r20rol r21rol r22rol r23rol r24rol r25adc r18, __zero_reg__dec r16brne 1b2: pop r16retENDF __rotldi3#endif /* defined (L_rotldi3) */.section .text.libgcc.fmul, "ax", @progbits/***********************************************************/;;; Softmul versions of FMUL, FMULS and FMULSU to implement;;; __builtin_avr_fmul* if !AVR_HAVE_MUL/***********************************************************/#define A1 24#define B1 25#define C0 22#define C1 23#define A0 __tmp_reg__#ifdef L_fmuls;;; r23:r22 = fmuls (r24, r25) like in FMULS instruction;;; Clobbers: r24, r25, __tmp_reg__DEFUN __fmuls;; A0.7 = negate result?mov A0, A1eor A0, B1;; B1 = |B1|sbrc B1, 7neg B1XJMP __fmulsu_exitENDF __fmuls#endif /* L_fmuls */#ifdef L_fmulsu;;; r23:r22 = fmulsu (r24, r25) like in FMULSU instruction;;; Clobbers: r24, r25, __tmp_reg__DEFUN __fmulsu;; A0.7 = negate result?mov A0, A1;; FALLTHRUENDF __fmulsu;; Helper for __fmuls and __fmulsuDEFUN __fmulsu_exit;; A1 = |A1|sbrc A1, 7neg A1#ifdef __AVR_ERRATA_SKIP_JMP_CALL__;; Some cores have problem skipping 2-word instructiontst A0brmi 1f#elsesbrs A0, 7#endif /* __AVR_HAVE_JMP_CALL__ */XJMP __fmul1: XCALL __fmul;; C = -C iff A0.7 = 1com C1neg C0sbci C1, -1retENDF __fmulsu_exit#endif /* L_fmulsu */#ifdef L_fmul;;; r22:r23 = fmul (r24, r25) like in FMUL instruction;;; Clobbers: r24, r25, __tmp_reg__DEFUN __fmul; clear resultclr C0clr C1clr A01: tst B1;; 1.0 = 0x80, so test for bit 7 of B to see if A must to be added to C.2: brpl 3f;; C += Aadd C0, A0adc C1, A13: ;; A >>= 1lsr A1ror A0;; B <<= 1lsl B1brne 2bretENDF __fmul#endif /* L_fmul */#undef A0#undef A1#undef B1#undef C0#undef C1
