Home | History | Annotate | Line # | Download | only in gdtoa
strtopdd.c revision 1.2
      1 /* $NetBSD: strtopdd.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */
      2 
      3 /****************************************************************
      4 
      5 The author of this software is David M. Gay.
      6 
      7 Copyright (C) 1998, 2000 by Lucent Technologies
      8 All Rights Reserved
      9 
     10 Permission to use, copy, modify, and distribute this software and
     11 its documentation for any purpose and without fee is hereby
     12 granted, provided that the above copyright notice appear in all
     13 copies and that both that the copyright notice and this
     14 permission notice and warranty disclaimer appear in supporting
     15 documentation, and that the name of Lucent or any of its entities
     16 not be used in advertising or publicity pertaining to
     17 distribution of the software without specific, written prior
     18 permission.
     19 
     20 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     21 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
     22 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
     23 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
     25 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
     26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
     27 THIS SOFTWARE.
     28 
     29 ****************************************************************/
     30 
     31 /* Please send bug reports to David M. Gay (dmg at acm dot org,
     32  * with " at " changed at "@" and " dot " changed to ".").	*/
     33 
     34 #include "gdtoaimp.h"
     35 
     36  int
     37 #ifdef KR_headers
     38 strtopdd(s, sp, dd) CONST char *s; char **sp; double *dd;
     39 #else
     40 strtopdd(CONST char *s, char **sp, double *dd)
     41 #endif
     42 {
     43 #ifdef Sudden_Underflow
     44 	static FPI fpi = { 106, 1-1023, 2046-1023-106+1, 1, 1 };
     45 #else
     46 	static FPI fpi = { 106, 1-1023-53+1, 2046-1023-106+1, 1, 0 };
     47 #endif
     48 	ULong bits[4];
     49 	Long exp;
     50 	int i, j, rv;
     51 	typedef union {
     52 		double d[2];
     53 		ULong L[4];
     54 		} U;
     55 	U *u;
     56 
     57 	rv = strtodg(s, sp, &fpi, &exp, bits);
     58 	if (rv == STRTOG_NoMemory)
     59 		return rv;
     60 	u = (U*)dd;
     61 	switch(rv & STRTOG_Retmask) {
     62 	  case STRTOG_NoNumber:
     63 	  case STRTOG_Zero:
     64 		u->d[0] = u->d[1] = 0.;
     65 		break;
     66 
     67 	  case STRTOG_Normal:
     68 		u->L[_1] = (bits[1] >> 21 | bits[2] << 11) & 0xffffffffL;
     69 		u->L[_0] = bits[2] >> 21 | bits[3] << 11 & 0xfffff
     70 			  | exp + 0x3ff + 105 << 20;
     71 		exp += 0x3ff + 52;
     72 		if (bits[1] &= 0x1fffff) {
     73 			i = hi0bits(bits[1]) - 11;
     74 			if (i >= exp) {
     75 				i = exp - 1;
     76 				exp = 0;
     77 				}
     78 			else
     79 				exp -= i;
     80 			if (i > 0) {
     81 				bits[1] = bits[1] << i | bits[0] >> 32-i;
     82 				bits[0] = bits[0] << i & 0xffffffffL;
     83 				}
     84 			}
     85 		else if (bits[0]) {
     86 			i = hi0bits(bits[0]) + 21;
     87 			if (i >= exp) {
     88 				i = exp - 1;
     89 				exp = 0;
     90 				}
     91 			else
     92 				exp -= i;
     93 			if (i < 32) {
     94 				bits[1] = bits[0] >> 32 - i;
     95 				bits[0] = bits[0] << i & 0xffffffffL;
     96 				}
     97 			else {
     98 				bits[1] = bits[0] << i - 32;
     99 				bits[0] = 0;
    100 				}
    101 			}
    102 		else {
    103 			u->L[2] = u->L[3] = 0;
    104 			break;
    105 			}
    106 		u->L[2+_1] = bits[0];
    107 		u->L[2+_0] = bits[1] & 0xfffff | exp << 20;
    108 		break;
    109 
    110 	  case STRTOG_Denormal:
    111 		if (bits[3])
    112 			goto nearly_normal;
    113 		if (bits[2])
    114 			goto partly_normal;
    115 		if (bits[1] & 0xffe00000)
    116 			goto hardly_normal;
    117 		/* completely denormal */
    118 		u->L[2] = u->L[3] = 0;
    119 		u->L[_1] = bits[0];
    120 		u->L[_0] = bits[1];
    121 		break;
    122 
    123 	  nearly_normal:
    124 		i = hi0bits(bits[3]) - 11;	/* i >= 12 */
    125 		j = 32 - i;
    126 		u->L[_0] = (bits[3] << i | bits[2] >> j) & 0xfffff
    127 			| 65 - i << 20;
    128 		u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
    129 		u->L[2+_0] = bits[1] & (1L << j) - 1;
    130 		u->L[2+_1] = bits[0];
    131 		break;
    132 
    133 	  partly_normal:
    134 		i = hi0bits(bits[2]) - 11;
    135 		if (i < 0) {
    136 			j = -i;
    137 			i += 32;
    138 			u->L[_0] = bits[2] >> j & 0xfffff | (33 + j) << 20;
    139 			u->L[_1] = (bits[2] << i | bits[1] >> j) & 0xffffffffL;
    140 			u->L[2+_0] = bits[1] & (1L << j) - 1;
    141 			u->L[2+_1] = bits[0];
    142 			break;
    143 			}
    144 		if (i == 0) {
    145 			u->L[_0] = bits[2] & 0xfffff | 33 << 20;
    146 			u->L[_1] = bits[1];
    147 			u->L[2+_0] = 0;
    148 			u->L[2+_1] = bits[0];
    149 			break;
    150 			}
    151 		j = 32 - i;
    152 		u->L[_0] = (bits[2] << i | bits[1] >> j) & 0xfffff
    153 				| j + 1 << 20;
    154 		u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
    155 		u->L[2+_0] = 0;
    156 		u->L[2+_1] = bits[0] & (1L << j) - 1;
    157 		break;
    158 
    159 	  hardly_normal:
    160 		j = 11 - hi0bits(bits[1]);
    161 		i = 32 - j;
    162 		u->L[_0] = bits[1] >> j & 0xfffff | j + 1 << 20;
    163 		u->L[_1] = (bits[1] << i | bits[0] >> j) & 0xffffffffL;
    164 		u->L[2+_0] = 0;
    165 		u->L[2+_1] = bits[0] & (1L << j) - 1;
    166 		break;
    167 
    168 	  case STRTOG_Infinite:
    169 		u->L[_0] = u->L[2+_0] = 0x7ff00000;
    170 		u->L[_1] = u->L[2+_1] = 0;
    171 		break;
    172 
    173 	  case STRTOG_NaN:
    174 		u->L[0] = u->L[2] = d_QNAN0;
    175 		u->L[1] = u->L[3] = d_QNAN1;
    176 	  }
    177 	if (rv & STRTOG_Neg) {
    178 		u->L[  _0] |= 0x80000000L;
    179 		u->L[2+_0] |= 0x80000000L;
    180 		}
    181 	return rv;
    182 	}
    183