1 /* DEC Alpha division and remainder support.
2 Copyright (C) 1994, 1999 Free Software Foundation, Inc.
4 This file is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 In addition to the permissions in the GNU General Public License, the
10 Free Software Foundation gives you unlimited permission to link the
11 compiled version of this file into combinations with other programs,
12 and to distribute those combinations without any restriction coming
13 from the use of this file. (The General Public License restrictions
14 do apply in other respects; for example, they cover modification of
15 the file, and distribution when not linked into a combine
18 This file is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 /* This had to be written in assembler because the division functions
28 use a non-standard calling convention.
30 This file provides an implementation of __divqu, __divq, __divlu,
31 __divl, __remqu, __remq, __remlu and __reml. CPP macros control
34 Operation performed: $27 := $24 o $25, clobber $28, return address to
35 caller in $23, where o one of the operations.
37 The following macros need to be defined:
39 SIZE, the number of bits, 32 or 64.
41 TYPE, either UNSIGNED or SIGNED
43 OPERATION, either DIVISION or REMAINDER
45 SPECIAL_CALLING_CONVENTION, 0 or 1. It is useful for debugging to
46 define this to 0. That removes the `__' prefix to make the function
47 name not collide with the existing libc.a names, and uses the
48 standard Alpha procedure calling convention.
51 #ifndef SPECIAL_CALLING_CONVENTION
52 #define SPECIAL_CALLING_CONVENTION 1
56 #if SPECIAL_CALLING_CONVENTION
57 #define FUNCTION_NAME __divl
59 #define FUNCTION_NAME divl
63 #define OPERATION DIVISION
67 #if SPECIAL_CALLING_CONVENTION
68 #define FUNCTION_NAME __divlu
70 #define FUNCTION_NAME divlu
74 #define OPERATION DIVISION
78 #if SPECIAL_CALLING_CONVENTION
79 #define FUNCTION_NAME __divq
81 #define FUNCTION_NAME divq
85 #define OPERATION DIVISION
89 #if SPECIAL_CALLING_CONVENTION
90 #define FUNCTION_NAME __divqu
92 #define FUNCTION_NAME divqu
96 #define OPERATION DIVISION
100 #if SPECIAL_CALLING_CONVENTION
101 #define FUNCTION_NAME __reml
103 #define FUNCTION_NAME reml
107 #define OPERATION REMAINDER
111 #if SPECIAL_CALLING_CONVENTION
112 #define FUNCTION_NAME __remlu
114 #define FUNCTION_NAME remlu
117 #define TYPE UNSIGNED
118 #define OPERATION REMAINDER
122 #if SPECIAL_CALLING_CONVENTION
123 #define FUNCTION_NAME __remq
125 #define FUNCTION_NAME remq
129 #define OPERATION REMAINDER
133 #if SPECIAL_CALLING_CONVENTION
134 #define FUNCTION_NAME __remqu
136 #define FUNCTION_NAME remqu
139 #define TYPE UNSIGNED
140 #define OPERATION REMAINDER
146 #define result_sign $2
148 #if SPECIAL_CALLING_CONVENTION
160 /* Misc symbols to make alpha assembler easier to read. */
164 /* Symbols to make interface nicer. */
181 /* Under the special calling convention, we have to preserve all register
182 values but $23 and $28. */
183 #if SPECIAL_CALLING_CONVENTION
185 #if OPERATION == DIVISION
190 stq result_sign,24(sp)
194 /* If we are computing the remainder, move N to the register that is used
195 for the return value, and redefine what register is used for N. */
196 #if OPERATION == REMAINDER
202 /* Perform conversion from 32 bit types to 64 bit types. */
205 /* If there are problems with the signed case, add these instructions.
206 The caller should already have done this.
207 addl N,0,N # sign extend N
208 addl D,0,D # sign extend D
211 zap N,0xf0,N # zero extend N (caller required to sign extend)
212 zap D,0xf0,D # zero extend D
216 /* Check for divide by zero. */
223 #if OPERATION == DIVISION
228 /* Get the absolute values of N and D. */
235 /* Compute CNT = ceil(log2(N)) - ceil(log2(D)). This is the number of
236 divide iterations we will have to perform. Should you wish to optimize
237 this, check a few bits at a time, preferably using zap/zapnot. Be
238 careful though, this code runs fast fro the most common cases, when the
239 quotient is small. */
261 /* Actual divide. Could be optimized with unrolling. */
262 #if OPERATION == DIVISION
272 #if OPERATION == DIVISION
280 /* The result is now in RETREG. NOTE! It was written to RETREG using
281 either N or Q as a synonym! */
284 /* Change the sign of the result as needed. */
286 subq zero,RETREG,tmp0
287 cmovlt result_sign,tmp0,RETREG
291 /* Restore clobbered registers. */
292 #if SPECIAL_CALLING_CONVENTION
293 #if OPERATION == DIVISION
298 ldq result_sign,24(sp)
305 /* Sign extend an *unsigned* 32 bit result, as required by the Alpha
307 #if TYPE == UNSIGNED && SIZE == 32
308 /* This could be avoided by adding some CPP hair to the divide loop.
309 It is probably not worth the added complexity. */
314 #if SPECIAL_CALLING_CONVENTION