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