chiark / gitweb /
[PATCH] added klibc version 0.82 (cvs tree) to the udev tree.
[elogind.git] / klibc / klibc / arch / sparc / smul.S
1 /*      $NetBSD: mul.S,v 1.3 1997/07/16 14:37:42 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: mul.s,v 1.5 92/06/25 13:24:03 torek Exp
40  */
41
42 #include <machine/asm.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
44 #if 0
45         .asciz "@(#)mul.s       8.1 (Berkeley) 6/4/93"
46 #else
47         RCSID("$NetBSD: mul.S,v 1.3 1997/07/16 14:37:42 christos Exp $")
48 #endif
49 #endif /* LIBC_SCCS and not lint */
50
51 /*
52  * Signed multiply, from Appendix E of the Sparc Version 8
53  * Architecture Manual.
54  *
55  * Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the upper 32 bits of
56  * the 64-bit product).
57  *
58  * This code optimizes short (less than 13-bit) multiplies.
59  */
60
61 FUNC(.mul)
62         mov     %o0, %y         ! multiplier -> Y
63         andncc  %o0, 0xfff, %g0 ! test bits 12..31
64         be      Lmul_shortway   ! if zero, can do it the short way
65         andcc   %g0, %g0, %o4   ! zero the partial product and clear N and V
66
67         /*
68          * Long multiply.  32 steps, followed by a final shift step.
69          */
70         mulscc  %o4, %o1, %o4   ! 1
71         mulscc  %o4, %o1, %o4   ! 2
72         mulscc  %o4, %o1, %o4   ! 3
73         mulscc  %o4, %o1, %o4   ! 4
74         mulscc  %o4, %o1, %o4   ! 5
75         mulscc  %o4, %o1, %o4   ! 6
76         mulscc  %o4, %o1, %o4   ! 7
77         mulscc  %o4, %o1, %o4   ! 8
78         mulscc  %o4, %o1, %o4   ! 9
79         mulscc  %o4, %o1, %o4   ! 10
80         mulscc  %o4, %o1, %o4   ! 11
81         mulscc  %o4, %o1, %o4   ! 12
82         mulscc  %o4, %o1, %o4   ! 13
83         mulscc  %o4, %o1, %o4   ! 14
84         mulscc  %o4, %o1, %o4   ! 15
85         mulscc  %o4, %o1, %o4   ! 16
86         mulscc  %o4, %o1, %o4   ! 17
87         mulscc  %o4, %o1, %o4   ! 18
88         mulscc  %o4, %o1, %o4   ! 19
89         mulscc  %o4, %o1, %o4   ! 20
90         mulscc  %o4, %o1, %o4   ! 21
91         mulscc  %o4, %o1, %o4   ! 22
92         mulscc  %o4, %o1, %o4   ! 23
93         mulscc  %o4, %o1, %o4   ! 24
94         mulscc  %o4, %o1, %o4   ! 25
95         mulscc  %o4, %o1, %o4   ! 26
96         mulscc  %o4, %o1, %o4   ! 27
97         mulscc  %o4, %o1, %o4   ! 28
98         mulscc  %o4, %o1, %o4   ! 29
99         mulscc  %o4, %o1, %o4   ! 30
100         mulscc  %o4, %o1, %o4   ! 31
101         mulscc  %o4, %o1, %o4   ! 32
102         mulscc  %o4, %g0, %o4   ! final shift
103
104         ! If %o0 was negative, the result is
105         !       (%o0 * %o1) + (%o1 << 32))
106         ! We fix that here.
107
108         tst     %o0
109         bge     1f
110         rd      %y, %o0
111
112         ! %o0 was indeed negative; fix upper 32 bits of result by subtracting 
113         ! %o1 (i.e., return %o4 - %o1 in %o1).
114         retl
115         sub     %o4, %o1, %o1
116
117 1:
118         retl
119         mov     %o4, %o1
120
121 Lmul_shortway:
122         /*
123          * Short multiply.  12 steps, followed by a final shift step.
124          * The resulting bits are off by 12 and (32-12) = 20 bit positions,
125          * but there is no problem with %o0 being negative (unlike above).
126          */
127         mulscc  %o4, %o1, %o4   ! 1
128         mulscc  %o4, %o1, %o4   ! 2
129         mulscc  %o4, %o1, %o4   ! 3
130         mulscc  %o4, %o1, %o4   ! 4
131         mulscc  %o4, %o1, %o4   ! 5
132         mulscc  %o4, %o1, %o4   ! 6
133         mulscc  %o4, %o1, %o4   ! 7
134         mulscc  %o4, %o1, %o4   ! 8
135         mulscc  %o4, %o1, %o4   ! 9
136         mulscc  %o4, %o1, %o4   ! 10
137         mulscc  %o4, %o1, %o4   ! 11
138         mulscc  %o4, %o1, %o4   ! 12
139         mulscc  %o4, %g0, %o4   ! final shift
140
141         /*
142          *  %o4 has 20 of the bits that should be in the low part of the
143          * result; %y has the bottom 12 (as %y's top 12).  That is:
144          *
145          *        %o4               %y
146          * +----------------+----------------+
147          * | -12- |   -20-  | -12- |   -20-  |
148          * +------(---------+------)---------+
149          *  --hi-- ----low-part----
150          *
151          * The upper 12 bits of %o4 should be sign-extended to form the
152          * high part of the product (i.e., highpart = %o4 >> 20).
153          */
154
155         rd      %y, %o5
156         sll     %o4, 12, %o0    ! shift middle bits left 12
157         srl     %o5, 20, %o5    ! shift low bits right 20, zero fill at left
158         or      %o5, %o0, %o0   ! construct low part of result
159         retl
160         sra     %o4, 20, %o1    ! ... and extract high part of result