Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
 
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
Line 73... Line 103...
73
#include "flpt.h"
103
#include "flpt.h"
74
 
104
 
75
/* MACROS */
105
/* MACROS */
76
 
106
 
77
#define MAX_USEFUL_DECEXP 10000
107
#define MAX_USEFUL_DECEXP 10000
78
	/* plenty big enough for IEEE and VAX */
108
/* plenty big enough for IEEE and VAX */
79
#define initial_flpts 50
109
#define initial_flpts 50
80
#define extra_flpts 50
110
#define extra_flpts 50
81
 
111
 
82
 
112
 
83
 
113
 
Line 86... Line 116...
86
 NB - lines which assume the use of an ASCII character set are accompanied
116
 NB - lines which assume the use of an ASCII character set are accompanied
87
      by the comment ASCII
117
      by the comment ASCII
88
 ***********************************************************************/
118
 ***********************************************************************/
89
 
119
 
90
typedef struct _dbl {
120
typedef struct _dbl {
91
				/* Double precision type - used for
121
  /* Double precision type - used for internal working */
92
				   internal working */
-
 
93
  Fdig  mant[2 * MANT_SIZE];
122
  Fdig mant[2 * MANT_SIZE];
94
  int   sign;
123
  int  sign;
95
  int  exp;
124
  int  exp;
96
}                   dbl;
125
} dbl;
97
 
126
 
98
/* The current rounding rule. This may be one of the following :
127
/* The current rounding rule. This may be one of the following :
99
     R2ZERO - round towards zero.
128
     R2ZERO - round towards zero.
100
     R2PINF - round towards positive infinity.
129
     R2PINF - round towards positive infinity.
101
     R2NINF - round towards negative infinity.
130
     R2NINF - round towards negative infinity.
Line 103... Line 132...
103
 It should be set using the "set_round" function.
132
 It should be set using the "set_round" function.
104
*/
133
*/
105
 
134
 
106
/* IDENTITIES */
135
/* IDENTITIES */
107
 
136
 
108
static Fdig bitround [16] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
137
static Fdig bitround[16] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80,
109
			     0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000,
138
			     0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000,
110
			     0x4000, 0x8000};
139
			     0x4000, 0x8000};
111
static Fdig bitmask [17] = {0xffff, 0xfffe, 0xfffc, 0xfff8,
140
static Fdig bitmask[17] = {0xffff, 0xfffe, 0xfffc, 0xfff8,
112
			    0xfff0, 0xffe0, 0xffc0, 0xff80,
141
			    0xfff0, 0xffe0, 0xffc0, 0xff80,
113
			    0xff00, 0xfe00, 0xfc00, 0xf800,
142
			    0xff00, 0xfe00, 0xfc00, 0xf800,
114
			    0xf000, 0xe000, 0xc000, 0x8000, 0x0};
143
			    0xf000, 0xe000, 0xc000, 0x8000, 0x0};
115
 
144
 
116
static flt powers[16] = {
145
static flt powers[16] = {
Line 132... Line 161...
132
	{{35, 34546, 28609, 0, 0, 0, 0, 0},1, 3}
161
	{{35, 34546, 28609, 0, 0, 0, 0, 0},1, 3}
133
};
162
};
134
 
163
 
135
static int two_powers[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
164
static int two_powers[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
136
			    2048, 4096, 8192, 16384, 32768};
165
			    2048, 4096, 8192, 16384, 32768};
137
static int  round_type = R2NEAR;
166
static int round_type = R2NEAR;
138
 
167
 
139
/* VARIABLES */
168
/* VARIABLES */
140
/* All variables initialised */
169
/* All variables initialised */
141
 
170
 
142
int  tot_flpts;		/* total number of floating point numbers
171
int tot_flpts;		/* total number of floating point numbers */
143
				*/
-
 
144
int  flptfree;			/* floating point free list */
172
int flptfree;		/* floating point free list */
145
int  flpt_left;		/* number of floating point numbers left
173
int flpt_left;		/* number of floating point numbers left */
146
				*/
-
 
147
 
174
 
148
flt * flptnos;			/* the extendable array of floating point
175
flt *flptnos;		/* the extendable array of floating point numbers */
149
				   numbers */
-
 
150
 
176
 
151
int fzero_no;
177
int fzero_no;
152
int fone_no;
178
int fone_no;
153
 
-
 
154
 
179
 
155
 
180
 
156
/* PROCEDURES */
181
/* PROCEDURES */
157
 
182
 
158
flpt new_flpt PROTO_S ((void));
183
flpt new_flpt(void);
159
 
184
 
160
/* ----- internal functions ----- */
185
/* ----- internal functions ----- */
161
 
186
 
162
 
187
 
163
/* "f1" and "f2" are two legal single
-
 
164
   precision numbers. The result is -1, 0
188
/* "f1" and "f2" are two legal single precision numbers. The result is -1, 0 or
165
   or +1 depending on whether the
-
 
166
   magnitude of "f1" is less than, equal
189
 * +1 depending on whether the magnitude of "f1" is less than, equal to or
167
   to or greater than that of "f2" */
190
 * greater than that of "f2" */
-
 
191
 
168
static int  mag_cmp
192
static int
169
    PROTO_N ( (f1, f2) )
-
 
170
    PROTO_T ( flt f1 X flt f2 )
193
mag_cmp(flt f1, flt f2)
171
{
194
{
172
  int   i;
195
  int i;
173
  if (f1.sign == 0) {
196
  if (f1.sign == 0) {
174
    if (f2.sign == 0) {
197
    if (f2.sign == 0) {
175
      return (0);
198
      return(0);
176
    }
199
    }
177
    return (-1);
200
    return(-1);
178
  }
201
  }
179
  if (f2.sign == 0) {
202
  if (f2.sign == 0) {
180
    return (1);
203
    return(1);
181
  }
204
  }
182
  if (f1.exp != f2.exp) {
205
  if (f1.exp != f2.exp) {
183
    return ((f1.exp < f2.exp) ? -1 : 1);
206
    return((f1.exp < f2.exp) ? -1 : 1);
184
  }
207
  }
185
  for (i = 0; i < MANT_SIZE; i++) {
208
  for (i = 0; i < MANT_SIZE; i++) {
186
    if ((f1.mant[i]) != (f2.mant[i])) {
209
    if ((f1.mant[i]) != (f2.mant[i])) {
187
      return (((f1.mant[i]) < (f2.mant[i])) ? -1 : 1);
210
      return(((f1.mant[i]) < (f2.mant[i])) ? -1 : 1);
188
    }
211
    }
189
  }
212
  }
190
  return (0);
213
  return(0);
191
}
214
}
192
 
215
 
193
 
216
 
194
/* "d1" and "d2" are two legal double
-
 
195
   precision numbers. The result is -1, 0
217
/* "d1" and "d2" are two legal double precision numbers. The result is -1, 0 or
196
   or +1 depending on whether the
-
 
197
   magnitude of "d1" is less than, equal
218
 * +1 depending on whether the magnitude of "d1" is less than, equal to or
198
   to or greater than that of "d2" */
219
 * greater than that of "d2" */
-
 
220
 
199
static int  dbl_mag_cmp
221
static int
200
    PROTO_N ( (d1, d2) )
-
 
201
    PROTO_T ( dbl d1 X dbl d2 )
222
dbl_mag_cmp (dbl d1, dbl d2)
202
{
223
{
203
  int   i;
224
  int i;
204
  if (d1.sign == 0) {
225
  if (d1.sign == 0) {
205
    if (d2.sign == 0) {
226
    if (d2.sign == 0) {
206
      return (0);
227
      return(0);
207
    }
228
    }
208
    return (-1);
229
    return(-1);
209
  }
230
  }
210
  if (d2.sign == 0) {
231
  if (d2.sign == 0) {
211
    return (1);
232
    return(1);
212
  }
233
  }
213
  if (d1.exp != d2.exp) {
234
  if (d1.exp != d2.exp) {
214
    return ((d1.exp < d2.exp) ? -1 : 1);
235
    return((d1.exp < d2.exp) ? -1 : 1);
215
  }
236
  }
216
  for (i = 0; i < (2 * MANT_SIZE); i++) {
237
  for (i = 0; i < (2 * MANT_SIZE); i++) {
217
    if ((d1.mant[i]) != (d2.mant[i])) {
238
    if ((d1.mant[i]) != (d2.mant[i])) {
218
      return (((d1.mant[i]) < (d2.mant[i])) ? -1 : 1);
239
      return(((d1.mant[i]) < (d2.mant[i]))? -1 : 1);
219
    }
240
    }
220
  }
241
  }
221
  return (0);
242
  return(0);
222
}
243
}
223
 
244
 
224
 
245
 
225
/* "f" is a pointer to a legal single
-
 
226
   precision number. "c" is a character
246
/* "f" is a pointer to a legal single precision number. "c" is a character
227
   value from 0 to FBASE-1. On return, the
247
 * value from 0 to FBASE-1. On return, the single precision number pointed to
228
   single precision number pointed to by
-
 
229
   "f" will have been modified as if "c"
248
 * by "f" will have been modified as if "c" were the digit following the least
230
   were the digit following the least
-
 
231
   significant digit of the number pointed
249
 * significant digit of the number pointed to by "f" and the number has been
232
   to by "f" and the number has been
-
 
233
   rounded according to the current
250
 * rounded according to the current rounding rule */
234
   rounding rule */
-
 
235
static  void flt_int_round
251
static void
236
    PROTO_N ( (f, ic) )
-
 
237
    PROTO_T ( flt * f X unsigned int ic )
252
flt_int_round(flt *f, unsigned int ic)
238
{
253
{
239
  int   i;
254
  int i;
240
  switch (round_type) {
255
  switch (round_type) {
241
    default:
256
  default:
242
    case R2ZERO:
257
  case R2ZERO:
-
 
258
    return;
-
 
259
  case R2PINF:
-
 
260
    if (((f->sign) != 1) || (ic == 0))
-
 
261
      return;
-
 
262
    break;
-
 
263
  case R2NINF:
-
 
264
    if (((f->sign) != -1) || (ic == 0))
-
 
265
      return;
-
 
266
    break;
-
 
267
  case R2NEAR:
-
 
268
    if (ic < (unsigned int)(FBASE / 2)) {
243
      return;
269
      return;
244
    case R2PINF:
-
 
245
      if (((f -> sign) != 1) || (ic == 0))
-
 
246
	return;
-
 
247
      break;
270
    }
248
    case R2NINF:
-
 
249
      if (((f -> sign) != -1) || (ic == 0))
-
 
250
	return;
-
 
251
      break;
-
 
252
    case R2NEAR:
-
 
253
      if (ic < (unsigned int)(FBASE/2))
-
 
254
	return;
-
 
255
      break;
271
    break;
256
  }
272
  }
257
  ic = 1;
273
  ic = 1;
258
  for (i = (MANT_SIZE - 1); i >= 0; i--) {
274
  for (i = (MANT_SIZE - 1); i >= 0; i--) {
259
    (f -> mant)[i] = (unsigned short)
-
 
260
			((ic += (unsigned int)((f -> mant)[i])) % FBASE);
275
    (f->mant)[i] = (unsigned short)((ic += (unsigned int)((f->mant)[i])) % FBASE);
261
	/* CAST:jmf: unsigned short since FBASE <= MAX_USHORT+1 */
276
    /* CAST:jmf: unsigned short since FBASE <= MAX_USHORT+1 */
262
    if ((ic /= FBASE) == 0) {
277
    if ((ic /= FBASE) == 0) {
263
      return;
278
      return;
264
    }
279
    }
265
  }
280
  }
266
  (f -> exp)++;
281
  (f->exp)++;
267
  (f -> mant)[0] = 1;
282
  (f->mant)[0] = 1;
268
  for (i = 1; i < MANT_SIZE; i++) {
283
  for (i = 1; i < MANT_SIZE; i++) {
269
    (f -> mant)[i] = 0;
284
    (f->mant)[i] = 0;
-
 
285
  }
-
 
286
}
-
 
287
 
-
 
288
 
-
 
289
/* "d" is a legal double precision number.  "f" is a pointer to a single
-
 
290
 * precision number. On return, the number pointed to by "f" contains the value
-
 
291
 * of "d" but to less precision. The current rounding rule is used to truncate
-
 
292
 * the mantissa */
-
 
293
static void
-
 
294
dbl2float(dbl d, flt *f)
-
 
295
{
-
 
296
  int i;
-
 
297
  (f->sign) = d.sign;
-
 
298
  (f->exp) = d.exp;
-
 
299
  for (i = 0; i < MANT_SIZE; i++) {
-
 
300
    (f->mant)[i] = d.mant[i];
270
  }
301
  }
-
 
302
  flt_int_round(f, (unsigned int)d.mant[MANT_SIZE]);
271
}
303
}
272
 
304
 
273
 
305
 
274
/* "d" is a legal double precision number.
306
/* "f" is a legal single precision number.  "d" is a pointer to a double
275
   "f" is a pointer to a single precision
-
 
276
   number. On return, the number pointed
307
 * precision number. On return, the number pointed to by "d" will contain the
277
   to by "f" contains the value of "d" but
308
 * value of "f" */
278
   to less precision. The current rounding
-
 
279
   rule is used to truncate the mantissa
-
 
280
*/
309
 
281
static  void dbl2float
310
static void
282
    PROTO_N ( (d, f) )
311
flt2double(flt f, dbl *d)
283
    PROTO_T ( dbl d X flt * f )
-
 
284
{
312
{
285
  int   i;
313
  int i;
286
  (f -> sign) = d.sign;
314
  (d->sign) = f.sign;
287
  (f -> exp) = d.exp;
315
  (d->exp) = f.exp;
288
  for (i = 0; i < MANT_SIZE; i++) {
316
  for (i = 0; i < MANT_SIZE; i++) {
289
    (f -> mant)[i] = d.mant[i];
-
 
290
  }
-
 
291
  flt_int_round (f, (unsigned int)d.mant[MANT_SIZE]);
-
 
292
}
-
 
293
 
-
 
294
 
-
 
295
/* "f" is a legal single precision number.
-
 
296
   "d" is a pointer to a double precision
-
 
297
   number. On return, the number pointed
-
 
298
   to by "d" will contain the value of "f"
-
 
299
   */
-
 
300
static  void flt2double
-
 
301
    PROTO_N ( (f, d) )
-
 
302
    PROTO_T ( flt f X dbl * d )
-
 
303
{
-
 
304
  int   i;
-
 
305
  (d -> sign) = f.sign;
-
 
306
  (d -> exp) = f.exp;
-
 
307
  for (i = 0; i < MANT_SIZE; i++) {
-
 
308
    (d -> mant)[i] = f.mant[i];
317
    (d->mant)[i] = f.mant[i];
309
  }
318
  }
310
  for (i = MANT_SIZE; i < (2 * MANT_SIZE); i++) {
319
  for (i = MANT_SIZE; i < (2 * MANT_SIZE); i++) {
311
    (d -> mant)[i] = 0;
320
    (d->mant)[i] = 0;
312
  }
321
  }
313
}
322
}
-
 
323
 
314
 
324
 
-
 
325
/* "d" is a pointer to a legal double precision number. "dist" is a int with
-
 
326
 * value greater than zero. On return, the number pointed to by "d" keeps the
-
 
327
 * same value ( although some loss of precision may occur ) but is no longer in
-
 
328
 * normalised form. The mantissa is shifted right "dist" places, and the
-
 
329
 * exponent is adjusted to keep the value the same */
315
 
330
 
316
/* "d" is a pointer to a legal double
-
 
317
   precision number. "dist" is a int with
-
 
318
   value greater than zero. On return, the
-
 
319
   number pointed to by "d" keeps the same
-
 
320
   value ( although some loss of precision
-
 
321
   may occur ) but is no longer in
-
 
322
   normalised form. The mantissa is
-
 
323
   shifted right "dist" places, and the
-
 
324
   exponent is adjusted to keep the value
-
 
325
   the same */
-
 
326
static  void shift_right
331
static void
327
    PROTO_N ( (d, dist) )
-
 
328
    PROTO_T ( dbl * d X int dist )
332
shift_right(dbl *d, int dist)
329
{
333
{
330
  int   i;
334
  int i;
331
  int  j = (2 * MANT_SIZE) - 1 - dist;
335
  int j = (2 * MANT_SIZE) - 1 - dist;
332
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--, j--) {
336
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--, j--) {
333
    (d -> mant)[i] = (unsigned short)((j >= 0) ? ((d -> mant)[j]) : 0);
337
    (d->mant)[i] = (unsigned short)((j >= 0) ? ((d->mant)[j]) : 0);
334
  }
338
  }
335
  (d -> exp) += dist;
339
  (d->exp) += dist;
336
}
340
}
337
 
341
 
338
 
342
 
339
/* "d1" and "d2" are double precision
-
 
340
   numbers, with the same exponent and
343
/* "d1" and "d2" are double precision numbers, with the same exponent and
341
   opposite (non-zero) signs. "res" is a
-
 
342
   pointer to a single precision number,
344
 * opposite (non-zero) signs. "res" is a pointer to a single precision number,
343
   whose exponent has been set to the same
345
 * whose exponent has been set to the same value as "d1" and "d2". On return,
344
   value as "d1" and "d2". On return, the
-
 
345
   number pointed to by "res" is the
-
 
346
   result of adding "d1" to "d2" and
346
 * the number pointed to by "res" is the result of adding "d1" to "d2" and
347
   rounding it by the current rounding
-
 
348
   rule. The return value is either OKAY
347
 * rounding it by the current rounding rule. The return value is either OKAY or
349
   or EXP2BIG ( in which case the value of
348
 * EXP2BIG ( in which case the value of the number pointed to by "res" is
350
   the number pointed to by "res" is
-
 
351
   undefined ) */
349
 * undefined ) */
352
static int  sub_mantissas
350
static int
353
    PROTO_N ( (d1, d2, res) )
-
 
354
    PROTO_T ( dbl d1 X dbl d2 X flt * res )
351
sub_mantissas(dbl d1, dbl d2, flt *res)
355
{
352
{
356
  dbl * dp1 = &d1, *dp2 = &d2, rdbl;
353
  dbl *dp1 = &d1, *dp2 = &d2, rdbl;
357
  unsigned int  c = 0;
354
  unsigned int  c = 0;
358
  int   i,
-
 
359
        cmp = dbl_mag_cmp (d1, d2);
355
  int i, cmp = dbl_mag_cmp(d1, d2);
360
  if (cmp == 0) {
356
  if (cmp == 0) {
361
    (res -> sign) = 0;
357
    (res->sign) = 0;
362
    (res -> exp) = 0;
358
    (res->exp) = 0;
363
    for (i = 0; i < MANT_SIZE; i++) {
359
    for (i = 0; i < MANT_SIZE; i++) {
364
      (res -> mant)[i] = 0;
360
      (res->mant)[i] = 0;
365
    }
361
    }
366
    return (OKAY);
362
    return(OKAY);
-
 
363
  } else if (cmp == -1) {
-
 
364
    dp1 = &d2;
-
 
365
    dp2 = &d1;
367
  }
366
  }
368
  else
-
 
369
    if (cmp == -1) {
-
 
370
      dp1 = &d2;
-
 
371
      dp2 = &d1;
-
 
372
    }
-
 
373
  rdbl.exp = (res -> exp);
367
  rdbl.exp = (res->exp);
374
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
368
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
375
    int   s = (int)(((dp1 -> mant)[i]) - ((dp2 -> mant)[i]) -
369
    int s = (int)(((dp1->mant)[i]) - ((dp2->mant)[i]) - c);
376
            c);
-
 
377
    c = 0;
370
    c = 0;
378
    if (s < 0) {
371
    if (s < 0) {
379
      c = 1;
372
      c = 1;
380
      s += FBASE;
373
      s += FBASE;
381
    }
374
    }
382
    rdbl.mant[i] = (unsigned short)s;	/* CAST:jmf: */
375
    rdbl.mant[i] = (unsigned short)s;	/* CAST:jmf: */
383
  }
376
  }
384
  rdbl.sign = (dp1 -> sign);
377
  rdbl.sign = (dp1->sign);
385
  while ((rdbl.mant[0]) == 0) {
378
  while ((rdbl.mant[0]) == 0) {
386
    for (i = 1; i < (2 * MANT_SIZE); i++) {
379
    for (i = 1; i < (2 * MANT_SIZE); i++) {
387
      rdbl.mant[i - 1] = rdbl.mant[i];
380
      rdbl.mant[i - 1] = rdbl.mant[i];
388
    }
381
    }
389
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
382
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
390
    if ((--rdbl.exp) < E_MIN) {
383
    if ((--rdbl.exp) < E_MIN) {
391
      return (EXP2BIG);
384
      return(EXP2BIG);
392
    }
385
    }
393
  }
386
  }
394
  dbl2float (rdbl, res);
387
  dbl2float(rdbl, res);
395
  return (OKAY);
388
  return(OKAY);
396
}
389
}
397
 
390
 
398
 
391
 
399
 
-
 
400
/* "d1" and "d2" are double precision
-
 
401
   numbers, with the same exponent and
392
/* "d1" and "d2" are double precision numbers, with the same exponent and
402
   non-zero signs. "res" is a pointer to a
393
 * non-zero signs. "res" is a pointer to a single precision number which has
403
   single precision number which has the
-
 
404
   same exponent as "d1" and "d2". On
394
 * the same exponent as "d1" and "d2". On return, the number pointed to by
405
   return, the number pointed to by "res"
-
 
406
   will be the result of adding "d1" to
395
 * "res" will be the result of adding "d1" to "d2" and rounding it according to
407
   "d2" and rounding it according to the
-
 
408
   current rounding rule. The return value
396
 * the current rounding rule. The return value will be either OKAY or EXP2BIG (
409
   will be either OKAY or EXP2BIG ( in
-
 
410
   which case the value of the number
-
 
411
   pointed to by "res" is undefined ) */
397
 * in which case the value of the number pointed to by "res" is undefined ) */
412
static int  add_mantissas
398
static int
413
    PROTO_N ( (d1, d2, res) )
-
 
414
    PROTO_T ( dbl d1 X dbl d2 X flt * res )
399
add_mantissas(dbl d1, dbl d2, flt * res)
415
{
400
{
416
  int   i;
401
  int i;
417
  unsigned int  c;
402
  unsigned int c;
418
  if (d1.sign == d2.sign) {
403
  if (d1.sign == d2.sign) {
419
    dbl rdbl;
404
    dbl rdbl;
420
    (res -> sign) = d1.sign;
405
    (res->sign) = d1.sign;
421
    flt2double (*res, &rdbl);
406
    flt2double(*res, &rdbl);
422
    c = 0;
407
    c = 0;
423
    for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
408
    for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
424
      c += (unsigned int)(d1.mant[i] + d2.mant[i]);
409
      c += (unsigned int)(d1.mant[i] + d2.mant[i]);
425
      rdbl.mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
410
      rdbl.mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
426
      c /= FBASE;
411
      c /= FBASE;
Line 428... Line 413...
428
    if (c) {
413
    if (c) {
429
      for (i = (2 * MANT_SIZE) - 1; i > 0; i--) {
414
      for (i = (2 * MANT_SIZE) - 1; i > 0; i--) {
430
	rdbl.mant[i] = rdbl.mant[i - 1];
415
	rdbl.mant[i] = rdbl.mant[i - 1];
431
      }
416
      }
432
      rdbl.mant[0] = (unsigned short)c;	/* CAST:jmf: */
417
      rdbl.mant[0] = (unsigned short)c;	/* CAST:jmf: */
433
      if (((++rdbl.exp) >= E_MAX) ||
418
      if (((++rdbl.exp) >= E_MAX) || (rdbl.exp <= E_MIN)) {
434
	  (rdbl.exp <= E_MIN)) {
-
 
435
	return (EXP2BIG);
419
	return(EXP2BIG);
436
      }
420
      }
437
    }
421
    }
438
    dbl2float (rdbl, res);
422
    dbl2float(rdbl, res);
439
    return (OKAY);
423
    return(OKAY);
440
  }
424
  }
441
  return (sub_mantissas (d1, d2, res));
425
  return(sub_mantissas(d1, d2, res));
442
}
426
}
443
 
427
 
444
 
428
 
445
/* ----- "public" emulator functions ----- */
429
/* ----- "public" emulator functions ----- */
446
 
430
 
447
/* "f" is a pointer to a single precision
431
/* "f" is a pointer to a single precision number. On return, the number pointed
448
   number. On return, the number pointed
-
 
449
   to by "f" will have the value zero */
432
 * to by "f" will have the value zero */
450
void flt_zero
433
void
451
    PROTO_N ( (f) )
-
 
452
    PROTO_T ( flt * f )
434
flt_zero(flt *f)
453
{
435
{
454
  int   i;
436
  int i;
455
  (f -> sign) = 0;
437
  (f->sign) = 0;
456
  (f -> exp) = 0;
438
  (f->exp) = 0;
457
  for (i = 0; i < MANT_SIZE; i++) {
439
  for (i = 0; i < MANT_SIZE; i++) {
458
    (f -> mant)[i] = 0;
440
    (f->mant)[i] = 0;
459
  }
441
  }
460
}
442
}
461
 
443
 
462
 
444
 
463
/* "f" is a legal single precision number.
445
/* "f" is a legal single precision number.  "res" is a pointer to a single
464
   "res" is a pointer to a single
-
 
465
   precision number. On return, the number
446
 * precision number. On return, the number pointed to by "res" is the same as
466
   pointed to by "res" is the same as "f"
-
 
467
*/
447
 * "f" */
468
void flt_copy
448
void
469
    PROTO_N ( (f, res) )
-
 
470
    PROTO_T ( flt f X flt *res )
449
flt_copy(flt f, flt *res)
471
{
450
{
472
  int   i;
451
  int i;
473
  (res -> sign) = f.sign;
452
  (res->sign) = f.sign;
474
  (res -> exp) = f.exp;
453
  (res->exp) = f.exp;
475
  for (i = 0; i < MANT_SIZE; i++) {
454
  for (i = 0; i < MANT_SIZE; i++) {
476
    (res -> mant)[i] = f.mant[i];
455
    (res->mant)[i] = f.mant[i];
477
  }
456
  }
478
  return;
457
  return;
479
}
458
}
480
 
459
 
481
 
460
 
482
/* "f1" and "f2" are legal single
-
 
483
   precision numbers. "res" is a pointer
461
/* "f1" and "f2" are legal single precision numbers. "res" is a pointer to a
484
   to a single precision number. On
-
 
485
   return, if the result is OKAY, then the
462
 * single precision number. On return, if the result is OKAY, then the number
486
   number pointed to by "res" will contain
-
 
487
   the value of adding "f1" to "f2". If
463
 * pointed to by "res" will contain the value of adding "f1" to "f2". If the
488
   the result is EXP2BIG, then the value
-
 
489
   of the number pointed to by "res" is
464
 * result is EXP2BIG, then the value of the number pointed to by "res" is
490
   undefined */
465
 * undefined */
491
int   flt_add
466
int
492
    PROTO_N ( (f1, f2, res) )
-
 
493
    PROTO_T ( flt f1 X flt f2 X flt *res )
467
flt_add(flt f1, flt f2, flt *res)
494
{
468
{
495
  dbl d1, d2;
469
  dbl d1, d2;
496
  if (f1.sign == 0) {
470
  if (f1.sign == 0) {
497
    flt_copy (f2, res);
471
    flt_copy(f2, res);
498
    return (OKAY);
472
    return(OKAY);
499
  }
-
 
500
  else
-
 
501
    if (f2.sign == 0) {
473
  } else if (f2.sign == 0) {
502
      flt_copy (f1, res);
474
    flt_copy(f1, res);
503
      return (OKAY);
475
    return(OKAY);
504
    }
476
  }
505
  flt2double (f1, &d1);
477
  flt2double(f1, &d1);
506
  flt2double (f2, &d2);
478
  flt2double(f2, &d2);
507
  if (d1.exp < d2.exp) {
479
  if (d1.exp < d2.exp) {
508
    shift_right (&d1, d2.exp - d1.exp);
480
    shift_right(&d1, d2.exp - d1.exp);
509
  }
-
 
510
  else
-
 
511
    if (d1.exp > d2.exp) {
481
  } else if (d1.exp > d2.exp) {
512
      shift_right (&d2, d1.exp - d2.exp);
482
    shift_right(&d2, d1.exp - d2.exp);
513
    }
483
  }
514
  (res -> exp) = d1.exp;
484
  (res->exp) = d1.exp;
515
  return (add_mantissas (d1, d2, res));
485
  return(add_mantissas(d1, d2, res));
516
}
-
 
517
 
-
 
518
 
-
 
519
/* "f1" and "f2" are legal single
-
 
520
   precision numbers. "res" is a pointer
-
 
521
   to a single precision number. On
-
 
522
   return, if the result is OKAY, then the
-
 
523
   number pointed to by "res" will contain
-
 
524
   the value of subtracting "f2" from
-
 
525
   "f1". If the result is EXP2BIG, then
-
 
526
   the value of the number pointed to by
-
 
527
   "res" is undefined */
-
 
528
int   flt_sub
-
 
529
    PROTO_N ( (f1, f2, res) )
-
 
530
    PROTO_T ( flt f1 X flt f2 X flt *res )
-
 
531
{
-
 
532
  f2.sign = -f2.sign;
-
 
533
  return (flt_add (f1, f2, res));
-
 
534
}
486
}
-
 
487
 
-
 
488
 
-
 
489
/* "f1" and "f2" are legal single precision numbers. "res" is a pointer to a
-
 
490
 * single precision number. On return, if the result is OKAY, then the number
-
 
491
 * pointed to by "res" will contain the value of subtracting "f2" from "f1". If
-
 
492
 * the result is EXP2BIG, then the value of the number pointed to by "res" is
-
 
493
 * undefined */
-
 
494
int
-
 
495
flt_sub(flt f1, flt f2, flt *res)
-
 
496
{
-
 
497
  f2.sign = -f2.sign;
-
 
498
  return(flt_add(f1, f2, res));
-
 
499
}
-
 
500
 
535
 
501
 
536
#if FBASE == 10
502
#if FBASE == 10
537
 
503
 
538
/* "f1" and "f2" are legal single
-
 
539
   precision numbers. "res" is a pointer
504
/* "f1" and "f2" are legal single precision numbers. "res" is a pointer to a
540
   to a single precision number. On
-
 
541
   return, if the result is OKAY, then the
505
 * single precision number. On return, if the result is OKAY, then the number
542
   number pointed to by "res" will contain
-
 
543
   the value of multiplying "f1" by "f2".
506
 * pointed to by "res" will contain the value of multiplying "f1" by "f2".  If
544
   If the result is EXP2BIG, then the
-
 
545
   value of the number pointed to by "res"
507
 * the result is EXP2BIG, then the value of the number pointed to by "res" is
546
   is undefined */
508
 * undefined */
547
int   flt_mul
509
int
548
    PROTO_N ( (f1, f2, res) )
-
 
549
    PROTO_T ( flt f1 X flt f2 X flt *res )
510
flt_mul(flt f1, flt f2, flt *res)
550
{
511
{
551
  dbl rdbl;
512
  dbl rdbl;
552
  int   i,
513
  int i, j;
553
        j;
-
 
554
  unsigned int c = 0;
514
  unsigned int c = 0;
555
  unsigned int acc[2 * MANT_SIZE];
515
  unsigned int acc[2 * MANT_SIZE];
556
  if ((f1.sign == 0) || (f2.sign == 0)) {
516
  if ((f1.sign == 0) || (f2.sign == 0)) {
557
    flt_zero (res);
517
    flt_zero(res);
558
    return (OKAY);
518
    return(OKAY);
559
  }
519
  }
560
  rdbl.sign = ((f1.sign == f2.sign) ? 1 : -1);
520
  rdbl.sign = ((f1.sign == f2.sign)? 1 : -1);
561
  rdbl.exp = (f1.exp + f2.exp + 1);
521
  rdbl.exp = (f1.exp + f2.exp + 1);
562
  if ((rdbl.exp >= E_MAX) ||
-
 
563
      (rdbl.exp <= E_MIN)) {
522
  if ((rdbl.exp >= E_MAX) || (rdbl.exp <= E_MIN)) {
564
    return (EXP2BIG);
523
    return(EXP2BIG);
565
  }
524
  }
566
  for (i = 0; i < (2 * MANT_SIZE); i++) {
525
  for (i = 0; i < (2 * MANT_SIZE); i++) {
567
    acc[i] = 0;
526
    acc[i] = 0;
568
  }
527
  }
569
  for (i = MANT_SIZE - 1; i >= 0; i--) {
528
  for (i = MANT_SIZE - 1; i >= 0; i--) {
570
    for (j = MANT_SIZE - 1; j >= 0; j--) {
529
    for (j = MANT_SIZE - 1; j >= 0; j--) {
571
      acc[i + j + 1] += (f1.mant[i] * f2.mant[j]);
530
      acc[i + j + 1] += (f1.mant[i] * f2.mant[j]);
572
    }
531
    }
573
  }
532
  }
574
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
533
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
575
    c += acc[i];
534
    c += acc[i];
576
    rdbl.mant[i] = (c % FBASE);
535
    rdbl.mant[i] = (c % FBASE);
577
    c /= FBASE;
536
    c /= FBASE;
578
  }
537
  }
579
  while (rdbl.mant[0] == 0) {
538
  while (rdbl.mant[0] == 0) {
580
    for (i = 1; i < (2 * MANT_SIZE); i++) {
539
    for (i = 1; i < (2 * MANT_SIZE); i++) {
581
      rdbl.mant[i - 1] = rdbl.mant[i];
540
      rdbl.mant[i - 1] = rdbl.mant[i];
582
    }
541
    }
583
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
542
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
584
    if ((--rdbl.exp) <= E_MIN) {
543
    if ((--rdbl.exp) <= E_MIN) {
585
      return (EXP2BIG);
544
      return(EXP2BIG);
586
    }
545
    }
587
  }
546
  }
588
  dbl2float (rdbl, res);
547
  dbl2float(rdbl, res);
589
  return (OKAY);
548
  return(OKAY);
590
}
549
}
591
#else
550
#else
592
/* "f1" and "f2" are legal single
-
 
593
   precision numbers. "res" is a pointer
551
/* "f1" and "f2" are legal single precision numbers. "res" is a pointer to a
594
   to a single precision number. On
-
 
595
   return, if the result is OKAY, then the
552
 * single precision number. On return, if the result is OKAY, then the number
596
   number pointed to by "res" will contain
-
 
597
   the value of multiplying "f1" by "f2".
553
 * pointed to by "res" will contain the value of multiplying "f1" by "f2".  If
598
   If the result is EXP2BIG, then the
-
 
599
   value of the number pointed to by "res"
554
 * the result is EXP2BIG, then the value of the number pointed to by "res" is
600
   is undefined */
555
 * undefined */
-
 
556
 
601
int   flt_mul
557
int
602
    PROTO_N ( (f1, f2, res) )
-
 
603
    PROTO_T ( flt f1 X flt f2 X flt *res )
558
flt_mul(flt f1, flt f2, flt *res)
604
{
559
{
605
  dbl rdbl;
560
  dbl rdbl;
606
  int   i,
561
  int i, j, k;
607
        j,
-
 
608
	k;
-
 
609
  unsigned int acc[2 * MANT_SIZE];
562
  unsigned int acc[2 * MANT_SIZE];
610
  if ((f1.sign == 0) || (f2.sign == 0)) {
563
  if ((f1.sign == 0) || (f2.sign == 0)) {
611
    flt_zero (res);
564
    flt_zero(res);
612
    return (OKAY);
565
    return(OKAY);
613
  }
566
  }
614
  rdbl.sign = ((f1.sign == f2.sign) ? 1 : -1);
567
  rdbl.sign = ((f1.sign == f2.sign)? 1 : -1);
615
  rdbl.exp = (f1.exp + f2.exp + 1);
568
  rdbl.exp = (f1.exp + f2.exp + 1);
616
  if ((rdbl.exp >= E_MAX) ||
-
 
617
      (rdbl.exp <= E_MIN)) {
569
  if ((rdbl.exp >= E_MAX) || (rdbl.exp <= E_MIN)) {
618
    return (EXP2BIG);
570
    return(EXP2BIG);
619
  }
571
  }
620
  for (i = 0; i < (2 * MANT_SIZE); i++) {
572
  for (i = 0; i < (2 * MANT_SIZE); i++) {
621
    acc[i] = 0;
573
    acc[i] = 0;
622
  }
574
  }
623
  SET(acc);
575
  SET(acc);
624
 
576
 
Line 628... Line 580...
628
      unsigned int atl;
580
      unsigned int atl;
629
      for (k = i + j + 1; temp != 0; --k) {
581
      for (k = i + j + 1; temp != 0; --k) {
630
	atl = acc[k] + (temp & 0xffff);
582
	atl = acc[k] + (temp & 0xffff);
631
	acc[k] = atl & 0xffff;
583
	acc[k] = atl & 0xffff;
632
	temp = (atl >> 16) + (temp >> 16);
584
	temp = (atl >> 16) + (temp >> 16);
633
      };
585
      }
634
    };
586
    }
635
  };
587
  }
636
 
588
 
637
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
589
  for (i = (2 * MANT_SIZE) - 1; i >= 0; i--) {
638
    rdbl.mant[i] = (unsigned short)(acc[i]);	/* CAST:jmf: */
590
    rdbl.mant[i] = (unsigned short)(acc[i]);	/* CAST:jmf: */
639
  };
591
  }
640
 
592
 
641
  while (rdbl.mant[0] == 0) {
593
  while (rdbl.mant[0] == 0) {
642
    for (i = 1; i < (2 * MANT_SIZE); i++) {
594
    for (i = 1; i < (2 * MANT_SIZE); i++) {
643
      rdbl.mant[i - 1] = rdbl.mant[i];
595
      rdbl.mant[i - 1] = rdbl.mant[i];
644
    }
596
    }
645
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
597
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
646
    if ((--rdbl.exp) <= E_MIN) {
598
    if ((--rdbl.exp) <= E_MIN) {
647
      return (EXP2BIG);
599
      return(EXP2BIG);
648
    }
600
    }
649
  };
601
  }
650
 
602
 
651
  dbl2float (rdbl, res);
603
  dbl2float(rdbl, res);
652
  return (OKAY);
604
  return(OKAY);
653
}
605
}
654
#endif
606
#endif
-
 
607
 
655
 
608
 
656
#if FBASE == 10
609
#if FBASE == 10
657
 
610
 
658
/* "f1" and "f2" are legal single
-
 
659
   precision numbers, and "f2" is
611
/* "f1" and "f2" are legal single precision numbers, and "f2" is non-zero.
660
   non-zero. "res" is a pointer to a
-
 
661
   single precision number. On return, if
612
 * "res" is a pointer to a single precision number. On return, if the result is
662
   the result is OKAY, then the number
-
 
663
   pointed to by "res" contains the result
613
 * OKAY, then the number pointed to by "res" contains the result of dividing
664
   of dividing "f1" by "f2". If the result
-
 
665
   is EXP2BIG or DIVBY0 then the contents
614
 * "f1" by "f2". If the result is EXP2BIG or DIVBY0 then the contents of the
666
   of the number pointed to by "res" are
615
 * number pointed to by "res" are undefined */
667
   undefined */
-
 
668
int   flt_div
616
int
669
    PROTO_N ( (f1, f2, res) )
-
 
670
    PROTO_T ( flt f1 X flt f2 X flt *res )
617
flt_div(flt f1, flt f2, flt *res)
671
{
618
{
672
  dbl rdbl;
619
  dbl rdbl;
673
  int   i = 0;
620
  int i = 0;
674
  flt f3;
621
  flt f3;
675
  if (f2.sign == 0) {
622
  if (f2.sign == 0) {
676
    return (DIVBY0);
623
    return(DIVBY0);
677
  }
624
  }
678
  if (f1.sign == 0) {
625
  if (f1.sign == 0) {
679
    flt_zero (res);
626
    flt_zero(res);
680
    return (OKAY);
627
    return(OKAY);
681
  }
628
  }
682
  rdbl.sign = ((f1.sign == f2.sign) ? 1 : -1);
629
  rdbl.sign = ((f1.sign == f2.sign) ? 1 : -1);
683
  rdbl.exp = (f1.exp - f2.exp);
630
  rdbl.exp = (f1.exp - f2.exp);
684
  if ((rdbl.exp >= E_MAX) ||
-
 
685
      (rdbl.exp <= E_MIN)) {
631
  if ((rdbl.exp >= E_MAX) || (rdbl.exp <= E_MIN)) {
686
    return (EXP2BIG);
632
    return(EXP2BIG);
687
  }
633
  }
688
  f1.sign = 1;
634
  f1.sign = 1;
689
  f2.sign = 1;
635
  f2.sign = 1;
690
  f1.exp = 0;
636
  f1.exp = 0;
691
  f2.exp = 0;
637
  f2.exp = 0;
692
  flt_copy (f1, &f3);
638
  flt_copy(f1, &f3);
693
  while (i < (2 * MANT_SIZE)) {
639
  while (i < (2 * MANT_SIZE)) {
694
    int   count = -1;
640
    int count = -1;
695
    while (f3.sign != -1) {
641
    while (f3.sign != -1) {
696
      if (flt_sub (f3, f2, &f3) != OKAY) {
642
      if (flt_sub(f3, f2, &f3) != OKAY) {
697
	return (EXP2BIG);
643
	return(EXP2BIG);
698
      }
644
      }
699
      count++;
645
      count++;
700
    }
646
    }
701
    if (flt_add (f3, f2, &f3) != OKAY) {
647
    if (flt_add(f3, f2, &f3) != OKAY) {
702
      return (EXP2BIG);
648
      return(EXP2BIG);
703
    }
649
    }
704
    rdbl.mant[i++] = count;
650
    rdbl.mant[i++] = count;
705
    if (f3.sign == 0) {
651
    if (f3.sign == 0) {
706
      break;
652
      break;
707
    }
653
    }
708
    if ((--f2.exp) <= E_MIN) {
654
    if ((--f2.exp) <= E_MIN) {
709
      return (EXP2BIG);
655
      return(EXP2BIG);
710
    }
656
    }
711
  }
657
  }
712
  while (i < (2 * MANT_SIZE)) {
658
  while (i < (2 * MANT_SIZE)) {
713
    rdbl.mant[i++] = 0;
659
    rdbl.mant[i++] = 0;
714
  }
660
  }
Line 716... Line 662...
716
    for (i = 1; i < (2 * MANT_SIZE); i++) {
662
    for (i = 1; i < (2 * MANT_SIZE); i++) {
717
      rdbl.mant[i - 1] = rdbl.mant[i];
663
      rdbl.mant[i - 1] = rdbl.mant[i];
718
    }
664
    }
719
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
665
    rdbl.mant[(2 * MANT_SIZE) - 1] = 0;
720
    if ((--rdbl.exp) <= E_MIN) {
666
    if ((--rdbl.exp) <= E_MIN) {
721
      return (EXP2BIG);
667
      return(EXP2BIG);
722
    }
668
    }
723
  }
669
  }
724
  dbl2float (rdbl, res);
670
  dbl2float(rdbl, res);
725
  return (OKAY);
671
  return(OKAY);
726
}
672
}
727
 
673
 
728
#else
674
#else
729
 
675
 
730
int   flt_div
676
int
731
    PROTO_N ( (f1, f2, res) )
-
 
732
    PROTO_T ( flt f1 X flt f2 X flt *res )
677
flt_div(flt f1, flt f2, flt *res)
733
{
678
{
734
  Fdig a1[MANT_SIZE+1];
679
  Fdig a1[MANT_SIZE+1];
735
  Fdig a2[MANT_SIZE+1];
680
  Fdig a2[MANT_SIZE+1];
736
  Fdig r[MANT_SIZE+1];
681
  Fdig r[MANT_SIZE+1];
737
  int bit_diff = 0;
682
  int bit_diff = 0;
Line 744... Line 689...
744
  int final_expt = f1.exp - f2.exp;
689
  int final_expt = f1.exp - f2.exp;
745
  int final_shift = 0;
690
  int final_shift = 0;
746
  unsigned int k;
691
  unsigned int k;
747
 
692
 
748
  if (f2.sign == 0) {
693
  if (f2.sign == 0) {
749
    return (DIVBY0);
694
    return(DIVBY0);
750
  };
695
  }
751
 
696
 
752
  if (f1.sign == 0) {
697
  if (f1.sign == 0) {
753
    flt_zero (res);
698
    flt_zero(res);
754
    return (OKAY);
699
    return(OKAY);
755
  };
700
  }
756
 
701
 
757
  for (i = 0; i < MANT_SIZE; ++i) {
702
  for (i = 0; i < MANT_SIZE; ++i) {
758
    a1[i] = f1.mant[i];
703
    a1[i] = f1.mant[i];
759
    a2[i] = f2.mant[i];
704
    a2[i] = f2.mant[i];
760
    r[i] = 0;
705
    r[i] = 0;
761
  };
706
  }
762
  a1[MANT_SIZE] = 0;
707
  a1[MANT_SIZE] = 0;
763
  a2[MANT_SIZE] = 0;
708
  a2[MANT_SIZE] = 0;
764
  r[MANT_SIZE] = 0;
709
  r[MANT_SIZE] = 0;
765
 
710
 
766
	/* Shift first digit of a1 or a2 right until
711
  /* Shift first digit of a1 or a2 right until a1[0] < a2[0] and a1[0] >=
767
	   a1[0] < a2[0] and a1[0] >= 2*a2[0]
-
 
768
	   Count the places in bit_diff.
712
   * 2*a2[0] Count the places in bit_diff.  */
769
	*/
-
 
770
  t = (int)(a1[0]);
713
  t = (int)(a1[0]);
771
  s = (int)(a2[0]);
714
  s = (int)(a2[0]);
772
  if (t >= s) {
715
  if (t >= s) {
773
    for (; t >= s; ++bit_diff)
716
    for (; t >= s; ++bit_diff) {
774
	t >>= 1;
717
      t >>= 1;
775
  }
718
    }
776
  else {
719
  } else {
777
    for (; t < s; --bit_diff)
720
    for (; t < s; --bit_diff) {
778
	s >>= 1;
721
      s >>= 1;
-
 
722
    }
779
    ++bit_diff;
723
    ++bit_diff;
780
  };
724
  }
781
 
-
 
782
 
725
 
-
 
726
 
783
	/* Shift a1 bit_diff places right if bit_diff positive.
727
  /* Shift a1 bit_diff places right if bit_diff positive.
784
	   Shift a2 -bit_diff places right if bit_diff negative.
728
     Shift a2 -bit_diff places right if bit_diff negative.
785
	*/
729
   */
786
  if (bit_diff > 0) {
730
  if (bit_diff > 0) {
787
    for (i = 0; i < (MANT_SIZE+1); ++i) {
731
    for (i = 0; i < (MANT_SIZE + 1); ++i) {
788
      c = (unsigned int)(a1[i] + (c << 16));
732
      c = (unsigned int)(a1[i] + (c << 16));
789
      a1[i] = (unsigned short)(c >> bit_diff);	/* CAST:jmf: */
733
      a1[i] = (unsigned short)(c >> bit_diff);	/* CAST:jmf: */
790
      c &= (unsigned int)((1 << (bit_diff+1)) -1);
734
      c &= (unsigned int)((1 << (bit_diff + 1)) -1);
791
    };
735
    }
792
  }
-
 
793
  else
-
 
794
  if (bit_diff < 0) {
736
  } else if (bit_diff < 0) {
795
    for (i = 0; i < (MANT_SIZE+1); ++i) {
737
    for (i = 0; i < (MANT_SIZE + 1); ++i) {
796
      c = (unsigned int)(a2[i] + (c << 16));
738
      c = (unsigned int)(a2[i] + (c << 16));
797
      a2[i] = (unsigned short)(c >> -bit_diff);	/* CAST:jmf: */
739
      a2[i] = (unsigned short)(c >> -bit_diff);	/* CAST:jmf: */
798
      c &= (unsigned int)((1 << (-bit_diff+1)) -1);
740
      c &= (unsigned int)((1 << (-bit_diff + 1)) -1);
799
    };
741
    }
800
  };
742
  }
801
 
743
 
802
	/* do the division */
744
  /* do the division */
803
  bit = 0;	/* current bit of result */
745
  bit = 0;	/* current bit of result */
804
  bitpos = 0;	/* current digit of result */
746
  bitpos = 0;	/* current digit of result */
805
  sg = 1;	/* 1 means subtract, 0 means add */
747
  sg = 1;	/* 1 means subtract, 0 means add */
806
  keep_on = 1;
748
  keep_on = 1;
807
  while (keep_on) {
749
  while (keep_on) {
808
    c = 0;
750
    c = 0;
809
	/* subtract or add */
751
    /* subtract or add */
810
    if (sg) {
752
    if (sg) {
811
      for (i = MANT_SIZE; i >= 0; --i) {
753
      for (i = MANT_SIZE; i >= 0; --i) {
812
	c = (unsigned int)(a1[i] - a2[i] + c);
754
	c = (unsigned int)(a1[i] - a2[i] + c);
813
	a1[i] = (unsigned short)(c & 0xffff);	/* CAST:jmf: */
755
	a1[i] = (unsigned short)(c & 0xffff);	/* CAST:jmf: */
814
	if (c & 0x10000)
756
	if (c & 0x10000) {
815
	  c = (unsigned int)(-1);
757
	  c = (unsigned int)(-1);
816
	else
758
	} else {
817
	  c = 0;
759
	  c = 0;
-
 
760
	}
818
      };
761
      }
819
      sg = (c) ? 0 : 1;
762
      sg = (c)? 0 : 1;
820
    }
-
 
821
    else {
763
    } else {
822
      for (i = MANT_SIZE; i >= 0; --i) {
764
      for (i = MANT_SIZE; i >= 0; --i) {
823
	c = (unsigned int)(a1[i] + a2[i] + c);
765
	c = (unsigned int)(a1[i] + a2[i] + c);
824
	a1[i] = (unsigned short)(c & 0xffff);	/* CAST:jmf: */
766
	a1[i] = (unsigned short)(c & 0xffff);	/* CAST:jmf: */
825
	if (c & 0x10000)
767
	if (c & 0x10000) {
826
	  c = 1;
768
	  c = 1;
827
	else
769
	} else {
828
	  c = 0;
770
	  c = 0;
-
 
771
	}
829
      };
772
      }
830
      sg = (c) ? 1 : 0;
773
      sg = (c)? 1 : 0;
831
    };
774
    }
832
    if (sg) {
775
    if (sg) {
833
      r[bitpos] = (unsigned short)((int)r[bitpos] | (1 << bit));
776
      r[bitpos] = (unsigned short)((int)r[bitpos] | (1 << bit));
834
		/* CAST:jmf: */
777
      /* CAST:jmf: */
835
    };
778
    }
836
    if (bit == 0) {
779
    if (bit == 0) {
837
      bit = 15;
780
      bit = 15;
838
      ++bitpos;
781
      ++bitpos;
839
    }
-
 
840
    else
782
    } else {
841
      --bit;
783
      --bit;
-
 
784
    }
842
    keep_on = 0;
785
    keep_on = 0;
843
	/* shift f2 right one place, if zero keep_on = 0 */
786
    /* shift f2 right one place, if zero keep_on = 0 */
844
    c = 0;
787
    c = 0;
845
    for (i = 0; i < (MANT_SIZE+1); ++i) {
788
    for (i = 0; i < (MANT_SIZE + 1); ++i) {
846
      c = (unsigned int)(a2[i] + (c << 16));
789
      c = (unsigned int)(a2[i] + (c << 16));
847
      if (c)
790
      if (c) {
848
	keep_on = 1;
791
	keep_on = 1;
-
 
792
      }
849
      a2[i] = (unsigned short)(c >> 1);	/* CAST:jmf: */
793
      a2[i] = (unsigned short)(c >> 1);	/* CAST:jmf: */
850
      c &= 1;
794
      c &= 1;
851
    };
795
    }
852
  };
796
  }
853
 
797
 
854
	/* correct line-up of r */
798
  /* correct line-up of r */
855
  if (bit_diff > 0) {
799
  if (bit_diff > 0) {
856
    final_shift = bit_diff;
800
    final_shift = bit_diff;
857
  }
-
 
858
  else {
801
  } else {
859
    --final_expt;
802
    --final_expt;
860
    if (bit_diff > -16)
803
    if (bit_diff > -16) {
861
      final_shift = 16 + bit_diff;
804
      final_shift = 16 + bit_diff;
-
 
805
    }
862
  };
806
  }
863
  k = (unsigned int)((((int)r[MANT_SIZE] << final_shift) & 0x8000) >> 15);
807
  k = (unsigned int)((((int)r[MANT_SIZE] << final_shift) & 0x8000) >> 15);
864
	/* (int) coercion OK because r is shorter */
808
  /* (int) coercion OK because r is shorter */
865
  k = (unsigned int)(k + (r[MANT_SIZE] >> (16 - final_shift)));
809
  k = (unsigned int)(k + (r[MANT_SIZE] >> (16 - final_shift)));
866
  for (i = MANT_SIZE-1; i >= 0; --i) {
810
  for (i = MANT_SIZE-1; i >= 0; --i) {
867
    k = (unsigned int)((r[i] << final_shift) + k);
811
    k = (unsigned int)((r[i] << final_shift) + k);
868
    res ->mant[i] = (unsigned short)(k & 0xffff);;	/* CAST:jmf: */
812
    res ->mant[i] = (unsigned short)(k & 0xffff);;	/* CAST:jmf: */
869
    k >>= 16;
813
    k >>= 16;
870
  };
814
  }
871
 
815
 
872
  if (res->mant[0] == 0) {
816
  if (res->mant[0] == 0) {
873
    for (i = 0; i < MANT_SIZE-1; ++i)
817
    for (i = 0; i < MANT_SIZE - 1; ++i) {
874
      res->mant[i] = res->mant[i+1];
818
      res->mant[i] = res->mant[i + 1];
-
 
819
    }
875
      res->mant[MANT_SIZE-1] = 0;
820
    res->mant[MANT_SIZE - 1] = 0;
876
    --final_expt;
821
    --final_expt;
877
  };
822
  }
878
 
823
 
879
  res -> exp = final_expt;
824
  res->exp = final_expt;
880
  res -> sign = (f1.sign == f2.sign) ? 1 : -1;
825
  res->sign = (f1.sign == f2.sign) ? 1 : -1;
881
  return OKAY;
826
  return OKAY;
882
}
827
}
883
 
828
 
884
#endif
829
#endif
885
 
830
 
886
/* "f1" and "f2" are legal single
-
 
887
   precision numbers. The return value
831
/* "f1" and "f2" are legal single precision numbers. The return value will be
888
   will be -1, 0 or 1 depending whether
-
 
889
   "f1" is less than, equal to or greater
832
 * -1, 0 or 1 depending whether "f1" is less than, equal to or greater than
890
   than "f2" */
833
 * "f2" */
891
int   flt_cmp
834
int
892
    PROTO_N ( (f1, f2) )
-
 
893
    PROTO_T ( flt f1 X flt f2 )
835
flt_cmp(flt f1, flt f2)
894
{
836
{
895
  int   ret;
837
  int ret;
896
  if (f1.sign < f2.sign) {
838
  if (f1.sign < f2.sign) {
897
    return (-1);
839
    return(-1);
898
  }
-
 
899
  if (f1.sign > f2.sign) {
-
 
900
    return (1);
-
 
901
  }
840
  }
-
 
841
  if (f1.sign > f2.sign) {
-
 
842
    return(1);
-
 
843
  }
902
  ret = mag_cmp (f1, f2);
844
  ret = mag_cmp(f1, f2);
903
  return ((f1.sign == -1) ? -ret : ret);
845
  return((f1.sign == -1) ? -ret : ret);
904
}
846
}
905
 
847
 
906
 
848
 
907
/* "f" is a legal single precision number.
849
/* "f" is a legal single precision number.  "res" is a pointer to a single
908
   "res" is a pointer to a single
-
 
909
   precision number. On return, the number
850
 * precision number. On return, the number pointed to by "res" will be the
910
   pointed to by "res" will be the integer
-
 
911
   value of "f" rounded using the current
851
 * integer value of "f" rounded using the current rounding rule */
912
   rounding rule */
-
 
913
void flt_round
852
void
914
    PROTO_N ( (f, res) )
-
 
915
    PROTO_T ( flt f X flt *res )
853
flt_round(flt f, flt *res)
916
{
854
{
917
  int   i;
855
  int i;
918
  unsigned int  ex;
856
  unsigned int ex;
919
  if (f.exp < -1) {
857
  if (f.exp < -1) {
920
    flt_zero (res);
858
    flt_zero(res);
921
    return;
-
 
922
  }
-
 
923
  flt_copy (f, res);
-
 
924
  if ((f.exp + 1) >= MANT_SIZE) {
-
 
925
    return;
859
    return;
926
  }
860
  }
-
 
861
  flt_copy(f, res);
-
 
862
  if ((f.exp + 1) >= MANT_SIZE) {
-
 
863
    return;
-
 
864
  }
927
  ex = ((res -> mant)[f.exp + 1]);
865
  ex = ((res->mant)[f.exp + 1]);
928
  for (i = f.exp + 1; i < MANT_SIZE; i++) {
866
  for (i = f.exp + 1; i < MANT_SIZE; i++) {
929
    (res -> mant)[i] = 0;
867
    (res->mant)[i] = 0;
930
  }
868
  }
931
  if (f.exp == -1) {
869
  if (f.exp == -1) {
932
    (res -> sign) = 0;
870
    (res->sign) = 0;
933
    (res -> exp) = 0;
871
    (res->exp) = 0;
934
  }
872
  }
935
  switch (round_type) {
873
  switch (round_type) {
936
    default:
874
  default:
937
    case R2ZERO:
875
  case R2ZERO:
938
      return;
876
    return;
939
    case R2PINF:
877
  case R2PINF:
940
      if ((f.sign != 1) || (ex == 0))
878
    if ((f.sign != 1) || (ex == 0)) {
941
	return;
879
      return;
-
 
880
    }
942
      break;
881
    break;
943
    case R2NINF:
882
  case R2NINF:
944
      if ((f.sign != -1) || (ex == 0))
883
    if ((f.sign != -1) || (ex == 0)) {
945
	return;
884
      return;
-
 
885
    }
946
      break;
886
    break;
947
    case R2NEAR:
887
  case R2NEAR:
948
      if (ex < (unsigned int)(FBASE/2))
888
    if (ex < (unsigned int)(FBASE/2)) {
949
	return;
889
      return;
-
 
890
    }
950
      break;
891
    break;
951
  }
892
  }
952
  ex = 1;
893
  ex = 1;
953
  for (i = f.exp; i >= 0; i--) {
894
  for (i = f.exp; i >= 0; i--) {
954
    (res -> mant)[i] =
895
    (res->mant)[i] =
955
	(unsigned short)((ex = ex +
-
 
956
			(unsigned int)((res -> mant)[i])) % FBASE);
896
	(unsigned short)((ex = ex + (unsigned int)((res->mant)[i])) % FBASE);
957
		/* CAST:jmf */
897
    /* CAST:jmf */
958
    if ((ex /= FBASE) == 0) {
898
    if ((ex /= FBASE) == 0) {
959
      return;
899
      return;
960
    }
900
    }
961
  }
901
  }
962
  if (f.exp != -1) {
902
  if (f.exp != -1) {
963
    (res -> exp)++;
903
    (res->exp) ++;
-
 
904
  } else {
-
 
905
    (res->sign) = f.sign;
964
  }
906
  }
965
  else {
-
 
966
    (res -> sign) = f.sign;
-
 
967
  }
-
 
968
  (res -> mant)[0] = 1;
907
  (res->mant)[0] = 1;
969
  for (i = 1; i < MANT_SIZE; i++) {
908
  for (i = 1; i < MANT_SIZE; i++) {
970
    (res -> mant)[i] = 0;
909
    (res->mant)[i] = 0;
971
  }
910
  }
972
}
911
}
973
 
912
 
974
 
913
 
975
/* "f" is a legal single precision number.
914
/* "f" is a legal single precision number.  "res" is a pointer to a single
976
   "res" is a pointer to a single
-
 
977
   precision number. On return, the number
915
 * precision number. On return, the number pointed to by "res" will be the
978
   pointed to by "res" will be the integer
-
 
979
   value of "f" rounded towards zero */
916
 * integer value of "f" rounded towards zero */
980
void flt_trunc
917
void
981
    PROTO_N ( (f, res) )
-
 
982
    PROTO_T ( flt f X flt *res )
918
flt_trunc(flt f, flt *res)
983
{
919
{
984
  int   i;
920
  int i;
985
  if (f.exp < 0) {
921
  if (f.exp < 0) {
986
    flt_zero (res);
922
    flt_zero(res);
987
    return;
923
    return;
988
  }
924
  }
989
  flt_copy (f, res);
925
  flt_copy(f, res);
990
  for (i = f.exp + 1; i < MANT_SIZE; i++) {
926
  for (i = f.exp + 1; i < MANT_SIZE; i++) {
991
    (res -> mant)[i] = 0;
927
   (res->mant)[i] = 0;
992
  }
928
  }
993
}
929
}
994
 
930
 
995
 
931
 
996
/* "s" is a pointer to an array of characters.
932
/* "s" is a pointer to an array of characters.  "f" is a pointer to a single
997
   "f" is a pointer to a single precision number.
-
 
998
   "ret" is NULL, or a pointer to a pointer to a character.
933
 * precision number.  "ret" is NULL, or a pointer to a pointer to a character.
999
   On return, if the return value is OKAY, then the number pointed to
934
 * On return, if the return value is OKAY, then the number pointed to by "f" is
1000
   by "f"
-
 
1001
   is the floating point number represented in the string "s". If "ret" was
935
 * the floating point number represented in the string "s". If "ret" was not
1002
   not NULL, then it will point to a pointer to the next character
936
 * NULL, then it will point to a pointer to the next character in "s" not used
1003
   in "s" not
-
 
1004
   used in the number. If the return value is SYNTAX or EXP2BIG, then the
937
 * in the number. If the return value is SYNTAX or EXP2BIG, then the value of
1005
   value of the number pointed to by "f" is undefined. In this case,
938
 * the number pointed to by "f" is undefined. In this case, if "ret" was not
1006
   if "ret"
-
 
1007
   was not NULL, then it will point to a pointer to the start of
939
 * NULL, then it will point to a pointer to the start of the string.  Leading
1008
   the string.
-
 
1009
   Leading white space in the string will be ignored. Numbers have the
940
 * white space in the string will be ignored. Numbers have the following form :
1010
   following form :
-
 
1011
       [+-]?(([0-9]+(\.[0-9]*)?)|([0-9]*\.[0-9]+))([Ee][+-]?[0-9]+)?
941
 * [+-]?(([0-9]+(\.[0-9]*)?)|([0-9]*\.[0-9]+))([Ee][+-]?[0-9]+)?
1012
*/
942
*/
1013
 
-
 
1014
 
943
 
1015
#if FBASE == 10
944
#if FBASE == 10
1016
 
945
 
1017
int   str2flt
946
int
1018
    PROTO_N ( (s, f, r) )
-
 
1019
    PROTO_T ( char *s X flt * f X char **r )
947
str2flt(char *s, flt *f, char **r)
1020
{
948
{
1021
  int   i = 0,
-
 
1022
        ids = -1,
-
 
1023
        rounded = 0,
-
 
1024
        mant_empty = 1,
949
  int i = 0, ids = -1, rounded = 0, mant_empty = 1, zero = 1;
1025
        zero = 1;
-
 
1026
  (f -> sign) = 1;
950
  (f->sign) = 1;
1027
  (f -> exp) = 0;
951
  (f->exp) = 0;
1028
  if (r) {
952
  if (r) {
1029
    *r = s;
953
    *r = s;
1030
  }
954
  }
1031
  while ((*s) && (' ' == (*s))) {
955
  while ((*s) && (' ' == (*s))) {
1032
    s++;
956
    s++;
1033
  }
957
  }
1034
  if (*s == '-') {
958
  if (*s == '-') {
1035
    (f -> sign) = -1;
959
    (f->sign) = -1;
-
 
960
    s++;
-
 
961
  } else if (*s == '+') {
1036
    s++;
962
    s++;
1037
  }
963
  }
1038
  else
-
 
1039
    if (*s == '+') {
-
 
1040
      s++;
-
 
1041
    }
-
 
1042
  while (*s == '0') {
964
  while (*s == '0') {
1043
    mant_empty = 0;
965
    mant_empty = 0;
1044
    s++;
966
    s++;
1045
  }
967
  }
1046
  while (*s >= '0' && *s <= '9') {
968
  while (*s >= '0' && *s <= '9') {
1047
    mant_empty = 0;
969
    mant_empty = 0;
1048
    zero = 0;
970
    zero = 0;
1049
    if (i < MANT_SIZE) {
971
    if (i < MANT_SIZE) {
1050
      (f -> mant)[i++] = ((*s) - '0');/* ASCII */
972
      (f->mant)[i++] = ((*s) - '0');/* ASCII */
-
 
973
    } else if (!rounded) {
-
 
974
      flt_int_round(f,(*s) - '0');
-
 
975
      rounded = 1;		/* ASCII */
1051
    }
976
    }
1052
    else
-
 
1053
      if (!rounded) {
-
 
1054
	flt_int_round (f, (*s) - '0');
-
 
1055
	rounded = 1;		/* ASCII */
-
 
1056
      }
-
 
1057
    if (ids >= E_MAX) {
977
    if (ids >= E_MAX) {
1058
      return (EXP2BIG);
978
      return(EXP2BIG);
1059
    }
979
    }
1060
    ids++;
980
    ids++;
1061
    s++;
981
    s++;
1062
  }
982
  }
1063
  if (*s == '.') {
983
  if (*s == '.') {
1064
    s++;
984
    s++;
1065
    if (zero) {
985
    if (zero) {
1066
      while (*s == '0') {
986
      while (*s == '0') {
1067
	if (ids <= E_MIN) {
987
	if (ids <= E_MIN) {
1068
	  return (EXP2BIG);
988
	  return(EXP2BIG);
1069
	}
989
	}
1070
	ids--;
990
	ids--;
1071
	s++;
991
	s++;
1072
	mant_empty = 0;
992
	mant_empty = 0;
1073
      }
993
      }
1074
    }
994
    }
1075
    while (*s >= '0' && *s <= '9') {
995
    while (*s >= '0' && *s <= '9') {
1076
      if (i < MANT_SIZE) {
996
      if (i < MANT_SIZE) {
1077
	(f -> mant)[i++] = ((*s) - '0');/* ASCII */
997
	(f->mant)[i++] = ((*s) - '0');/* ASCII */
-
 
998
      } else if (!rounded) {
-
 
999
	flt_int_round(f,(*s) - '0');
-
 
1000
	rounded = 1;		/* ASCII */
1078
      }
1001
      }
1079
      else
-
 
1080
	if (!rounded) {
-
 
1081
	  flt_int_round (f, (*s) - '0');
-
 
1082
	  rounded = 1;		/* ASCII */
-
 
1083
	}
-
 
1084
      s++;
1002
      s++;
1085
      zero = 0;
1003
      zero = 0;
1086
      mant_empty = 0;
1004
      mant_empty = 0;
1087
    }
1005
    }
1088
  }
1006
  }
1089
  while (i < MANT_SIZE) {
1007
  while (i < MANT_SIZE) {
1090
    (f -> mant)[i++] = 0;
1008
    (f->mant)[i++] = 0;
1091
  }
1009
  }
1092
  if ((*s == 'E') || (*s == 'e')) {
1010
  if ((*s == 'E') || (*s == 'e')) {
1093
    int   e_sign = 1,
-
 
1094
          exp_empty = 1;
1011
    int e_sign = 1, exp_empty = 1;
1095
    int  e = 0;
1012
    int e = 0;
1096
    if (*++s == '-') {
1013
    if (*++s == '-') {
1097
      e_sign = -1;
1014
      e_sign = -1;
-
 
1015
      s++;
-
 
1016
    } else if (*s == '+') {
1098
      s++;
1017
      s++;
1099
    }
1018
    }
1100
    else
-
 
1101
      if (*s == '+') {
-
 
1102
	s++;
-
 
1103
      }
-
 
1104
    while ((*s) && (*s >= '0' && *s <= '9')) {
1019
    while ((*s) && (*s >= '0' && *s <= '9')) {
1105
      if (e >= (E_MAX / 10)) {
1020
      if (e >= (E_MAX / 10)) {
1106
	return (EXP2BIG);
1021
	return(EXP2BIG);
1107
      }
1022
      }
1108
      e = e * 10 + ((*s++) - '0');
1023
      e = e * 10 + ((*s++) - '0');
1109
      exp_empty = 0;		/* ASCII */
1024
      exp_empty = 0;		/* ASCII */
1110
    }
1025
    }
1111
    if (exp_empty) {
1026
    if (exp_empty) {
1112
      return (SYNTAX);
1027
      return(SYNTAX);
1113
    }
1028
    }
1114
    e *= e_sign;
1029
    e *= e_sign;
1115
    (f -> exp) += (e + ids);
1030
    (f->exp) += (e + ids);
-
 
1031
  } else {
-
 
1032
    (f->exp) += ids;
1116
  }
1033
  }
1117
  else {
-
 
1118
    (f -> exp) += ids;
-
 
1119
  }
-
 
1120
  if (((f -> exp) >= E_MAX) ||
-
 
1121
      ((f -> exp) <= E_MIN)) {
1034
  if (((f->exp) >= E_MAX) || ((f->exp) <= E_MIN)) {
1122
    return (EXP2BIG);
1035
    return(EXP2BIG);
1123
  }
1036
  }
1124
  if (zero) {
1037
  if (zero) {
1125
    (f -> sign) = 0;
1038
    (f->sign) = 0;
1126
    (f -> exp) = 0;
1039
    (f->exp) = 0;
1127
  }
1040
  }
1128
  if (mant_empty) {
1041
  if (mant_empty) {
1129
    return (SYNTAX);
1042
    return(SYNTAX);
1130
  }
1043
  }
1131
  if (r) {
1044
  if (r) {
1132
    *r = s;
1045
    *r = s;
1133
  }
1046
  }
1134
  return (OKAY);
1047
  return(OKAY);
1135
}
1048
}
1136
 
1049
 
1137
#endif
1050
#endif
1138
 
1051
 
1139
 
1052
 
1140
/***********************************************************************
1053
/***********************************************************************
1141
          ========== interface to  TDF translator ==========
1054
          ========== interface to  TDF translator ==========
1142
 ***********************************************************************/
1055
 ***********************************************************************/
1143
 
1056
 
1144
/* memory allocation */
1057
/* memory allocation */
1145
 
1058
 
1146
 
1059
void
1147
void init_flpt
1060
init_flpt(void)
1148
    PROTO_Z ()
-
 
1149
{
1061
{
1150
  /* initialise */
1062
  /* initialise */
1151
  int  i;
1063
  int i;
1152
  flt * fzr;
1064
  flt *fzr;
1153
  flt * forf;
1065
  flt *forf;
1154
  flptnos = (flt *) xcalloc (initial_flpts, sizeof (flt));
1066
  flptnos = (flt *)xcalloc(initial_flpts, sizeof(flt));
1155
  tot_flpts = initial_flpts;
1067
  tot_flpts = initial_flpts;
1156
  for (i = 1; i < tot_flpts; ++i)
1068
  for (i = 1; i < tot_flpts; ++i) {
1157
    flptnos[i].exp = i - 1;
1069
    flptnos[i].exp = i - 1;
-
 
1070
  }
1158
  flptfree = tot_flpts - 1;
1071
  flptfree = tot_flpts - 1;
1159
  flpt_left = tot_flpts;
1072
  flpt_left = tot_flpts;
1160
 
1073
 
1161
  fzero_no = new_flpt();
1074
  fzero_no = new_flpt();
1162
  fone_no = new_flpt();
1075
  fone_no = new_flpt();
1163
  fzr = &flptnos[fzero_no];
1076
  fzr = &flptnos[fzero_no];
1164
  fzr -> sign = 0;
1077
  fzr->sign = 0;
1165
  fzr -> exp = 0;
1078
  fzr->exp = 0;
1166
  forf = &flptnos[fone_no];
1079
  forf = &flptnos[fone_no];
1167
  forf -> sign = 1;
1080
  forf->sign = 1;
1168
  forf -> exp = 0;
1081
  forf->exp = 0;
1169
  for (i = 0; i < MANT_SIZE; i++) {
1082
  for (i = 0; i < MANT_SIZE; i++) {
1170
    (fzr -> mant)[i] = 0;
1083
    (fzr->mant)[i] = 0;
1171
    (forf -> mant)[i] = 0;
1084
    (forf->mant)[i] = 0;
1172
  };
1085
  }
1173
  (forf -> mant)[0] = 1;
1086
  (forf->mant)[0] = 1;
1174
 
1087
 
1175
  return;
1088
  return;
1176
}
1089
}
1177
 
1090
 
-
 
1091
 
1178
void more_flpts
1092
void
1179
    PROTO_Z ()
1093
more_flpts(void)
1180
{
1094
{
1181
  /* extend the floating point array */
1095
  /* extend the floating point array */
1182
  int  i;
1096
  int i;
1183
  tot_flpts += extra_flpts;
1097
  tot_flpts += extra_flpts;
1184
  flptnos = (flt *) xrealloc ((void*)(CH flptnos),
1098
  flptnos = (flt *)xrealloc((void*)(CH flptnos),
1185
  				TOSZ (tot_flpts * (int)sizeof (flt)));
1099
			    TOSZ(tot_flpts *(int)sizeof(flt)));
1186
  for (i = tot_flpts - extra_flpts + 1; i < tot_flpts; ++i)
1100
  for (i = tot_flpts - extra_flpts + 1; i < tot_flpts; ++i) {
1187
    flptnos[i].exp = i - 1;
1101
    flptnos[i].exp = i - 1;
-
 
1102
  }
1188
  flptfree = tot_flpts - 1;
1103
  flptfree = tot_flpts - 1;
1189
  flpt_left = extra_flpts;
1104
  flpt_left = extra_flpts;
1190
  return;
1105
  return;
1191
}
1106
}
1192
 
1107
 
-
 
1108
 
1193
flpt new_flpt
1109
flpt
1194
    PROTO_Z ()
1110
new_flpt(void)
1195
{
1111
{
1196
  /* allocate space for a new floating point number */
1112
  /* allocate space for a new floating point number */
1197
  flpt r = flptfree;
1113
  flpt r = flptfree;
1198
  flptfree = flptnos[r].exp;
1114
  flptfree = flptnos[r].exp;
1199
  if (--flpt_left == 0)
1115
  if (--flpt_left == 0) {
1200
    more_flpts ();
1116
    more_flpts();
-
 
1117
  }
1201
  return (r);
1118
  return(r);
1202
}
1119
}
1203
 
1120
 
-
 
1121
 
1204
void flpt_ret
1122
void
1205
    PROTO_N ( (f) )
-
 
1206
    PROTO_T ( flpt f )
1123
flpt_ret(flpt f)
1207
{
1124
{
1208
  /* return a floating point number to free */
1125
  /* return a floating point number to free */
1209
  flptnos[f].exp = flptfree;
1126
  flptnos[f].exp = flptfree;
1210
  flptfree = f;
1127
  flptfree = f;
1211
  ++flpt_left;
1128
  ++flpt_left;
1212
  return;
1129
  return;
1213
}
1130
}
1214
 
1131
 
1215
 
1132
 
1216
/***********************************************************************/
1133
/***********************************************************************/
1217
 
-
 
1218
 
1134
 
1219
/* do the test testno on and and b deliver
1135
/* do the test testno on and and b deliver
1220
   1 if true, 0 otherwise */
1136
   1 if true, 0 otherwise */
-
 
1137
 
1221
int cmpflpt
1138
int
1222
    PROTO_N ( (a, b, testno) )
-
 
1223
    PROTO_T ( flpt a X flpt b X int testno )
1139
cmpflpt(flpt a, flpt b, int testno)
1224
{
1140
{
1225
  int   res = flt_cmp (flptnos[a], flptnos[b]);
1141
  int res = flt_cmp(flptnos[a], flptnos[b]);
1226
 
1142
 
1227
  switch (testno) {
1143
  switch (testno) {
1228
    case 1:
1144
  case 1:
1229
      return (res == 1);
1145
    return(res == 1);
1230
    case 2:
1146
  case 2:
1231
      return (res != -1);
1147
    return(res != -1);
1232
    case 3:
1148
  case 3:
1233
      return (res == -1);
1149
    return(res == -1);
1234
    case 4:
1150
  case 4:
1235
      return (res != 1);
1151
    return(res != 1);
1236
    case 5:
1152
  case 5:
1237
      return (res == 0);
1153
    return(res == 0);
1238
    default:
1154
  default:
1239
      return (res != 0);
1155
    return(res != 0);
1240
  };
1156
  }
1241
}
1157
}
1242
 
1158
 
1243
 
1159
 
1244
#if FBASE == 10
1160
#if FBASE == 10
1245
 
1161
 
1246
flpt floatrep
1162
flpt
1247
    PROTO_N ( (n) )
-
 
1248
    PROTO_T ( int n )
1163
floatrep(int n)
1249
{
1164
{
1250
  flpt res = new_flpt ();
1165
  flpt res = new_flpt();
1251
  char *pr = intchars (n);
1166
  char *pr = intchars(n);
1252
	/* this is wrong for unsigned integers */
1167
  /* this is wrong for unsigned integers */
1253
  flt fr;
1168
  flt fr;
1254
  IGNORE str2flt (pr, &fr, (char **) 0);
1169
  IGNORE str2flt(pr, &fr,(char **)0);
1255
  flptnos[res] = fr;
1170
  flptnos[res] = fr;
1256
  return (res);
1171
  return(res);
1257
}
1172
}
-
 
1173
 
1258
 
1174
 
1259
flpt floatrep_unsigned
1175
flpt
1260
    PROTO_N ( (n) )
-
 
1261
    PROTO_T ( unsigned int n )
1176
floatrep_unsigned(unsigned int n)
1262
{
1177
{
1263
  failer("floatrep_unsigned not used");
1178
  failer("floatrep_unsigned not used");
1264
  return 0;
1179
  return 0;
1265
}
1180
}
-
 
1181
 
1266
 
1182
 
1267
int flpt_bits
1183
int
1268
    PROTO_N ( (fv) )
-
 
1269
    PROTO_T ( floating_variety fv )
1184
flpt_bits(floating_variety fv)
1270
{
1185
{
1271
  return 0;
1186
  return 0;
1272
}
1187
}
-
 
1188
 
1273
 
1189
 
1274
void flpt_round
1190
void
1275
    PROTO_N ( (round_t, posn, res) )
-
 
1276
    PROTO_T ( int round_t X int posn X flt * res )
1191
flpt_round(int round_t, int posn, flt *res)
1277
{
1192
{
1278
  return;
1193
  return;
1279
}
1194
}
1280
 
1195
 
1281
#else
1196
#else
1282
 
1197
 
-
 
1198
 
1283
static flpt floatrep_aux
1199
static flpt
1284
    PROTO_N ( (n, sign) )
-
 
1285
    PROTO_T ( int n X int sign )
1200
floatrep_aux(int n, int sign)
1286
{
1201
{
1287
  flpt res = new_flpt ();
1202
  flpt res = new_flpt();
1288
  flt fr;
1203
  flt fr;
1289
  unsigned int mask = 0xffff;
1204
  unsigned int mask = 0xffff;
1290
  int i;
1205
  int i;
1291
  int supp = 1;
1206
  int supp = 1;
1292
  int index = 0;
1207
  int index = 0;
1293
 
1208
 
1294
  flt_zero(&fr);
1209
  flt_zero(&fr);
1295
  if (n == 0)  {
1210
  if (n == 0) {
1296
    flptnos[res] = fr;
1211
    flptnos[res] = fr;
1297
    return (res);
1212
    return(res);
1298
  };
1213
  }
1299
 
1214
 
1300
  fr.sign = sign;
1215
  fr.sign = sign;
1301
 
1216
 
1302
  for (i = 1; i >= 0; --i)  {
1217
  for (i = 1; i >= 0; --i) {
1303
    int t = (int)((n >> (i*16)) & (int)mask);
1218
    int t = (int)((n >> (i * 16)) & (int)mask);
1304
    if (supp && t)  {
1219
    if (supp && t) {
1305
      supp = 0;
1220
      supp = 0;
1306
      fr.exp = i;
1221
      fr.exp = i;
1307
    };
1222
    }
1308
    if (!supp) {
1223
    if (!supp) {
1309
      fr.mant[index++] = (unsigned short)t;	/* CAST:jmf: */
1224
      fr.mant[index++] = (unsigned short)t;	/* CAST:jmf: */
1310
    };
1225
    }
1311
  };
1226
  }
1312
 
1227
 
1313
  flptnos[res] = fr;
1228
  flptnos[res] = fr;
1314
  return (res);
1229
  return(res);
1315
}
1230
}
1316
 
1231
 
-
 
1232
 
1317
flpt floatrep
1233
flpt
1318
    PROTO_N ( (n) )
-
 
1319
    PROTO_T ( int n )
1234
floatrep(int n)
1320
{
1235
{
1321
  if (n < 0)
1236
  if (n < 0) {
1322
    return floatrep_aux(-n, -1);
1237
    return floatrep_aux(-n, -1);
-
 
1238
  }
1323
  return floatrep_aux(n, 1);
1239
  return floatrep_aux(n, 1);
1324
}
1240
}
-
 
1241
 
1325
 
1242
 
1326
flpt floatrep_unsigned
1243
flpt
1327
    PROTO_N ( (n) )
-
 
1328
    PROTO_T ( unsigned int n )
1244
floatrep_unsigned(unsigned int n)
1329
{
1245
{
1330
  return floatrep_aux((int)n, 1);
1246
  return floatrep_aux((int)n, 1);
1331
}
1247
}
1332
 
1248
 
-
 
1249
 
1333
void flpt_newdig
1250
void
1334
    PROTO_N ( (dig, res, base) )
-
 
1335
    PROTO_T ( unsigned int dig X flt * res X int base )
1251
flpt_newdig(unsigned int dig, flt *res, int base)
1336
{
1252
{
1337
  int i;
1253
  int i;
1338
  unsigned int c = 0;
1254
  unsigned int c = 0;
1339
 
1255
 
1340
  if (dig != 0)
1256
  if (dig != 0) {
1341
    res -> sign = 1;
1257
    res->sign = 1;
1342
 
1258
  }
1343
  i = MANT_SIZE - 1;
-
 
1344
 
1259
 
-
 
1260
  i = MANT_SIZE - 1;
-
 
1261
 
1345
  for ( ; i >= 0; --i)  {
1262
  for (; i >= 0; --i) {
1346
    c = (unsigned int)(((int)res -> mant[i] * base) + (int)c);
1263
    c = (unsigned int)(((int)res->mant[i] * base) + (int)c);
1347
    res -> mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1264
    res->mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1348
    c = c / FBASE;
1265
    c = c / FBASE;
1349
  };
1266
  }
1350
  if (c) {
1267
  if (c) {
1351
    ++res -> exp;
1268
    ++res->exp;
1352
    i = res -> exp;
1269
    i = res->exp;
1353
    if (i >= MANT_SIZE)
1270
    if (i >= MANT_SIZE) {
1354
      i = MANT_SIZE - 1;
1271
      i = MANT_SIZE - 1;
-
 
1272
    }
1355
    for ( ; i >= 0; --i)
1273
    for (; i >= 0; --i)
1356
      res -> mant[i] = res -> mant[i-1];
1274
      res->mant[i] = res->mant[i - 1];
1357
    res -> mant[0] = (unsigned short)c;	/* CAST:jmf: */
1275
    res->mant[0] = (unsigned short)c;	/* CAST:jmf: */
1358
  };
1276
  }
1359
 
1277
 
1360
  if (res -> exp >= MANT_SIZE)
1278
  if (res->exp >= MANT_SIZE)
1361
    return;
1279
    return;
1362
 
1280
 
1363
  c = dig;
1281
  c = dig;
1364
  for (i = res -> exp; c && i >= 0; --i)  {
1282
  for (i = res->exp; c && i >= 0; --i) {
1365
    c = (unsigned int)(res -> mant[i] + c);
1283
    c = (unsigned int)(res->mant[i] + c);
1366
    res -> mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1284
    res->mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1367
    c = c / FBASE;
1285
    c = c / FBASE;
1368
  };
1286
  }
1369
  if (c) {
1287
  if (c) {
1370
    ++res -> exp;
1288
    ++res->exp;
1371
    i = res -> exp;
1289
    i = res->exp;
1372
    if (i >= MANT_SIZE)
1290
    if (i >= MANT_SIZE) {
1373
      i = MANT_SIZE - 1;
1291
      i = MANT_SIZE - 1;
-
 
1292
    }
1374
    for ( ; i >= 0; --i)
1293
    for (; i >= 0; --i)
1375
      res -> mant[i] = res -> mant[i-1];
1294
      res->mant[i] = res->mant[i-1];
1376
    res -> mant[0] = (unsigned short)c;	/* CAST:jmf: */
1295
    res->mant[0] = (unsigned short)c;	/* CAST:jmf: */
1377
  };
1296
  }
1378
  return;
1297
  return;
1379
}
1298
}
1380
 
1299
 
1381
 
1300
 
1382
void flpt_scale
1301
void
1383
    PROTO_N ( (expt, res, base) )
-
 
1384
    PROTO_T ( int expt X flt * res X int base )
1302
flpt_scale(int expt, flt *res, int base)
1385
{
1303
{
1386
  flt ft;
1304
  flt ft;
1387
 
1305
 
1388
  if (base == 10) {
1306
  if (base == 10) {
1389
    if (expt > 0) {
1307
    if (expt > 0) {
1390
      if (expt > MAX_USEFUL_DECEXP) {
1308
      if (expt > MAX_USEFUL_DECEXP) {
1391
  	failer(BIG_FLPT);
1309
	failer(BIG_FLPT);
1392
	exit(EXIT_FAILURE);
1310
	exit(EXIT_FAILURE);
1393
  	/* UNREACHED */
1311
	/* UNREACHED */
1394
      };
1312
      }
1395
      while (expt > 16) {
1313
      while (expt > 16) {
1396
        IGNORE flt_mul(*res, powers[15], &ft); /* cannot fail */
1314
	IGNORE flt_mul(*res, powers[15], &ft); /* cannot fail */
1397
        flt_copy(ft, res);
1315
	flt_copy(ft, res);
1398
        expt -= 16;
1316
	expt -= 16;
1399
      };
1317
      }
1400
      IGNORE flt_mul(*res, powers[expt-1], &ft); /* cannot fail */
1318
      IGNORE flt_mul(*res, powers[expt - 1], &ft); /* cannot fail */
1401
      flt_copy(ft, res);
1319
      flt_copy(ft, res);
1402
    }
-
 
1403
    else
-
 
1404
    if (expt < 0) {
1320
    } else if (expt < 0) {
1405
      if (expt < - MAX_USEFUL_DECEXP) {
1321
      if (expt < - MAX_USEFUL_DECEXP) {
1406
	  flt_zero(res);
1322
	flt_zero(res);
1407
	  return;
1323
	return;
1408
      };
1324
      }
1409
      while (expt < -16) {
1325
      while (expt < -16) {
1410
        IGNORE flt_div(*res, powers[15], &ft); /* cannot fail */
1326
	IGNORE flt_div(*res, powers[15], &ft); /* cannot fail */
1411
        flt_copy(ft, res);
1327
	flt_copy(ft, res);
1412
        expt += 16;
1328
	expt += 16;
1413
      };
1329
      }
1414
      IGNORE flt_div(*res, powers[-1 - expt], &ft); /* cannot fail */
1330
      IGNORE flt_div(*res, powers[-1 - expt], &ft); /* cannot fail */
1415
      flt_copy(ft, res);
1331
      flt_copy(ft, res);
1416
    };
1332
    }
1417
  }
-
 
1418
  else  {
1333
  } else {
1419
    if (base == 4)
1334
    if (base == 4) {
1420
      expt += expt;
1335
      expt += expt;
-
 
1336
    }
1421
    if (base == 8)
1337
    if (base == 8) {
1422
      expt *= 3;
1338
      expt *= 3;
-
 
1339
    }
1423
    if (base == 16)
1340
    if (base == 16) {
1424
      expt *= 4;
1341
      expt *= 4;
-
 
1342
    }
1425
 
1343
 
1426
    if (expt > 0) {
1344
    if (expt > 0) {
1427
      res->exp += (expt / 16);
1345
      res->exp += (expt / 16);
1428
      expt = expt % 16;
1346
      expt = expt % 16;
1429
      if (expt != 0)
1347
      if (expt != 0) {
1430
        flpt_newdig((unsigned int)0, res, two_powers[expt]);
1348
	flpt_newdig((unsigned int)0, res, two_powers[expt]);
-
 
1349
      }
1431
      return;
1350
      return;
1432
    }
-
 
1433
    else
-
 
1434
    if (expt < 0) {
1351
    } else if (expt < 0) {
1435
      int temp = ((-expt) / 16);
1352
      int temp = ((-expt) / 16);
1436
      expt = (-expt) % 16;
1353
      expt = (-expt)% 16;
1437
      if (expt == 0)
1354
      if (expt == 0) {
1438
	res->exp -= temp;
1355
	res->exp -= temp;
1439
      else  {
1356
      } else {
1440
        flpt_newdig((unsigned int)0, res, two_powers[16 - expt]);
1357
	flpt_newdig((unsigned int)0, res, two_powers[16 - expt]);
1441
	res->exp -= (temp+1);
1358
	res->exp -= (temp+1);
1442
      };
1359
      }
1443
    };
1360
    }
1444
  };
1361
  }
1445
  return;
1362
  return;
1446
}
1363
}
1447
 
1364
 
-
 
1365
 
1448
/* posn is the number of bits to be left correct after the
1366
/* posn is the number of bits to be left correct after the rounding operation */
1449
   rounding operation */
-
 
-
 
1367
 
1450
void flpt_round
1368
void
1451
    PROTO_N ( (round_t, posn, res) )
-
 
1452
    PROTO_T ( int round_t X int posn X flt * res )
1369
flpt_round(int round_t, int posn, flt * res)
1453
{
1370
{
1454
  unsigned int c = res -> mant[0];
1371
  unsigned int c = res->mant[0];
1455
  unsigned int mask;
1372
  unsigned int mask;
1456
  int ndig0 = 0;
1373
  int ndig0 = 0;
1457
  int i, bitpos;
1374
  int i, bitpos;
1458
  int digpos = 0;
1375
  int digpos = 0;
1459
  int bits_discarded = 0;
1376
  int bits_discarded = 0;
1460
 
1377
 
1461
  if (res -> sign == 0)
1378
  if (res->sign == 0) {
1462
    return;
1379
    return;
-
 
1380
  }
1463
 
1381
 
1464
  for (mask = FBASE - 1; mask & c; mask <<= 1)
1382
  for (mask = FBASE - 1; mask & c; mask <<= 1) {
1465
   ++ndig0;
1383
    ++ndig0;
1466
 
1384
  }
1467
  bitpos = ndig0 - posn;
-
 
1468
 
1385
 
-
 
1386
  bitpos = ndig0 - posn;
-
 
1387
 
1469
  while (bitpos < 1)  {
1388
  while (bitpos < 1) {
1470
    bitpos += FBITS;
1389
    bitpos += FBITS;
1471
    ++digpos;
1390
    ++digpos;
1472
  };
1391
  };
1473
 
1392
 
1474
  --bitpos;
1393
  --bitpos;
1475
 
1394
 
1476
  /* digpos holds the number of the FBASE digit to be rounded,
1395
  /* digpos holds the number of the FBASE digit to be rounded, bitpos holds the
1477
     bitpos holds the number of the bit within that digit to have
1396
   * number of the bit within that digit to have one added */
1478
     one added
-
 
1479
  */
-
 
1480
 
1397
 
1481
  for (i = digpos + 1; i < MANT_SIZE; ++i)  {
1398
  for (i = digpos + 1; i < MANT_SIZE; ++i) {
1482
    if (res -> mant[i]) {
1399
    if (res->mant[i]) {
1483
      bits_discarded = 1;
1400
      bits_discarded = 1;
1484
      res -> mant[i] = 0;
1401
      res->mant[i] = 0;
1485
    };
1402
    }
1486
  };
1403
  }
1487
 
1404
 
1488
  switch (round_t) {
1405
  switch (round_t) {
1489
    default:
1406
  default:
1490
    case R2ZERO:
1407
  case R2ZERO:
-
 
1408
    res->mant[digpos] =
-
 
1409
	(unsigned short)(res->mant[digpos] & bitmask[bitpos + 1]);
-
 
1410
    return;
-
 
1411
  case R2PINF:
-
 
1412
    if (res->sign != 1 ||
-
 
1413
	(!bits_discarded && ((res->mant[digpos] & bitmask[bitpos + 1]) ==
-
 
1414
			     (int)res->mant[digpos]))) {
1491
      res -> mant[digpos] =
1415
      res->mant[digpos] =
1492
	(unsigned short)(res -> mant[digpos] & bitmask[bitpos+1]);
1416
	  (unsigned short)(res->mant[digpos] & bitmask[bitpos + 1]);
-
 
1417
 
-
 
1418
      return;
-
 
1419
    }
-
 
1420
    res->mant[digpos] = (unsigned short)(res->mant[digpos] | bitround[bitpos]);
-
 
1421
    break;
-
 
1422
  case R2NINF:
-
 
1423
    if (res->sign == 1 ||
-
 
1424
	(!bits_discarded && ((res->mant[digpos] & bitmask[bitpos + 1]) ==
-
 
1425
			     (int)res->mant[digpos]))) {
-
 
1426
      res->mant[digpos] = (unsigned short)((int)res->mant[digpos] &
-
 
1427
					   (int)bitmask[bitpos + 1]);
-
 
1428
 
1493
      return;
1429
      return;
1494
    case R2PINF:
-
 
1495
      if (res -> sign != 1 ||
-
 
1496
	    (!bits_discarded && ((res -> mant[digpos] & bitmask[bitpos+1])
-
 
1497
		 == (int)res -> mant[digpos]))) {
-
 
1498
        res -> mant[digpos] =
-
 
1499
		(unsigned short)(res -> mant[digpos] & bitmask[bitpos+1]);
-
 
1500
 
-
 
1501
        return;
-
 
1502
      };
1430
    }
1503
      res -> mant[digpos] =
1431
    res->mant[digpos] = (unsigned short)((int)res->mant[digpos] |
1504
	(unsigned short)(res -> mant[digpos] | bitround[bitpos]);
1432
					 (int)bitround[bitpos]);
1505
      break;
1433
    break;
1506
    case R2NINF:
1434
  case R2NEAR:
1507
      if (res -> sign == 1 ||
1435
    break;
1508
	    (!bits_discarded && ((res -> mant[digpos] & bitmask[bitpos+1])
-
 
1509
		 == (int)res -> mant[digpos]))) {
-
 
1510
        res -> mant[digpos] =
1436
  }
1511
	(unsigned short)((int)res -> mant[digpos] & (int)bitmask[bitpos+1]);
-
 
1512
 
1437
 
1513
        return;
-
 
1514
      };
-
 
1515
      res -> mant[digpos] =
-
 
1516
	(unsigned short)((int)res -> mant[digpos] | (int)bitround[bitpos]);
1438
  res->mant[digpos] = (unsigned short)((int)res->mant[digpos] &
1517
      break;
-
 
1518
    case R2NEAR:
-
 
1519
      break;
1439
				       (int)bitmask[bitpos]);
1520
   };
-
 
1521
 
1440
 
1522
   res -> mant[digpos] =
-
 
1523
	(unsigned short)((int)res -> mant[digpos] & (int)bitmask[bitpos]);
-
 
1524
 
-
 
1525
   c = bitround[bitpos];
1441
  c = bitround[bitpos];
1526
    for (i = digpos; c && i >= 0; --i)  {
1442
  for (i = digpos; c && i >= 0; --i) {
1527
        c = (unsigned int)((int)res -> mant[i] + (int)c);
1443
    c = (unsigned int)((int)res->mant[i] + (int)c);
1528
        res -> mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1444
    res->mant[i] = (unsigned short)(c % FBASE);	/* CAST:jmf: */
1529
        c = c / FBASE;
1445
    c = c / FBASE;
1530
    };
1446
  }
1531
   if (c) {
1447
  if (c) {
1532
        ++res -> exp;
1448
    ++res->exp;
1533
        i = res -> exp;
1449
    i = res->exp;
1534
        if (i >= MANT_SIZE)
1450
    if (i >= MANT_SIZE) {
1535
          i = MANT_SIZE - 1;
1451
      i = MANT_SIZE - 1;
-
 
1452
    }
1536
        for ( ; i >= 0; --i)
1453
    for (; i >= 0; --i) {
1537
          res -> mant[i] = res -> mant[i-1];
1454
      res->mant[i] = res->mant[i-1];
-
 
1455
    }
1538
        res -> mant[0] = (unsigned short)c;
1456
    res-> mant[0] = (unsigned short)c;
1539
   };
1457
  }
1540
  res -> mant[digpos+(int)c] =
-
 
1541
	(unsigned short)((int)res -> mant[digpos+(int)c] &
1458
  res->mant[digpos+ (int)c] = (unsigned short)((int)res->mant[digpos + (int)c] &
1542
				(int)bitmask[bitpos+1]);
1459
					       (int)bitmask[bitpos + 1]);
1543
  return;
1460
  return;
1544
}
1461
}
-
 
1462
 
1545
 
1463
 
1546
int flpt_bits
1464
int
1547
    PROTO_N ( (fv) )
-
 
1548
    PROTO_T ( floating_variety fv )
1465
flpt_bits(floating_variety fv)
1549
{
1466
{
1550
  switch (fv) 
1467
  switch (fv)
1551
   {
1468
  {
1552
     case 0: return FLOAT_BITS;
1469
  case 0: return FLOAT_BITS;
1553
				/* FLOAT_BITS is defined in config.h
1470
	  /* FLOAT_BITS is defined in config.h 24 for IEEE */
1554
				   24 for IEEE */
-
 
1555
     case 1: return DOUBLE_BITS;
1471
  case 1: return DOUBLE_BITS;
1556
				/* FLOAT_BITS is defined in config.h
1472
	  /* FLOAT_BITS is defined in config.h 53 for IEEE */
1557
				   53 for IEEE */
-
 
1558
     case 2: return LDOUBLE_BITS;
1473
  case 2: return LDOUBLE_BITS;
1559
				/* FLOAT_BITS is defined in config.h
1474
	  /* FLOAT_BITS is defined in config.h 64 for IEEE */
1560
				   64 for IEEE */
-
 
1561
   };
1475
  }
1562
  return 0;
1476
  return 0;
1563
}
1477
}
-
 
1478
 
1564
 
1479
 
1565
int flpt_round_to_integer
1480
int
1566
    PROTO_N ( (rndmd, f) )
-
 
1567
    PROTO_T ( int rndmd X flt * f )
1481
flpt_round_to_integer(int rndmd, flt *f)
1568
{
1482
{
1569
  unsigned int c = f -> mant[0];
1483
  unsigned int c = f->mant[0];
1570
  unsigned int mask;
1484
  unsigned int mask;
1571
  int ndig0 = 0;
1485
  int ndig0 = 0;
1572
  int intbits;
1486
  int intbits;
1573
  int res;
1487
  int res;
1574
 
1488
 
1575
  if (f -> sign == 0)
1489
  if (f->sign == 0) {
1576
    return 0;
1490
    return 0;
-
 
1491
  }
1577
 
1492
 
1578
  for (mask = FBASE - 1; mask & c; mask <<= 1)
1493
  for (mask = FBASE - 1; mask & c; mask <<= 1)
1579
   ++ndig0;
1494
    ++ndig0;
1580
  intbits = ndig0 + (f -> exp * FBITS);
1495
  intbits = ndig0 + (f->exp * FBITS);
1581
 
-
 
1582
  if (intbits >= 0)
-
 
1583
    flpt_round(rndmd, intbits, f);
-
 
1584
  else {
-
 
1585
    switch(rndmd) EXHAUSTIVE {
-
 
1586
      case R2ZERO:
-
 
1587
        break;
-
 
1588
      case R2PINF:
-
 
1589
	if (f->sign == 1)
-
 
1590
	  flt_copy(flptnos[fone_no], f);
-
 
1591
	else
-
 
1592
	  flt_zero(f);
-
 
1593
        break;
-
 
1594
      case R2NINF:
-
 
1595
	if (f->sign == -1) {
-
 
1596
	  flt_copy(flptnos[fone_no], f);
-
 
1597
	  f->sign = -1;
-
 
1598
	}
-
 
1599
	else
-
 
1600
	  flt_zero(f);
-
 
1601
        break;
-
 
1602
      case R2NEAR:
-
 
1603
        break;
-
 
1604
    };
-
 
1605
  };
-
 
1606
 
1496
 
-
 
1497
  if (intbits >= 0) {
-
 
1498
    flpt_round(rndmd, intbits, f);
-
 
1499
  } else {
-
 
1500
    switch (rndmd)EXHAUSTIVE {
-
 
1501
    case R2ZERO:
-
 
1502
      break;
-
 
1503
    case R2PINF:
-
 
1504
      if (f->sign == 1) {
-
 
1505
	flt_copy(flptnos[fone_no], f);
-
 
1506
      } else {
-
 
1507
	flt_zero(f);
-
 
1508
      }
-
 
1509
      break;
-
 
1510
    case R2NINF:
-
 
1511
      if (f->sign == -1) {
-
 
1512
	flt_copy(flptnos[fone_no], f);
-
 
1513
	f->sign = -1;
-
 
1514
      } else {
-
 
1515
	flt_zero(f);
-
 
1516
      }
-
 
1517
      break;
-
 
1518
    case R2NEAR:
-
 
1519
      break;
-
 
1520
    }
-
 
1521
  }
1607
 
1522
 
-
 
1523
 
1608
/*
1524
  /*
1609
#if has64bits
1525
#if has64bits
1610
  if (f -> exp > 3) {
1526
  if (f->exp > 3) {
1611
    int ij;
1527
    int ij;
1612
    f -> mant[0] = f -> mant[f -> exp - 3];
1528
    f->mant[0] = f->mant[f->exp - 3];
1613
    f -> mant[1] = f -> mant[f -> exp - 2];
1529
    f->mant[1] = f->mant[f->exp - 2];
1614
    f -> mant[2] = f -> mant[f -> exp - 1];
1530
    f->mant[2] = f->mant[f->exp - 1];
1615
    f -> mant[3] = f -> mant[f -> exp];
1531
    f->mant[3] = f->mant[f->exp];
1616
    for (ij = 4; ij <= f -> exp; ++ij) f -> mant[ij] = 0;
1532
    for (ij = 4; ij <= f->exp; ++ij) {
-
 
1533
      f->mant[ij] = 0;
-
 
1534
    }
1617
    f -> exp = 3;
1535
    f->exp = 3;
1618
  };
1536
  }
1619
#else
1537
#else
1620
  if (f -> exp > 1) {
1538
  if (f->exp > 1) {
1621
    f -> mant[0] = f -> mant[f -> exp - 1];
1539
    f->mant[0] = f->mant[f->exp - 1];
1622
    f -> mant[1] = f -> mant[f -> exp];
1540
    f->mant[1] = f->mant[f->exp];
1623
    f -> exp = 1;
1541
    f->exp = 1;
1624
  };
1542
  }
1625
#endif
1543
#endif
1626
*/
1544
   */
1627
  if (f -> exp == 1)
1545
  if (f->exp == 1) {
1628
    res = ((int)f -> mant[1] + ((int)f -> mant[0] << 16));
1546
    res = ((int)f->mant[1] + ((int)f->mant[0] << 16));
1629
  else
1547
  } else {
1630
    res = (int)(f -> mant[0]);
1548
    res = (int)(f->mant[0]);
-
 
1549
  }
1631
  return res * f -> sign;  /* this is only valid for 32-bit results */
1550
  return res * f->sign;  /* this is only valid for 32-bit results */
1632
 
1551
}
1633
}
1552
 
1634
 
1553
 
1635
/* convert flt* to IEEE representation in ints.
1554
/* convert flt* to IEEE representation in ints.  sw is 0 for single, 1 for
1636
   sw is 0 for single, 1 for double,
-
 
1637
   2 for extended */
1555
 * double, 2 for extended */
1638
r2l real2longs_IEEE
1556
r2l
1639
    PROTO_N ( (fp, sw) )
-
 
1640
    PROTO_T ( flt * fp X int sw )
1557
real2longs_IEEE(flt *fp, int sw)
1641
{
1558
{
1642
  r2l res;
1559
  r2l res;
1643
  flt f;
1560
  flt f;
1644
  unsigned int c = fp -> mant[0];
1561
  unsigned int c = fp->mant[0];
1645
  unsigned int mask;
1562
  unsigned int mask;
1646
  int ndig0 = 0;
1563
  int ndig0 = 0;
1647
  int expt;
1564
  int expt;
1648
  int bias, expt_size, precision;
1565
  int bias, expt_size, precision;
1649
  unsigned int sig1, sig2 = 0, sig3 = 0, sig4 = 0;
1566
  unsigned int sig1, sig2 = 0, sig3 = 0, sig4 = 0;
1650
  int i, index;
1567
  int i, index;
1651
  res.i1 = 0;
1568
  res.i1 = 0;
1652
  res.i2 = 0;
1569
  res.i2 = 0;
1653
  res.i3 = 0;
1570
  res.i3 = 0;
1654
  res.i4 = 0;
1571
  res.i4 = 0;
1655
 
1572
 
1656
  if (fp -> sign == 0) {
1573
  if (fp->sign == 0) {
1657
    return res;
1574
    return res;
1658
  };
1575
  }
1659
 
1576
 
1660
  f = *fp;
1577
  f = *fp;
1661
 
1578
 
1662
  switch (sw) EXHAUSTIVE {
1579
  switch (sw)EXHAUSTIVE {
-
 
1580
  case 0:
-
 
1581
    bias = 127;
-
 
1582
    expt_size = 8;
-
 
1583
    precision = 24;
-
 
1584
    break;
-
 
1585
  case 1:
-
 
1586
    bias = 1023;
-
 
1587
    expt_size = 11;
-
 
1588
    precision = 53;
-
 
1589
    break;
-
 
1590
  case 2:
-
 
1591
    bias = 16383;
-
 
1592
    expt_size = 15;
-
 
1593
    precision = LDOUBLE_BITS;
-
 
1594
    break;
-
 
1595
  }
-
 
1596
 
-
 
1597
  for (mask = FBASE - 1; mask & c; mask <<= 1) {
-
 
1598
    ++ndig0;
-
 
1599
  }
-
 
1600
  expt = ndig0 + (f.exp * FBITS) - 1;
-
 
1601
 
-
 
1602
  if (expt < (1-bias-precision)) {
-
 
1603
    /* 0 - underflowed */
-
 
1604
    return res;
-
 
1605
  }
-
 
1606
 
-
 
1607
  if (expt > bias) {
-
 
1608
    if (flpt_const_overflow_fail) {
-
 
1609
      failer(BIG_FLPT);
-
 
1610
      exit(EXIT_FAILURE);
-
 
1611
    }
-
 
1612
    switch (sw) {
1663
    case 0:
1613
    case 0:
1664
      bias = 127;
1614
      if (f.sign == -1) {
1665
      expt_size = 8;
1615
	res.i1 = 0x80000000;
-
 
1616
      }
1666
      precision = 24;
1617
      res.i1 += 0x7f800000;
1667
      break;
1618
      return res;
1668
    case 1:
1619
    case 1:
1669
      bias = 1023;
-
 
1670
      expt_size = 11;
-
 
1671
      precision = 53;
-
 
1672
      break;
-
 
1673
    case 2:
-
 
1674
      bias = 16383;
-
 
1675
      expt_size = 15;
-
 
1676
      precision = LDOUBLE_BITS;
-
 
1677
      break;
-
 
1678
  };
-
 
1679
 
-
 
1680
    for (mask = FBASE - 1; mask & c; mask <<= 1)
-
 
1681
     ++ndig0;
-
 
1682
    expt = ndig0 + (f.exp * FBITS) - 1;
-
 
1683
 
-
 
1684
    if (expt < (1-bias-precision)) return res; /* 0 - underflowed */
-
 
1685
 
-
 
1686
    if (expt > bias) {
-
 
1687
      if (flpt_const_overflow_fail) {
-
 
1688
        failer(BIG_FLPT);
-
 
1689
	exit(EXIT_FAILURE);
-
 
1690
      };
-
 
1691
      switch (sw) {
-
 
1692
	case 0:
-
 
1693
	  if (f.sign == -1)
1620
      if (f.sign == -1) {
1694
	    res.i1 = 0x80000000;
-
 
1695
	  res.i1 += 0x7f800000;
-
 
1696
	  return res;
-
 
1697
	case 1:
-
 
1698
	  if (f.sign == -1)
-
 
1699
	    res.i2 = 0x80000000;
-
 
1700
	  res.i2 += 0x7ff00000;
-
 
1701
	  return res;
-
 
1702
	case 2:
-
 
1703
#if is80x86
-
 
1704
	if (f.sign == -1)
-
 
1705
	  res.i3 = 0x8000;
-
 
1706
	res.i3 += 0x7fff;
-
 
1707
	res.i2 = 0x80000000;
1621
	res.i2 = 0x80000000;
-
 
1622
      }
-
 
1623
      res.i2 += 0x7ff00000;
1708
	return res;
1624
      return res;
-
 
1625
    case 2:
-
 
1626
#if is80x86
-
 
1627
      if (f.sign == -1) {
-
 
1628
	res.i3 = 0x8000;
-
 
1629
      }
-
 
1630
      res.i3 += 0x7fff;
-
 
1631
      res.i2 = 0x80000000;
-
 
1632
      return res;
1709
#else
1633
#else
1710
#if issparc || ishppa
1634
#if issparc || ishppa
1711
	if (f.sign == -1)
1635
      if (f.sign == -1) {
1712
	  res.i4 = 0x8000;
1636
	res.i4 = 0x8000;
-
 
1637
      }
1713
	res.i4 += 0x7fff;
1638
      res.i4 += 0x7fff;
1714
	return res;
1639
      return res;
1715
#else
1640
#else
1716
      failer("long double not implemented");
1641
      failer("long double not implemented");
1717
      return res;
1642
      return res;
1718
#endif
1643
#endif
1719
#endif
1644
#endif
1720
      };
1645
    }
1721
    };
1646
  }
1722
 
1647
 
1723
    expt += bias;
1648
  expt += bias;
1724
    i = precision - ndig0;;
1649
  i = precision - ndig0;;
1725
    sig1 = f.mant[0];
1650
  sig1 = f.mant[0];
1726
    index = 1;
1651
  index = 1;
1727
    while (i >= 16)  {
1652
  while (i >= 16) {
1728
      sig4 <<= 16;
1653
    sig4 <<= 16;
1729
      sig4 += (sig3 >> 16);
1654
    sig4 += (sig3 >> 16);
1730
      sig3 <<= 16;
1655
    sig3 <<= 16;
1731
      sig3 += (sig2 >> 16);
1656
    sig3 += (sig2 >> 16);
1732
      sig2 <<= 16;
1657
    sig2 <<= 16;
1733
      sig2 += (sig1 >> 16);
1658
    sig2 += (sig1 >> 16);
1734
      sig1 <<= 16;
1659
    sig1 <<= 16;
1735
      sig1 = (unsigned int)(sig1 + f.mant[index++]);
1660
    sig1 = (unsigned int)(sig1 + f.mant[index++]);
1736
      i -= 16;
1661
    i -= 16;
1737
    };
1662
  }
1738
    if (i != 0) {
1663
  if (i != 0) {
1739
      sig4 <<= i;
1664
    sig4 <<= i;
1740
      sig4 += (sig3 >> (32-i));
1665
    sig4 += (sig3 >> (32-i));
1741
      sig3 <<= i;
1666
    sig3 <<= i;
1742
      sig3 += (sig2 >> (32-i));
1667
    sig3 += (sig2 >> (32-i));
1743
      sig2 <<= i;
1668
    sig2 <<= i;
1744
      sig2 += (sig1 >> (32-i));
1669
    sig2 += (sig1 >> (32-i));
1745
      sig1 <<= i;
1670
    sig1 <<= i;
1746
      sig1 = (unsigned int)(sig1 + (f.mant[index] >> (16-i)));
1671
    sig1 = (unsigned int)(sig1 + (f.mant[index] >> (16 - i)));
1747
    };
1672
  }
1748
 
1673
 
1749
    if (expt < 1) {
1674
  if (expt < 1) {
1750
      int places = 1 - expt;
1675
    int places = 1 - expt;
1751
      while (places >= 32) {
1676
    while (places >= 32) {
1752
	sig1 = sig2;
1677
      sig1 = sig2;
1753
        sig2 = sig3;
1678
      sig2 = sig3;
1754
	sig3 = sig4;
1679
      sig3 = sig4;
1755
	sig4 = 0;
1680
      sig4 = 0;
1756
	places -= 32;
1681
      places -= 32;
1757
      };
-
 
1758
      if (places > 0) {
-
 
1759
        sig1 = (sig1 >> places) + (sig2 << (32 - places));
-
 
1760
        sig2 = (sig2 >> places) + (sig3 << (32 - places));
-
 
1761
        sig3 = (sig3 >> places) + (sig4 << (32 - places));
-
 
1762
        sig4 >>= places;
-
 
1763
      };
-
 
1764
      expt = 0;
-
 
1765
    }
1682
    }
-
 
1683
    if (places > 0) {
-
 
1684
      sig1 = (sig1 >> places) + (sig2 << (32 - places));
-
 
1685
      sig2 = (sig2 >> places) + (sig3 << (32 - places));
-
 
1686
      sig3 = (sig3 >> places) + (sig4 << (32 - places));
-
 
1687
      sig4 >>= places;
-
 
1688
    }
-
 
1689
    expt = 0;
1766
    else
1690
  } else {
1767
      expt &= ((1 << (expt_size+1)) -1);
1691
    expt &= ((1 << (expt_size+1)) -1);
-
 
1692
  }
1768
 
1693
 
1769
    switch (sw) {
1694
  switch (sw) {
1770
      case 0:
1695
  case 0:
1771
	if (f.sign == -1)
1696
    if (f.sign == -1) {
1772
	  res.i1 = 0x80000000;
1697
      res.i1 = 0x80000000;
-
 
1698
    }
1773
	res.i1 += (expt << 23);
1699
    res.i1 += (expt << 23);
1774
	res.i1 += (int)(sig1 & 0x007fffff);
1700
    res.i1 += (int)(sig1 & 0x007fffff);
1775
	break;
1701
    break;
1776
      case 1:
1702
  case 1:
1777
	if (f.sign == -1)
1703
    if (f.sign == -1) {
1778
	  res.i2 = 0x80000000;
1704
      res.i2 = 0x80000000;
-
 
1705
    }
1779
	res.i2 += (expt << 20);
1706
    res.i2 += (expt << 20);
1780
	res.i2 += (int)(sig2 & 0xfffff);
1707
    res.i2 += (int)(sig2 & 0xfffff);
1781
	res.i1 = (int)sig1;
1708
    res.i1 = (int)sig1;
1782
	break;
1709
    break;
1783
      case 2:
1710
  case 2:
1784
#if is80x86
1711
#if is80x86
1785
	res.i1 = (int)sig1;
1712
    res.i1 = (int)sig1;
1786
	res.i2 = (int)sig2;
1713
    res.i2 = (int)sig2;
1787
	if (f.sign == -1)
1714
    if (f.sign == -1) {
1788
	  res.i3 = 0x8000;
1715
      res.i3 = 0x8000;
-
 
1716
    }
1789
	res.i3 += expt;
1717
    res.i3 += expt;
1790
	UNUSED(sig3);
1718
    UNUSED(sig3);
1791
	UNUSED(sig4);
1719
    UNUSED(sig4);
1792
	break;
1720
    break;
1793
#else
1721
#else
1794
#if issparc || ishppa
1722
#if issparc || ishppa
1795
	if (f.sign == -1)
1723
    if (f.sign == -1) {
1796
	  res.i4 = 0x80000000;
1724
      res.i4 = 0x80000000;
-
 
1725
    }
1797
	res.i4 += (expt << 16);
1726
    res.i4 += (expt << 16);
1798
	res.i4 += (int)(sig4 & 0xffff);
1727
    res.i4 += (int)(sig4 & 0xffff);
1799
	res.i3 = (int)sig3;
1728
    res.i3 = (int)sig3;
1800
	res.i2 = (int)sig2;
1729
    res.i2 = (int)sig2;
1801
	res.i1 = (int)sig1;
1730
    res.i1 = (int)sig1;
1802
	break;
1731
    break;
1803
#else
1732
#else
1804
      failer("long double not implemented");
1733
    failer("long double not implemented");
1805
      return res;
1734
    return res;
1806
#endif
1735
#endif
1807
#endif
1736
#endif
1808
    };
1737
  }
1809
 
1738
 
1810
  return res;
1739
  return res;
1811
}
1740
}
1812
 
-
 
1813
 
1741
 
1814
#endif
1742
#endif
1815
 
-