Annotation of sys/arch/hppa/spmath/fcnvfxt.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: fcnvfxt.c,v 1.7 2003/04/10 17:27:58 mickey Exp $ */
! 2: /*
! 3: (c) Copyright 1986 HEWLETT-PACKARD COMPANY
! 4: To anyone who acknowledges that this file is provided "AS IS"
! 5: without any express or implied warranty:
! 6: permission to use, copy, modify, and distribute this file
! 7: for any purpose is hereby granted without fee, provided that
! 8: the above copyright notice and this notice appears in all
! 9: copies, and that the name of Hewlett-Packard Company not be
! 10: used in advertising or publicity pertaining to distribution
! 11: of the software without specific, written prior permission.
! 12: Hewlett-Packard Company makes no representations about the
! 13: suitability of this software for any purpose.
! 14: */
! 15: /* @(#)fcnvfxt.c: Revision: 2.8.88.2 Date: 93/12/08 13:27:34 */
! 16:
! 17: #include "float.h"
! 18: #include "sgl_float.h"
! 19: #include "dbl_float.h"
! 20: #include "cnv_float.h"
! 21:
! 22: /*
! 23: * Convert single floating-point to single fixed-point format
! 24: * with truncated result
! 25: */
! 26: /*ARGSUSED*/
! 27: int
! 28: sgl_to_sgl_fcnvfxt(srcptr, null, dstptr, status)
! 29: sgl_floating_point *srcptr, *null;
! 30: int *dstptr;
! 31: unsigned int *status;
! 32: {
! 33: register unsigned int src, temp;
! 34: register int src_exponent, result;
! 35:
! 36: src = *srcptr;
! 37: src_exponent = Sgl_exponent(src) - SGL_BIAS;
! 38:
! 39: /*
! 40: * Test for overflow
! 41: */
! 42: if (src_exponent > SGL_FX_MAX_EXP) {
! 43: /* check for MININT */
! 44: if ((src_exponent > SGL_FX_MAX_EXP + 1) ||
! 45: Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
! 46: if( Sgl_isnan(src) )
! 47: /*
! 48: * On NaN go unimplemented.
! 49: */
! 50: return(UNIMPLEMENTEDEXCEPTION);
! 51: else {
! 52: if (Sgl_iszero_sign(src)) result = 0x7fffffff;
! 53: else result = 0x80000000;
! 54:
! 55: if (Is_overflowtrap_enabled()) {
! 56: if (Is_inexacttrap_enabled())
! 57: return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
! 58: else Set_inexactflag();
! 59: return(OVERFLOWEXCEPTION);
! 60: }
! 61: Set_overflowflag();
! 62: *dstptr = result;
! 63: if (Is_inexacttrap_enabled() )
! 64: return(INEXACTEXCEPTION);
! 65: else Set_inexactflag();
! 66: return(NOEXCEPTION);
! 67: }
! 68: }
! 69: }
! 70: /*
! 71: * Generate result
! 72: */
! 73: if (src_exponent >= 0) {
! 74: temp = src;
! 75: Sgl_clear_signexponent_set_hidden(temp);
! 76: Int_from_sgl_mantissa(temp,src_exponent);
! 77: if (Sgl_isone_sign(src)) result = -Sgl_all(temp);
! 78: else result = Sgl_all(temp);
! 79: *dstptr = result;
! 80:
! 81: /* check for inexact */
! 82: if (Sgl_isinexact_to_fix(src,src_exponent)) {
! 83: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 84: else Set_inexactflag();
! 85: }
! 86: }
! 87: else {
! 88: *dstptr = 0;
! 89:
! 90: /* check for inexact */
! 91: if (Sgl_isnotzero_exponentmantissa(src)) {
! 92: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 93: else Set_inexactflag();
! 94: }
! 95: }
! 96: return(NOEXCEPTION);
! 97: }
! 98:
! 99: /*
! 100: * Single Floating-point to Double Fixed-point
! 101: */
! 102: /*ARGSUSED*/
! 103: int
! 104: sgl_to_dbl_fcnvfxt(srcptr, null, dstptr, status)
! 105: sgl_floating_point *srcptr, *null;
! 106: dbl_integer *dstptr;
! 107: unsigned int *status;
! 108: {
! 109: register int src_exponent, resultp1;
! 110: register unsigned int src, temp, resultp2;
! 111:
! 112: src = *srcptr;
! 113: src_exponent = Sgl_exponent(src) - SGL_BIAS;
! 114:
! 115: /*
! 116: * Test for overflow
! 117: */
! 118: if (src_exponent > DBL_FX_MAX_EXP) {
! 119: /* check for MININT */
! 120: if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
! 121: Sgl_isnotzero_mantissa(src) || Sgl_iszero_sign(src)) {
! 122: if( Sgl_isnan(src) )
! 123: /*
! 124: * On NaN go unimplemented.
! 125: */
! 126: return(UNIMPLEMENTEDEXCEPTION);
! 127: else {
! 128: if (Sgl_iszero_sign(src)) {
! 129: resultp1 = 0x7fffffff;
! 130: resultp2 = 0xffffffff;
! 131: }
! 132: else {
! 133: resultp1 = 0x80000000;
! 134: resultp2 = 0;
! 135: }
! 136: if (Is_overflowtrap_enabled()) {
! 137: if (Is_inexacttrap_enabled())
! 138: return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
! 139: else Set_inexactflag();
! 140: return(OVERFLOWEXCEPTION);
! 141: }
! 142: Set_overflowflag();
! 143: Dint_copytoptr(resultp1,resultp2,dstptr);
! 144: if (Is_inexacttrap_enabled() )
! 145: return(INEXACTEXCEPTION);
! 146: else Set_inexactflag();
! 147: return(NOEXCEPTION);
! 148: }
! 149: }
! 150: Dint_set_minint(resultp1,resultp2);
! 151: Dint_copytoptr(resultp1,resultp2,dstptr);
! 152: return(NOEXCEPTION);
! 153: }
! 154: /*
! 155: * Generate result
! 156: */
! 157: if (src_exponent >= 0) {
! 158: temp = src;
! 159: Sgl_clear_signexponent_set_hidden(temp);
! 160: Dint_from_sgl_mantissa(temp,src_exponent,resultp1,resultp2);
! 161: if (Sgl_isone_sign(src)) {
! 162: Dint_setone_sign(resultp1,resultp2);
! 163: }
! 164: Dint_copytoptr(resultp1,resultp2,dstptr);
! 165:
! 166: /* check for inexact */
! 167: if (Sgl_isinexact_to_fix(src,src_exponent)) {
! 168: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 169: else Set_inexactflag();
! 170: }
! 171: }
! 172: else {
! 173: Dint_setzero(resultp1,resultp2);
! 174: Dint_copytoptr(resultp1,resultp2,dstptr);
! 175:
! 176: /* check for inexact */
! 177: if (Sgl_isnotzero_exponentmantissa(src)) {
! 178: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 179: else Set_inexactflag();
! 180: }
! 181: }
! 182: return(NOEXCEPTION);
! 183: }
! 184:
! 185: /*
! 186: * Double Floating-point to Single Fixed-point
! 187: */
! 188: /*ARGSUSED*/
! 189: int
! 190: dbl_to_sgl_fcnvfxt(srcptr, null, dstptr, status)
! 191: dbl_floating_point *srcptr, *null;
! 192: int *dstptr;
! 193: unsigned int *status;
! 194: {
! 195: register unsigned int srcp1, srcp2, tempp1, tempp2;
! 196: register int src_exponent, result;
! 197:
! 198: Dbl_copyfromptr(srcptr,srcp1,srcp2);
! 199: src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
! 200:
! 201: /*
! 202: * Test for overflow
! 203: */
! 204: if (src_exponent > SGL_FX_MAX_EXP) {
! 205: /* check for MININT */
! 206: if (Dbl_isoverflow_to_int(src_exponent,srcp1,srcp2)) {
! 207: if( Dbl_isnan(srcp1,srcp2) )
! 208: /*
! 209: * On NaN go unimplemented.
! 210: */
! 211: return(UNIMPLEMENTEDEXCEPTION);
! 212: else {
! 213: if (Dbl_iszero_sign(srcp1)) result = 0x7fffffff;
! 214: else result = 0x80000000;
! 215:
! 216: if (Is_overflowtrap_enabled()) {
! 217: if (Is_inexacttrap_enabled())
! 218: return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
! 219: else Set_inexactflag();
! 220: return(OVERFLOWEXCEPTION);
! 221: }
! 222: Set_overflowflag();
! 223: *dstptr = result;
! 224: if (Is_inexacttrap_enabled() )
! 225: return(INEXACTEXCEPTION);
! 226: else Set_inexactflag();
! 227: return(NOEXCEPTION);
! 228: }
! 229: }
! 230: }
! 231: /*
! 232: * Generate result
! 233: */
! 234: if (src_exponent >= 0) {
! 235: tempp1 = srcp1;
! 236: tempp2 = srcp2;
! 237: Dbl_clear_signexponent_set_hidden(tempp1);
! 238: Int_from_dbl_mantissa(tempp1,tempp2,src_exponent);
! 239: if (Dbl_isone_sign(srcp1) && (src_exponent <= SGL_FX_MAX_EXP))
! 240: result = -Dbl_allp1(tempp1);
! 241: else result = Dbl_allp1(tempp1);
! 242: *dstptr = result;
! 243:
! 244: /* check for inexact */
! 245: if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
! 246: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 247: else Set_inexactflag();
! 248: }
! 249: }
! 250: else {
! 251: *dstptr = 0;
! 252:
! 253: /* check for inexact */
! 254: if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
! 255: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 256: else Set_inexactflag();
! 257: }
! 258: }
! 259: return(NOEXCEPTION);
! 260: }
! 261:
! 262: /*
! 263: * Double Floating-point to Double Fixed-point
! 264: */
! 265: /*ARGSUSED*/
! 266: int
! 267: dbl_to_dbl_fcnvfxt(srcptr, null, dstptr, status)
! 268: dbl_floating_point *srcptr, *null;
! 269: dbl_integer *dstptr;
! 270: unsigned int *status;
! 271: {
! 272: register int src_exponent, resultp1;
! 273: register unsigned int srcp1, srcp2, tempp1, tempp2, resultp2;
! 274:
! 275: Dbl_copyfromptr(srcptr,srcp1,srcp2);
! 276: src_exponent = Dbl_exponent(srcp1) - DBL_BIAS;
! 277:
! 278: /*
! 279: * Test for overflow
! 280: */
! 281: if (src_exponent > DBL_FX_MAX_EXP) {
! 282: /* check for MININT */
! 283: if ((src_exponent > DBL_FX_MAX_EXP + 1) ||
! 284: Dbl_isnotzero_mantissa(srcp1,srcp2) || Dbl_iszero_sign(srcp1)) {
! 285: if( Dbl_isnan(srcp1,srcp2) )
! 286: /*
! 287: * On NaN go unimplemented.
! 288: */
! 289: return(UNIMPLEMENTEDEXCEPTION);
! 290: else {
! 291: if (Dbl_iszero_sign(srcp1)) {
! 292: resultp1 = 0x7fffffff;
! 293: resultp2 = 0xffffffff;
! 294: }
! 295: else {
! 296: resultp1 = 0x80000000;
! 297: resultp2 = 0;
! 298: }
! 299: if (Is_overflowtrap_enabled()) {
! 300: if (Is_inexacttrap_enabled())
! 301: return(OVERFLOWEXCEPTION|INEXACTEXCEPTION);
! 302: else Set_inexactflag();
! 303: return(OVERFLOWEXCEPTION);
! 304: }
! 305: Set_overflowflag();
! 306: Dint_copytoptr(resultp1,resultp2,dstptr);
! 307: if (Is_inexacttrap_enabled() )
! 308: return(INEXACTEXCEPTION);
! 309: else Set_inexactflag();
! 310: return(NOEXCEPTION);
! 311: }
! 312: }
! 313: }
! 314: /*
! 315: * Generate result
! 316: */
! 317: if (src_exponent >= 0) {
! 318: tempp1 = srcp1;
! 319: tempp2 = srcp2;
! 320: Dbl_clear_signexponent_set_hidden(tempp1);
! 321: Dint_from_dbl_mantissa(tempp1,tempp2,src_exponent,
! 322: resultp1,resultp2);
! 323: if (Dbl_isone_sign(srcp1)) {
! 324: Dint_setone_sign(resultp1,resultp2);
! 325: }
! 326: Dint_copytoptr(resultp1,resultp2,dstptr);
! 327:
! 328: /* check for inexact */
! 329: if (Dbl_isinexact_to_fix(srcp1,srcp2,src_exponent)) {
! 330: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 331: else Set_inexactflag();
! 332: }
! 333: }
! 334: else {
! 335: Dint_setzero(resultp1,resultp2);
! 336: Dint_copytoptr(resultp1,resultp2,dstptr);
! 337:
! 338: /* check for inexact */
! 339: if (Dbl_isnotzero_exponentmantissa(srcp1,srcp2)) {
! 340: if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION);
! 341: else Set_inexactflag();
! 342: }
! 343: }
! 344: return(NOEXCEPTION);
! 345: }
CVSweb