1 1.6 rin /* $NetBSD: udivsi3.S,v 1.6 2020/04/23 03:12:49 rin Exp $ */ 2 1.1 fredette 3 1.1 fredette /*- 4 1.5 rin * Copyright (c) 1990 The Regents of the University of California. 5 1.1 fredette * All rights reserved. 6 1.1 fredette * 7 1.5 rin * This code is derived from software contributed to Berkeley by 8 1.5 rin * the Systems Programming Group of the University of Utah Computer 9 1.5 rin * Science Department. 10 1.1 fredette * 11 1.1 fredette * Redistribution and use in source and binary forms, with or without 12 1.1 fredette * modification, are permitted provided that the following conditions 13 1.1 fredette * are met: 14 1.1 fredette * 1. Redistributions of source code must retain the above copyright 15 1.1 fredette * notice, this list of conditions and the following disclaimer. 16 1.1 fredette * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 fredette * notice, this list of conditions and the following disclaimer in the 18 1.1 fredette * documentation and/or other materials provided with the distribution. 19 1.5 rin * 3. Neither the name of the University nor the names of its contributors 20 1.5 rin * may be used to endorse or promote products derived from this software 21 1.5 rin * without specific prior written permission. 22 1.1 fredette * 23 1.5 rin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.5 rin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.5 rin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.5 rin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.5 rin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.5 rin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.5 rin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.5 rin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.5 rin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.5 rin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.5 rin * SUCH DAMAGE. 34 1.1 fredette */ 35 1.1 fredette 36 1.1 fredette #include <machine/asm.h> 37 1.1 fredette 38 1.1 fredette #if defined(LIBC_SCCS) && !defined(lint) 39 1.5 rin #if 0 40 1.5 rin RCSID("from: @(#)udivsi3.s 5.1 (Berkeley) 6/7/90") 41 1.5 rin #else 42 1.6 rin RCSID("$NetBSD: udivsi3.S,v 1.6 2020/04/23 03:12:49 rin Exp $") 43 1.5 rin #endif 44 1.1 fredette #endif /* LIBC_SCCS and not lint */ 45 1.1 fredette 46 1.5 rin /* unsigned / unsigned */ 47 1.5 rin #ifndef __mc68010__ 48 1.6 rin /* XXX sun3 need this for bootloader */ 49 1.1 fredette ENTRY(__udivsi3) 50 1.5 rin movel 4(%sp),%d0 51 1.5 rin divul 8(%sp),%d0 52 1.5 rin rts 53 1.5 rin END(__udivsi3) 54 1.5 rin #else 55 1.5 rin ENTRY(__udivsi3) 56 1.5 rin movel %d2, -(%sp) | save %d2 57 1.5 rin movel 12(%sp), %d0 | load divisor 58 1.5 rin movel 8(%sp), %d1 | load dividend 59 1.1 fredette 60 1.5 rin | first, we divide the divisor and dividend by two until 61 1.1 fredette | the divisor fits into 16 bits: 62 1.1 fredette 1: cmpil #0x10000, %d0 63 1.1 fredette bcs 2f 64 1.1 fredette lsrl #1, %d0 65 1.1 fredette lsrl #1, %d1 66 1.1 fredette bra 1b 67 1.1 fredette 2: 68 1.1 fredette 69 1.5 rin | now we can do the divide. to avoid overflow, we have to 70 1.5 rin | do the divide in two parts, high and low, and add the 71 1.1 fredette | results together: 72 1.1 fredette movew %d1, %d2 | save low(dividend) 73 1.1 fredette clrw %d1 74 1.1 fredette swap %d1 | %d1 = dividend >> 16 75 1.1 fredette divuw %d0, %d1 | do the high divide 76 1.1 fredette moveal %d1, %a1 | save high divide result 77 1.1 fredette movew %d2, %d1 | concat(remainder, low(dividend)) 78 1.1 fredette divuw %d0, %d1 | do the low divide 79 1.1 fredette movel %a1, %d0 | recover high divide result 80 1.1 fredette swap %d0 81 1.1 fredette clrw %d0 | %d0 = finished high divide result 82 1.1 fredette andil #0xffff, %d1 | %d1 = finished low divide result 83 1.1 fredette addl %d1, %d0 | %d0 = quotient guess 84 1.1 fredette 85 1.5 rin | the quotient we have so far is only a guess. the divide we 86 1.5 rin | did above was really the divide of some dividendB by some 87 1.1 fredette | divisorB, where the following hold: 88 1.1 fredette | 89 1.1 fredette | (dividend - divisor) <= dividendB <= dividend 90 1.1 fredette | (divisor / 2) < divisorB <= divisor 91 1.1 fredette | 92 1.1 fredette | so our guess quotient cannot be less than our real desired 93 1.1 fredette | quotient. however, it might be one too big. 94 1.1 fredette | 95 1.5 rin | to adjust this quotient, we multiply it by the original 96 1.5 rin | divisor and subtract the result from the original dividend. 97 1.5 rin | if the result is nonnegative, our guessed quotient was 98 1.5 rin | correct, and the subtraction result is our remainder. 99 1.5 rin | if the result is negative, our guessed quotient was one 100 1.5 rin | too big, and the subtraction result plus the original 101 1.1 fredette | divisor is our remainder. 102 1.1 fredette | 103 1.5 rin | as in mulsi3, we have to do the multiply in stages to avoid 104 1.1 fredette | overflow: 105 1.1 fredette 106 1.5 rin movel 12(%sp), %d2 | load divisor 107 1.1 fredette swap %d2 108 1.1 fredette movel %d0, %d1 109 1.1 fredette muluw %d2, %d1 | high(divisor) * low(guess) 110 1.1 fredette moveal %d1, %a1 | save high(divisor) * low(guess) 111 1.1 fredette swap %d2 112 1.1 fredette movel %d0, %d1 113 1.1 fredette swap %d1 114 1.1 fredette muluw %d2, %d1 | low(divisor) * high(guess) 115 1.1 fredette addl %a1, %d1 116 1.1 fredette swap %d1 117 1.1 fredette clrw %d1 | %d1 = finished high multiply result 118 1.1 fredette moveal %d2, %a1 | save original divisor 119 1.1 fredette muluw %d0, %d2 | low(guess) * low(divisor) 120 1.1 fredette addl %d1, %d2 | %d2 = guess * divisor 121 1.5 rin 122 1.5 rin movel 8(%sp), %d1 | load original dividend 123 1.1 fredette subl %d2, %d1 | subtract 124 1.1 fredette bcc 3f 125 1.1 fredette subql #1, %d0 | adjust quotient 126 1.1 fredette addl %a1, %d1 | adjust remainder 127 1.5 rin 3: movel (%sp)+, %d2 | restore %d2 128 1.1 fredette rts 129 1.5 rin END(__udivsi3) 130 1.5 rin #endif /* __mc68010__ */ 131