2 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4 * Floating-point emulation code
5 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * @(#) pa/spmath/sfmpy.c $Revision: 1.1 $
28 * Single Precision Floating-point Multiply
30 * External Interfaces:
31 * sgl_fmpy(srcptr1,srcptr2,dstptr,status)
33 * Internal Interfaces:
36 * <<please update with a overview of the operation of this file>>
43 #include "sgl_float.h"
46 * Single Precision Floating-point Multiply
51 sgl_floating_point
*srcptr1
,
52 sgl_floating_point
*srcptr2
,
53 sgl_floating_point
*dstptr
,
56 register unsigned int opnd1
, opnd2
, opnd3
, result
;
57 register int dest_exponent
, count
;
58 register boolean inexact
= FALSE
, guardbit
= FALSE
, stickybit
= FALSE
;
64 * set sign bit of result
66 if (Sgl_sign(opnd1
) ^ Sgl_sign(opnd2
)) Sgl_setnegativezero(result
);
67 else Sgl_setzero(result
);
69 * check first operand for NaN's or infinity
71 if (Sgl_isinfinity_exponent(opnd1
)) {
72 if (Sgl_iszero_mantissa(opnd1
)) {
73 if (Sgl_isnotnan(opnd2
)) {
74 if (Sgl_iszero_exponentmantissa(opnd2
)) {
76 * invalid since operands are infinity
79 if (Is_invalidtrap_enabled())
80 return(INVALIDEXCEPTION
);
82 Sgl_makequietnan(result
);
89 Sgl_setinfinity_exponentmantissa(result
);
96 * is NaN; signaling or quiet?
98 if (Sgl_isone_signaling(opnd1
)) {
99 /* trap if INVALIDTRAP enabled */
100 if (Is_invalidtrap_enabled())
101 return(INVALIDEXCEPTION
);
104 Sgl_set_quiet(opnd1
);
107 * is second operand a signaling NaN?
109 else if (Sgl_is_signalingnan(opnd2
)) {
110 /* trap if INVALIDTRAP enabled */
111 if (Is_invalidtrap_enabled())
112 return(INVALIDEXCEPTION
);
115 Sgl_set_quiet(opnd2
);
127 * check second operand for NaN's or infinity
129 if (Sgl_isinfinity_exponent(opnd2
)) {
130 if (Sgl_iszero_mantissa(opnd2
)) {
131 if (Sgl_iszero_exponentmantissa(opnd1
)) {
132 /* invalid since operands are zero & infinity */
133 if (Is_invalidtrap_enabled())
134 return(INVALIDEXCEPTION
);
136 Sgl_makequietnan(opnd2
);
143 Sgl_setinfinity_exponentmantissa(result
);
148 * is NaN; signaling or quiet?
150 if (Sgl_isone_signaling(opnd2
)) {
151 /* trap if INVALIDTRAP enabled */
152 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
156 Sgl_set_quiet(opnd2
);
167 dest_exponent
= Sgl_exponent(opnd1
) + Sgl_exponent(opnd2
) - SGL_BIAS
;
172 if (Sgl_isnotzero_exponent(opnd1
)) {
174 Sgl_clear_signexponent_set_hidden(opnd1
);
178 if (Sgl_iszero_mantissa(opnd1
)) {
179 Sgl_setzero_exponentmantissa(result
);
183 /* is denormalized, adjust exponent */
184 Sgl_clear_signexponent(opnd1
);
185 Sgl_leftshiftby1(opnd1
);
186 Sgl_normalize(opnd1
,dest_exponent
);
188 /* opnd2 needs to have hidden bit set with msb in hidden bit */
189 if (Sgl_isnotzero_exponent(opnd2
)) {
190 Sgl_clear_signexponent_set_hidden(opnd2
);
194 if (Sgl_iszero_mantissa(opnd2
)) {
195 Sgl_setzero_exponentmantissa(result
);
199 /* is denormalized; want to normalize */
200 Sgl_clear_signexponent(opnd2
);
201 Sgl_leftshiftby1(opnd2
);
202 Sgl_normalize(opnd2
,dest_exponent
);
205 /* Multiply two source mantissas together */
207 Sgl_leftshiftby4(opnd2
); /* make room for guard bits */
210 * Four bits at a time are inspected in each loop, and a
211 * simple shift and add multiply algorithm is used.
213 for (count
=1;count
<SGL_P
;count
+=4) {
214 stickybit
|= Slow4(opnd3
);
215 Sgl_rightshiftby4(opnd3
);
216 if (Sbit28(opnd1
)) Sall(opnd3
) += (Sall(opnd2
) << 3);
217 if (Sbit29(opnd1
)) Sall(opnd3
) += (Sall(opnd2
) << 2);
218 if (Sbit30(opnd1
)) Sall(opnd3
) += (Sall(opnd2
) << 1);
219 if (Sbit31(opnd1
)) Sall(opnd3
) += Sall(opnd2
);
220 Sgl_rightshiftby4(opnd1
);
222 /* make sure result is left-justified */
223 if (Sgl_iszero_sign(opnd3
)) {
224 Sgl_leftshiftby1(opnd3
);
227 /* result mantissa >= 2. */
230 /* check for denormalized result */
231 while (Sgl_iszero_sign(opnd3
)) {
232 Sgl_leftshiftby1(opnd3
);
236 * check for guard, sticky and inexact bits
238 stickybit
|= Sgl_all(opnd3
) << (SGL_BITLENGTH
- SGL_EXP_LENGTH
+ 1);
239 guardbit
= Sbit24(opnd3
);
240 inexact
= guardbit
| stickybit
;
242 /* re-align mantissa */
243 Sgl_rightshiftby8(opnd3
);
248 if (inexact
&& (dest_exponent
>0 || Is_underflowtrap_enabled())) {
249 Sgl_clear_signexponent(opnd3
);
250 switch (Rounding_mode()) {
252 if (Sgl_iszero_sign(result
))
253 Sgl_increment(opnd3
);
256 if (Sgl_isone_sign(result
))
257 Sgl_increment(opnd3
);
261 if (stickybit
|| Sgl_isone_lowmantissa(opnd3
))
262 Sgl_increment(opnd3
);
265 if (Sgl_isone_hidden(opnd3
)) dest_exponent
++;
267 Sgl_set_mantissa(result
,opnd3
);
272 if (dest_exponent
>= SGL_INFINITY_EXPONENT
) {
273 /* trap if OVERFLOWTRAP enabled */
274 if (Is_overflowtrap_enabled()) {
276 * Adjust bias of result
278 Sgl_setwrapped_exponent(result
,dest_exponent
,ovfl
);
281 if (Is_inexacttrap_enabled())
282 return(OVERFLOWEXCEPTION
| INEXACTEXCEPTION
);
283 else Set_inexactflag();
284 return(OVERFLOWEXCEPTION
);
288 /* set result to infinity or largest number */
289 Sgl_setoverflow(result
);
294 else if (dest_exponent
<= 0) {
295 /* trap if UNDERFLOWTRAP enabled */
296 if (Is_underflowtrap_enabled()) {
298 * Adjust bias of result
300 Sgl_setwrapped_exponent(result
,dest_exponent
,unfl
);
303 if (Is_inexacttrap_enabled())
304 return(UNDERFLOWEXCEPTION
| INEXACTEXCEPTION
);
305 else Set_inexactflag();
306 return(UNDERFLOWEXCEPTION
);
309 /* Determine if should set underflow flag */
311 if (dest_exponent
== 0 && inexact
) {
312 switch (Rounding_mode()) {
314 if (Sgl_iszero_sign(result
)) {
315 Sgl_increment(opnd3
);
316 if (Sgl_isone_hiddenoverflow(opnd3
))
318 Sgl_decrement(opnd3
);
322 if (Sgl_isone_sign(result
)) {
323 Sgl_increment(opnd3
);
324 if (Sgl_isone_hiddenoverflow(opnd3
))
326 Sgl_decrement(opnd3
);
330 if (guardbit
&& (stickybit
||
331 Sgl_isone_lowmantissa(opnd3
))) {
332 Sgl_increment(opnd3
);
333 if (Sgl_isone_hiddenoverflow(opnd3
))
335 Sgl_decrement(opnd3
);
342 * denormalize result or set to signed zero
345 Sgl_denormalize(opnd3
,dest_exponent
,guardbit
,stickybit
,inexact
);
347 /* return zero or smallest number */
349 switch (Rounding_mode()) {
351 if (Sgl_iszero_sign(result
)) {
352 Sgl_increment(opnd3
);
356 if (Sgl_isone_sign(result
)) {
357 Sgl_increment(opnd3
);
361 if (guardbit
&& (stickybit
||
362 Sgl_isone_lowmantissa(opnd3
))) {
363 Sgl_increment(opnd3
);
367 if (is_tiny
) Set_underflowflag();
369 Sgl_set_exponentmantissa(result
,opnd3
);
371 else Sgl_set_exponent(result
,dest_exponent
);
374 /* check for inexact */
376 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
377 else Set_inexactflag();
This page took 0.037263 seconds and 5 git commands to generate.