/*1* Linux/PA-RISC Project (http://www.parisc-linux.org/)2*3* Floating-point emulation code4* Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2, or (at your option)9* any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/20/*21* BEGIN_DESC22*23* File:24* @(#) pa/spmath/sfrem.c $Revision: 1.1 $25*26* Purpose:27* Single Precision Floating-point Remainder28*29* External Interfaces:30* sgl_frem(srcptr1,srcptr2,dstptr,status)31*32* Internal Interfaces:33*34* Theory:35* <<please update with a overview of the operation of this file>>36*37* END_DESC38*/39404142#include "float.h"43#include "sgl_float.h"4445/*46* Single Precision Floating-point Remainder47*/4849int50sgl_frem (sgl_floating_point * srcptr1, sgl_floating_point * srcptr2,51sgl_floating_point * dstptr, unsigned int *status)52{53register unsigned int opnd1, opnd2, result;54register int opnd1_exponent, opnd2_exponent, dest_exponent, stepcount;55register boolean roundup = FALSE;5657opnd1 = *srcptr1;58opnd2 = *srcptr2;59/*60* check first operand for NaN's or infinity61*/62if ((opnd1_exponent = Sgl_exponent(opnd1)) == SGL_INFINITY_EXPONENT) {63if (Sgl_iszero_mantissa(opnd1)) {64if (Sgl_isnotnan(opnd2)) {65/* invalid since first operand is infinity */66if (Is_invalidtrap_enabled())67return(INVALIDEXCEPTION);68Set_invalidflag();69Sgl_makequietnan(result);70*dstptr = result;71return(NOEXCEPTION);72}73}74else {75/*76* is NaN; signaling or quiet?77*/78if (Sgl_isone_signaling(opnd1)) {79/* trap if INVALIDTRAP enabled */80if (Is_invalidtrap_enabled())81return(INVALIDEXCEPTION);82/* make NaN quiet */83Set_invalidflag();84Sgl_set_quiet(opnd1);85}86/*87* is second operand a signaling NaN?88*/89else if (Sgl_is_signalingnan(opnd2)) {90/* trap if INVALIDTRAP enabled */91if (Is_invalidtrap_enabled())92return(INVALIDEXCEPTION);93/* make NaN quiet */94Set_invalidflag();95Sgl_set_quiet(opnd2);96*dstptr = opnd2;97return(NOEXCEPTION);98}99/*100* return quiet NaN101*/102*dstptr = opnd1;103return(NOEXCEPTION);104}105}106/*107* check second operand for NaN's or infinity108*/109if ((opnd2_exponent = Sgl_exponent(opnd2)) == SGL_INFINITY_EXPONENT) {110if (Sgl_iszero_mantissa(opnd2)) {111/*112* return first operand113*/114*dstptr = opnd1;115return(NOEXCEPTION);116}117/*118* is NaN; signaling or quiet?119*/120if (Sgl_isone_signaling(opnd2)) {121/* trap if INVALIDTRAP enabled */122if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);123/* make NaN quiet */124Set_invalidflag();125Sgl_set_quiet(opnd2);126}127/*128* return quiet NaN129*/130*dstptr = opnd2;131return(NOEXCEPTION);132}133/*134* check second operand for zero135*/136if (Sgl_iszero_exponentmantissa(opnd2)) {137/* invalid since second operand is zero */138if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);139Set_invalidflag();140Sgl_makequietnan(result);141*dstptr = result;142return(NOEXCEPTION);143}144145/*146* get sign of result147*/148result = opnd1;149150/*151* check for denormalized operands152*/153if (opnd1_exponent == 0) {154/* check for zero */155if (Sgl_iszero_mantissa(opnd1)) {156*dstptr = opnd1;157return(NOEXCEPTION);158}159/* normalize, then continue */160opnd1_exponent = 1;161Sgl_normalize(opnd1,opnd1_exponent);162}163else {164Sgl_clear_signexponent_set_hidden(opnd1);165}166if (opnd2_exponent == 0) {167/* normalize, then continue */168opnd2_exponent = 1;169Sgl_normalize(opnd2,opnd2_exponent);170}171else {172Sgl_clear_signexponent_set_hidden(opnd2);173}174175/* find result exponent and divide step loop count */176dest_exponent = opnd2_exponent - 1;177stepcount = opnd1_exponent - opnd2_exponent;178179/*180* check for opnd1/opnd2 < 1181*/182if (stepcount < 0) {183/*184* check for opnd1/opnd2 > 1/2185*186* In this case n will round to 1, so187* r = opnd1 - opnd2188*/189if (stepcount == -1 && Sgl_isgreaterthan(opnd1,opnd2)) {190Sgl_all(result) = ~Sgl_all(result); /* set sign */191/* align opnd2 with opnd1 */192Sgl_leftshiftby1(opnd2);193Sgl_subtract(opnd2,opnd1,opnd2);194/* now normalize */195while (Sgl_iszero_hidden(opnd2)) {196Sgl_leftshiftby1(opnd2);197dest_exponent--;198}199Sgl_set_exponentmantissa(result,opnd2);200goto testforunderflow;201}202/*203* opnd1/opnd2 <= 1/2204*205* In this case n will round to zero, so206* r = opnd1207*/208Sgl_set_exponentmantissa(result,opnd1);209dest_exponent = opnd1_exponent;210goto testforunderflow;211}212213/*214* Generate result215*216* Do iterative subtract until remainder is less than operand 2.217*/218while (stepcount-- > 0 && Sgl_all(opnd1)) {219if (Sgl_isnotlessthan(opnd1,opnd2))220Sgl_subtract(opnd1,opnd2,opnd1);221Sgl_leftshiftby1(opnd1);222}223/*224* Do last subtract, then determine which way to round if remainder225* is exactly 1/2 of opnd2226*/227if (Sgl_isnotlessthan(opnd1,opnd2)) {228Sgl_subtract(opnd1,opnd2,opnd1);229roundup = TRUE;230}231if (stepcount > 0 || Sgl_iszero(opnd1)) {232/* division is exact, remainder is zero */233Sgl_setzero_exponentmantissa(result);234*dstptr = result;235return(NOEXCEPTION);236}237238/*239* Check for cases where opnd1/opnd2 < n240*241* In this case the result's sign will be opposite that of242* opnd1. The mantissa also needs some correction.243*/244Sgl_leftshiftby1(opnd1);245if (Sgl_isgreaterthan(opnd1,opnd2)) {246Sgl_invert_sign(result);247Sgl_subtract((opnd2<<1),opnd1,opnd1);248}249/* check for remainder being exactly 1/2 of opnd2 */250else if (Sgl_isequal(opnd1,opnd2) && roundup) {251Sgl_invert_sign(result);252}253254/* normalize result's mantissa */255while (Sgl_iszero_hidden(opnd1)) {256dest_exponent--;257Sgl_leftshiftby1(opnd1);258}259Sgl_set_exponentmantissa(result,opnd1);260261/*262* Test for underflow263*/264testforunderflow:265if (dest_exponent <= 0) {266/* trap if UNDERFLOWTRAP enabled */267if (Is_underflowtrap_enabled()) {268/*269* Adjust bias of result270*/271Sgl_setwrapped_exponent(result,dest_exponent,unfl);272*dstptr = result;273/* frem is always exact */274return(UNDERFLOWEXCEPTION);275}276/*277* denormalize result or set to signed zero278*/279if (dest_exponent >= (1 - SGL_P)) {280Sgl_rightshift_exponentmantissa(result,1-dest_exponent);281}282else {283Sgl_setzero_exponentmantissa(result);284}285}286else Sgl_set_exponent(result,dest_exponent);287*dstptr = result;288return(NOEXCEPTION);289}290291292