treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / parisc / math-emu / sfrem.c
blob4ac88d8290302106d82b0e391ca8b5f97310ad5d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
5 * Floating-point emulation code
6 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7 */
8 /*
9 * BEGIN_DESC
11 * File:
12 * @(#) pa/spmath/sfrem.c $Revision: 1.1 $
14 * Purpose:
15 * Single Precision Floating-point Remainder
17 * External Interfaces:
18 * sgl_frem(srcptr1,srcptr2,dstptr,status)
20 * Internal Interfaces:
22 * Theory:
23 * <<please update with a overview of the operation of this file>>
25 * END_DESC
30 #include "float.h"
31 #include "sgl_float.h"
34 * Single Precision Floating-point Remainder
37 int
38 sgl_frem (sgl_floating_point * srcptr1, sgl_floating_point * srcptr2,
39 sgl_floating_point * dstptr, unsigned int *status)
41 register unsigned int opnd1, opnd2, result;
42 register int opnd1_exponent, opnd2_exponent, dest_exponent, stepcount;
43 register boolean roundup = FALSE;
45 opnd1 = *srcptr1;
46 opnd2 = *srcptr2;
48 * check first operand for NaN's or infinity
50 if ((opnd1_exponent = Sgl_exponent(opnd1)) == SGL_INFINITY_EXPONENT) {
51 if (Sgl_iszero_mantissa(opnd1)) {
52 if (Sgl_isnotnan(opnd2)) {
53 /* invalid since first operand is infinity */
54 if (Is_invalidtrap_enabled())
55 return(INVALIDEXCEPTION);
56 Set_invalidflag();
57 Sgl_makequietnan(result);
58 *dstptr = result;
59 return(NOEXCEPTION);
62 else {
64 * is NaN; signaling or quiet?
66 if (Sgl_isone_signaling(opnd1)) {
67 /* trap if INVALIDTRAP enabled */
68 if (Is_invalidtrap_enabled())
69 return(INVALIDEXCEPTION);
70 /* make NaN quiet */
71 Set_invalidflag();
72 Sgl_set_quiet(opnd1);
74 /*
75 * is second operand a signaling NaN?
77 else if (Sgl_is_signalingnan(opnd2)) {
78 /* trap if INVALIDTRAP enabled */
79 if (Is_invalidtrap_enabled())
80 return(INVALIDEXCEPTION);
81 /* make NaN quiet */
82 Set_invalidflag();
83 Sgl_set_quiet(opnd2);
84 *dstptr = opnd2;
85 return(NOEXCEPTION);
88 * return quiet NaN
90 *dstptr = opnd1;
91 return(NOEXCEPTION);
95 * check second operand for NaN's or infinity
97 if ((opnd2_exponent = Sgl_exponent(opnd2)) == SGL_INFINITY_EXPONENT) {
98 if (Sgl_iszero_mantissa(opnd2)) {
100 * return first operand
102 *dstptr = opnd1;
103 return(NOEXCEPTION);
106 * is NaN; signaling or quiet?
108 if (Sgl_isone_signaling(opnd2)) {
109 /* trap if INVALIDTRAP enabled */
110 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
111 /* make NaN quiet */
112 Set_invalidflag();
113 Sgl_set_quiet(opnd2);
116 * return quiet NaN
118 *dstptr = opnd2;
119 return(NOEXCEPTION);
122 * check second operand for zero
124 if (Sgl_iszero_exponentmantissa(opnd2)) {
125 /* invalid since second operand is zero */
126 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
127 Set_invalidflag();
128 Sgl_makequietnan(result);
129 *dstptr = result;
130 return(NOEXCEPTION);
134 * get sign of result
136 result = opnd1;
139 * check for denormalized operands
141 if (opnd1_exponent == 0) {
142 /* check for zero */
143 if (Sgl_iszero_mantissa(opnd1)) {
144 *dstptr = opnd1;
145 return(NOEXCEPTION);
147 /* normalize, then continue */
148 opnd1_exponent = 1;
149 Sgl_normalize(opnd1,opnd1_exponent);
151 else {
152 Sgl_clear_signexponent_set_hidden(opnd1);
154 if (opnd2_exponent == 0) {
155 /* normalize, then continue */
156 opnd2_exponent = 1;
157 Sgl_normalize(opnd2,opnd2_exponent);
159 else {
160 Sgl_clear_signexponent_set_hidden(opnd2);
163 /* find result exponent and divide step loop count */
164 dest_exponent = opnd2_exponent - 1;
165 stepcount = opnd1_exponent - opnd2_exponent;
168 * check for opnd1/opnd2 < 1
170 if (stepcount < 0) {
172 * check for opnd1/opnd2 > 1/2
174 * In this case n will round to 1, so
175 * r = opnd1 - opnd2
177 if (stepcount == -1 && Sgl_isgreaterthan(opnd1,opnd2)) {
178 Sgl_all(result) = ~Sgl_all(result); /* set sign */
179 /* align opnd2 with opnd1 */
180 Sgl_leftshiftby1(opnd2);
181 Sgl_subtract(opnd2,opnd1,opnd2);
182 /* now normalize */
183 while (Sgl_iszero_hidden(opnd2)) {
184 Sgl_leftshiftby1(opnd2);
185 dest_exponent--;
187 Sgl_set_exponentmantissa(result,opnd2);
188 goto testforunderflow;
191 * opnd1/opnd2 <= 1/2
193 * In this case n will round to zero, so
194 * r = opnd1
196 Sgl_set_exponentmantissa(result,opnd1);
197 dest_exponent = opnd1_exponent;
198 goto testforunderflow;
202 * Generate result
204 * Do iterative subtract until remainder is less than operand 2.
206 while (stepcount-- > 0 && Sgl_all(opnd1)) {
207 if (Sgl_isnotlessthan(opnd1,opnd2))
208 Sgl_subtract(opnd1,opnd2,opnd1);
209 Sgl_leftshiftby1(opnd1);
212 * Do last subtract, then determine which way to round if remainder
213 * is exactly 1/2 of opnd2
215 if (Sgl_isnotlessthan(opnd1,opnd2)) {
216 Sgl_subtract(opnd1,opnd2,opnd1);
217 roundup = TRUE;
219 if (stepcount > 0 || Sgl_iszero(opnd1)) {
220 /* division is exact, remainder is zero */
221 Sgl_setzero_exponentmantissa(result);
222 *dstptr = result;
223 return(NOEXCEPTION);
227 * Check for cases where opnd1/opnd2 < n
229 * In this case the result's sign will be opposite that of
230 * opnd1. The mantissa also needs some correction.
232 Sgl_leftshiftby1(opnd1);
233 if (Sgl_isgreaterthan(opnd1,opnd2)) {
234 Sgl_invert_sign(result);
235 Sgl_subtract((opnd2<<1),opnd1,opnd1);
237 /* check for remainder being exactly 1/2 of opnd2 */
238 else if (Sgl_isequal(opnd1,opnd2) && roundup) {
239 Sgl_invert_sign(result);
242 /* normalize result's mantissa */
243 while (Sgl_iszero_hidden(opnd1)) {
244 dest_exponent--;
245 Sgl_leftshiftby1(opnd1);
247 Sgl_set_exponentmantissa(result,opnd1);
250 * Test for underflow
252 testforunderflow:
253 if (dest_exponent <= 0) {
254 /* trap if UNDERFLOWTRAP enabled */
255 if (Is_underflowtrap_enabled()) {
257 * Adjust bias of result
259 Sgl_setwrapped_exponent(result,dest_exponent,unfl);
260 *dstptr = result;
261 /* frem is always exact */
262 return(UNDERFLOWEXCEPTION);
265 * denormalize result or set to signed zero
267 if (dest_exponent >= (1 - SGL_P)) {
268 Sgl_rightshift_exponentmantissa(result,1-dest_exponent);
270 else {
271 Sgl_setzero_exponentmantissa(result);
274 else Sgl_set_exponent(result,dest_exponent);
275 *dstptr = result;
276 return(NOEXCEPTION);