xref: /netbsd/sys/arch/hppa/spmath/sgl_float.h (revision 6550d01e)
1 /*	$NetBSD: sgl_float.h,v 1.3 2009/01/27 11:49:54 skrll Exp $	*/
2 
3 /*	$OpenBSD: sgl_float.h,v 1.10 2004/01/02 14:39:01 mickey Exp $	*/
4 
5 /*
6  * Copyright 1996 1995 by Open Software Foundation, Inc.
7  *              All Rights Reserved
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby granted,
11  * provided that the above copyright notice appears in all copies and
12  * that both the copyright notice and this permission notice appear in
13  * supporting documentation.
14  *
15  * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21  * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22  * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24  */
25 /*
26  * pmk1.1
27  */
28 /*
29  * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
30  *
31  * To anyone who acknowledges that this file is provided "AS IS"
32  * without any express or implied warranty:
33  *     permission to use, copy, modify, and distribute this file
34  * for any purpose is hereby granted without fee, provided that
35  * the above copyright notice and this notice appears in all
36  * copies, and that the name of Hewlett-Packard Company not be
37  * used in advertising or publicity pertaining to distribution
38  * of the software without specific, written prior permission.
39  * Hewlett-Packard Company makes no representations about the
40  * suitability of this software for any purpose.
41  */
42 
43 #include <sys/cdefs.h>
44 
45 /******************************
46  * Single precision functions *
47  ******************************/
48 
49 /* 32-bit word grabing functions */
50 #define Sgl_firstword(value) Sall(value)
51 #define Sgl_secondword(value) dummy_location
52 #define Sgl_thirdword(value) dummy_location
53 #define Sgl_fourthword(value) dummy_location
54 
55 #define Sgl_sign(object) Ssign(object)
56 #define Sgl_exponent(object) Sexponent(object)
57 #define Sgl_signexponent(object) Ssignexponent(object)
58 #define Sgl_mantissa(object) Smantissa(object)
59 #define Sgl_exponentmantissa(object) Sexponentmantissa(object)
60 #define Sgl_all(object) Sall(object)
61 
62 /* sgl_and_signs ands the sign bits of each argument and puts the result
63  * into the first argument. sgl_or_signs ors those same sign bits */
64 #define Sgl_and_signs( src1dst, src2)		\
65     Sall(src1dst) = (Sall(src2)|~(1<<31)) & Sall(src1dst)
66 #define Sgl_or_signs( src1dst, src2)		\
67     Sall(src1dst) = (Sall(src2)&(1<<31)) | Sall(src1dst)
68 
69 /* The hidden bit is always the low bit of the exponent */
70 #define Sgl_clear_exponent_set_hidden(srcdst) Deposit_sexponent(srcdst,1)
71 #define Sgl_clear_signexponent_set_hidden(srcdst) \
72     Deposit_ssignexponent(srcdst,1)
73 #define Sgl_clear_sign(srcdst) Sall(srcdst) &= ~(1<<31)
74 #define Sgl_clear_signexponent(srcdst) Sall(srcdst) &= 0x007fffff
75 
76 /* varamount must be less than 32 for the next three functions */
77 #define Sgl_rightshift(srcdst, varamount)	\
78     Sall(srcdst) >>= varamount
79 #define Sgl_leftshift(srcdst, varamount)	\
80     Sall(srcdst) <<= varamount
81 #define Sgl_rightshift_exponentmantissa(srcdst, varamount) \
82     Sall(srcdst) = \
83 	(Sexponentmantissa(srcdst) >> (varamount)) | (Sall(srcdst) & (1<<31))
84 
85 #define Sgl_leftshiftby1_withextent(left,right,result) \
86     Shiftdouble(Sall(left),Extall(right),31,Sall(result))
87 
88 #define Sgl_rightshiftby1_withextent(left,right,dst)		\
89     Shiftdouble(Sall(left),Extall(right),1,Extall(right))
90 #define Sgl_arithrightshiftby1(srcdst)	\
91     Sall(srcdst) = (int)Sall(srcdst) >> 1
92 
93 /* Sign extend the sign bit with an integer destination */
94 #define Sgl_signextendedsign(value) Ssignedsign(value)
95 
96 #define Sgl_isone_hidden(sgl_value) (Shidden(sgl_value))
97 #define Sgl_increment(sgl_value) Sall(sgl_value) += 1
98 #define Sgl_increment_mantissa(sgl_value) \
99     Deposit_smantissa(sgl_value,sgl_value+1)
100 #define Sgl_decrement(sgl_value) Sall(sgl_value) -= 1
101 
102 #define Sgl_isone_sign(sgl_value) (Is_ssign(sgl_value)!=0)
103 #define Sgl_isone_hiddenoverflow(sgl_value) \
104     (Is_shiddenoverflow(sgl_value)!=0)
105 #define Sgl_isone_lowmantissa(sgl_value) (Is_slow(sgl_value)!=0)
106 #define Sgl_isone_signaling(sgl_value) (Is_ssignaling(sgl_value)!=0)
107 #define Sgl_is_signalingnan(sgl_value) (Ssignalingnan(sgl_value)==0x1ff)
108 #define Sgl_isnotzero(sgl_value) (Sall(sgl_value)!=0)
109 #define Sgl_isnotzero_hiddenhigh7mantissa(sgl_value) \
110     (Shiddenhigh7mantissa(sgl_value)!=0)
111 #define Sgl_isnotzero_low4(sgl_value) (Slow4(sgl_value)!=0)
112 #define Sgl_isnotzero_exponent(sgl_value) (Sexponent(sgl_value)!=0)
113 #define Sgl_isnotzero_mantissa(sgl_value) (Smantissa(sgl_value)!=0)
114 #define Sgl_isnotzero_exponentmantissa(sgl_value) \
115     (Sexponentmantissa(sgl_value)!=0)
116 #define Sgl_iszero(sgl_value) (Sall(sgl_value)==0)
117 #define Sgl_iszero_signaling(sgl_value) (Is_ssignaling(sgl_value)==0)
118 #define Sgl_iszero_hidden(sgl_value) (Is_shidden(sgl_value)==0)
119 #define Sgl_iszero_hiddenoverflow(sgl_value) \
120     (Is_shiddenoverflow(sgl_value)==0)
121 #define Sgl_iszero_hiddenhigh3mantissa(sgl_value) \
122     (Shiddenhigh3mantissa(sgl_value)==0)
123 #define Sgl_iszero_hiddenhigh7mantissa(sgl_value) \
124     (Shiddenhigh7mantissa(sgl_value)==0)
125 #define Sgl_iszero_sign(sgl_value) (Is_ssign(sgl_value)==0)
126 #define Sgl_iszero_exponent(sgl_value) (Sexponent(sgl_value)==0)
127 #define Sgl_iszero_mantissa(sgl_value) (Smantissa(sgl_value)==0)
128 #define Sgl_iszero_exponentmantissa(sgl_value) \
129     (Sexponentmantissa(sgl_value)==0)
130 #define Sgl_isinfinity_exponent(sgl_value)		\
131     (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT)
132 #define Sgl_isnotinfinity_exponent(sgl_value)		\
133     (Sgl_exponent(sgl_value)!=SGL_INFINITY_EXPONENT)
134 #define Sgl_isinfinity(sgl_value)			\
135     (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT &&	\
136     Sgl_mantissa(sgl_value)==0)
137 #define Sgl_isnan(sgl_value)				\
138     (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT &&	\
139     Sgl_mantissa(sgl_value)!=0)
140 #define Sgl_isnotnan(sgl_value)				\
141     (Sgl_exponent(sgl_value)!=SGL_INFINITY_EXPONENT ||	\
142     Sgl_mantissa(sgl_value)==0)
143 #define Sgl_islessthan(sgl_op1,sgl_op2)			\
144     (Sall(sgl_op1) < Sall(sgl_op2))
145 #define Sgl_isgreaterthan(sgl_op1,sgl_op2)		\
146     (Sall(sgl_op1) > Sall(sgl_op2))
147 #define Sgl_isnotlessthan(sgl_op1,sgl_op2)		\
148     (Sall(sgl_op1) >= Sall(sgl_op2))
149 #define Sgl_isequal(sgl_op1,sgl_op2)			\
150     (Sall(sgl_op1) == Sall(sgl_op2))
151 
152 #define Sgl_leftshiftby8(sgl_value) \
153     Sall(sgl_value) <<= 8
154 #define Sgl_leftshiftby4(sgl_value) \
155     Sall(sgl_value) <<= 4
156 #define Sgl_leftshiftby3(sgl_value) \
157     Sall(sgl_value) <<= 3
158 #define Sgl_leftshiftby2(sgl_value) \
159     Sall(sgl_value) <<= 2
160 #define Sgl_leftshiftby1(sgl_value) \
161     Sall(sgl_value) <<= 1
162 #define Sgl_rightshiftby1(sgl_value) \
163     Sall(sgl_value) >>= 1
164 #define Sgl_rightshiftby4(sgl_value) \
165     Sall(sgl_value) >>= 4
166 #define Sgl_rightshiftby8(sgl_value) \
167     Sall(sgl_value) >>= 8
168 
169 #define Sgl_ismagnitudeless(signlessleft,signlessright)			\
170 /*  unsigned int signlessleft, signlessright; */			\
171       (signlessleft < signlessright)
172 
173 
174 #define Sgl_copytoint_exponentmantissa(source,dest)     \
175     dest = Sexponentmantissa(source)
176 
177 /* A quiet NaN has the high mantissa bit clear and at least on other (in this
178  * case the adjacent bit) bit set. */
179 #define Sgl_set_quiet(sgl_value) Deposit_shigh2mantissa(sgl_value,1)
180 #define Sgl_set_exponent(sgl_value,exp) Deposit_sexponent(sgl_value,exp)
181 
182 #define Sgl_set_mantissa(dest,value) Deposit_smantissa(dest,value)
183 #define Sgl_set_exponentmantissa(dest,value) \
184     Deposit_sexponentmantissa(dest,value)
185 
186 /*  An infinity is represented with the max exponent and a zero mantissa */
187 #define Sgl_setinfinity_exponent(sgl_value) \
188     Deposit_sexponent(sgl_value,SGL_INFINITY_EXPONENT)
189 #define Sgl_setinfinity_exponentmantissa(sgl_value)	\
190     Deposit_sexponentmantissa(sgl_value, \
191 	(SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))))
192 #define Sgl_setinfinitypositive(sgl_value)		\
193     Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH)))
194 #define Sgl_setinfinitynegative(sgl_value)		\
195     Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))) \
196     | (1<<31)
197 #define Sgl_setinfinity(sgl_value,sign)					\
198     Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))) | \
199      (sign << 31)
200 #define Sgl_sethigh4bits(sgl_value, extsign)  \
201     Deposit_shigh4(sgl_value,extsign)
202 #define Sgl_set_sign(sgl_value,sign) Deposit_ssign(sgl_value,sign)
203 #define Sgl_invert_sign(sgl_value)  \
204     Deposit_ssign(sgl_value,~Ssign(sgl_value))
205 #define Sgl_setone_sign(sgl_value) Deposit_ssign(sgl_value,1)
206 #define Sgl_setone_lowmantissa(sgl_value) Deposit_slow(sgl_value,1)
207 #define Sgl_setzero_sign(sgl_value)  Sall(sgl_value) &= 0x7fffffff
208 #define Sgl_setzero_exponent(sgl_value) Sall(sgl_value) &= 0x807fffff
209 #define Sgl_setzero_mantissa(sgl_value) Sall(sgl_value) &= 0xff800000
210 #define Sgl_setzero_exponentmantissa(sgl_value)  Sall(sgl_value) &= 0x80000000
211 #define Sgl_setzero(sgl_value) Sall(sgl_value) = 0
212 #define Sgl_setnegativezero(sgl_value) Sall(sgl_value) = 1 << 31
213 
214 /* Use following macro for both overflow & underflow conditions */
215 #define ovfl -
216 #define unfl +
217 #define Sgl_setwrapped_exponent(sgl_value,exponent,op) \
218     Deposit_sexponent(sgl_value,(exponent op SGL_WRAP))
219 
220 #define Sgl_setlargestpositive(sgl_value)				\
221     Sall(sgl_value) = ((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))	\
222 			| ((1<<(32-(1+SGL_EXP_LENGTH))) - 1)
223 #define Sgl_setlargestnegative(sgl_value)				\
224     Sall(sgl_value) = ((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))	\
225 			| ((1<<(32-(1+SGL_EXP_LENGTH))) - 1 ) | (1<<31)
226 
227 #define Sgl_setnegativeinfinity(sgl_value)	\
228     Sall(sgl_value) =				\
229     ((1<<SGL_EXP_LENGTH) | SGL_INFINITY_EXPONENT) << (32-(1+SGL_EXP_LENGTH))
230 #define Sgl_setlargest(sgl_value,sign)					\
231     Sall(sgl_value) = ((sign) << 31) |					\
232 	(((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))		\
233 	  | ((1 << (32-(1+SGL_EXP_LENGTH))) - 1 ))
234 #define Sgl_setlargest_exponentmantissa(sgl_value)			\
235     Sall(sgl_value) = (Sall(sgl_value) & (1<<31)) |			\
236 	(((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))		\
237 	  | ((1 << (32-(1+SGL_EXP_LENGTH))) - 1 ))
238 
239 /* The high bit is always zero so arithmetic or logical shifts will work. */
240 #define Sgl_right_align(srcdst,shift,extent)				\
241     /* sgl_floating_point srcdst; int shift; extension extent */	\
242     if (shift < 32) {							\
243 	Extall(extent) = Sall(srcdst) << (32-(shift));			\
244 	Sall(srcdst) >>= shift;						\
245     }									\
246     else {								\
247 	Extall(extent) = Sall(srcdst);					\
248 	Sall(srcdst) = 0;						\
249     }
250 #define Sgl_hiddenhigh3mantissa(sgl_value) Shiddenhigh3mantissa(sgl_value)
251 #define Sgl_hidden(sgl_value) Shidden(sgl_value)
252 #define Sgl_lowmantissa(sgl_value) Slow(sgl_value)
253 
254 /* The left argument is never smaller than the right argument */
255 #define Sgl_subtract(sgl_left,sgl_right,sgl_result) \
256     Sall(sgl_result) = Sall(sgl_left) - Sall(sgl_right)
257 
258 /* Subtract right augmented with extension from left augmented with zeros and
259  * store into result and extension. */
260 #define Sgl_subtract_withextension(left,right,extent,result)		\
261     /* sgl_floating_point left,right,result; extension extent */	\
262   Sgl_subtract(left,right,result);					\
263   if((Extall(extent) = 0-Extall(extent)))				\
264       Sall(result) = Sall(result)-1
265 
266 #define Sgl_addition(sgl_left,sgl_right,sgl_result) \
267     Sall(sgl_result) = Sall(sgl_left) + Sall(sgl_right)
268 
269 #define Sgl_xortointp1(left,right,result)			\
270     result = Sall(left) XOR Sall(right);
271 
272 #define Sgl_xorfromintp1(left,right,result)			\
273     Sall(result) = left XOR Sall(right)
274 
275 /* Need to Initialize */
276 #define Sgl_makequietnan(dest)						\
277     Sall(dest) = ((SGL_EMAX+SGL_BIAS)+1)<< (32-(1+SGL_EXP_LENGTH))	\
278 		| (1<<(32-(1+SGL_EXP_LENGTH+2)))
279 #define Sgl_makesignalingnan(dest)					\
280     Sall(dest) = ((SGL_EMAX+SGL_BIAS)+1)<< (32-(1+SGL_EXP_LENGTH))	\
281 		| (1<<(32-(1+SGL_EXP_LENGTH+1)))
282 
283 #define Sgl_normalize(sgl_opnd,exponent)			\
284 	while(Sgl_iszero_hiddenhigh7mantissa(sgl_opnd)) {	\
285 		Sgl_leftshiftby8(sgl_opnd);			\
286 		exponent -= 8;					\
287 	}							\
288 	if(Sgl_iszero_hiddenhigh3mantissa(sgl_opnd)) {		\
289 		Sgl_leftshiftby4(sgl_opnd);			\
290 		exponent -= 4;					\
291 	}							\
292 	while(Sgl_iszero_hidden(sgl_opnd)) {			\
293 		Sgl_leftshiftby1(sgl_opnd);			\
294 		exponent -= 1;					\
295 	}
296 
297 #define Sgl_setoverflow(sgl_opnd)				\
298 	/* set result to infinity or largest number */		\
299 	switch (Rounding_mode()) {				\
300 		case ROUNDPLUS:					\
301 			if (Sgl_isone_sign(sgl_opnd)) {		\
302 				Sgl_setlargestnegative(sgl_opnd); \
303 			}					\
304 			else {					\
305 				Sgl_setinfinitypositive(sgl_opnd); \
306 			}					\
307 			break;					\
308 		case ROUNDMINUS:				\
309 			if (Sgl_iszero_sign(sgl_opnd)) {	\
310 				Sgl_setlargestpositive(sgl_opnd); \
311 			}					\
312 			else {					\
313 				Sgl_setinfinitynegative(sgl_opnd); \
314 			}					\
315 			break;					\
316 		case ROUNDNEAREST:				\
317 			Sgl_setinfinity_exponentmantissa(sgl_opnd); \
318 			break;					\
319 		case ROUNDZERO:					\
320 			Sgl_setlargest_exponentmantissa(sgl_opnd); \
321 	}
322 
323 #define Sgl_denormalize(opnd,exponent,guard,sticky,inexact)		\
324 	Sgl_clear_signexponent_set_hidden(opnd);			\
325 	if (exponent >= (1 - SGL_P)) {					\
326 		guard = (Sall(opnd) >> (-(exponent))) & 1;		\
327 		if (exponent < 0) sticky |= Sall(opnd) << (32+exponent); \
328 		inexact = (guard) | (sticky);				\
329 		Sall(opnd) >>= (1-exponent);				\
330 	}								\
331 	else {								\
332 		guard = 0;						\
333 		sticky |= Sall(opnd);					\
334 		inexact = sticky;					\
335 		Sgl_setzero(opnd);					\
336 	}
337 
338 sgl_floating_point sgl_setoverflow(unsigned int);
339 int sgl_fadd(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
340 int sgl_fcmp(sgl_floating_point *, sgl_floating_point *, unsigned int, unsigned int *);
341 int sgl_fdiv(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
342 int sgl_fmpy(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
343 int sgl_frem(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
344 int sgl_fsqrt(sgl_floating_point *, sgl_floating_point *, unsigned int *);
345 int sgl_fsub(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
346 int sgl_frnd(sgl_floating_point *, sgl_floating_point *, unsigned int *);
347 
348