1 /* $NetBSD: fpu_int.c,v 1.7 2005/12/11 12:17:52 christos Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Ken Nakata 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * @(#)fpu_int.c 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: fpu_int.c,v 1.7 2005/12/11 12:17:52 christos Exp $"); 33 34 #include <sys/types.h> 35 36 #include <machine/reg.h> 37 38 #include "fpu_arith.h" 39 #include "fpu_emulate.h" 40 41 /* FINTRZ - always round to zero */ 42 struct fpn * 43 fpu_intrz(fe) 44 struct fpemu *fe; 45 { 46 register struct fpn *x = &fe->fe_f2; 47 register int sh, clr, mask, i; 48 49 /* special cases first */ 50 if (x->fp_class != FPC_NUM) { 51 return x; 52 } 53 /* when |x| < 1.0 */ 54 if (x->fp_exp < 0) { 55 x->fp_class = FPC_ZERO; 56 x->fp_mant[0] = x->fp_mant[1] = x->fp_mant[2] = 0; 57 return x; 58 } 59 60 /* real work */ 61 sh = FP_NMANT - 1 - x->fp_exp; 62 if (sh <= 0) { 63 return x; 64 } 65 66 clr = 2 - sh / 32; 67 mask = (0xffffffff << (sh % 32)); 68 69 for (i = 2; i > clr; i--) { 70 x->fp_mant[i] = 0; 71 } 72 x->fp_mant[i] &= mask; 73 74 return x; 75 } 76 77 /* FINT */ 78 struct fpn * 79 fpu_int(fe) 80 struct fpemu *fe; 81 { 82 register struct fpn *x = &fe->fe_f2; 83 register int rsh, lsh, wsh, i; 84 85 /* special cases first */ 86 if (x->fp_class != FPC_NUM) { 87 return x; 88 } 89 /* even if we have exponent == -1, we still have possiblity 90 that the result >= 1.0 when mantissa ~= 1.0 and rounded up */ 91 if (x->fp_exp < -1) { 92 x->fp_class = FPC_ZERO; 93 x->fp_mant[0] = x->fp_mant[1] = x->fp_mant[2] = 0; 94 return x; 95 } 96 97 /* real work */ 98 rsh = FP_NMANT - 1 - x->fp_exp; 99 if (rsh - FP_NG <= 0) { 100 return x; 101 } 102 103 fpu_shr(x, rsh - FP_NG); /* shift to the right */ 104 105 if (fpu_round(fe, x) == 1 /* rounded up */ && 106 x->fp_mant[2 - (FP_NMANT-rsh)/32] & (1 << ((FP_NMANT-rsh)%32)) 107 /* x >= 2.0 */) { 108 rsh--; /* reduce shift count by 1 */ 109 x->fp_exp++; /* adjust exponent */ 110 } 111 112 /* shift it back to the left */ 113 wsh = rsh / 32; 114 lsh = rsh % 32; 115 rsh = 32 - lsh; 116 for (i = 0; i + wsh < 2; i++) { 117 x->fp_mant[i] = (x->fp_mant[i+wsh] << lsh) | (x->fp_mant[i+wsh+1] >> rsh); 118 } 119 x->fp_mant[i] = (x->fp_mant[i+wsh] << lsh); 120 i++; 121 for (; i < 3; i++) { 122 x->fp_mant[i] = 0; 123 } 124 125 return x; 126 } 127