/* $NetBSD: fpu_implode.c,v 1.6 2005/12/11 12:18:42 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Lawrence Berkeley Laboratory.16*17* Redistribution and use in source and binary forms, with or without18* modification, are permitted provided that the following conditions19* are met:20* 1. Redistributions of source code must retain the above copyright21* notice, this list of conditions and the following disclaimer.22* 2. Redistributions in binary form must reproduce the above copyright23* notice, this list of conditions and the following disclaimer in the24* documentation and/or other materials provided with the distribution.25* 3. Neither the name of the University nor the names of its contributors26* may be used to endorse or promote products derived from this software27* without specific prior written permission.28*29* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND30* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*/4142/*43* FPU subroutines: `implode' internal format numbers into the machine's44* `packed binary' format.45*/4647#include <sys/types.h>48#include <sys/systm.h>4950#include <machine/fpu.h>51#include <machine/ieee.h>52#include <machine/ieeefp.h>5354#include <powerpc/fpu/fpu_arith.h>55#include <powerpc/fpu/fpu_emu.h>56#include <powerpc/fpu/fpu_extern.h>57#include <powerpc/fpu/fpu_instr.h>5859static int round(struct fpemu *, struct fpn *);60static int toinf(struct fpemu *, int);6162/*63* Round a number (algorithm from Motorola MC68882 manual, modified for64* our internal format). Set inexact exception if rounding is required.65* Return true iff we rounded up.66*67* After rounding, we discard the guard and round bits by shifting right68* 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).69* This saves effort later.70*71* Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's72* responsibility to fix this if necessary.73*/74static int75round(struct fpemu *fe, struct fpn *fp)76{77u_int m0, m1, m2, m3;78int gr, s;79FPU_DECL_CARRY;8081m0 = fp->fp_mant[0];82m1 = fp->fp_mant[1];83m2 = fp->fp_mant[2];84m3 = fp->fp_mant[3];85gr = m3 & 3;86s = fp->fp_sticky;8788/* mant >>= FP_NG */89m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));90m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));91m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));92m0 >>= FP_NG;9394if ((gr | s) == 0) /* result is exact: no rounding needed */95goto rounddown;9697fe->fe_cx |= FPSCR_XX|FPSCR_FI; /* inexact */9899/* Go to rounddown to round down; break to round up. */100switch ((fe->fe_fpscr) & FPSCR_RN) {101case FP_RN:102default:103/*104* Round only if guard is set (gr & 2). If guard is set,105* but round & sticky both clear, then we want to round106* but have a tie, so round to even, i.e., add 1 iff odd.107*/108if ((gr & 2) == 0)109goto rounddown;110if ((gr & 1) || fp->fp_sticky || (m3 & 1))111break;112goto rounddown;113114case FP_RZ:115/* Round towards zero, i.e., down. */116goto rounddown;117118case FP_RM:119/* Round towards -Inf: up if negative, down if positive. */120if (fp->fp_sign)121break;122goto rounddown;123124case FP_RP:125/* Round towards +Inf: up if positive, down otherwise. */126if (!fp->fp_sign)127break;128goto rounddown;129}130131/* Bump low bit of mantissa, with carry. */132fe->fe_cx |= FPSCR_FR;133134FPU_ADDS(m3, m3, 1);135FPU_ADDCS(m2, m2, 0);136FPU_ADDCS(m1, m1, 0);137FPU_ADDC(m0, m0, 0);138fp->fp_mant[0] = m0;139fp->fp_mant[1] = m1;140fp->fp_mant[2] = m2;141fp->fp_mant[3] = m3;142return (1);143144rounddown:145fp->fp_mant[0] = m0;146fp->fp_mant[1] = m1;147fp->fp_mant[2] = m2;148fp->fp_mant[3] = m3;149return (0);150}151152/*153* For overflow: return true if overflow is to go to +/-Inf, according154* to the sign of the overflowing result. If false, overflow is to go155* to the largest magnitude value instead.156*/157static int158toinf(struct fpemu *fe, int sign)159{160int inf;161162/* look at rounding direction */163switch ((fe->fe_fpscr) & FPSCR_RN) {164default:165case FP_RN: /* the nearest value is always Inf */166inf = 1;167break;168169case FP_RZ: /* toward 0 => never towards Inf */170inf = 0;171break;172173case FP_RP: /* toward +Inf iff positive */174inf = sign == 0;175break;176177case FP_RM: /* toward -Inf iff negative */178inf = sign;179break;180}181if (inf)182fe->fe_cx |= FPSCR_OX;183return (inf);184}185186/*187* fpn -> int (int value returned as return value).188*189* N.B.: this conversion always rounds towards zero (this is a peculiarity190* of the SPARC instruction set).191*/192u_int193fpu_ftoi(struct fpemu *fe, struct fpn *fp)194{195u_int i;196int sign, exp;197198sign = fp->fp_sign;199switch (fp->fp_class) {200case FPC_ZERO:201return (0);202203case FPC_NUM:204/*205* If exp >= 2^32, overflow. Otherwise shift value right206* into last mantissa word (this will not exceed 0xffffffff),207* shifting any guard and round bits out into the sticky208* bit. Then ``round'' towards zero, i.e., just set an209* inexact exception if sticky is set (see round()).210* If the result is > 0x80000000, or is positive and equals211* 0x80000000, overflow; otherwise the last fraction word212* is the result.213*/214if ((exp = fp->fp_exp) >= 32)215break;216/* NB: the following includes exp < 0 cases */217if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)218fe->fe_cx |= FPSCR_UX;219i = fp->fp_mant[3];220if (i >= ((u_int)0x80000000 + sign))221break;222return (sign ? -i : i);223224default: /* Inf, qNaN, sNaN */225break;226}227/* overflow: replace any inexact exception with invalid */228fe->fe_cx |= FPSCR_VXCVI;229return (0x7fffffff + sign);230}231232/*233* fpn -> extended int (high bits of int value returned as return value).234*235* N.B.: this conversion always rounds towards zero (this is a peculiarity236* of the SPARC instruction set).237*/238u_int239fpu_ftox(struct fpemu *fe, struct fpn *fp, u_int *res)240{241u_int64_t i;242int sign, exp;243244sign = fp->fp_sign;245switch (fp->fp_class) {246case FPC_ZERO:247res[1] = 0;248return (0);249250case FPC_NUM:251/*252* If exp >= 2^64, overflow. Otherwise shift value right253* into last mantissa word (this will not exceed 0xffffffffffffffff),254* shifting any guard and round bits out into the sticky255* bit. Then ``round'' towards zero, i.e., just set an256* inexact exception if sticky is set (see round()).257* If the result is > 0x8000000000000000, or is positive and equals258* 0x8000000000000000, overflow; otherwise the last fraction word259* is the result.260*/261if ((exp = fp->fp_exp) >= 64)262break;263/* NB: the following includes exp < 0 cases */264if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)265fe->fe_cx |= FPSCR_UX;266i = ((u_int64_t)fp->fp_mant[2]<<32)|fp->fp_mant[3];267if (i >= ((u_int64_t)0x8000000000000000LL + sign))268break;269return (sign ? -i : i);270271default: /* Inf, qNaN, sNaN */272break;273}274/* overflow: replace any inexact exception with invalid */275fe->fe_cx |= FPSCR_VXCVI;276return (0x7fffffffffffffffLL + sign);277}278279/*280* fpn -> single (32 bit single returned as return value).281* We assume <= 29 bits in a single-precision fraction (1.f part).282*/283u_int284fpu_ftos(struct fpemu *fe, struct fpn *fp)285{286u_int sign = fp->fp_sign << 31;287int exp;288289#define SNG_EXP(e) ((e) << SNG_FRACBITS) /* makes e an exponent */290#define SNG_MASK (SNG_EXP(1) - 1) /* mask for fraction */291292/* Take care of non-numbers first. */293if (ISNAN(fp)) {294/*295* Preserve upper bits of NaN, per SPARC V8 appendix N.296* Note that fp->fp_mant[0] has the quiet bit set,297* even if it is classified as a signalling NaN.298*/299(void) fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);300exp = SNG_EXP_INFNAN;301goto done;302}303if (ISINF(fp))304return (sign | SNG_EXP(SNG_EXP_INFNAN));305if (ISZERO(fp))306return (sign);307308/*309* Normals (including subnormals). Drop all the fraction bits310* (including the explicit ``implied'' 1 bit) down into the311* single-precision range. If the number is subnormal, move312* the ``implied'' 1 into the explicit range as well, and shift313* right to introduce leading zeroes. Rounding then acts314* differently for normals and subnormals: the largest subnormal315* may round to the smallest normal (1.0 x 2^minexp), or may316* remain subnormal. In the latter case, signal an underflow317* if the result was inexact or if underflow traps are enabled.318*319* Rounding a normal, on the other hand, always produces another320* normal (although either way the result might be too big for321* single precision, and cause an overflow). If rounding a322* normal produces 2.0 in the fraction, we need not adjust that323* fraction at all, since both 1.0 and 2.0 are zero under the324* fraction mask.325*326* Note that the guard and round bits vanish from the number after327* rounding.328*/329if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) { /* subnormal */330/* -NG for g,r; -SNG_FRACBITS-exp for fraction */331(void) fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);332if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(1))333return (sign | SNG_EXP(1) | 0);334if ((fe->fe_cx & FPSCR_FI) ||335(fe->fe_fpscr & FPSCR_UX))336fe->fe_cx |= FPSCR_UX;337return (sign | SNG_EXP(0) | fp->fp_mant[3]);338}339/* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */340(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);341#ifdef DIAGNOSTIC342if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)343panic("fpu_ftos");344#endif345if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))346exp++;347if (exp >= SNG_EXP_INFNAN) {348/* overflow to inf or to max single */349if (toinf(fe, sign))350return (sign | SNG_EXP(SNG_EXP_INFNAN));351return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);352}353done:354/* phew, made it */355return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));356}357358/*359* fpn -> double (32 bit high-order result returned; 32-bit low order result360* left in res[1]). Assumes <= 61 bits in double precision fraction.361*362* This code mimics fpu_ftos; see it for comments.363*/364u_int365fpu_ftod(struct fpemu *fe, struct fpn *fp, u_int *res)366{367u_int sign = fp->fp_sign << 31;368int exp;369370#define DBL_EXP(e) ((e) << (DBL_FRACBITS & 31))371#define DBL_MASK (DBL_EXP(1) - 1)372373if (ISNAN(fp)) {374(void) fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);375exp = DBL_EXP_INFNAN;376goto done;377}378if (ISINF(fp)) {379sign |= DBL_EXP(DBL_EXP_INFNAN);380goto zero;381}382if (ISZERO(fp)) {383zero: res[1] = 0;384return (sign);385}386387if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {388(void) fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);389if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {390res[1] = 0;391return (sign | DBL_EXP(1) | 0);392}393if ((fe->fe_cx & FPSCR_FI) ||394(fe->fe_fpscr & FPSCR_UX))395fe->fe_cx |= FPSCR_UX;396exp = 0;397goto done;398}399(void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);400if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))401exp++;402if (exp >= DBL_EXP_INFNAN) {403fe->fe_cx |= FPSCR_OX | FPSCR_UX;404if (toinf(fe, sign)) {405res[1] = 0;406return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);407}408res[1] = ~0;409return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);410}411done:412res[1] = fp->fp_mant[3];413return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));414}415416/*417* Implode an fpn, writing the result into the given space.418*/419void420fpu_implode(struct fpemu *fe, struct fpn *fp, int type, u_int *space)421{422423switch (type) {424case FTYPE_LNG:425space[0] = fpu_ftox(fe, fp, space);426DPRINTF(FPE_REG, ("fpu_implode: long %x %x\n",427space[0], space[1]));428break;429430case FTYPE_INT:431space[0] = 0;432space[1] = fpu_ftoi(fe, fp);433DPRINTF(FPE_REG, ("fpu_implode: int %x\n",434space[1]));435break;436437case FTYPE_SNG:438space[0] = fpu_ftos(fe, fp);439DPRINTF(FPE_REG, ("fpu_implode: single %x\n",440space[0]));441break;442443case FTYPE_DBL:444space[0] = fpu_ftod(fe, fp, space);445DPRINTF(FPE_REG, ("fpu_implode: double %x %x\n",446space[0], space[1]));447break; break;448449default:450panic("fpu_implode: invalid type %d", type);451}452}453454455