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_Qfmt, strtoIQ, strtopQ, and strtorQ. 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_Qfmt. 50 */ 51 52#include "gdtoa.h" 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56 57 extern int getround ANSI((int,char*)); 58 59 static char ibuf[2048], obuf[2048]; 60#undef _0 61#undef _1 62 63/* one or the other of IEEE_BIG_ENDIAN or IEEE_LITTLE_ENDIAN should be #defined */ 64 65#ifdef IEEE_BIG_ENDIAN 66#define _0 0 67#define _1 1 68#define _2 2 69#define _3 3 70#endif 71#ifdef IEEE_LITTLE_ENDIAN 72#define _0 3 73#define _1 2 74#define _2 1 75#define _3 0 76#endif 77 78#define U (unsigned long) 79 80 int 81main(Void) 82{ 83 char *s, *s1, *se, *se1; 84 int Ltest, i, dItry, ndig = 0, r = 1; 85 union { long double d; ULong bits[4]; } u, v[2], w; 86 87 w.bits[0] = w.bits[3] = 0; 88 w.d = 1.; 89 u.d = 3.; 90 w.d = w.d / u.d; 91 Ltest = sizeof(long double) == 16 && w.bits[0] && w.bits[3]; 92 while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { 93 while(*s <= ' ') 94 if (!*s++) 95 continue; 96 dItry = 0; 97 switch(*s) { 98 case 'r': 99 r = getround(r, s); 100 continue; 101 case 'n': 102 i = s[1]; 103 if (i <= ' ' || (i >= '0' && i <= '9')) { 104 ndig = atoi(s+1); 105 continue; 106 } 107 break; /* nan? */ 108 case '#': 109 /* sscanf(s+1, "%lx %lx %lx %lx", &u.bits[_0], */ 110 /* &u.bits[_1], &u.bits[_2], &u.bits[_3]); */ 111 u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16); 112 if (se > s1) { 113 u.bits[_1] = (ULong)strtoul(s1 = se, &se, 16); 114 if (se > s1) { 115 u.bits[_2] = (ULong)strtoul(s1 = se, &se, 16); 116 if (se > s1) 117 u.bits[_3] = (ULong)strtoul(s1 = se, &se, 16); 118 } 119 } 120 printf("\nInput: %s", ibuf); 121 printf(" --> f = #%lx %lx %lx %lx\n", U u.bits[_0], 122 U u.bits[_1], U u.bits[_2], U u.bits[_3]); 123 goto fmt_test; 124 } 125 dItry = 1; 126 printf("\nInput: %s", ibuf); 127 i = strtorQ(ibuf, &se, r, u.bits); 128 if (r == 1 && (strtopQ(ibuf,&se1,v[0].bits) != i 129 || se != se1 || memcmp(u.bits, v[0].bits, 16))) 130 printf("***strtoQ and strtorQ disagree!!\n:"); 131 printf("\nstrtoQ consumes %d bytes and returns %d\n", 132 (int)(se-ibuf), i); 133 printf("with bits = #%lx %lx %lx %lx\n", 134 U u.bits[_0], U u.bits[_1], U u.bits[_2], U u.bits[_3]); 135 fmt_test: 136 if (Ltest) 137 printf("printf(\"%%.35Lg\") gives %.35Lg\n", u.d); 138 se = g_Qfmt(obuf, u.bits, ndig, sizeof(obuf)); 139 printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig, 140 (int)(se-obuf), se ? obuf : "<null>"); 141 if (!dItry) 142 continue; 143 printf("strtoIQ returns %d,", 144 strtoIQ(ibuf, &se, v[0].bits, v[1].bits)); 145 printf(" consuming %d bytes.\n", (int)(se-ibuf)); 146 if (!memcmp(v[0].bits, v[1].bits, 16)) { 147 if (!memcpy(u.bits, v[0].bits, 16)) 148 printf("fI[0] == fI[1] == strtoQ\n"); 149 else { 150 printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n", 151 U v[0].bits[_0], U v[0].bits[_1], 152 U v[0].bits[_2], U v[0].bits[_3]); 153 if (Ltest) 154 printf("= %.35Lg\n", v[0].d); 155 } 156 } 157 else { 158 printf("fI[0] = #%lx %lx %lx %lx\n", 159 U v[0].bits[_0], U v[0].bits[_1], 160 U v[0].bits[_2], U v[0].bits[_3]); 161 if (Ltest) 162 printf("= %.35Lg\n", v[0].d); 163 printf("fI[1] = #%lx %lx %lx %lx\n", 164 U v[1].bits[_0], U v[1].bits[_1], 165 U v[1].bits[_2], U v[1].bits[_3]); 166 if (Ltest) 167 printf("= %.35Lg\n", v[1].d); 168 if (!memcmp(v[0].bits, u.bits, 16)) 169 printf("fI[0] == strtod\n"); 170 else if (!memcmp(v[1].bits, u.bits, 16)) 171 printf("fI[1] == strtod\n"); 172 else 173 printf("**** Both differ from strtod ****\n"); 174 } 175 printf("\n"); 176 } 177 return 0; 178 } 179