1*810390e3Srobert//===------------ udivmodqi4.S - uint8 div & mod --------------------------===//
2*810390e3Srobert//
3*810390e3Srobert// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*810390e3Srobert// See https://llvm.org/LICENSE.txt for license information.
5*810390e3Srobert// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*810390e3Srobert//
7*810390e3Srobert//===----------------------------------------------------------------------===//
8*810390e3Srobert//
9*810390e3Srobert// As described at
10*810390e3Srobert// https://gcc.gnu.org/wiki/avr-gcc#Exceptions_to_the_Calling_Convention, the
11*810390e3Srobert// prototype is `struct {uint8, uint8} __udivmodqi4(uint8, uint8)`.
12*810390e3Srobert// The uint8 quotient is returned via R24, and the uint8 remainder is returned
13*810390e3Srobert// via R25, while R23 is clobbered.
14*810390e3Srobert//
15*810390e3Srobert//===----------------------------------------------------------------------===//
16*810390e3Srobert
17*810390e3Srobert	.text
18*810390e3Srobert	.align 2
19*810390e3Srobert
20*810390e3Srobert	.globl __udivmodqi4
21*810390e3Srobert	.type  __udivmodqi4, @function
22*810390e3Srobert
23*810390e3Srobert__udivmodqi4:
24*810390e3Srobert	sub     r25, r25           ; Initialize the remainder to zero.
25*810390e3Srobert	ldi     r23, 9             ; Only loop 8 rounds for uint8.
26*810390e3Srobert
27*810390e3Srobert__udivmodqi4_loop:
28*810390e3Srobert	adc     r24, r24
29*810390e3Srobert	dec     r23
30*810390e3Srobert	breq    __udivmodqi4_end
31*810390e3Srobert	adc     r25, r25
32*810390e3Srobert	cp      r25, r22           ; Compare with the divisor.
33*810390e3Srobert	brcs    __udivmodqi4_loop
34*810390e3Srobert	sub     r25, r22           ; Subtract the divisor.
35*810390e3Srobert	rjmp    __udivmodqi4_loop
36*810390e3Srobert
37*810390e3Srobert__udivmodqi4_end:
38*810390e3Srobert	com     r24                ; The uint8 quotient is returned via R24.
39*810390e3Srobert	ret                        ; The uint8 remainder is returned via R25.
40