1 //===------------ udivmodhi4.S - uint16 div & mod -------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 // https://gcc.gnu.org/wiki/avr-gcc#Exceptions_to_the_Calling_Convention, the
11 // prototype is `struct {uint16, uint16} __udivmodhi4(uint16, uint16)`.
12 // The uint16 quotient is returned via R23:R22, and the uint16 remainder is
13 // returned via R25:R24, while R21/R26/R27 are clobbered.
15 //===----------------------------------------------------------------------===//
21 .type __udivmodhi4, @function
25 sub r27, r27 ; Initialize the remainder to zero.
26 ldi r21, 17 ; Only loop 16 rounds for uint16.
36 cpc r27, r23 ; Compare with the divisor.
37 brcs __udivmodhi4_loop
39 sbc r27, r23 ; Subtract the divisor.
40 rjmp __udivmodhi4_loop
46 mov r23, r25 ; The quotient is returned in R23:R22.
48 mov r25, r27 ; The remainder is returned in in R25:R24.