1/**************************************************************** 2 3The author of this software is David M. Gay. 4 5Copyright (C) 1998-2001 by Lucent Technologies 6All Rights Reserved 7 8Permission to use, copy, modify, and distribute this software and 9its documentation for any purpose and without fee is hereby 10granted, provided that the above copyright notice appear in all 11copies and that both that the copyright notice and this 12permission notice and warranty disclaimer appear in supporting 13documentation, and that the name of Lucent or any of its entities 14not be used in advertising or publicity pertaining to 15distribution of the software without specific, written prior 16permission. 17 18LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25THIS SOFTWARE. 26 27****************************************************************/ 28 29/* Please send bug reports to David M. Gay (dmg at acm dot org, 30 * with " at " changed at "@" and " dot " changed to "."). */ 31 32/* Test program for g_ddfmt, strtoIdd, strtopdd, and strtordd. 33 * 34 * Inputs (on stdin): 35 * r rounding_mode 36 * n ndig 37 * number 38 * #hex0 hex1 hex2 hex3 39 * 40 * rounding_mode values: 41 * 0 = toward zero 42 * 1 = nearest 43 * 2 = toward +Infinity 44 * 3 = toward -Infinity 45 * 46 * where number is a decimal floating-point number, 47 * hex0 is a string of <= 8 Hex digits for the most significant 48 * word of the number, hex1 is a similar string for the next 49 * word, etc., and ndig is a parameters to g_ddfmt. 50 */ 51 52#include "gdtoaimp.h" 53#include <stdio.h> 54#include <stdlib.h> 55 56 extern int getround ANSI((int,char*)); 57 58 static char ibuf[2048], obuf[1024]; 59 60#define U (unsigned long) 61 62 static void 63#ifdef KR_headers 64dprint(what, d) char *what; double d; 65#else 66dprint(char *what, double d) 67#endif 68{ 69 char buf[32]; 70 union { double d; ULong L[2]; } u; 71 72 u.d = d; 73 g_dfmt(buf,&d,0,sizeof(buf)); 74 printf("%s = %s = #%lx %lx\n", what, buf, U u.L[_0], U u.L[_1]); 75 } 76 77 int 78main(Void) 79{ 80 char *s, *s1, *se, *se1; 81 int dItry, i, j, r = 1, ndig = 0; 82 double ddI[4]; 83 long LL[4]; 84 union { double dd[2]; ULong L[4]; } u; 85 86 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 87 while(*s <= ' ') 88 if (!*s++) 89 continue; 90 dItry = 0; 91 switch(*s) { 92 case 'r': 93 r = getround(r, s); 94 continue; 95 case 'n': 96 i = s[1]; 97 if (i <= ' ' || (i >= '0' && i <= '9')) { 98 ndig = atoi(s+1); 99 continue; 100 } 101 break; /* nan? */ 102 case '#': 103 LL[0] = u.L[_0]; 104 LL[1] = u.L[_1]; 105 LL[2] = u.L[2+_0]; 106 LL[3] = u.L[2+_1]; 107 sscanf(s+1, "%lx %lx %lx %lx", &LL[0], &LL[1], 108 &LL[2], &LL[3]); 109 u.L[_0] = LL[0]; 110 u.L[_1] = LL[1]; 111 u.L[2+_0] = LL[2]; 112 u.L[2+_1] = LL[3]; 113 printf("\nInput: %s", ibuf); 114 printf(" --> f = #%lx %lx %lx %lx\n", 115 LL[0],LL[1],LL[2],LL[3]); 116 goto fmt_test; 117 } 118 printf("\nInput: %s", ibuf); 119 for(s1 = s; *s1 > ' '; s1++){}; 120 while(*s1 <= ' ' && *s1) s1++; 121 if (!*s1) { 122 dItry = 1; 123 i = strtordd(ibuf, &se, r, u.dd); 124 if (r == 1) { 125 j = strtopdd(ibuf, &se1, ddI); 126 if (i != j || u.dd[0] != ddI[0] 127 || u.dd[1] != ddI[1] || se != se1) 128 printf("***strtopdd and strtordd disagree!!\n:"); 129 } 130 printf("strtopdd consumes %d bytes and returns %d\n", 131 (int)(se-ibuf), i); 132 } 133 else { 134 u.dd[0] = strtod(s, &se); 135 u.dd[1] = strtod(se, &se); 136 } 137 fmt_test: 138 dprint("dd[0]", u.dd[0]); 139 dprint("dd[1]", u.dd[1]); 140 se = g_ddfmt(obuf, u.dd, ndig, sizeof(obuf)); 141 printf("g_ddfmt(%d) gives %d bytes: \"%s\"\n\n", 142 ndig, (int)(se-obuf), se ? obuf : "<null>"); 143 if (!dItry) 144 continue; 145 printf("strtoIdd returns %d,", strtoIdd(ibuf, &se, ddI,&ddI[2])); 146 printf(" consuming %d bytes.\n", (int)(se-ibuf)); 147 if (ddI[0] == ddI[2] && ddI[1] == ddI[3]) { 148 if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1]) 149 printf("ddI[0] == ddI[1] == strtopdd\n"); 150 else 151 printf("ddI[0] == ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %17.g\n", 152 U ((ULong*)ddI)[_0], 153 U ((ULong*)ddI)[_1], 154 U ((ULong*)ddI)[2+_0], 155 U ((ULong*)ddI)[2+_1], 156 ddI[0], ddI[1]); 157 } 158 else { 159 printf("ddI[0] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n", 160 U ((ULong*)ddI)[_0], U ((ULong*)ddI)[_1], 161 U ((ULong*)ddI)[2+_0], U ((ULong*)ddI)[2+_1], 162 ddI[0], ddI[1]); 163 printf("ddI[1] = #%lx %lx + %lx %lx\n= %.17g + %.17g\n", 164 U ((ULong*)ddI)[4+_0], U ((ULong*)ddI)[4+_1], 165 U ((ULong*)ddI)[6+_0], U ((ULong*)ddI)[6+_1], 166 ddI[2], ddI[3]); 167 if (ddI[0] == u.dd[0] && ddI[1] == u.dd[1]) 168 printf("ddI[0] == strtod\n"); 169 else if (ddI[2] == u.dd[0] && ddI[3] == u.dd[1]) 170 printf("ddI[1] == strtod\n"); 171 else 172 printf("**** Both differ from strtopdd ****\n"); 173 } 174 printf("\n"); 175 } 176 return 0; 177 } 178