chiark / gitweb /
[PATCH] added klibc version 0.82 (cvs tree) to the udev tree.
[elogind.git] / klibc / klibc / arch / sparc / umul.S
1 /*      $NetBSD: umul.S,v 1.3 1997/07/16 14:37:44 christos Exp $        */
2
3 /*
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * from: Header: umul.s,v 1.4 92/06/25 13:24:05 torek Exp
40  */
41
42 #include <machine/asm.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
44 #if 0
45         .asciz "@(#)umul.s      8.1 (Berkeley) 6/4/93"
46 #else
47         RCSID("$NetBSD: umul.S,v 1.3 1997/07/16 14:37:44 christos Exp $")
48 #endif
49 #endif /* LIBC_SCCS and not lint */
50
51 /*
52  * Unsigned multiply.  Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the
53  * upper 32 bits of the 64-bit product).
54  *
55  * This code optimizes short (less than 13-bit) multiplies.  Short
56  * multiplies require 25 instruction cycles, and long ones require
57  * 45 instruction cycles.
58  *
59  * On return, overflow has occurred (%o1 is not zero) if and only if
60  * the Z condition code is clear, allowing, e.g., the following:
61  *
62  *      call    .umul
63  *      nop
64  *      bnz     overflow        (or tnz)
65  */
66
67 FUNC(.umul)
68         or      %o0, %o1, %o4
69         mov     %o0, %y         ! multiplier -> Y
70         andncc  %o4, 0xfff, %g0 ! test bits 12..31 of *both* args
71         be      Lmul_shortway   ! if zero, can do it the short way
72         andcc   %g0, %g0, %o4   ! zero the partial product and clear N and V
73
74         /*
75          * Long multiply.  32 steps, followed by a final shift step.
76          */
77         mulscc  %o4, %o1, %o4   ! 1
78         mulscc  %o4, %o1, %o4   ! 2
79         mulscc  %o4, %o1, %o4   ! 3
80         mulscc  %o4, %o1, %o4   ! 4
81         mulscc  %o4, %o1, %o4   ! 5
82         mulscc  %o4, %o1, %o4   ! 6
83         mulscc  %o4, %o1, %o4   ! 7
84         mulscc  %o4, %o1, %o4   ! 8
85         mulscc  %o4, %o1, %o4   ! 9
86         mulscc  %o4, %o1, %o4   ! 10
87         mulscc  %o4, %o1, %o4   ! 11
88         mulscc  %o4, %o1, %o4   ! 12
89         mulscc  %o4, %o1, %o4   ! 13
90         mulscc  %o4, %o1, %o4   ! 14
91         mulscc  %o4, %o1, %o4   ! 15
92         mulscc  %o4, %o1, %o4   ! 16
93         mulscc  %o4, %o1, %o4   ! 17
94         mulscc  %o4, %o1, %o4   ! 18
95         mulscc  %o4, %o1, %o4   ! 19
96         mulscc  %o4, %o1, %o4   ! 20
97         mulscc  %o4, %o1, %o4   ! 21
98         mulscc  %o4, %o1, %o4   ! 22
99         mulscc  %o4, %o1, %o4   ! 23
100         mulscc  %o4, %o1, %o4   ! 24
101         mulscc  %o4, %o1, %o4   ! 25
102         mulscc  %o4, %o1, %o4   ! 26
103         mulscc  %o4, %o1, %o4   ! 27
104         mulscc  %o4, %o1, %o4   ! 28
105         mulscc  %o4, %o1, %o4   ! 29
106         mulscc  %o4, %o1, %o4   ! 30
107         mulscc  %o4, %o1, %o4   ! 31
108         mulscc  %o4, %o1, %o4   ! 32
109         mulscc  %o4, %g0, %o4   ! final shift
110
111
112         /*
113          * Normally, with the shift-and-add approach, if both numbers are
114          * positive you get the correct result.  WIth 32-bit two's-complement
115          * numbers, -x is represented as
116          *
117          *                x                 32
118          *      ( 2  -  ------ ) mod 2  *  2
119          *                 32
120          *                2
121          *
122          * (the `mod 2' subtracts 1 from 1.bbbb).  To avoid lots of 2^32s,
123          * we can treat this as if the radix point were just to the left
124          * of the sign bit (multiply by 2^32), and get
125          *
126          *      -x  =  (2 - x) mod 2
127          *
128          * Then, ignoring the `mod 2's for convenience:
129          *
130          *   x *  y     = xy
131          *  -x *  y     = 2y - xy
132          *   x * -y     = 2x - xy
133          *  -x * -y     = 4 - 2x - 2y + xy
134          *
135          * For signed multiplies, we subtract (x << 32) from the partial
136          * product to fix this problem for negative multipliers (see mul.s).
137          * Because of the way the shift into the partial product is calculated
138          * (N xor V), this term is automatically removed for the multiplicand,
139          * so we don't have to adjust.
140          *
141          * But for unsigned multiplies, the high order bit wasn't a sign bit,
142          * and the correction is wrong.  So for unsigned multiplies where the
143          * high order bit is one, we end up with xy - (y << 32).  To fix it
144          * we add y << 32.
145          */
146         tst     %o1
147         bl,a    1f              ! if %o1 < 0 (high order bit = 1),
148         add     %o4, %o0, %o4   ! %o4 += %o0 (add y to upper half)
149 1:      rd      %y, %o0         ! get lower half of product
150         retl
151         addcc   %o4, %g0, %o1   ! put upper half in place and set Z for %o1==0
152
153 Lmul_shortway:
154         /*
155          * Short multiply.  12 steps, followed by a final shift step.
156          * The resulting bits are off by 12 and (32-12) = 20 bit positions,
157          * but there is no problem with %o0 being negative (unlike above),
158          * and overflow is impossible (the answer is at most 24 bits long).
159          */
160         mulscc  %o4, %o1, %o4   ! 1
161         mulscc  %o4, %o1, %o4   ! 2
162         mulscc  %o4, %o1, %o4   ! 3
163         mulscc  %o4, %o1, %o4   ! 4
164         mulscc  %o4, %o1, %o4   ! 5
165         mulscc  %o4, %o1, %o4   ! 6
166         mulscc  %o4, %o1, %o4   ! 7
167         mulscc  %o4, %o1, %o4   ! 8
168         mulscc  %o4, %o1, %o4   ! 9
169         mulscc  %o4, %o1, %o4   ! 10
170         mulscc  %o4, %o1, %o4   ! 11
171         mulscc  %o4, %o1, %o4   ! 12
172         mulscc  %o4, %g0, %o4   ! final shift
173
174         /*
175          * %o4 has 20 of the bits that should be in the result; %y has
176          * the bottom 12 (as %y's top 12).  That is:
177          *
178          *        %o4               %y
179          * +----------------+----------------+
180          * | -12- |   -20-  | -12- |   -20-  |
181          * +------(---------+------)---------+
182          *         -----result-----
183          *
184          * The 12 bits of %o4 left of the `result' area are all zero;
185          * in fact, all top 20 bits of %o4 are zero.
186          */
187
188         rd      %y, %o5
189         sll     %o4, 12, %o0    ! shift middle bits left 12
190         srl     %o5, 20, %o5    ! shift low bits right 20
191         or      %o5, %o0, %o0
192         retl
193         addcc   %g0, %g0, %o1   ! %o1 = zero, and set Z