Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  /* Extended precision arithmetic functions for long double I/O.
  2.   * This program has been placed in the public domain.
  3.   */
  4.  
  5. #include <_ansi.h>
  6. #include <reent.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include "mprec.h"
  10.  
  11. /* These are the externally visible entries. */
  12. /* linux name:  long double _IO_strtold (char *, char **); */
  13. long double _strtold (char *, char **);
  14. char *_ldtoa_r (struct _reent *, long double, int, int, int *, int *,
  15.                 char **);
  16. int _ldcheck (long double *);
  17. #if 0
  18. void _IO_ldtostr (long double *, char *, int, int, char);
  19. #endif
  20.  
  21.  /* Number of 16 bit words in external x type format */
  22. #define NE 10
  23.  
  24.  /* Number of 16 bit words in internal format */
  25. #define NI (NE+3)
  26.  
  27.  /* Array offset to exponent */
  28. #define E 1
  29.  
  30.  /* Array offset to high guard word */
  31. #define M 2
  32.  
  33.  /* Number of bits of precision */
  34. #define NBITS ((NI-4)*16)
  35.  
  36.  /* Maximum number of decimal digits in ASCII conversion
  37.   * = NBITS*log10(2)
  38.   */
  39. #define NDEC (NBITS*8/27)
  40.  
  41.  /* The exponent of 1.0 */
  42. #define EXONE (0x3fff)
  43.  
  44.  /* Maximum exponent digits - base 10 */
  45. #define MAX_EXP_DIGITS 5
  46.  
  47. /* Control structure for long double conversion including rounding precision values.
  48.  * rndprc can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
  49.  */
  50. typedef struct
  51. {
  52.   int rlast;
  53.   int rndprc;
  54.   int rw;
  55.   int re;
  56.   int outexpon;
  57.   unsigned short rmsk;
  58.   unsigned short rmbit;
  59.   unsigned short rebit;
  60.   unsigned short rbit[NI];
  61.   unsigned short equot[NI];
  62. } LDPARMS;
  63.  
  64. static void esub (_CONST short unsigned int *a, _CONST short unsigned int *b,
  65.                   short unsigned int *c, LDPARMS * ldp);
  66. static void emul (_CONST short unsigned int *a, _CONST short unsigned int *b,
  67.                   short unsigned int *c, LDPARMS * ldp);
  68. static void ediv (_CONST short unsigned int *a, _CONST short unsigned int *b,
  69.                   short unsigned int *c, LDPARMS * ldp);
  70. static int ecmp (_CONST short unsigned int *a, _CONST short unsigned int *b);
  71. static int enormlz (short unsigned int *x);
  72. static int eshift (short unsigned int *x, int sc);
  73. static void eshup1 (register short unsigned int *x);
  74. static void eshup8 (register short unsigned int *x);
  75. static void eshup6 (register short unsigned int *x);
  76. static void eshdn1 (register short unsigned int *x);
  77. static void eshdn8 (register short unsigned int *x);
  78. static void eshdn6 (register short unsigned int *x);
  79. static void eneg (short unsigned int *x);
  80. static void emov (register _CONST short unsigned int *a,
  81.                   register short unsigned int *b);
  82. static void eclear (register short unsigned int *x);
  83. static void einfin (register short unsigned int *x, register LDPARMS * ldp);
  84. static void efloor (short unsigned int *x, short unsigned int *y,
  85.                     LDPARMS * ldp);
  86. static void etoasc (short unsigned int *x, char *string, int ndigs,
  87.                     int outformat, LDPARMS * ldp);
  88.  
  89. union uconv
  90. {
  91.   unsigned short pe;
  92.   long double d;
  93. };
  94.  
  95. #if LDBL_MANT_DIG == 24
  96. static void e24toe (short unsigned int *pe, short unsigned int *y,
  97.                     LDPARMS * ldp);
  98. #elif LDBL_MANT_DIG == 53
  99. static void e53toe (short unsigned int *pe, short unsigned int *y,
  100.                     LDPARMS * ldp);
  101. #elif LDBL_MANT_DIG == 64
  102. static void e64toe (short unsigned int *pe, short unsigned int *y,
  103.                     LDPARMS * ldp);
  104. #else
  105. static void e113toe (short unsigned int *pe, short unsigned int *y,
  106.                      LDPARMS * ldp);
  107. #endif
  108.  
  109. /*                                                      econst.c        */
  110. /*  e type constants used by high precision check routines */
  111.  
  112. #if NE == 10
  113. /* 0.0 */
  114. static _CONST unsigned short ezero[NE] = { 0x0000, 0x0000, 0x0000, 0x0000,
  115.   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  116. };
  117.  
  118. /* 1.0E0 */
  119. static _CONST unsigned short eone[NE] = { 0x0000, 0x0000, 0x0000, 0x0000,
  120.   0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,
  121. };
  122.  
  123. #else
  124.  
  125. /* 0.0 */
  126. static _CONST unsigned short ezero[NE] = {
  127.   0, 0000000, 0000000, 0000000, 0000000, 0000000,
  128. };
  129.  
  130. /* 1.0E0 */
  131. static _CONST unsigned short eone[NE] = {
  132.   0, 0000000, 0000000, 0000000, 0100000, 0x3fff,
  133. };
  134.  
  135. #endif
  136.  
  137. /* Debugging routine for displaying errors */
  138. #ifdef DEBUG
  139. /* Notice: the order of appearance of the following
  140.  * messages is bound to the error codes defined
  141.  * in mconf.h.
  142.  */
  143. static _CONST char *_CONST ermsg[7] = {
  144.   "unknown",                    /* error code 0 */
  145.   "domain",                     /* error code 1 */
  146.   "singularity",                /* et seq.      */
  147.   "overflow",
  148.   "underflow",
  149.   "total loss of precision",
  150.   "partial loss of precision"
  151. };
  152.  
  153. #define mtherr(name, code) printf( "\n%s %s error\n", name, ermsg[code] );
  154. #else
  155. #define mtherr(name, code)
  156. #endif
  157.  
  158. /*                                                      ieee.c
  159.  *
  160.  *    Extended precision IEEE binary floating point arithmetic routines
  161.  *
  162.  * Numbers are stored in C language as arrays of 16-bit unsigned
  163.  * short integers.  The arguments of the routines are pointers to
  164.  * the arrays.
  165.  *
  166.  *
  167.  * External e type data structure, simulates Intel 8087 chip
  168.  * temporary real format but possibly with a larger significand:
  169.  *
  170.  *      NE-1 significand words  (least significant word first,
  171.  *                               most significant bit is normally set)
  172.  *      exponent                (value = EXONE for 1.0,
  173.  *                              top bit is the sign)
  174.  *
  175.  *
  176.  * Internal data structure of a number (a "word" is 16 bits):
  177.  *
  178.  * ei[0]        sign word       (0 for positive, 0xffff for negative)
  179.  * ei[1]        biased exponent (value = EXONE for the number 1.0)
  180.  * ei[2]        high guard word (always zero after normalization)
  181.  * ei[3]
  182.  * to ei[NI-2]  significand     (NI-4 significand words,
  183.  *                               most significant word first,
  184.  *                               most significant bit is set)
  185.  * ei[NI-1]     low guard word  (0x8000 bit is rounding place)
  186.  *
  187.  *
  188.  *
  189.  *              Routines for external format numbers
  190.  *
  191.  *      asctoe( string, e )     ASCII string to extended double e type
  192.  *      asctoe64( string, &d )  ASCII string to long double
  193.  *      asctoe53( string, &d )  ASCII string to double
  194.  *      asctoe24( string, &f )  ASCII string to single
  195.  *      asctoeg( string, e, prec, ldp ) ASCII string to specified precision
  196.  *      e24toe( &f, e, ldp )    IEEE single precision to e type
  197.  *      e53toe( &d, e, ldp )    IEEE double precision to e type
  198.  *      e64toe( &d, e, ldp )    IEEE long double precision to e type
  199.  *      e113toe( &d, e, ldp )   IEEE long double precision to e type
  200.  *      eabs(e)                 absolute value
  201.  *      eadd( a, b, c )         c = b + a
  202.  *      eclear(e)               e = 0
  203.  *      ecmp (a, b)             Returns 1 if a > b, 0 if a == b,
  204.  *                              -1 if a < b, -2 if either a or b is a NaN.
  205.  *      ediv( a, b, c, ldp )    c = b / a
  206.  *      efloor( a, b, ldp )     truncate to integer, toward -infinity
  207.  *      efrexp( a, exp, s )     extract exponent and significand
  208.  *      eifrac( e, &l, frac )   e to long integer and e type fraction
  209.  *      euifrac( e, &l, frac )  e to unsigned long integer and e type fraction
  210.  *      einfin( e, ldp )        set e to infinity, leaving its sign alone
  211.  *      eldexp( a, n, b )       multiply by 2**n
  212.  *      emov( a, b )            b = a
  213.  *      emul( a, b, c, ldp )    c = b * a
  214.  *      eneg(e)                 e = -e
  215.  *      eround( a, b )          b = nearest integer value to a
  216.  *      esub( a, b, c, ldp )    c = b - a
  217.  *      e24toasc( &f, str, n )  single to ASCII string, n digits after decimal
  218.  *      e53toasc( &d, str, n )  double to ASCII string, n digits after decimal
  219.  *      e64toasc( &d, str, n )  long double to ASCII string
  220.  *      etoasc(e,str,n,fmt,ldp)e to ASCII string, n digits after decimal
  221.  *      etoe24( e, &f )         convert e type to IEEE single precision
  222.  *      etoe53( e, &d )         convert e type to IEEE double precision
  223.  *      etoe64( e, &d )         convert e type to IEEE long double precision
  224.  *      ltoe( &l, e )           long (32 bit) integer to e type
  225.  *      ultoe( &l, e )          unsigned long (32 bit) integer to e type
  226.  *      eisneg( e )             1 if sign bit of e != 0, else 0
  227.  *      eisinf( e )             1 if e has maximum exponent (non-IEEE)
  228.  *                              or is infinite (IEEE)
  229.  *      eisnan( e )             1 if e is a NaN
  230.  *      esqrt( a, b )           b = square root of a
  231.  *
  232.  *
  233.  *              Routines for internal format numbers
  234.  *
  235.  *      eaddm( ai, bi )         add significands, bi = bi + ai
  236.  *      ecleaz(ei)              ei = 0
  237.  *      ecleazs(ei)             set ei = 0 but leave its sign alone
  238.  *      ecmpm( ai, bi )         compare significands, return 1, 0, or -1
  239.  *      edivm( ai, bi, ldp )    divide  significands, bi = bi / ai
  240.  *      emdnorm(ai,l,s,exp,ldp) normalize and round off
  241.  *      emovi( a, ai )          convert external a to internal ai
  242.  *      emovo( ai, a, ldp )     convert internal ai to external a
  243.  *      emovz( ai, bi )         bi = ai, low guard word of bi = 0
  244.  *      emulm( ai, bi, ldp )    multiply significands, bi = bi * ai
  245.  *      enormlz(ei)             left-justify the significand
  246.  *      eshdn1( ai )            shift significand and guards down 1 bit
  247.  *      eshdn8( ai )            shift down 8 bits
  248.  *      eshdn6( ai )            shift down 16 bits
  249.  *      eshift( ai, n )         shift ai n bits up (or down if n < 0)
  250.  *      eshup1( ai )            shift significand and guards up 1 bit
  251.  *      eshup8( ai )            shift up 8 bits
  252.  *      eshup6( ai )            shift up 16 bits
  253.  *      esubm( ai, bi )         subtract significands, bi = bi - ai
  254.  *
  255.  *
  256.  * The result is always normalized and rounded to NI-4 word precision
  257.  * after each arithmetic operation.
  258.  *
  259.  * Exception flags are NOT fully supported.
  260.  *
  261.  * Define USE_INFINITY in mconf.h for support of infinity; otherwise a
  262.  * saturation arithmetic is implemented.
  263.  *
  264.  * Define NANS for support of Not-a-Number items; otherwise the
  265.  * arithmetic will never produce a NaN output, and might be confused
  266.  * by a NaN input.
  267.  * If NaN's are supported, the output of ecmp(a,b) is -2 if
  268.  * either a or b is a NaN. This means asking if(ecmp(a,b) < 0)
  269.  * may not be legitimate. Use if(ecmp(a,b) == -1) for less-than
  270.  * if in doubt.
  271.  * Signaling NaN's are NOT supported; they are treated the same
  272.  * as quiet NaN's.
  273.  *
  274.  * Denormals are always supported here where appropriate (e.g., not
  275.  * for conversion to DEC numbers).
  276.  */
  277.  
  278. /*
  279.  * Revision history:
  280.  *
  281.  *  5 Jan 84    PDP-11 assembly language version
  282.  *  6 Dec 86    C language version
  283.  * 30 Aug 88    100 digit version, improved rounding
  284.  * 15 May 92    80-bit long double support
  285.  * 22 Nov 00    Revised to fit into newlib by Jeff Johnston <jjohnstn@redhat.com>
  286.  *
  287.  * Author:  S. L. Moshier.
  288.  *
  289.  * Copyright (c) 1984,2000 S.L. Moshier
  290.  *
  291.  * Permission to use, copy, modify, and distribute this software for any
  292.  * purpose without fee is hereby granted, provided that this entire notice
  293.  * is included in all copies of any software which is or includes a copy
  294.  * or modification of this software and in all copies of the supporting
  295.  * documentation for such software.
  296.  *
  297.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  298.  * WARRANTY.  IN PARTICULAR,  THE AUTHOR MAKES NO REPRESENTATION
  299.  * OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  300.  * SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  301.  *
  302.  */
  303.  
  304. #include <stdio.h>
  305. /* #include "\usr\include\stdio.h" */
  306. /*#include "ehead.h"*/
  307. /*#include "mconf.h"*/
  308. /*                                                      mconf.h
  309.  *
  310.  *      Common include file for math routines
  311.  *
  312.  *
  313.  *
  314.  * SYNOPSIS:
  315.  *
  316.  * #include "mconf.h"
  317.  *
  318.  *
  319.  *
  320.  * DESCRIPTION:
  321.  *
  322.  * This file contains definitions for error codes that are
  323.  * passed to the common error handling routine mtherr()
  324.  * (which see).
  325.  *
  326.  * The file also includes a conditional assembly definition
  327.  * for the type of computer arithmetic (IEEE, DEC, Motorola
  328.  * IEEE, or UNKnown).
  329.  *
  330.  * For Digital Equipment PDP-11 and VAX computers, certain
  331.  * IBM systems, and others that use numbers with a 56-bit
  332.  * significand, the symbol DEC should be defined.  In this
  333.  * mode, most floating point constants are given as arrays
  334.  * of octal integers to eliminate decimal to binary conversion
  335.  * errors that might be introduced by the compiler.
  336.  *
  337.  * For computers, such as IBM PC, that follow the IEEE
  338.  * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
  339.  * Std 754-1985), the symbol IBMPC should be defined.  These
  340.  * numbers have 53-bit significands.  In this mode, constants
  341.  * are provided as arrays of hexadecimal 16 bit integers.
  342.  *
  343.  * To accommodate other types of computer arithmetic, all
  344.  * constants are also provided in a normal decimal radix
  345.  * which one can hope are correctly converted to a suitable
  346.  * format by the available C language compiler.  To invoke
  347.  * this mode, the symbol UNK is defined.
  348.  *
  349.  * An important difference among these modes is a predefined
  350.  * set of machine arithmetic constants for each.  The numbers
  351.  * MACHEP (the machine roundoff error), MAXNUM (largest number
  352.  * represented), and several other parameters are preset by
  353.  * the configuration symbol.  Check the file const.c to
  354.  * ensure that these values are correct for your computer.
  355.  *
  356.  * For ANSI C compatibility, define ANSIC equal to 1.  Currently
  357.  * this affects only the atan2() function and others that use it.
  358.  */
  359.  
  360. /* Constant definitions for math error conditions
  361.  */
  362.  
  363. #define DOMAIN          1       /* argument domain error */
  364. #define SING            2       /* argument singularity */
  365. #define OVERFLOW        3       /* overflow range error */
  366. #define UNDERFLOW       4       /* underflow range error */
  367. #define TLOSS           5       /* total loss of precision */
  368. #define PLOSS           6       /* partial loss of precision */
  369.  
  370. #define EDOM            33
  371. #define ERANGE          34
  372.  
  373. typedef struct
  374. {
  375.   double r;
  376.   double i;
  377. } cmplx;
  378.  
  379. /* Type of computer arithmetic */
  380.  
  381. #ifndef DEC
  382. #ifdef __IEEE_LITTLE_ENDIAN
  383. #define IBMPC 1
  384. #else /* !__IEEE_LITTLE_ENDIAN */
  385. #define MIEEE 1
  386. #endif /* !__IEEE_LITTLE_ENDIAN */
  387. #endif /* !DEC */
  388.  
  389. /* Define 1 for ANSI C atan2() function
  390.  * See atan.c and clog.c.
  391.  */
  392. #define ANSIC 1
  393.  
  394. /*define VOLATILE volatile*/
  395. #define VOLATILE
  396.  
  397. #define NANS
  398. #define USE_INFINITY
  399.  
  400. /* NaN's require infinity support. */
  401. #ifdef NANS
  402. #ifndef INFINITY
  403. #define USE_INFINITY
  404. #endif
  405. #endif
  406.  
  407. /* This handles 64-bit long ints. */
  408. #define LONGBITS (8 * sizeof(long))
  409.  
  410.  
  411. static void eaddm (short unsigned int *x, short unsigned int *y);
  412. static void esubm (short unsigned int *x, short unsigned int *y);
  413. static void emdnorm (short unsigned int *s, int lost, int subflg,
  414.                      long int exp, int rcntrl, LDPARMS * ldp);
  415. static int asctoeg (char *ss, short unsigned int *y, int oprec,
  416.                     LDPARMS * ldp);
  417. static void enan (short unsigned int *nan, int size);
  418. #if LDBL_MANT_DIG == 24
  419. static void toe24 (short unsigned int *x, short unsigned int *y);
  420. #elif LDBL_MANT_DIG == 53
  421. static void toe53 (short unsigned int *x, short unsigned int *y);
  422. #elif LDBL_MANT_DIG == 64
  423. static void toe64 (short unsigned int *a, short unsigned int *b);
  424. #else
  425. static void toe113 (short unsigned int *a, short unsigned int *b);
  426. #endif
  427. static void eiremain (short unsigned int *den, short unsigned int *num,
  428.                       LDPARMS * ldp);
  429. static int ecmpm (register short unsigned int *a,
  430.                   register short unsigned int *b);
  431. static int edivm (short unsigned int *den, short unsigned int *num,
  432.                   LDPARMS * ldp);
  433. static int emulm (short unsigned int *a, short unsigned int *b,
  434.                   LDPARMS * ldp);
  435. static int eisneg (_CONST short unsigned int *x);
  436. static int eisinf (_CONST short unsigned int *x);
  437. static void emovi (_CONST short unsigned int *a, short unsigned int *b);
  438. static void emovo (short unsigned int *a, short unsigned int *b,
  439.                    LDPARMS * ldp);
  440. static void emovz (register short unsigned int *a,
  441.                    register short unsigned int *b);
  442. static void ecleaz (register short unsigned int *xi);
  443. static void eadd1 (_CONST short unsigned int *a, _CONST short unsigned int *b,
  444.                    short unsigned int *c, int subflg, LDPARMS * ldp);
  445. static int eisnan (_CONST short unsigned int *x);
  446. static int eiisnan (short unsigned int *x);
  447.  
  448. #ifdef DEC
  449. static void etodec (), todec (), dectoe ();
  450. #endif
  451.  
  452. /*
  453. ; Clear out entire external format number.
  454. ;
  455. ; unsigned short x[];
  456. ; eclear( x );
  457. */
  458.  
  459. static void
  460. eclear (register short unsigned int *x)
  461. {
  462.   register int i;
  463.  
  464.   for (i = 0; i < NE; i++)
  465.     *x++ = 0;
  466. }
  467.  
  468.  
  469.  
  470. /* Move external format number from a to b.
  471.  *
  472.  * emov( a, b );
  473.  */
  474.  
  475. static void
  476. emov (register _CONST short unsigned int *a, register short unsigned int *b)
  477. {
  478.   register int i;
  479.  
  480.   for (i = 0; i < NE; i++)
  481.     *b++ = *a++;
  482. }
  483.  
  484.  
  485. /*
  486. ;       Negate external format number
  487. ;
  488. ;       unsigned short x[NE];
  489. ;       eneg( x );
  490. */
  491.  
  492. static void
  493. eneg (short unsigned int *x)
  494. {
  495.  
  496. #ifdef NANS
  497.   if (eisnan (x))
  498.     return;
  499. #endif
  500.   x[NE - 1] ^= 0x8000;          /* Toggle the sign bit */
  501. }
  502.  
  503.  
  504.  
  505. /* Return 1 if external format number is negative,
  506.  * else return zero.
  507.  */
  508. static int
  509. eisneg (_CONST short unsigned int *x)
  510. {
  511.  
  512. #ifdef NANS
  513.   if (eisnan (x))
  514.     return (0);
  515. #endif
  516.   if (x[NE - 1] & 0x8000)
  517.     return (1);
  518.   else
  519.     return (0);
  520. }
  521.  
  522.  
  523. /* Return 1 if external format number has maximum possible exponent,
  524.  * else return zero.
  525.  */
  526. static int
  527. eisinf (_CONST short unsigned int *x)
  528. {
  529.  
  530.   if ((x[NE - 1] & 0x7fff) == 0x7fff)
  531.     {
  532. #ifdef NANS
  533.       if (eisnan (x))
  534.         return (0);
  535. #endif
  536.       return (1);
  537.     }
  538.   else
  539.     return (0);
  540. }
  541.  
  542. /* Check if e-type number is not a number.
  543.  */
  544. static int
  545. eisnan (_CONST short unsigned int *x)
  546. {
  547.  
  548. #ifdef NANS
  549.   int i;
  550. /* NaN has maximum exponent */
  551.   if ((x[NE - 1] & 0x7fff) != 0x7fff)
  552.     return (0);
  553. /* ... and non-zero significand field. */
  554.   for (i = 0; i < NE - 1; i++)
  555.     {
  556.       if (*x++ != 0)
  557.         return (1);
  558.     }
  559. #endif
  560.   return (0);
  561. }
  562.  
  563. /*
  564. ; Fill entire number, including exponent and significand, with
  565. ; largest possible number.  These programs implement a saturation
  566. ; value that is an ordinary, legal number.  A special value
  567. ; "infinity" may also be implemented; this would require tests
  568. ; for that value and implementation of special rules for arithmetic
  569. ; operations involving inifinity.
  570. */
  571.  
  572. static void
  573. einfin (register short unsigned int *x, register LDPARMS * ldp)
  574. {
  575.   register int i;
  576.  
  577. #ifdef USE_INFINITY
  578.   for (i = 0; i < NE - 1; i++)
  579.     *x++ = 0;
  580.   *x |= 32767;
  581.   ldp = ldp;
  582. #else
  583.   for (i = 0; i < NE - 1; i++)
  584.     *x++ = 0xffff;
  585.   *x |= 32766;
  586.   if (ldp->rndprc < NBITS)
  587.     {
  588.       if (ldp->rndprc == 113)
  589.         {
  590.           *(x - 9) = 0;
  591.           *(x - 8) = 0;
  592.         }
  593.       if (ldp->rndprc == 64)
  594.         {
  595.           *(x - 5) = 0;
  596.         }
  597.       if (ldp->rndprc == 53)
  598.         {
  599.           *(x - 4) = 0xf800;
  600.         }
  601.       else
  602.         {
  603.           *(x - 4) = 0;
  604.           *(x - 3) = 0;
  605.           *(x - 2) = 0xff00;
  606.         }
  607.     }
  608. #endif
  609. }
  610.  
  611. /* Move in external format number,
  612.  * converting it to internal format.
  613.  */
  614. static void
  615. emovi (_CONST short unsigned int *a, short unsigned int *b)
  616. {
  617.   register _CONST unsigned short *p;
  618.   register unsigned short *q;
  619.   int i;
  620.  
  621.   q = b;
  622.   p = a + (NE - 1);             /* point to last word of external number */
  623. /* get the sign bit */
  624.   if (*p & 0x8000)
  625.     *q++ = 0xffff;
  626.   else
  627.     *q++ = 0;
  628. /* get the exponent */
  629.   *q = *p--;
  630.   *q++ &= 0x7fff;               /* delete the sign bit */
  631. #ifdef USE_INFINITY
  632.   if ((*(q - 1) & 0x7fff) == 0x7fff)
  633.     {
  634. #ifdef NANS
  635.       if (eisnan (a))
  636.         {
  637.           *q++ = 0;
  638.           for (i = 3; i < NI; i++)
  639.             *q++ = *p--;
  640.           return;
  641.         }
  642. #endif
  643.       for (i = 2; i < NI; i++)
  644.         *q++ = 0;
  645.       return;
  646.     }
  647. #endif
  648. /* clear high guard word */
  649.   *q++ = 0;
  650. /* move in the significand */
  651.   for (i = 0; i < NE - 1; i++)
  652.     *q++ = *p--;
  653. /* clear low guard word */
  654.   *q = 0;
  655. }
  656.  
  657.  
  658. /* Move internal format number out,
  659.  * converting it to external format.
  660.  */
  661. static void
  662. emovo (short unsigned int *a, short unsigned int *b, LDPARMS * ldp)
  663. {
  664.   register unsigned short *p, *q;
  665.   unsigned short i;
  666.  
  667.   p = a;
  668.   q = b + (NE - 1);             /* point to output exponent */
  669. /* combine sign and exponent */
  670.   i = *p++;
  671.   if (i)
  672.     *q-- = *p++ | 0x8000;
  673.   else
  674.     *q-- = *p++;
  675. #ifdef USE_INFINITY
  676.   if (*(p - 1) == 0x7fff)
  677.     {
  678. #ifdef NANS
  679.       if (eiisnan (a))
  680.         {
  681.           enan (b, NBITS);
  682.           return;
  683.         }
  684. #endif
  685.       einfin (b, ldp);
  686.       return;
  687.     }
  688. #endif
  689. /* skip over guard word */
  690.   ++p;
  691. /* move the significand */
  692.   for (i = 0; i < NE - 1; i++)
  693.     *q-- = *p++;
  694. }
  695.  
  696.  
  697. /* Clear out internal format number.
  698.  */
  699.  
  700. static void
  701. ecleaz (register short unsigned int *xi)
  702. {
  703.   register int i;
  704.  
  705.   for (i = 0; i < NI; i++)
  706.     *xi++ = 0;
  707. }
  708.  
  709. /* same, but don't touch the sign. */
  710.  
  711. static void
  712. ecleazs (register short unsigned int *xi)
  713. {
  714.   register int i;
  715.  
  716.   ++xi;
  717.   for (i = 0; i < NI - 1; i++)
  718.     *xi++ = 0;
  719. }
  720.  
  721.  
  722.  
  723.  
  724. /* Move internal format number from a to b.
  725.  */
  726. static void
  727. emovz (register short unsigned int *a, register short unsigned int *b)
  728. {
  729.   register int i;
  730.  
  731.   for (i = 0; i < NI - 1; i++)
  732.     *b++ = *a++;
  733. /* clear low guard word */
  734.   *b = 0;
  735. }
  736.  
  737. /* Return nonzero if internal format number is a NaN.
  738.  */
  739.  
  740. static int
  741. eiisnan (short unsigned int *x)
  742. {
  743.   int i;
  744.  
  745.   if ((x[E] & 0x7fff) == 0x7fff)
  746.     {
  747.       for (i = M + 1; i < NI; i++)
  748.         {
  749.           if (x[i] != 0)
  750.             return (1);
  751.         }
  752.     }
  753.   return (0);
  754. }
  755.  
  756. #if LDBL_MANT_DIG == 64
  757.  
  758. /* Return nonzero if internal format number is infinite. */
  759. static int
  760. eiisinf (unsigned short x[])
  761. {
  762.  
  763. #ifdef NANS
  764.   if (eiisnan (x))
  765.     return (0);
  766. #endif
  767.   if ((x[E] & 0x7fff) == 0x7fff)
  768.     return (1);
  769.   return (0);
  770. }
  771. #endif /* LDBL_MANT_DIG == 64 */
  772.  
  773. /*
  774. ;       Compare significands of numbers in internal format.
  775. ;       Guard words are included in the comparison.
  776. ;
  777. ;       unsigned short a[NI], b[NI];
  778. ;       cmpm( a, b );
  779. ;
  780. ;       for the significands:
  781. ;       returns +1 if a > b
  782. ;                0 if a == b
  783. ;               -1 if a < b
  784. */
  785. static int
  786. ecmpm (register short unsigned int *a, register short unsigned int *b)
  787. {
  788.   int i;
  789.  
  790.   a += M;                       /* skip up to significand area */
  791.   b += M;
  792.   for (i = M; i < NI; i++)
  793.     {
  794.       if (*a++ != *b++)
  795.         goto difrnt;
  796.     }
  797.   return (0);
  798.  
  799. difrnt:
  800.   if (*(--a) > *(--b))
  801.     return (1);
  802.   else
  803.     return (-1);
  804. }
  805.  
  806.  
  807. /*
  808. ;       Shift significand down by 1 bit
  809. */
  810.  
  811. static void
  812. eshdn1 (register short unsigned int *x)
  813. {
  814.   register unsigned short bits;
  815.   int i;
  816.  
  817.   x += M;                       /* point to significand area */
  818.  
  819.   bits = 0;
  820.   for (i = M; i < NI; i++)
  821.     {
  822.       if (*x & 1)
  823.         bits |= 1;
  824.       *x >>= 1;
  825.       if (bits & 2)
  826.         *x |= 0x8000;
  827.       bits <<= 1;
  828.       ++x;
  829.     }
  830. }
  831.  
  832.  
  833.  
  834. /*
  835. ;       Shift significand up by 1 bit
  836. */
  837.  
  838. static void
  839. eshup1 (register short unsigned int *x)
  840. {
  841.   register unsigned short bits;
  842.   int i;
  843.  
  844.   x += NI - 1;
  845.   bits = 0;
  846.  
  847.   for (i = M; i < NI; i++)
  848.     {
  849.       if (*x & 0x8000)
  850.         bits |= 1;
  851.       *x <<= 1;
  852.       if (bits & 2)
  853.         *x |= 1;
  854.       bits <<= 1;
  855.       --x;
  856.     }
  857. }
  858.  
  859.  
  860.  
  861. /*
  862. ;       Shift significand down by 8 bits
  863. */
  864.  
  865. static void
  866. eshdn8 (register short unsigned int *x)
  867. {
  868.   register unsigned short newbyt, oldbyt;
  869.   int i;
  870.  
  871.   x += M;
  872.   oldbyt = 0;
  873.   for (i = M; i < NI; i++)
  874.     {
  875.       newbyt = *x << 8;
  876.       *x >>= 8;
  877.       *x |= oldbyt;
  878.       oldbyt = newbyt;
  879.       ++x;
  880.     }
  881. }
  882.  
  883. /*
  884. ;       Shift significand up by 8 bits
  885. */
  886.  
  887. static void
  888. eshup8 (register short unsigned int *x)
  889. {
  890.   int i;
  891.   register unsigned short newbyt, oldbyt;
  892.  
  893.   x += NI - 1;
  894.   oldbyt = 0;
  895.  
  896.   for (i = M; i < NI; i++)
  897.     {
  898.       newbyt = *x >> 8;
  899.       *x <<= 8;
  900.       *x |= oldbyt;
  901.       oldbyt = newbyt;
  902.       --x;
  903.     }
  904. }
  905.  
  906. /*
  907. ;       Shift significand up by 16 bits
  908. */
  909.  
  910. static void
  911. eshup6 (register short unsigned int *x)
  912. {
  913.   int i;
  914.   register unsigned short *p;
  915.  
  916.   p = x + M;
  917.   x += M + 1;
  918.  
  919.   for (i = M; i < NI - 1; i++)
  920.     *p++ = *x++;
  921.  
  922.   *p = 0;
  923. }
  924.  
  925. /*
  926. ;       Shift significand down by 16 bits
  927. */
  928.  
  929. static void
  930. eshdn6 (register short unsigned int *x)
  931. {
  932.   int i;
  933.   register unsigned short *p;
  934.  
  935.   x += NI - 1;
  936.   p = x + 1;
  937.  
  938.   for (i = M; i < NI - 1; i++)
  939.     *(--p) = *(--x);
  940.  
  941.   *(--p) = 0;
  942. }
  943. /*
  944. ;       Add significands
  945. ;       x + y replaces y
  946. */
  947.  
  948. static void
  949. eaddm (short unsigned int *x, short unsigned int *y)
  950. {
  951.   register unsigned long a;
  952.   int i;
  953.   unsigned int carry;
  954.  
  955.   x += NI - 1;
  956.   y += NI - 1;
  957.   carry = 0;
  958.   for (i = M; i < NI; i++)
  959.     {
  960.       a = (unsigned long) (*x) + (unsigned long) (*y) + carry;
  961.       if (a & 0x10000)
  962.         carry = 1;
  963.       else
  964.         carry = 0;
  965.       *y = (unsigned short) a;
  966.       --x;
  967.       --y;
  968.     }
  969. }
  970.  
  971. /*
  972. ;       Subtract significands
  973. ;       y - x replaces y
  974. */
  975.  
  976. static void
  977. esubm (short unsigned int *x, short unsigned int *y)
  978. {
  979.   unsigned long a;
  980.   int i;
  981.   unsigned int carry;
  982.  
  983.   x += NI - 1;
  984.   y += NI - 1;
  985.   carry = 0;
  986.   for (i = M; i < NI; i++)
  987.     {
  988.       a = (unsigned long) (*y) - (unsigned long) (*x) - carry;
  989.       if (a & 0x10000)
  990.         carry = 1;
  991.       else
  992.         carry = 0;
  993.       *y = (unsigned short) a;
  994.       --x;
  995.       --y;
  996.     }
  997. }
  998.  
  999.  
  1000. /* Divide significands */
  1001.  
  1002.  
  1003. /* Multiply significand of e-type number b
  1004. by 16-bit quantity a, e-type result to c. */
  1005.  
  1006. static void
  1007. m16m (short unsigned int a, short unsigned int *b, short unsigned int *c)
  1008. {
  1009.   register unsigned short *pp;
  1010.   register unsigned long carry;
  1011.   unsigned short *ps;
  1012.   unsigned short p[NI];
  1013.   unsigned long aa, m;
  1014.   int i;
  1015.  
  1016.   aa = a;
  1017.   pp = &p[NI - 2];
  1018.   *pp++ = 0;
  1019.   *pp = 0;
  1020.   ps = &b[NI - 1];
  1021.  
  1022.   for (i = M + 1; i < NI; i++)
  1023.     {
  1024.       if (*ps == 0)
  1025.         {
  1026.           --ps;
  1027.           --pp;
  1028.           *(pp - 1) = 0;
  1029.         }
  1030.       else
  1031.         {
  1032.           m = (unsigned long) aa **ps--;
  1033.           carry = (m & 0xffff) + *pp;
  1034.           *pp-- = (unsigned short) carry;
  1035.           carry = (carry >> 16) + (m >> 16) + *pp;
  1036.           *pp = (unsigned short) carry;
  1037.           *(pp - 1) = carry >> 16;
  1038.         }
  1039.     }
  1040.   for (i = M; i < NI; i++)
  1041.     c[i] = p[i];
  1042. }
  1043.  
  1044.  
  1045. /* Divide significands. Neither the numerator nor the denominator
  1046. is permitted to have its high guard word nonzero.  */
  1047.  
  1048.  
  1049. static int
  1050. edivm (short unsigned int *den, short unsigned int *num, LDPARMS * ldp)
  1051. {
  1052.   int i;
  1053.   register unsigned short *p;
  1054.   unsigned long tnum;
  1055.   unsigned short j, tdenm, tquot;
  1056.   unsigned short tprod[NI + 1];
  1057.   unsigned short *equot = ldp->equot;
  1058.  
  1059.   p = &equot[0];
  1060.   *p++ = num[0];
  1061.   *p++ = num[1];
  1062.  
  1063.   for (i = M; i < NI; i++)
  1064.     {
  1065.       *p++ = 0;
  1066.     }
  1067.   eshdn1 (num);
  1068.   tdenm = den[M + 1];
  1069.   for (i = M; i < NI; i++)
  1070.     {
  1071.       /* Find trial quotient digit (the radix is 65536). */
  1072.       tnum = (((unsigned long) num[M]) << 16) + num[M + 1];
  1073.  
  1074.       /* Do not execute the divide instruction if it will overflow. */
  1075.       if ((tdenm * 0xffffUL) < tnum)
  1076.         tquot = 0xffff;
  1077.       else
  1078.         tquot = tnum / tdenm;
  1079.  
  1080.       /* Prove that the divide worked. */
  1081. /*
  1082.         tcheck = (unsigned long )tquot * tdenm;
  1083.         if( tnum - tcheck > tdenm )
  1084.                 tquot = 0xffff;
  1085. */
  1086.       /* Multiply denominator by trial quotient digit. */
  1087.       m16m (tquot, den, tprod);
  1088.       /* The quotient digit may have been overestimated. */
  1089.       if (ecmpm (tprod, num) > 0)
  1090.         {
  1091.           tquot -= 1;
  1092.           esubm (den, tprod);
  1093.           if (ecmpm (tprod, num) > 0)
  1094.             {
  1095.               tquot -= 1;
  1096.               esubm (den, tprod);
  1097.             }
  1098.         }
  1099. /*
  1100.         if( ecmpm( tprod, num ) > 0 )
  1101.                 {
  1102.                 eshow( "tprod", tprod );
  1103.                 eshow( "num  ", num );
  1104.                 printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",
  1105.                          tnum, den[M+1], tquot );
  1106.                 }
  1107. */
  1108.       esubm (tprod, num);
  1109. /*
  1110.         if( ecmpm( num, den ) >= 0 )
  1111.                 {
  1112.                 eshow( "num  ", num );
  1113.                 eshow( "den  ", den );
  1114.                 printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",
  1115.                          tnum, den[M+1], tquot );
  1116.                 }
  1117. */
  1118.       equot[i] = tquot;
  1119.       eshup6 (num);
  1120.     }
  1121. /* test for nonzero remainder after roundoff bit */
  1122.   p = &num[M];
  1123.   j = 0;
  1124.   for (i = M; i < NI; i++)
  1125.     {
  1126.       j |= *p++;
  1127.     }
  1128.   if (j)
  1129.     j = 1;
  1130.  
  1131.   for (i = 0; i < NI; i++)
  1132.     num[i] = equot[i];
  1133.  
  1134.   return ((int) j);
  1135. }
  1136.  
  1137.  
  1138.  
  1139. /* Multiply significands */
  1140. static int
  1141. emulm (short unsigned int *a, short unsigned int *b, LDPARMS * ldp)
  1142. {
  1143.   unsigned short *p, *q;
  1144.   unsigned short pprod[NI];
  1145.   unsigned short j;
  1146.   int i;
  1147.   unsigned short *equot = ldp->equot;
  1148.  
  1149.   equot[0] = b[0];
  1150.   equot[1] = b[1];
  1151.   for (i = M; i < NI; i++)
  1152.     equot[i] = 0;
  1153.  
  1154.   j = 0;
  1155.   p = &a[NI - 1];
  1156.   q = &equot[NI - 1];
  1157.   for (i = M + 1; i < NI; i++)
  1158.     {
  1159.       if (*p == 0)
  1160.         {
  1161.           --p;
  1162.         }
  1163.       else
  1164.         {
  1165.           m16m (*p--, b, pprod);
  1166.           eaddm (pprod, equot);
  1167.         }
  1168.       j |= *q;
  1169.       eshdn6 (equot);
  1170.     }
  1171.  
  1172.   for (i = 0; i < NI; i++)
  1173.     b[i] = equot[i];
  1174.  
  1175. /* return flag for lost nonzero bits */
  1176.   return ((int) j);
  1177. }
  1178.  
  1179.  
  1180. /*
  1181. static void eshow(str, x)
  1182. char *str;
  1183. unsigned short *x;
  1184. {
  1185. int i;
  1186.  
  1187. printf( "%s ", str );
  1188. for( i=0; i<NI; i++ )
  1189.         printf( "%04x ", *x++ );
  1190. printf( "\n" );
  1191. }
  1192. */
  1193.  
  1194.  
  1195. /*
  1196.  * Normalize and round off.
  1197.  *
  1198.  * The internal format number to be rounded is "s".
  1199.  * Input "lost" indicates whether the number is exact.
  1200.  * This is the so-called sticky bit.
  1201.  *
  1202.  * Input "subflg" indicates whether the number was obtained
  1203.  * by a subtraction operation.  In that case if lost is nonzero
  1204.  * then the number is slightly smaller than indicated.
  1205.  *
  1206.  * Input "exp" is the biased exponent, which may be negative.
  1207.  * the exponent field of "s" is ignored but is replaced by
  1208.  * "exp" as adjusted by normalization and rounding.
  1209.  *
  1210.  * Input "rcntrl" is the rounding control.
  1211.  */
  1212.  
  1213.  
  1214. static void
  1215. emdnorm (short unsigned int *s, int lost, int subflg, long int exp,
  1216.          int rcntrl, LDPARMS * ldp)
  1217. {
  1218.   int i, j;
  1219.   unsigned short r;
  1220.  
  1221. /* Normalize */
  1222.   j = enormlz (s);
  1223.  
  1224. /* a blank significand could mean either zero or infinity. */
  1225. #ifndef USE_INFINITY
  1226.   if (j > NBITS)
  1227.     {
  1228.       ecleazs (s);
  1229.       return;
  1230.     }
  1231. #endif
  1232.   exp -= j;
  1233. #ifndef USE_INFINITY
  1234.   if (exp >= 32767L)
  1235.     goto overf;
  1236. #else
  1237.   if ((j > NBITS) && (exp < 32767L))
  1238.     {
  1239.       ecleazs (s);
  1240.       return;
  1241.     }
  1242. #endif
  1243.   if (exp < 0L)
  1244.     {
  1245.       if (exp > (long) (-NBITS - 1))
  1246.         {
  1247.           j = (int) exp;
  1248.           i = eshift (s, j);
  1249.           if (i)
  1250.             lost = 1;
  1251.         }
  1252.       else
  1253.         {
  1254.           ecleazs (s);
  1255.           return;
  1256.         }
  1257.     }
  1258. /* Round off, unless told not to by rcntrl. */
  1259.   if (rcntrl == 0)
  1260.     goto mdfin;
  1261. /* Set up rounding parameters if the control register changed. */
  1262.   if (ldp->rndprc != ldp->rlast)
  1263.     {
  1264.       ecleaz (ldp->rbit);
  1265.       switch (ldp->rndprc)
  1266.         {
  1267.         default:
  1268.         case NBITS:
  1269.           ldp->rw = NI - 1;     /* low guard word */
  1270.           ldp->rmsk = 0xffff;
  1271.           ldp->rmbit = 0x8000;
  1272.           ldp->rebit = 1;
  1273.           ldp->re = ldp->rw - 1;
  1274.           break;
  1275.         case 113:
  1276.           ldp->rw = 10;
  1277.           ldp->rmsk = 0x7fff;
  1278.           ldp->rmbit = 0x4000;
  1279.           ldp->rebit = 0x8000;
  1280.           ldp->re = ldp->rw;
  1281.           break;
  1282.         case 64:
  1283.           ldp->rw = 7;
  1284.           ldp->rmsk = 0xffff;
  1285.           ldp->rmbit = 0x8000;
  1286.           ldp->rebit = 1;
  1287.           ldp->re = ldp->rw - 1;
  1288.           break;
  1289. /* For DEC arithmetic */
  1290.         case 56:
  1291.           ldp->rw = 6;
  1292.           ldp->rmsk = 0xff;
  1293.           ldp->rmbit = 0x80;
  1294.           ldp->rebit = 0x100;
  1295.           ldp->re = ldp->rw;
  1296.           break;
  1297.         case 53:
  1298.           ldp->rw = 6;
  1299.           ldp->rmsk = 0x7ff;
  1300.           ldp->rmbit = 0x0400;
  1301.           ldp->rebit = 0x800;
  1302.           ldp->re = ldp->rw;
  1303.           break;
  1304.         case 24:
  1305.           ldp->rw = 4;
  1306.           ldp->rmsk = 0xff;
  1307.           ldp->rmbit = 0x80;
  1308.           ldp->rebit = 0x100;
  1309.           ldp->re = ldp->rw;
  1310.           break;
  1311.         }
  1312.       ldp->rbit[ldp->re] = ldp->rebit;
  1313.       ldp->rlast = ldp->rndprc;
  1314.     }
  1315.  
  1316. /* Shift down 1 temporarily if the data structure has an implied
  1317.  * most significant bit and the number is denormal.
  1318.  * For rndprc = 64 or NBITS, there is no implied bit.
  1319.  * But Intel long double denormals lose one bit of significance even so.
  1320.  */
  1321. #if IBMPC
  1322.   if ((exp <= 0) && (ldp->rndprc != NBITS))
  1323. #else
  1324.   if ((exp <= 0) && (ldp->rndprc != 64) && (ldp->rndprc != NBITS))
  1325. #endif
  1326.     {
  1327.       lost |= s[NI - 1] & 1;
  1328.       eshdn1 (s);
  1329.     }
  1330. /* Clear out all bits below the rounding bit,
  1331.  * remembering in r if any were nonzero.
  1332.  */
  1333.   r = s[ldp->rw] & ldp->rmsk;
  1334.   if (ldp->rndprc < NBITS)
  1335.     {
  1336.       i = ldp->rw + 1;
  1337.       while (i < NI)
  1338.         {
  1339.           if (s[i])
  1340.             r |= 1;
  1341.           s[i] = 0;
  1342.           ++i;
  1343.         }
  1344.     }
  1345.   s[ldp->rw] &= ~ldp->rmsk;
  1346.   if ((r & ldp->rmbit) != 0)
  1347.     {
  1348.       if (r == ldp->rmbit)
  1349.         {
  1350.           if (lost == 0)
  1351.             {                   /* round to even */
  1352.               if ((s[ldp->re] & ldp->rebit) == 0)
  1353.                 goto mddone;
  1354.             }
  1355.           else
  1356.             {
  1357.               if (subflg != 0)
  1358.                 goto mddone;
  1359.             }
  1360.         }
  1361.       eaddm (ldp->rbit, s);
  1362.     }
  1363. mddone:
  1364. #if IBMPC
  1365.   if ((exp <= 0) && (ldp->rndprc != NBITS))
  1366. #else
  1367.   if ((exp <= 0) && (ldp->rndprc != 64) && (ldp->rndprc != NBITS))
  1368. #endif
  1369.     {
  1370.       eshup1 (s);
  1371.     }
  1372.   if (s[2] != 0)
  1373.     {                           /* overflow on roundoff */
  1374.       eshdn1 (s);
  1375.       exp += 1;
  1376.     }
  1377. mdfin:
  1378.   s[NI - 1] = 0;
  1379.   if (exp >= 32767L)
  1380.     {
  1381. #ifndef USE_INFINITY
  1382.     overf:
  1383. #endif
  1384. #ifdef USE_INFINITY
  1385.       s[1] = 32767;
  1386.       for (i = 2; i < NI - 1; i++)
  1387.         s[i] = 0;
  1388. #else
  1389.       s[1] = 32766;
  1390.       s[2] = 0;
  1391.       for (i = M + 1; i < NI - 1; i++)
  1392.         s[i] = 0xffff;
  1393.       s[NI - 1] = 0;
  1394.       if ((ldp->rndprc < 64) || (ldp->rndprc == 113))
  1395.         {
  1396.           s[ldp->rw] &= ~ldp->rmsk;
  1397.           if (ldp->rndprc == 24)
  1398.             {
  1399.               s[5] = 0;
  1400.               s[6] = 0;
  1401.             }
  1402.         }
  1403. #endif
  1404.       return;
  1405.     }
  1406.   if (exp < 0)
  1407.     s[1] = 0;
  1408.   else
  1409.     s[1] = (unsigned short) exp;
  1410. }
  1411.  
  1412.  
  1413.  
  1414. /*
  1415. ;       Subtract external format numbers.
  1416. ;
  1417. ;       unsigned short a[NE], b[NE], c[NE];
  1418. ;       LDPARMS *ldp;
  1419. ;       esub( a, b, c, ldp );    c = b - a
  1420. */
  1421.  
  1422. static void
  1423. esub (_CONST short unsigned int *a, _CONST short unsigned int *b,
  1424.       short unsigned int *c, LDPARMS * ldp)
  1425. {
  1426.  
  1427. #ifdef NANS
  1428.   if (eisnan (a))
  1429.     {
  1430.       emov (a, c);
  1431.       return;
  1432.     }
  1433.   if (eisnan (b))
  1434.     {
  1435.       emov (b, c);
  1436.       return;
  1437.     }
  1438. /* Infinity minus infinity is a NaN.
  1439.  * Test for subtracting infinities of the same sign.
  1440.  */
  1441.   if (eisinf (a) && eisinf (b) && ((eisneg (a) ^ eisneg (b)) == 0))
  1442.     {
  1443.       mtherr ("esub", DOMAIN);
  1444.       enan (c, NBITS);
  1445.       return;
  1446.     }
  1447. #endif
  1448.   eadd1 (a, b, c, 1, ldp);
  1449. }
  1450.  
  1451.  
  1452.  
  1453. static void
  1454. eadd1 (_CONST short unsigned int *a, _CONST short unsigned int *b,
  1455.        short unsigned int *c, int subflg, LDPARMS * ldp)
  1456. {
  1457.   unsigned short ai[NI], bi[NI], ci[NI];
  1458.   int i, lost, j, k;
  1459.   long lt, lta, ltb;
  1460.  
  1461. #ifdef USE_INFINITY
  1462.   if (eisinf (a))
  1463.     {
  1464.       emov (a, c);
  1465.       if (subflg)
  1466.         eneg (c);
  1467.       return;
  1468.     }
  1469.   if (eisinf (b))
  1470.     {
  1471.       emov (b, c);
  1472.       return;
  1473.     }
  1474. #endif
  1475.   emovi (a, ai);
  1476.   emovi (b, bi);
  1477.   if (subflg)
  1478.     ai[0] = ~ai[0];
  1479.  
  1480. /* compare exponents */
  1481.   lta = ai[E];
  1482.   ltb = bi[E];
  1483.   lt = lta - ltb;
  1484.   if (lt > 0L)
  1485.     {                           /* put the larger number in bi */
  1486.       emovz (bi, ci);
  1487.       emovz (ai, bi);
  1488.       emovz (ci, ai);
  1489.       ltb = bi[E];
  1490.       lt = -lt;
  1491.     }
  1492.   lost = 0;
  1493.   if (lt != 0L)
  1494.     {
  1495.       if (lt < (long) (-NBITS - 1))
  1496.         goto done;              /* answer same as larger addend */
  1497.       k = (int) lt;
  1498.       lost = eshift (ai, k);    /* shift the smaller number down */
  1499.     }
  1500.   else
  1501.     {
  1502. /* exponents were the same, so must compare significands */
  1503.       i = ecmpm (ai, bi);
  1504.       if (i == 0)
  1505.         {                       /* the numbers are identical in magnitude */
  1506.           /* if different signs, result is zero */
  1507.           if (ai[0] != bi[0])
  1508.             {
  1509.               eclear (c);
  1510.               return;
  1511.             }
  1512.           /* if same sign, result is double */
  1513.           /* double denomalized tiny number */
  1514.           if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
  1515.             {
  1516.               eshup1 (bi);
  1517.               goto done;
  1518.             }
  1519.           /* add 1 to exponent unless both are zero! */
  1520.           for (j = 1; j < NI - 1; j++)
  1521.             {
  1522.               if (bi[j] != 0)
  1523.                 {
  1524. /* This could overflow, but let emovo take care of that. */
  1525.                   ltb += 1;
  1526.                   break;
  1527.                 }
  1528.             }
  1529.           bi[E] = (unsigned short) ltb;
  1530.           goto done;
  1531.         }
  1532.       if (i > 0)
  1533.         {                       /* put the larger number in bi */
  1534.           emovz (bi, ci);
  1535.           emovz (ai, bi);
  1536.           emovz (ci, ai);
  1537.         }
  1538.     }
  1539.   if (ai[0] == bi[0])
  1540.     {
  1541.       eaddm (ai, bi);
  1542.       subflg = 0;
  1543.     }
  1544.   else
  1545.     {
  1546.       esubm (ai, bi);
  1547.       subflg = 1;
  1548.     }
  1549.   emdnorm (bi, lost, subflg, ltb, 64, ldp);
  1550.  
  1551. done:
  1552.   emovo (bi, c, ldp);
  1553. }
  1554.  
  1555.  
  1556.  
  1557. /*
  1558. ;       Divide.
  1559. ;
  1560. ;       unsigned short a[NE], b[NE], c[NE];
  1561. ;       LDPARMS *ldp;
  1562. ;       ediv( a, b, c, ldp );   c = b / a
  1563. */
  1564. static void
  1565. ediv (_CONST short unsigned int *a, _CONST short unsigned int *b,
  1566.       short unsigned int *c, LDPARMS * ldp)
  1567. {
  1568.   unsigned short ai[NI], bi[NI];
  1569.   int i;
  1570.   long lt, lta, ltb;
  1571.  
  1572. #ifdef NANS
  1573. /* Return any NaN input. */
  1574.   if (eisnan (a))
  1575.     {
  1576.       emov (a, c);
  1577.       return;
  1578.     }
  1579.   if (eisnan (b))
  1580.     {
  1581.       emov (b, c);
  1582.       return;
  1583.     }
  1584. /* Zero over zero, or infinity over infinity, is a NaN. */
  1585.   if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
  1586.       || (eisinf (a) && eisinf (b)))
  1587.     {
  1588.       mtherr ("ediv", DOMAIN);
  1589.       enan (c, NBITS);
  1590.       return;
  1591.     }
  1592. #endif
  1593. /* Infinity over anything else is infinity. */
  1594. #ifdef USE_INFINITY
  1595.   if (eisinf (b))
  1596.     {
  1597.       if (eisneg (a) ^ eisneg (b))
  1598.         *(c + (NE - 1)) = 0x8000;
  1599.       else
  1600.         *(c + (NE - 1)) = 0;
  1601.       einfin (c, ldp);
  1602.       return;
  1603.     }
  1604.   if (eisinf (a))
  1605.     {
  1606.       eclear (c);
  1607.       return;
  1608.     }
  1609. #endif
  1610.   emovi (a, ai);
  1611.   emovi (b, bi);
  1612.   lta = ai[E];
  1613.   ltb = bi[E];
  1614.   if (bi[E] == 0)
  1615.     {                           /* See if numerator is zero. */
  1616.       for (i = 1; i < NI - 1; i++)
  1617.         {
  1618.           if (bi[i] != 0)
  1619.             {
  1620.               ltb -= enormlz (bi);
  1621.               goto dnzro1;
  1622.             }
  1623.         }
  1624.       eclear (c);
  1625.       return;
  1626.     }
  1627. dnzro1:
  1628.  
  1629.   if (ai[E] == 0)
  1630.     {                           /* possible divide by zero */
  1631.       for (i = 1; i < NI - 1; i++)
  1632.         {
  1633.           if (ai[i] != 0)
  1634.             {
  1635.               lta -= enormlz (ai);
  1636.               goto dnzro2;
  1637.             }
  1638.         }
  1639.       if (ai[0] == bi[0])
  1640.         *(c + (NE - 1)) = 0;
  1641.       else
  1642.         *(c + (NE - 1)) = 0x8000;
  1643.       einfin (c, ldp);
  1644.       mtherr ("ediv", SING);
  1645.       return;
  1646.     }
  1647. dnzro2:
  1648.  
  1649.   i = edivm (ai, bi, ldp);
  1650. /* calculate exponent */
  1651.   lt = ltb - lta + EXONE;
  1652.   emdnorm (bi, i, 0, lt, 64, ldp);
  1653. /* set the sign */
  1654.   if (ai[0] == bi[0])
  1655.     bi[0] = 0;
  1656.   else
  1657.     bi[0] = 0Xffff;
  1658.   emovo (bi, c, ldp);
  1659. }
  1660.  
  1661.  
  1662.  
  1663. /*
  1664. ;       Multiply.
  1665. ;
  1666. ;       unsigned short a[NE], b[NE], c[NE];
  1667. ;       LDPARMS *ldp
  1668. ;       emul( a, b, c, ldp );   c = b * a
  1669. */
  1670. static void
  1671. emul (_CONST short unsigned int *a, _CONST short unsigned int *b,
  1672.       short unsigned int *c, LDPARMS * ldp)
  1673. {
  1674.   unsigned short ai[NI], bi[NI];
  1675.   int i, j;
  1676.   long lt, lta, ltb;
  1677.  
  1678. #ifdef NANS
  1679. /* NaN times anything is the same NaN. */
  1680.   if (eisnan (a))
  1681.     {
  1682.       emov (a, c);
  1683.       return;
  1684.     }
  1685.   if (eisnan (b))
  1686.     {
  1687.       emov (b, c);
  1688.       return;
  1689.     }
  1690. /* Zero times infinity is a NaN. */
  1691.   if ((eisinf (a) && (ecmp (b, ezero) == 0))
  1692.       || (eisinf (b) && (ecmp (a, ezero) == 0)))
  1693.     {
  1694.       mtherr ("emul", DOMAIN);
  1695.       enan (c, NBITS);
  1696.       return;
  1697.     }
  1698. #endif
  1699. /* Infinity times anything else is infinity. */
  1700. #ifdef USE_INFINITY
  1701.   if (eisinf (a) || eisinf (b))
  1702.     {
  1703.       if (eisneg (a) ^ eisneg (b))
  1704.         *(c + (NE - 1)) = 0x8000;
  1705.       else
  1706.         *(c + (NE - 1)) = 0;
  1707.       einfin (c, ldp);
  1708.       return;
  1709.     }
  1710. #endif
  1711.   emovi (a, ai);
  1712.   emovi (b, bi);
  1713.   lta = ai[E];
  1714.   ltb = bi[E];
  1715.   if (ai[E] == 0)
  1716.     {
  1717.       for (i = 1; i < NI - 1; i++)
  1718.         {
  1719.           if (ai[i] != 0)
  1720.             {
  1721.               lta -= enormlz (ai);
  1722.               goto mnzer1;
  1723.             }
  1724.         }
  1725.       eclear (c);
  1726.       return;
  1727.     }
  1728. mnzer1:
  1729.  
  1730.   if (bi[E] == 0)
  1731.     {
  1732.       for (i = 1; i < NI - 1; i++)
  1733.         {
  1734.           if (bi[i] != 0)
  1735.             {
  1736.               ltb -= enormlz (bi);
  1737.               goto mnzer2;
  1738.             }
  1739.         }
  1740.       eclear (c);
  1741.       return;
  1742.     }
  1743. mnzer2:
  1744.  
  1745. /* Multiply significands */
  1746.   j = emulm (ai, bi, ldp);
  1747. /* calculate exponent */
  1748.   lt = lta + ltb - (EXONE - 1);
  1749.   emdnorm (bi, j, 0, lt, 64, ldp);
  1750. /* calculate sign of product */
  1751.   if (ai[0] == bi[0])
  1752.     bi[0] = 0;
  1753.   else
  1754.     bi[0] = 0xffff;
  1755.   emovo (bi, c, ldp);
  1756. }
  1757.  
  1758.  
  1759.  
  1760. #if LDBL_MANT_DIG > 64
  1761. static void
  1762. e113toe (short unsigned int *pe, short unsigned int *y, LDPARMS * ldp)
  1763. {
  1764.   register unsigned short r;
  1765.   unsigned short *e, *p;
  1766.   unsigned short yy[NI];
  1767.   int denorm, i;
  1768.  
  1769.   e = pe;
  1770.   denorm = 0;
  1771.   ecleaz (yy);
  1772. #ifdef IBMPC
  1773.   e += 7;
  1774. #endif
  1775.   r = *e;
  1776.   yy[0] = 0;
  1777.   if (r & 0x8000)
  1778.     yy[0] = 0xffff;
  1779.   r &= 0x7fff;
  1780. #ifdef USE_INFINITY
  1781.   if (r == 0x7fff)
  1782.     {
  1783. #ifdef NANS
  1784. #ifdef IBMPC
  1785.       for (i = 0; i < 7; i++)
  1786.         {
  1787.           if (pe[i] != 0)
  1788.             {
  1789.               enan (y, NBITS);
  1790.               return;
  1791.             }
  1792.         }
  1793. #else /* !IBMPC */
  1794.       for (i = 1; i < 8; i++)
  1795.         {
  1796.           if (pe[i] != 0)
  1797.             {
  1798.               enan (y, NBITS);
  1799.               return;
  1800.             }
  1801.         }
  1802. #endif /* !IBMPC */
  1803. #endif /* NANS */
  1804.       eclear (y);
  1805.       einfin (y, ldp);
  1806.       if (*e & 0x8000)
  1807.         eneg (y);
  1808.       return;
  1809.     }
  1810. #endif /* INFINITY */
  1811.   yy[E] = r;
  1812.   p = &yy[M + 1];
  1813. #ifdef IBMPC
  1814.   for (i = 0; i < 7; i++)
  1815.     *p++ = *(--e);
  1816. #else /* IBMPC */
  1817.   ++e;
  1818.   for (i = 0; i < 7; i++)
  1819.     *p++ = *e++;
  1820. #endif /* IBMPC */
  1821. /* If denormal, remove the implied bit; else shift down 1. */
  1822.   if (r == 0)
  1823.     {
  1824.       yy[M] = 0;
  1825.     }
  1826.   else
  1827.     {
  1828.       yy[M] = 1;
  1829.       eshift (yy, -1);
  1830.     }
  1831.   emovo (yy, y, ldp);
  1832. }
  1833.  
  1834. /* move out internal format to ieee long double */
  1835. static void
  1836. toe113 (short unsigned int *a, short unsigned int *b)
  1837. {
  1838.   register unsigned short *p, *q;
  1839.   unsigned short i;
  1840.  
  1841. #ifdef NANS
  1842.   if (eiisnan (a))
  1843.     {
  1844.       enan (b, 113);
  1845.       return;
  1846.     }
  1847. #endif
  1848.   p = a;
  1849. #ifdef MIEEE
  1850.   q = b;
  1851. #else
  1852.   q = b + 7;                    /* point to output exponent */
  1853. #endif
  1854.  
  1855. /* If not denormal, delete the implied bit. */
  1856.   if (a[E] != 0)
  1857.     {
  1858.       eshup1 (a);
  1859.     }
  1860. /* combine sign and exponent */
  1861.   i = *p++;
  1862. #ifdef MIEEE
  1863.   if (i)
  1864.     *q++ = *p++ | 0x8000;
  1865.   else
  1866.     *q++ = *p++;
  1867. #else
  1868.   if (i)
  1869.     *q-- = *p++ | 0x8000;
  1870.   else
  1871.     *q-- = *p++;
  1872. #endif
  1873. /* skip over guard word */
  1874.   ++p;
  1875. /* move the significand */
  1876. #ifdef MIEEE
  1877.   for (i = 0; i < 7; i++)
  1878.     *q++ = *p++;
  1879. #else
  1880.   for (i = 0; i < 7; i++)
  1881.     *q-- = *p++;
  1882. #endif
  1883. }
  1884. #endif /* LDBL_MANT_DIG > 64 */
  1885.  
  1886.  
  1887. #if LDBL_MANT_DIG == 64
  1888. static void
  1889. e64toe (short unsigned int *pe, short unsigned int *y, LDPARMS * ldp)
  1890. {
  1891.   unsigned short yy[NI];
  1892.   unsigned short *p, *q, *e;
  1893.   int i;
  1894.  
  1895.   e = pe;
  1896.   p = yy;
  1897.  
  1898.   for (i = 0; i < NE - 5; i++)
  1899.     *p++ = 0;
  1900. #ifdef IBMPC
  1901.   for (i = 0; i < 5; i++)
  1902.     *p++ = *e++;
  1903. #endif
  1904. #ifdef DEC
  1905.   for (i = 0; i < 5; i++)
  1906.     *p++ = *e++;
  1907. #endif
  1908. #ifdef MIEEE
  1909.   p = &yy[0] + (NE - 1);
  1910.   *p-- = *e++;
  1911.   ++e;                          /* MIEEE skips over 2nd short */
  1912.   for (i = 0; i < 4; i++)
  1913.     *p-- = *e++;
  1914. #endif
  1915.  
  1916. #ifdef IBMPC
  1917. /* For Intel long double, shift denormal significand up 1
  1918.    -- but only if the top significand bit is zero.  */
  1919.   if ((yy[NE - 1] & 0x7fff) == 0 && (yy[NE - 2] & 0x8000) == 0)
  1920.     {
  1921.       unsigned short temp[NI + 1];
  1922.       emovi (yy, temp);
  1923.       eshup1 (temp);
  1924.       emovo (temp, y, ldp);
  1925.       return;
  1926.     }
  1927. #endif
  1928. #ifdef USE_INFINITY
  1929. /* Point to the exponent field.  */
  1930.   p = &yy[NE - 1];
  1931.   if ((*p & 0x7fff) == 0x7fff)
  1932.     {
  1933. #ifdef NANS
  1934. #ifdef IBMPC
  1935.       for (i = 0; i < 4; i++)
  1936.         {
  1937.           if ((i != 3 && pe[i] != 0)
  1938.               /* Check for Intel long double infinity pattern.  */
  1939.               || (i == 3 && pe[i] != 0x8000))
  1940.             {
  1941.               enan (y, NBITS);
  1942.               return;
  1943.             }
  1944.         }
  1945. #endif
  1946. #ifdef MIEEE
  1947.       for (i = 2; i <= 5; i++)
  1948.         {
  1949.           if (pe[i] != 0)
  1950.             {
  1951.               enan (y, NBITS);
  1952.               return;
  1953.             }
  1954.         }
  1955. #endif
  1956. #endif /* NANS */
  1957.       eclear (y);
  1958.       einfin (y, ldp);
  1959.       if (*p & 0x8000)
  1960.         eneg (y);
  1961.       return;
  1962.     }
  1963. #endif /* USE_INFINITY */
  1964.   p = yy;
  1965.   q = y;
  1966.   for (i = 0; i < NE; i++)
  1967.     *q++ = *p++;
  1968. }
  1969.  
  1970. /* move out internal format to ieee long double */
  1971. static void
  1972. toe64 (short unsigned int *a, short unsigned int *b)
  1973. {
  1974.   register unsigned short *p, *q;
  1975.   unsigned short i;
  1976.  
  1977. #ifdef NANS
  1978.   if (eiisnan (a))
  1979.     {
  1980.       enan (b, 64);
  1981.       return;
  1982.     }
  1983. #endif
  1984. #ifdef IBMPC
  1985. /* Shift Intel denormal significand down 1.  */
  1986.   if (a[E] == 0)
  1987.     eshdn1 (a);
  1988. #endif
  1989.   p = a;
  1990. #ifdef MIEEE
  1991.   q = b;
  1992. #else
  1993.   q = b + 4;                    /* point to output exponent */
  1994. /* NOTE: Intel data type is 96 bits wide, clear the last word here. */
  1995.   *(q + 1) = 0;
  1996. #endif
  1997.  
  1998. /* combine sign and exponent */
  1999.   i = *p++;
  2000. #ifdef MIEEE
  2001.   if (i)
  2002.     *q++ = *p++ | 0x8000;
  2003.   else
  2004.     *q++ = *p++;
  2005.   *q++ = 0;                     /* leave 2nd short blank */
  2006. #else
  2007.   if (i)
  2008.     *q-- = *p++ | 0x8000;
  2009.   else
  2010.     *q-- = *p++;
  2011. #endif
  2012. /* skip over guard word */
  2013.   ++p;
  2014. /* move the significand */
  2015. #ifdef MIEEE
  2016.   for (i = 0; i < 4; i++)
  2017.     *q++ = *p++;
  2018. #else
  2019. #ifdef USE_INFINITY
  2020. #ifdef IBMPC
  2021.   if (eiisinf (a))
  2022.     {
  2023.       /* Intel long double infinity.  */
  2024.       *q-- = 0x8000;
  2025.       *q-- = 0;
  2026.       *q-- = 0;
  2027.       *q = 0;
  2028.       return;
  2029.     }
  2030. #endif /* IBMPC */
  2031. #endif /* USE_INFINITY */
  2032.   for (i = 0; i < 4; i++)
  2033.     *q-- = *p++;
  2034. #endif
  2035. }
  2036.  
  2037. #endif /* LDBL_MANT_DIG == 64 */
  2038.  
  2039. #if LDBL_MANT_DIG == 53
  2040. /*
  2041. ; Convert IEEE double precision to e type
  2042. ;       double d;
  2043. ;       unsigned short x[N+2];
  2044. ;       e53toe( &d, x );
  2045. */
  2046. static void
  2047. e53toe (short unsigned int *pe, short unsigned int *y, LDPARMS * ldp)
  2048. {
  2049. #ifdef DEC
  2050.  
  2051.   dectoe (pe, y);               /* see etodec.c */
  2052.  
  2053. #else
  2054.  
  2055.   register unsigned short r;
  2056.   register unsigned short *p, *e;
  2057.   unsigned short yy[NI];
  2058.   int denorm, k;
  2059.  
  2060.   e = pe;
  2061.   denorm = 0;                   /* flag if denormalized number */
  2062.   ecleaz (yy);
  2063. #ifdef IBMPC
  2064.   e += 3;
  2065. #endif
  2066. #ifdef DEC
  2067.   e += 3;
  2068. #endif
  2069.   r = *e;
  2070.   yy[0] = 0;
  2071.   if (r & 0x8000)
  2072.     yy[0] = 0xffff;
  2073.   yy[M] = (r & 0x0f) | 0x10;
  2074.   r &= ~0x800f;                 /* strip sign and 4 significand bits */
  2075. #ifdef USE_INFINITY
  2076.   if (r == 0x7ff0)
  2077.     {
  2078. #ifdef NANS
  2079. #ifdef IBMPC
  2080.       if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
  2081.           || (pe[1] != 0) || (pe[0] != 0))
  2082.         {
  2083.           enan (y, NBITS);
  2084.           return;
  2085.         }
  2086. #else /* !IBMPC */
  2087.       if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
  2088.           || (pe[2] != 0) || (pe[3] != 0))
  2089.         {
  2090.           enan (y, NBITS);
  2091.           return;
  2092.         }
  2093. #endif /* !IBMPC */
  2094. #endif /* NANS */
  2095.       eclear (y);
  2096.       einfin (y, ldp);
  2097.       if (yy[0])
  2098.         eneg (y);
  2099.       return;
  2100.     }
  2101. #endif
  2102.   r >>= 4;
  2103. /* If zero exponent, then the significand is denormalized.
  2104.  * So, take back the understood high significand bit. */
  2105.   if (r == 0)
  2106.     {
  2107.       denorm = 1;
  2108.       yy[M] &= ~0x10;
  2109.     }
  2110.   r += EXONE - 01777;
  2111.   yy[E] = r;
  2112.   p = &yy[M + 1];
  2113. #ifdef IBMPC
  2114.   *p++ = *(--e);
  2115.   *p++ = *(--e);
  2116.   *p++ = *(--e);
  2117. #else /* !IBMPC */
  2118.   ++e;
  2119.   *p++ = *e++;
  2120.   *p++ = *e++;
  2121.   *p++ = *e++;
  2122. #endif /* !IBMPC */
  2123.   (void) eshift (yy, -5);
  2124.   if (denorm)
  2125.     {                           /* if zero exponent, then normalize the significand */
  2126.       if ((k = enormlz (yy)) > NBITS)
  2127.         ecleazs (yy);
  2128.       else
  2129.         yy[E] -= (unsigned short) (k - 1);
  2130.     }
  2131.   emovo (yy, y, ldp);
  2132. #endif /* !DEC */
  2133. }
  2134.  
  2135. /*
  2136. ; e type to IEEE double precision
  2137. ;       double d;
  2138. ;       unsigned short x[NE];
  2139. ;       etoe53( x, &d );
  2140. */
  2141.  
  2142. #ifdef DEC
  2143.  
  2144. static void
  2145. etoe53 (x, e)
  2146.      unsigned short *x, *e;
  2147. {
  2148.   etodec (x, e);                /* see etodec.c */
  2149. }
  2150.  
  2151. static void
  2152. toe53 (x, y)
  2153.      unsigned short *x, *y;
  2154. {
  2155.   todec (x, y);
  2156. }
  2157.  
  2158. #else
  2159.  
  2160. static void
  2161. toe53 (short unsigned int *x, short unsigned int *y)
  2162. {
  2163.   unsigned short i;
  2164.   unsigned short *p;
  2165.  
  2166.  
  2167. #ifdef NANS
  2168.   if (eiisnan (x))
  2169.     {
  2170.       enan (y, 53);
  2171.       return;
  2172.     }
  2173. #endif
  2174.   p = &x[0];
  2175. #ifdef IBMPC
  2176.   y += 3;
  2177. #endif
  2178. #ifdef DEC
  2179.   y += 3;
  2180. #endif
  2181.   *y = 0;                       /* output high order */
  2182.   if (*p++)
  2183.     *y = 0x8000;                /* output sign bit */
  2184.  
  2185.   i = *p++;
  2186.   if (i >= (unsigned int) 2047)
  2187.     {                           /* Saturate at largest number less than infinity. */
  2188. #ifdef USE_INFINITY
  2189.       *y |= 0x7ff0;
  2190. #ifdef IBMPC
  2191.       *(--y) = 0;
  2192.       *(--y) = 0;
  2193.       *(--y) = 0;
  2194. #else /* !IBMPC */
  2195.       ++y;
  2196.       *y++ = 0;
  2197.       *y++ = 0;
  2198.       *y++ = 0;
  2199. #endif /* IBMPC */
  2200. #else /* !USE_INFINITY */
  2201.       *y |= (unsigned short) 0x7fef;
  2202. #ifdef IBMPC
  2203.       *(--y) = 0xffff;
  2204.       *(--y) = 0xffff;
  2205.       *(--y) = 0xffff;
  2206. #else /* !IBMPC */
  2207.       ++y;
  2208.       *y++ = 0xffff;
  2209.       *y++ = 0xffff;
  2210.       *y++ = 0xffff;
  2211. #endif
  2212. #endif /* !USE_INFINITY */
  2213.       return;
  2214.     }
  2215.   if (i == 0)
  2216.     {
  2217.       (void) eshift (x, 4);
  2218.     }
  2219.   else
  2220.     {
  2221.       i <<= 4;
  2222.       (void) eshift (x, 5);
  2223.     }
  2224.   i |= *p++ & (unsigned short) 0x0f;    /* *p = xi[M] */
  2225.   *y |= (unsigned short) i;     /* high order output already has sign bit set */
  2226. #ifdef IBMPC
  2227.   *(--y) = *p++;
  2228.   *(--y) = *p++;
  2229.   *(--y) = *p;
  2230. #else /* !IBMPC */
  2231.   ++y;
  2232.   *y++ = *p++;
  2233.   *y++ = *p++;
  2234.   *y++ = *p++;
  2235. #endif /* !IBMPC */
  2236. }
  2237.  
  2238. #endif /* not DEC */
  2239. #endif /* LDBL_MANT_DIG == 53 */
  2240.  
  2241. #if LDBL_MANT_DIG == 24
  2242. /*
  2243. ; Convert IEEE single precision to e type
  2244. ;       float d;
  2245. ;       unsigned short x[N+2];
  2246. ;       dtox( &d, x );
  2247. */
  2248. void
  2249. e24toe (short unsigned int *pe, short unsigned int *y, LDPARMS * ldp)
  2250. {
  2251.   register unsigned short r;
  2252.   register unsigned short *p, *e;
  2253.   unsigned short yy[NI];
  2254.   int denorm, k;
  2255.  
  2256.   e = pe;
  2257.   denorm = 0;                   /* flag if denormalized number */
  2258.   ecleaz (yy);
  2259. #ifdef IBMPC
  2260.   e += 1;
  2261. #endif
  2262. #ifdef DEC
  2263.   e += 1;
  2264. #endif
  2265.   r = *e;
  2266.   yy[0] = 0;
  2267.   if (r & 0x8000)
  2268.     yy[0] = 0xffff;
  2269.   yy[M] = (r & 0x7f) | 0200;
  2270.   r &= ~0x807f;                 /* strip sign and 7 significand bits */
  2271. #ifdef USE_INFINITY
  2272.   if (r == 0x7f80)
  2273.     {
  2274. #ifdef NANS
  2275. #ifdef MIEEE
  2276.       if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
  2277.         {
  2278.           enan (y, NBITS);
  2279.           return;
  2280.         }
  2281. #else /* !MIEEE */
  2282.       if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
  2283.         {
  2284.           enan (y, NBITS);
  2285.           return;
  2286.         }
  2287. #endif /* !MIEEE */
  2288. #endif /* NANS */
  2289.       eclear (y);
  2290.       einfin (y, ldp);
  2291.       if (yy[0])
  2292.         eneg (y);
  2293.       return;
  2294.     }
  2295. #endif
  2296.   r >>= 7;
  2297. /* If zero exponent, then the significand is denormalized.
  2298.  * So, take back the understood high significand bit. */
  2299.   if (r == 0)
  2300.     {
  2301.       denorm = 1;
  2302.       yy[M] &= ~0200;
  2303.     }
  2304.   r += EXONE - 0177;
  2305.   yy[E] = r;
  2306.   p = &yy[M + 1];
  2307. #ifdef IBMPC
  2308.   *p++ = *(--e);
  2309. #endif
  2310. #ifdef DEC
  2311.   *p++ = *(--e);
  2312. #endif
  2313. #ifdef MIEEE
  2314.   ++e;
  2315.   *p++ = *e++;
  2316. #endif
  2317.   (void) eshift (yy, -8);
  2318.   if (denorm)
  2319.     {                           /* if zero exponent, then normalize the significand */
  2320.       if ((k = enormlz (yy)) > NBITS)
  2321.         ecleazs (yy);
  2322.       else
  2323.         yy[E] -= (unsigned short) (k - 1);
  2324.     }
  2325.   emovo (yy, y, ldp);
  2326. }
  2327.  
  2328. static void
  2329. toe24 (short unsigned int *x, short unsigned int *y)
  2330. {
  2331.   unsigned short i;
  2332.   unsigned short *p;
  2333.  
  2334. #ifdef NANS
  2335.   if (eiisnan (x))
  2336.     {
  2337.       enan (y, 24);
  2338.       return;
  2339.     }
  2340. #endif
  2341.   p = &x[0];
  2342. #ifdef IBMPC
  2343.   y += 1;
  2344. #endif
  2345. #ifdef DEC
  2346.   y += 1;
  2347. #endif
  2348.   *y = 0;                       /* output high order */
  2349.   if (*p++)
  2350.     *y = 0x8000;                /* output sign bit */
  2351.  
  2352.   i = *p++;
  2353.   if (i >= 255)
  2354.     {                           /* Saturate at largest number less than infinity. */
  2355. #ifdef USE_INFINITY
  2356.       *y |= (unsigned short) 0x7f80;
  2357. #ifdef IBMPC
  2358.       *(--y) = 0;
  2359. #endif
  2360. #ifdef DEC
  2361.       *(--y) = 0;
  2362. #endif
  2363. #ifdef MIEEE
  2364.       ++y;
  2365.       *y = 0;
  2366. #endif
  2367. #else /* !USE_INFINITY */
  2368.       *y |= (unsigned short) 0x7f7f;
  2369. #ifdef IBMPC
  2370.       *(--y) = 0xffff;
  2371. #endif
  2372. #ifdef DEC
  2373.       *(--y) = 0xffff;
  2374. #endif
  2375. #ifdef MIEEE
  2376.       ++y;
  2377.       *y = 0xffff;
  2378. #endif
  2379. #endif /* !USE_INFINITY */
  2380.       return;
  2381.     }
  2382.   if (i == 0)
  2383.     {
  2384.       (void) eshift (x, 7);
  2385.     }
  2386.   else
  2387.     {
  2388.       i <<= 7;
  2389.       (void) eshift (x, 8);
  2390.     }
  2391.   i |= *p++ & (unsigned short) 0x7f;    /* *p = xi[M] */
  2392.   *y |= i;                      /* high order output already has sign bit set */
  2393. #ifdef IBMPC
  2394.   *(--y) = *p;
  2395. #endif
  2396. #ifdef DEC
  2397.   *(--y) = *p;
  2398. #endif
  2399. #ifdef MIEEE
  2400.   ++y;
  2401.   *y = *p;
  2402. #endif
  2403. }
  2404. #endif /* LDBL_MANT_DIG == 24 */
  2405.  
  2406. /* Compare two e type numbers.
  2407.  *
  2408.  * unsigned short a[NE], b[NE];
  2409.  * ecmp( a, b );
  2410.  *
  2411.  *  returns +1 if a > b
  2412.  *           0 if a == b
  2413.  *          -1 if a < b
  2414.  *          -2 if either a or b is a NaN.
  2415.  */
  2416. static int
  2417. ecmp (_CONST short unsigned int *a, _CONST short unsigned int *b)
  2418. {
  2419.   unsigned short ai[NI], bi[NI];
  2420.   register unsigned short *p, *q;
  2421.   register int i;
  2422.   int msign;
  2423.  
  2424. #ifdef NANS
  2425.   if (eisnan (a) || eisnan (b))
  2426.     return (-2);
  2427. #endif
  2428.   emovi (a, ai);
  2429.   p = ai;
  2430.   emovi (b, bi);
  2431.   q = bi;
  2432.  
  2433.   if (*p != *q)
  2434.     {                           /* the signs are different */
  2435. /* -0 equals + 0 */
  2436.       for (i = 1; i < NI - 1; i++)
  2437.         {
  2438.           if (ai[i] != 0)
  2439.             goto nzro;
  2440.           if (bi[i] != 0)
  2441.             goto nzro;
  2442.         }
  2443.       return (0);
  2444.     nzro:
  2445.       if (*p == 0)
  2446.         return (1);
  2447.       else
  2448.         return (-1);
  2449.     }
  2450. /* both are the same sign */
  2451.   if (*p == 0)
  2452.     msign = 1;
  2453.   else
  2454.     msign = -1;
  2455.   i = NI - 1;
  2456.   do
  2457.     {
  2458.       if (*p++ != *q++)
  2459.         {
  2460.           goto diff;
  2461.         }
  2462.     }
  2463.   while (--i > 0);
  2464.  
  2465.   return (0);                   /* equality */
  2466.  
  2467.  
  2468.  
  2469. diff:
  2470.  
  2471.   if (*(--p) > *(--q))
  2472.     return (msign);             /* p is bigger */
  2473.   else
  2474.     return (-msign);            /* p is littler */
  2475. }
  2476.  
  2477.  
  2478. /*
  2479. ;       Shift significand
  2480. ;
  2481. ;       Shifts significand area up or down by the number of bits
  2482. ;       given by the variable sc.
  2483. */
  2484. static int
  2485. eshift (short unsigned int *x, int sc)
  2486. {
  2487.   unsigned short lost;
  2488.   unsigned short *p;
  2489.  
  2490.   if (sc == 0)
  2491.     return (0);
  2492.  
  2493.   lost = 0;
  2494.   p = x + NI - 1;
  2495.  
  2496.   if (sc < 0)
  2497.     {
  2498.       sc = -sc;
  2499.       while (sc >= 16)
  2500.         {
  2501.           lost |= *p;           /* remember lost bits */
  2502.           eshdn6 (x);
  2503.           sc -= 16;
  2504.         }
  2505.  
  2506.       while (sc >= 8)
  2507.         {
  2508.           lost |= *p & 0xff;
  2509.           eshdn8 (x);
  2510.           sc -= 8;
  2511.         }
  2512.  
  2513.       while (sc > 0)
  2514.         {
  2515.           lost |= *p & 1;
  2516.           eshdn1 (x);
  2517.           sc -= 1;
  2518.         }
  2519.     }
  2520.   else
  2521.     {
  2522.       while (sc >= 16)
  2523.         {
  2524.           eshup6 (x);
  2525.           sc -= 16;
  2526.         }
  2527.  
  2528.       while (sc >= 8)
  2529.         {
  2530.           eshup8 (x);
  2531.           sc -= 8;
  2532.         }
  2533.  
  2534.       while (sc > 0)
  2535.         {
  2536.           eshup1 (x);
  2537.           sc -= 1;
  2538.         }
  2539.     }
  2540.   if (lost)
  2541.     lost = 1;
  2542.   return ((int) lost);
  2543. }
  2544.  
  2545.  
  2546.  
  2547. /*
  2548. ;       normalize
  2549. ;
  2550. ; Shift normalizes the significand area pointed to by argument
  2551. ; shift count (up = positive) is returned.
  2552. */
  2553. static int
  2554. enormlz (short unsigned int *x)
  2555. {
  2556.   register unsigned short *p;
  2557.   int sc;
  2558.  
  2559.   sc = 0;
  2560.   p = &x[M];
  2561.   if (*p != 0)
  2562.     goto normdn;
  2563.   ++p;
  2564.   if (*p & 0x8000)
  2565.     return (0);                 /* already normalized */
  2566.   while (*p == 0)
  2567.     {
  2568.       eshup6 (x);
  2569.       sc += 16;
  2570. /* With guard word, there are NBITS+16 bits available.
  2571.  * return true if all are zero.
  2572.  */
  2573.       if (sc > NBITS)
  2574.         return (sc);
  2575.     }
  2576. /* see if high byte is zero */
  2577.   while ((*p & 0xff00) == 0)
  2578.     {
  2579.       eshup8 (x);
  2580.       sc += 8;
  2581.     }
  2582. /* now shift 1 bit at a time */
  2583.   while ((*p & 0x8000) == 0)
  2584.     {
  2585.       eshup1 (x);
  2586.       sc += 1;
  2587.       if (sc > (NBITS + 16))
  2588.         {
  2589.           mtherr ("enormlz", UNDERFLOW);
  2590.           return (sc);
  2591.         }
  2592.     }
  2593.   return (sc);
  2594.  
  2595. /* Normalize by shifting down out of the high guard word
  2596.    of the significand */
  2597. normdn:
  2598.  
  2599.   if (*p & 0xff00)
  2600.     {
  2601.       eshdn8 (x);
  2602.       sc -= 8;
  2603.     }
  2604.   while (*p != 0)
  2605.     {
  2606.       eshdn1 (x);
  2607.       sc -= 1;
  2608.  
  2609.       if (sc < -NBITS)
  2610.         {
  2611.           mtherr ("enormlz", OVERFLOW);
  2612.           return (sc);
  2613.         }
  2614.     }
  2615.   return (sc);
  2616. }
  2617.  
  2618.  
  2619.  
  2620.  
  2621. /* Convert e type number to decimal format ASCII string.
  2622.  * The constants are for 64 bit precision.
  2623.  */
  2624.  
  2625. #define NTEN 12
  2626. #define MAXP 4096
  2627.  
  2628. #if NE == 10
  2629. static _CONST unsigned short etens[NTEN + 1][NE] = {
  2630.   {0x6576, 0x4a92, 0x804a, 0x153f,
  2631.    0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,},    /* 10**4096 */
  2632.   {0x6a32, 0xce52, 0x329a, 0x28ce,
  2633.    0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,},    /* 10**2048 */
  2634.   {0x526c, 0x50ce, 0xf18b, 0x3d28,
  2635.    0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
  2636.   {0x9c66, 0x58f8, 0xbc50, 0x5c54,
  2637.    0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
  2638.   {0x851e, 0xeab7, 0x98fe, 0x901b,
  2639.    0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
  2640.   {0x0235, 0x0137, 0x36b1, 0x336c,
  2641.    0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
  2642.   {0x50f8, 0x25fb, 0xc76b, 0x6b71,
  2643.    0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
  2644.   {0x0000, 0x0000, 0x0000, 0x0000,
  2645.    0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
  2646.   {0x0000, 0x0000, 0x0000, 0x0000,
  2647.    0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
  2648.   {0x0000, 0x0000, 0x0000, 0x0000,
  2649.    0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
  2650.   {0x0000, 0x0000, 0x0000, 0x0000,
  2651.    0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
  2652.   {0x0000, 0x0000, 0x0000, 0x0000,
  2653.    0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
  2654.   {0x0000, 0x0000, 0x0000, 0x0000,
  2655.    0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,},    /* 10**1 */
  2656. };
  2657.  
  2658. static _CONST unsigned short emtens[NTEN + 1][NE] = {
  2659.   {0x2030, 0xcffc, 0xa1c3, 0x8123,
  2660.    0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,},    /* 10**-4096 */
  2661.   {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
  2662.    0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,},    /* 10**-2048 */
  2663.   {0xf53f, 0xf698, 0x6bd3, 0x0158,
  2664.    0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
  2665.   {0xe731, 0x04d4, 0xe3f2, 0xd332,
  2666.    0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
  2667.   {0xa23e, 0x5308, 0xfefb, 0x1155,
  2668.    0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
  2669.   {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
  2670.    0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
  2671.   {0x2a20, 0x6224, 0x47b3, 0x98d7,
  2672.    0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
  2673.   {0x0b5b, 0x4af2, 0xa581, 0x18ed,
  2674.    0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
  2675.   {0xbf71, 0xa9b3, 0x7989, 0xbe68,
  2676.    0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
  2677.   {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
  2678.    0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
  2679.   {0xc155, 0xa4a8, 0x404e, 0x6113,
  2680.    0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
  2681.   {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
  2682.    0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
  2683.   {0xcccd, 0xcccc, 0xcccc, 0xcccc,
  2684.    0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,},    /* 10**-1 */
  2685. };
  2686. #else
  2687. static _CONST unsigned short etens[NTEN + 1][NE] = {
  2688.   {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,},    /* 10**4096 */
  2689.   {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,},    /* 10**2048 */
  2690.   {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
  2691.   {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
  2692.   {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
  2693.   {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
  2694.   {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
  2695.   {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
  2696.   {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
  2697.   {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
  2698.   {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
  2699.   {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
  2700.   {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,},    /* 10**1 */
  2701. };
  2702.  
  2703. static _CONST unsigned short emtens[NTEN + 1][NE] = {
  2704.   {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,},    /* 10**-4096 */
  2705.   {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,},    /* 10**-2048 */
  2706.   {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
  2707.   {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
  2708.   {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
  2709.   {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
  2710.   {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
  2711.   {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
  2712.   {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
  2713.   {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
  2714.   {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
  2715.   {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
  2716.   {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,},    /* 10**-1 */
  2717. };
  2718. #endif
  2719.  
  2720.  
  2721.  
  2722. /* ASCII string outputs for unix */
  2723.  
  2724.  
  2725. #if 0
  2726. void
  2727. _IO_ldtostr (x, string, ndigs, flags, fmt)
  2728.      long double *x;
  2729.      char *string;
  2730.      int ndigs;
  2731.      int flags;
  2732.      char fmt;
  2733. {
  2734.   unsigned short w[NI];
  2735.   char *t, *u;
  2736.   LDPARMS rnd;
  2737.   LDPARMS *ldp = &rnd;
  2738.  
  2739.   rnd.rlast = -1;
  2740.   rnd.rndprc = NBITS;
  2741.  
  2742.   if (sizeof (long double) == 16)
  2743.     e113toe ((unsigned short *) x, w, ldp);
  2744.   else
  2745.     e64toe ((unsigned short *) x, w, ldp);
  2746.  
  2747.   etoasc (w, string, ndigs, -1, ldp);
  2748.   if (ndigs == 0 && flags == 0)
  2749.     {
  2750.       /* Delete the decimal point unless alternate format.  */
  2751.       t = string;
  2752.       while (*t != '.')
  2753.         ++t;
  2754.       u = t + 1;
  2755.       while (*t != '\0')
  2756.         *t++ = *u++;
  2757.     }
  2758.   if (*string == ' ')
  2759.     {
  2760.       t = string;
  2761.       u = t + 1;
  2762.       while (*t != '\0')
  2763.         *t++ = *u++;
  2764.     }
  2765.   if (fmt == 'E')
  2766.     {
  2767.       t = string;
  2768.       while (*t != 'e')
  2769.         ++t;
  2770.       *t = 'E';
  2771.     }
  2772. }
  2773.  
  2774. #endif
  2775.  
  2776. /* This routine will not return more than NDEC+1 digits. */
  2777.  
  2778. char *
  2779. _ldtoa_r (struct _reent *ptr, long double d, int mode, int ndigits,
  2780.           int *decpt, int *sign, char **rve)
  2781. {
  2782.   unsigned short e[NI];
  2783.   char *s, *p;
  2784.   int i, j, k;
  2785.   int orig_ndigits;
  2786.   LDPARMS rnd;
  2787.   LDPARMS *ldp = &rnd;
  2788.   char *outstr;
  2789.   char outbuf[NDEC + MAX_EXP_DIGITS + 10];
  2790.   union uconv du;
  2791.   du.d = d;
  2792.  
  2793.   orig_ndigits = ndigits;
  2794.   rnd.rlast = -1;
  2795.   rnd.rndprc = NBITS;
  2796.  
  2797.   _REENT_CHECK_MP (ptr);
  2798.  
  2799. /* reentrancy addition to use mprec storage pool */
  2800.   if (_REENT_MP_RESULT (ptr))
  2801.     {
  2802.       _REENT_MP_RESULT (ptr)->_k = _REENT_MP_RESULT_K (ptr);
  2803.       _REENT_MP_RESULT (ptr)->_maxwds = 1 << _REENT_MP_RESULT_K (ptr);
  2804.       Bfree (ptr, _REENT_MP_RESULT (ptr));
  2805.       _REENT_MP_RESULT (ptr) = 0;
  2806.     }
  2807.  
  2808. #if LDBL_MANT_DIG == 24
  2809.   e24toe (&du.pe, e, ldp);
  2810. #elif LDBL_MANT_DIG == 53
  2811.   e53toe (&du.pe, e, ldp);
  2812. #elif LDBL_MANT_DIG == 64
  2813.   e64toe (&du.pe, e, ldp);
  2814. #else
  2815.   e113toe (&du.pe, e, ldp);
  2816. #endif
  2817.  
  2818.   if (eisneg (e))
  2819.     *sign = 1;
  2820.   else
  2821.     *sign = 0;
  2822. /* Mode 3 is "f" format.  */
  2823.   if (mode != 3)
  2824.     ndigits -= 1;
  2825. /* Mode 0 is for %.999 format, which is supposed to give a
  2826.    minimum length string that will convert back to the same binary value.
  2827.    For now, just ask for 20 digits which is enough but sometimes too many.  */
  2828.   if (mode == 0)
  2829.     ndigits = 20;
  2830.  
  2831. /* This sanity limit must agree with the corresponding one in etoasc, to
  2832.    keep straight the returned value of outexpon.  */
  2833.   if (ndigits > NDEC)
  2834.     ndigits = NDEC;
  2835.  
  2836.   etoasc (e, outbuf, ndigits, mode, ldp);
  2837.   s = outbuf;
  2838.   if (eisinf (e) || eisnan (e))
  2839.     {
  2840.       *decpt = 9999;
  2841.       goto stripspaces;
  2842.     }
  2843.   *decpt = ldp->outexpon + 1;
  2844.  
  2845. /* Transform the string returned by etoasc into what the caller wants.  */
  2846.  
  2847. /* Look for decimal point and delete it from the string. */
  2848.   s = outbuf;
  2849.   while (*s != '\0')
  2850.     {
  2851.       if (*s == '.')
  2852.         goto yesdecpt;
  2853.       ++s;
  2854.     }
  2855.   goto nodecpt;
  2856.  
  2857. yesdecpt:
  2858.  
  2859. /* Delete the decimal point.  */
  2860.   while (*s != '\0')
  2861.     {
  2862.       *s = *(s + 1);
  2863.       ++s;
  2864.     }
  2865.  
  2866. nodecpt:
  2867.  
  2868. /* Back up over the exponent field. */
  2869.   while (*s != 'E' && s > outbuf)
  2870.     --s;
  2871.   *s = '\0';
  2872.  
  2873. stripspaces:
  2874.  
  2875. /* Strip leading spaces and sign. */
  2876.   p = outbuf;
  2877.   while (*p == ' ' || *p == '-')
  2878.     ++p;
  2879.  
  2880. /* Find new end of string.  */
  2881.   s = outbuf;
  2882.   while ((*s++ = *p++) != '\0')
  2883.     ;
  2884.   --s;
  2885.  
  2886. /* Strip trailing zeros.  */
  2887.   if (mode == 2)
  2888.     k = 1;
  2889.   else if (ndigits > ldp->outexpon)
  2890.     k = ndigits;
  2891.   else
  2892.     k = ldp->outexpon;
  2893.  
  2894.   while (*(s - 1) == '0' && ((s - outbuf) > k))
  2895.     *(--s) = '\0';
  2896.  
  2897. /* In f format, flush small off-scale values to zero.
  2898.    Rounding has been taken care of by etoasc. */
  2899.   if (mode == 3 && ((ndigits + ldp->outexpon) < 0))
  2900.     {
  2901.       s = outbuf;
  2902.       *s = '\0';
  2903.       *decpt = 0;
  2904.     }
  2905.  
  2906. /* reentrancy addition to use mprec storage pool */
  2907. /* we want to have enough space to hold the formatted result */
  2908.  
  2909.   if (mode == 3)                /* f format, account for sign + dec digits + decpt + frac */
  2910.     i = *decpt + orig_ndigits + 3;
  2911.   else                          /* account for sign + max precision digs + E + exp sign + exponent */
  2912.     i = orig_ndigits + MAX_EXP_DIGITS + 4;
  2913.  
  2914.   j = sizeof (__ULong);
  2915.   for (_REENT_MP_RESULT_K (ptr) = 0;
  2916.        sizeof (_Bigint) - sizeof (__ULong) + j <= i; j <<= 1)
  2917.     _REENT_MP_RESULT_K (ptr)++;
  2918.   _REENT_MP_RESULT (ptr) = Balloc (ptr, _REENT_MP_RESULT_K (ptr));
  2919.  
  2920. /* Copy from internal temporary buffer to permanent buffer.  */
  2921.   outstr = (char *) _REENT_MP_RESULT (ptr);
  2922.   strcpy (outstr, outbuf);
  2923.  
  2924.   if (rve)
  2925.     *rve = outstr + (s - outbuf);
  2926.  
  2927.   return outstr;
  2928. }
  2929.  
  2930. /* Routine used to tell if long double is NaN or Infinity or regular number.
  2931.    Returns:  0 = regular number
  2932.              1 = Nan
  2933.              2 = Infinity
  2934. */
  2935. int
  2936. _ldcheck (long double *d)
  2937. {
  2938.   unsigned short e[NI];
  2939.   LDPARMS rnd;
  2940.   LDPARMS *ldp = &rnd;
  2941.  
  2942.   union uconv du;
  2943.  
  2944.   rnd.rlast = -1;
  2945.   rnd.rndprc = NBITS;
  2946.   du.d = *d;
  2947. #if LDBL_MANT_DIG == 24
  2948.   e24toe (&du.pe, e, ldp);
  2949. #elif LDBL_MANT_DIG == 53
  2950.   e53toe (&du.pe, e, ldp);
  2951. #elif LDBL_MANT_DIG == 64
  2952.   e64toe (&du.pe, e, ldp);
  2953. #else
  2954.   e113toe (&du.pe, e, ldp);
  2955. #endif
  2956.  
  2957.   if ((e[NE - 1] & 0x7fff) == 0x7fff)
  2958.     {
  2959. #ifdef NANS
  2960.       if (eisnan (e))
  2961.         return (1);
  2962. #endif
  2963.       return (2);
  2964.     }
  2965.   else
  2966.     return (0);
  2967. }                               /* _ldcheck */
  2968.  
  2969. static void
  2970. etoasc (short unsigned int *x, char *string, int ndigits, int outformat,
  2971.         LDPARMS * ldp)
  2972. {
  2973.   long digit;
  2974.   unsigned short y[NI], t[NI], u[NI], w[NI];
  2975.   _CONST unsigned short *p, *r, *ten;
  2976.   unsigned short sign;
  2977.   int i, j, k, expon, rndsav, ndigs;
  2978.   char *s, *ss;
  2979.   unsigned short m;
  2980.   unsigned short *equot = ldp->equot;
  2981.  
  2982.   ndigs = ndigits;
  2983.   rndsav = ldp->rndprc;
  2984. #ifdef NANS
  2985.   if (eisnan (x))
  2986.     {
  2987.       sprintf (string, " NaN ");
  2988.       expon = 9999;
  2989.       goto bxit;
  2990.     }
  2991. #endif
  2992.   ldp->rndprc = NBITS;          /* set to full precision */
  2993.   emov (x, y);                  /* retain external format */
  2994.   if (y[NE - 1] & 0x8000)
  2995.     {
  2996.       sign = 0xffff;
  2997.       y[NE - 1] &= 0x7fff;
  2998.     }
  2999.   else
  3000.     {
  3001.       sign = 0;
  3002.     }
  3003.   expon = 0;
  3004.   ten = &etens[NTEN][0];
  3005.   emov (eone, t);
  3006. /* Test for zero exponent */
  3007.   if (y[NE - 1] == 0)
  3008.     {
  3009.       for (k = 0; k < NE - 1; k++)
  3010.         {
  3011.           if (y[k] != 0)
  3012.             goto tnzro;         /* denormalized number */
  3013.         }
  3014.       goto isone;               /* legal all zeros */
  3015.     }
  3016. tnzro:
  3017.  
  3018. /* Test for infinity.
  3019.  */
  3020.   if (y[NE - 1] == 0x7fff)
  3021.     {
  3022.       if (sign)
  3023.         sprintf (string, " -Infinity ");
  3024.       else
  3025.         sprintf (string, " Infinity ");
  3026.       expon = 9999;
  3027.       goto bxit;
  3028.     }
  3029.  
  3030. /* Test for exponent nonzero but significand denormalized.
  3031.  * This is an error condition.
  3032.  */
  3033.   if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
  3034.     {
  3035.       mtherr ("etoasc", DOMAIN);
  3036.       sprintf (string, "NaN");
  3037.       expon = 9999;
  3038.       goto bxit;
  3039.     }
  3040.  
  3041. /* Compare to 1.0 */
  3042.   i = ecmp (eone, y);
  3043.   if (i == 0)
  3044.     goto isone;
  3045.  
  3046.   if (i < 0)
  3047.     {                           /* Number is greater than 1 */
  3048. /* Convert significand to an integer and strip trailing decimal zeros. */
  3049.       emov (y, u);
  3050.       u[NE - 1] = EXONE + NBITS - 1;
  3051.  
  3052.       p = &etens[NTEN - 4][0];
  3053.       m = 16;
  3054.       do
  3055.         {
  3056.           ediv (p, u, t, ldp);
  3057.           efloor (t, w, ldp);
  3058.           for (j = 0; j < NE - 1; j++)
  3059.             {
  3060.               if (t[j] != w[j])
  3061.                 goto noint;
  3062.             }
  3063.           emov (t, u);
  3064.           expon += (int) m;
  3065.         noint:
  3066.           p += NE;
  3067.           m >>= 1;
  3068.         }
  3069.       while (m != 0);
  3070.  
  3071. /* Rescale from integer significand */
  3072.       u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
  3073.       emov (u, y);
  3074. /* Find power of 10 */
  3075.       emov (eone, t);
  3076.       m = MAXP;
  3077.       p = &etens[0][0];
  3078.       while (ecmp (ten, u) <= 0)
  3079.         {
  3080.           if (ecmp (p, u) <= 0)
  3081.             {
  3082.               ediv (p, u, u, ldp);
  3083.               emul (p, t, t, ldp);
  3084.               expon += (int) m;
  3085.             }
  3086.           m >>= 1;
  3087.           if (m == 0)
  3088.             break;
  3089.           p += NE;
  3090.         }
  3091.     }
  3092.   else
  3093.     {                           /* Number is less than 1.0 */
  3094. /* Pad significand with trailing decimal zeros. */
  3095.       if (y[NE - 1] == 0)
  3096.         {
  3097.           while ((y[NE - 2] & 0x8000) == 0)
  3098.             {
  3099.               emul (ten, y, y, ldp);
  3100.               expon -= 1;
  3101.             }
  3102.         }
  3103.       else
  3104.         {
  3105.           emovi (y, w);
  3106.           for (i = 0; i < NDEC + 1; i++)
  3107.             {
  3108.               if ((w[NI - 1] & 0x7) != 0)
  3109.                 break;
  3110. /* multiply by 10 */
  3111.               emovz (w, u);
  3112.               eshdn1 (u);
  3113.               eshdn1 (u);
  3114.               eaddm (w, u);
  3115.               u[1] += 3;
  3116.               while (u[2] != 0)
  3117.                 {
  3118.                   eshdn1 (u);
  3119.                   u[1] += 1;
  3120.                 }
  3121.               if (u[NI - 1] != 0)
  3122.                 break;
  3123.               if (eone[NE - 1] <= u[1])
  3124.                 break;
  3125.               emovz (u, w);
  3126.               expon -= 1;
  3127.             }
  3128.           emovo (w, y, ldp);
  3129.         }
  3130.       k = -MAXP;
  3131.       p = &emtens[0][0];
  3132.       r = &etens[0][0];
  3133.       emov (y, w);
  3134.       emov (eone, t);
  3135.       while (ecmp (eone, w) > 0)
  3136.         {
  3137.           if (ecmp (p, w) >= 0)
  3138.             {
  3139.               emul (r, w, w, ldp);
  3140.               emul (r, t, t, ldp);
  3141.               expon += k;
  3142.             }
  3143.           k /= 2;
  3144.           if (k == 0)
  3145.             break;
  3146.           p += NE;
  3147.           r += NE;
  3148.         }
  3149.       ediv (t, eone, t, ldp);
  3150.     }
  3151. isone:
  3152. /* Find the first (leading) digit. */
  3153.   emovi (t, w);
  3154.   emovz (w, t);
  3155.   emovi (y, w);
  3156.   emovz (w, y);
  3157.   eiremain (t, y, ldp);
  3158.   digit = equot[NI - 1];
  3159.   while ((digit == 0) && (ecmp (y, ezero) != 0))
  3160.     {
  3161.       eshup1 (y);
  3162.       emovz (y, u);
  3163.       eshup1 (u);
  3164.       eshup1 (u);
  3165.       eaddm (u, y);
  3166.       eiremain (t, y, ldp);
  3167.       digit = equot[NI - 1];
  3168.       expon -= 1;
  3169.     }
  3170.   s = string;
  3171.   if (sign)
  3172.     *s++ = '-';
  3173.   else
  3174.     *s++ = ' ';
  3175. /* Examine number of digits requested by caller. */
  3176.   if (outformat == 3)
  3177.     ndigs += expon;
  3178. /*
  3179. else if( ndigs < 0 )
  3180.         ndigs = 0;
  3181. */
  3182.   if (ndigs > NDEC)
  3183.     ndigs = NDEC;
  3184.   if (digit == 10)
  3185.     {
  3186.       *s++ = '1';
  3187.       *s++ = '.';
  3188.       if (ndigs > 0)
  3189.         {
  3190.           *s++ = '0';
  3191.           ndigs -= 1;
  3192.         }
  3193.       expon += 1;
  3194.       if (ndigs < 0)
  3195.         {
  3196.           ss = s;
  3197.           goto doexp;
  3198.         }
  3199.     }
  3200.   else
  3201.     {
  3202.       *s++ = (char) digit + '0';
  3203.       *s++ = '.';
  3204.     }
  3205. /* Generate digits after the decimal point. */
  3206.   for (k = 0; k <= ndigs; k++)
  3207.     {
  3208. /* multiply current number by 10, without normalizing */
  3209.       eshup1 (y);
  3210.       emovz (y, u);
  3211.       eshup1 (u);
  3212.       eshup1 (u);
  3213.       eaddm (u, y);
  3214.       eiremain (t, y, ldp);
  3215.       *s++ = (char) equot[NI - 1] + '0';
  3216.     }
  3217.   digit = equot[NI - 1];
  3218.   --s;
  3219.   ss = s;
  3220. /* round off the ASCII string */
  3221.   if (digit > 4)
  3222.     {
  3223. /* Test for critical rounding case in ASCII output. */
  3224.       if (digit == 5)
  3225.         {
  3226.           emovo (y, t, ldp);
  3227.           if (ecmp (t, ezero) != 0)
  3228.             goto roun;          /* round to nearest */
  3229.           if (ndigs < 0 || (*(s - 1 - (*(s - 1) == '.')) & 1) == 0)
  3230.             goto doexp;         /* round to even */
  3231.         }
  3232. /* Round up and propagate carry-outs */
  3233.     roun:
  3234.       --s;
  3235.       k = *s & 0x7f;
  3236. /* Carry out to most significant digit? */
  3237.       if (ndigs < 0)
  3238.         {
  3239.           /* This will print like "1E-6". */
  3240.           *s = '1';
  3241.           expon += 1;
  3242.           goto doexp;
  3243.         }
  3244.       else if (k == '.')
  3245.         {
  3246.           --s;
  3247.           k = *s;
  3248.           k += 1;
  3249.           *s = (char) k;
  3250. /* Most significant digit carries to 10? */
  3251.           if (k > '9')
  3252.             {
  3253.               expon += 1;
  3254.               *s = '1';
  3255.             }
  3256.           goto doexp;
  3257.         }
  3258. /* Round up and carry out from less significant digits */
  3259.       k += 1;
  3260.       *s = (char) k;
  3261.       if (k > '9')
  3262.         {
  3263.           *s = '0';
  3264.           goto roun;
  3265.         }
  3266.     }
  3267. doexp:
  3268. #ifdef __GO32__
  3269.   if (expon >= 0)
  3270.     sprintf (ss, "e+%02d", expon);
  3271.   else
  3272.     sprintf (ss, "e-%02d", -expon);
  3273. #else
  3274.   sprintf (ss, "E%d", expon);
  3275. #endif
  3276. bxit:
  3277.   ldp->rndprc = rndsav;
  3278.   ldp->outexpon = expon;
  3279. }
  3280.  
  3281.  
  3282. #if 0 /* Broken, unusable implementation of strtold */
  3283.  
  3284. /*
  3285. ;                                                               ASCTOQ
  3286. ;               ASCTOQ.MAC              LATEST REV: 11 JAN 84
  3287. ;                                       SLM, 3 JAN 78
  3288. ;
  3289. ;       Convert ASCII string to quadruple precision floating point
  3290. ;
  3291. ;               Numeric input is free field decimal number
  3292. ;               with max of 15 digits with or without
  3293. ;               decimal point entered as ASCII from teletype.
  3294. ;       Entering E after the number followed by a second
  3295. ;       number causes the second number to be interpreted
  3296. ;       as a power of 10 to be multiplied by the first number
  3297. ;       (i.e., "scientific" notation).
  3298. ;
  3299. ;       Usage:
  3300. ;               asctoq( string, q );
  3301. */
  3302.  
  3303. long double
  3304. _strtold (char *s, char **se)
  3305. {
  3306.   union uconv x;
  3307.   LDPARMS rnd;
  3308.   LDPARMS *ldp = &rnd;
  3309.   int lenldstr;
  3310.  
  3311.   rnd.rlast = -1;
  3312.   rnd.rndprc = NBITS;
  3313.  
  3314.   lenldstr = asctoeg (s, &x.pe, LDBL_MANT_DIG, ldp);
  3315.   if (se)
  3316.     *se = s + lenldstr;
  3317.   return x.d;
  3318. }
  3319.  
  3320. #define REASONABLE_LEN 200
  3321.  
  3322. static int
  3323. asctoeg (char *ss, short unsigned int *y, int oprec, LDPARMS * ldp)
  3324. {
  3325.   unsigned short yy[NI], xt[NI], tt[NI];
  3326.   int esign, decflg, sgnflg, nexp, exp, prec, lost;
  3327.   int k, trail, c, rndsav;
  3328.   long lexp;
  3329.   unsigned short nsign;
  3330.   _CONST unsigned short *p;
  3331.   char *sp, *s, *lstr;
  3332.   int lenldstr;
  3333.   int mflag = 0;
  3334.   char tmpstr[REASONABLE_LEN];
  3335.  
  3336. /* Copy the input string. */
  3337.   c = strlen (ss) + 2;
  3338.   if (c <= REASONABLE_LEN)
  3339.     lstr = tmpstr;
  3340.   else
  3341.     {
  3342.       lstr = (char *) calloc (c, 1);
  3343.       mflag = 1;
  3344.     }
  3345.   s = ss;
  3346.   lenldstr = 0;
  3347.   while (*s == ' ')             /* skip leading spaces */
  3348.     {
  3349.       ++s;
  3350.       ++lenldstr;
  3351.     }
  3352.   sp = lstr;
  3353.   for (k = 0; k < c; k++)
  3354.     {
  3355.       if ((*sp++ = *s++) == '\0')
  3356.         break;
  3357.     }
  3358.   *sp = '\0';
  3359.   s = lstr;
  3360.  
  3361.   rndsav = ldp->rndprc;
  3362.   ldp->rndprc = NBITS;          /* Set to full precision */
  3363.   lost = 0;
  3364.   nsign = 0;
  3365.   decflg = 0;
  3366.   sgnflg = 0;
  3367.   nexp = 0;
  3368.   exp = 0;
  3369.   prec = 0;
  3370.   ecleaz (yy);
  3371.   trail = 0;
  3372.  
  3373. nxtcom:
  3374.   k = *s - '0';
  3375.   if ((k >= 0) && (k <= 9))
  3376.     {
  3377. /* Ignore leading zeros */
  3378.       if ((prec == 0) && (decflg == 0) && (k == 0))
  3379.         goto donchr;
  3380. /* Identify and strip trailing zeros after the decimal point. */
  3381.       if ((trail == 0) && (decflg != 0))
  3382.         {
  3383.           sp = s;
  3384.           while ((*sp >= '0') && (*sp <= '9'))
  3385.             ++sp;
  3386. /* Check for syntax error */
  3387.           c = *sp & 0x7f;
  3388.           if ((c != 'e') && (c != 'E') && (c != '\0')
  3389.               && (c != '\n') && (c != '\r') && (c != ' ') && (c != ','))
  3390.             goto error;
  3391.           --sp;
  3392.           while (*sp == '0')
  3393.             *sp-- = 'z';
  3394.           trail = 1;
  3395.           if (*s == 'z')
  3396.             goto donchr;
  3397.         }
  3398. /* If enough digits were given to more than fill up the yy register,
  3399.  * continuing until overflow into the high guard word yy[2]
  3400.  * guarantees that there will be a roundoff bit at the top
  3401.  * of the low guard word after normalization.
  3402.  */
  3403.       if (yy[2] == 0)
  3404.         {
  3405.           if (decflg)
  3406.             nexp += 1;          /* count digits after decimal point */
  3407.           eshup1 (yy);          /* multiply current number by 10 */
  3408.           emovz (yy, xt);
  3409.           eshup1 (xt);
  3410.           eshup1 (xt);
  3411.           eaddm (xt, yy);
  3412.           ecleaz (xt);
  3413.           xt[NI - 2] = (unsigned short) k;
  3414.           eaddm (xt, yy);
  3415.         }
  3416.       else
  3417.         {
  3418.           /* Mark any lost non-zero digit.  */
  3419.           lost |= k;
  3420.           /* Count lost digits before the decimal point.  */
  3421.           if (decflg == 0)
  3422.             nexp -= 1;
  3423.         }
  3424.       prec += 1;
  3425.       goto donchr;
  3426.     }
  3427.  
  3428.   switch (*s)
  3429.     {
  3430.     case 'z':
  3431.       break;
  3432.     case 'E':
  3433.     case 'e':
  3434.       goto expnt;
  3435.     case '.':                   /* decimal point */
  3436.       if (decflg)
  3437.         goto error;
  3438.       ++decflg;
  3439.       break;
  3440.     case '-':
  3441.       nsign = 0xffff;
  3442.       if (sgnflg)
  3443.         goto error;
  3444.       ++sgnflg;
  3445.       break;
  3446.     case '+':
  3447.       if (sgnflg)
  3448.         goto error;
  3449.       ++sgnflg;
  3450.       break;
  3451.     case ',':
  3452.     case ' ':
  3453.     case '\0':
  3454.     case '\n':
  3455.     case '\r':
  3456.       goto daldone;
  3457.     case 'i':
  3458.     case 'I':
  3459.       goto infinite;
  3460.     default:
  3461.     error:
  3462. #ifdef NANS
  3463.       enan (yy, NI * 16);
  3464. #else
  3465.       mtherr ("asctoe", DOMAIN);
  3466.       ecleaz (yy);
  3467. #endif
  3468.       goto aexit;
  3469.     }
  3470. donchr:
  3471.   ++s;
  3472.   goto nxtcom;
  3473.  
  3474. /* Exponent interpretation */
  3475. expnt:
  3476.  
  3477.   esign = 1;
  3478.   exp = 0;
  3479.   ++s;
  3480. /* check for + or - */
  3481.   if (*s == '-')
  3482.     {
  3483.       esign = -1;
  3484.       ++s;
  3485.     }
  3486.   if (*s == '+')
  3487.     ++s;
  3488.   while ((*s >= '0') && (*s <= '9'))
  3489.     {
  3490.       exp *= 10;
  3491.       exp += *s++ - '0';
  3492.       if (exp > 4977)
  3493.         {
  3494.           if (esign < 0)
  3495.             goto zero;
  3496.           else
  3497.             goto infinite;
  3498.         }
  3499.     }
  3500.   if (esign < 0)
  3501.     exp = -exp;
  3502.   if (exp > 4932)
  3503.     {
  3504.     infinite:
  3505.       ecleaz (yy);
  3506.       yy[E] = 0x7fff;           /* infinity */
  3507.       goto aexit;
  3508.     }
  3509.   if (exp < -4977)
  3510.     {
  3511.     zero:
  3512.       ecleaz (yy);
  3513.       goto aexit;
  3514.     }
  3515.  
  3516. daldone:
  3517.   nexp = exp - nexp;
  3518. /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
  3519.   while ((nexp > 0) && (yy[2] == 0))
  3520.     {
  3521.       emovz (yy, xt);
  3522.       eshup1 (xt);
  3523.       eshup1 (xt);
  3524.       eaddm (yy, xt);
  3525.       eshup1 (xt);
  3526.       if (xt[2] != 0)
  3527.         break;
  3528.       nexp -= 1;
  3529.       emovz (xt, yy);
  3530.     }
  3531.   if ((k = enormlz (yy)) > NBITS)
  3532.     {
  3533.       ecleaz (yy);
  3534.       goto aexit;
  3535.     }
  3536.   lexp = (EXONE - 1 + NBITS) - k;
  3537.   emdnorm (yy, lost, 0, lexp, 64, ldp);
  3538. /* convert to external format */
  3539.  
  3540.  
  3541. /* Multiply by 10**nexp.  If precision is 64 bits,
  3542.  * the maximum relative error incurred in forming 10**n
  3543.  * for 0 <= n <= 324 is 8.2e-20, at 10**180.
  3544.  * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
  3545.  * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
  3546.  */
  3547.   lexp = yy[E];
  3548.   if (nexp == 0)
  3549.     {
  3550.       k = 0;
  3551.       goto expdon;
  3552.     }
  3553.   esign = 1;
  3554.   if (nexp < 0)
  3555.     {
  3556.       nexp = -nexp;
  3557.       esign = -1;
  3558.       if (nexp > 4096)
  3559.         {                       /* Punt.  Can't handle this without 2 divides. */
  3560.           emovi (etens[0], tt);
  3561.           lexp -= tt[E];
  3562.           k = edivm (tt, yy, ldp);
  3563.           lexp += EXONE;
  3564.           nexp -= 4096;
  3565.         }
  3566.     }
  3567.   p = &etens[NTEN][0];
  3568.   emov (eone, xt);
  3569.   exp = 1;
  3570.   do
  3571.     {
  3572.       if (exp & nexp)
  3573.         emul (p, xt, xt, ldp);
  3574.       p -= NE;
  3575.       exp = exp + exp;
  3576.     }
  3577.   while (exp <= MAXP);
  3578.  
  3579.   emovi (xt, tt);
  3580.   if (esign < 0)
  3581.     {
  3582.       lexp -= tt[E];
  3583.       k = edivm (tt, yy, ldp);
  3584.       lexp += EXONE;
  3585.     }
  3586.   else
  3587.     {
  3588.       lexp += tt[E];
  3589.       k = emulm (tt, yy, ldp);
  3590.       lexp -= EXONE - 1;
  3591.     }
  3592.  
  3593. expdon:
  3594.  
  3595. /* Round and convert directly to the destination type */
  3596.   if (oprec == 53)
  3597.     lexp -= EXONE - 0x3ff;
  3598.   else if (oprec == 24)
  3599.     lexp -= EXONE - 0177;
  3600. #ifdef DEC
  3601.   else if (oprec == 56)
  3602.     lexp -= EXONE - 0201;
  3603. #endif
  3604.   ldp->rndprc = oprec;
  3605.   emdnorm (yy, k, 0, lexp, 64, ldp);
  3606.  
  3607. aexit:
  3608.  
  3609.   ldp->rndprc = rndsav;
  3610.   yy[0] = nsign;
  3611.   switch (oprec)
  3612.     {
  3613. #ifdef DEC
  3614.     case 56:
  3615.       todec (yy, y);            /* see etodec.c */
  3616.       break;
  3617. #endif
  3618. #if LDBL_MANT_DIG == 53
  3619.     case 53:
  3620.       toe53 (yy, y);
  3621.       break;
  3622. #elif LDBL_MANT_DIG == 24
  3623.     case 24:
  3624.       toe24 (yy, y);
  3625.       break;
  3626. #elif LDBL_MANT_DIG == 64
  3627.     case 64:
  3628.       toe64 (yy, y);
  3629.       break;
  3630. #elif LDBL_MANT_DIG == 113
  3631.     case 113:
  3632.       toe113 (yy, y);
  3633.       break;
  3634. #else
  3635.     case NBITS:
  3636.       emovo (yy, y, ldp);
  3637.       break;
  3638. #endif
  3639.     }
  3640.   lenldstr += s - lstr;
  3641.   if (mflag)
  3642.     free (lstr);
  3643.   return lenldstr;
  3644. }
  3645.  
  3646. #endif
  3647.  
  3648. /* y = largest integer not greater than x
  3649.  * (truncated toward minus infinity)
  3650.  *
  3651.  * unsigned short x[NE], y[NE]
  3652.  * LDPARMS *ldp
  3653.  *
  3654.  * efloor( x, y, ldp );
  3655.  */
  3656. static _CONST unsigned short bmask[] = {
  3657.   0xffff,
  3658.   0xfffe,
  3659.   0xfffc,
  3660.   0xfff8,
  3661.   0xfff0,
  3662.   0xffe0,
  3663.   0xffc0,
  3664.   0xff80,
  3665.   0xff00,
  3666.   0xfe00,
  3667.   0xfc00,
  3668.   0xf800,
  3669.   0xf000,
  3670.   0xe000,
  3671.   0xc000,
  3672.   0x8000,
  3673.   0x0000,
  3674. };
  3675.  
  3676. static void
  3677. efloor (short unsigned int *x, short unsigned int *y, LDPARMS * ldp)
  3678. {
  3679.   register unsigned short *p;
  3680.   int e, expon, i;
  3681.   unsigned short f[NE];
  3682.  
  3683.   emov (x, f);                  /* leave in external format */
  3684.   expon = (int) f[NE - 1];
  3685.   e = (expon & 0x7fff) - (EXONE - 1);
  3686.   if (e <= 0)
  3687.     {
  3688.       eclear (y);
  3689.       goto isitneg;
  3690.     }
  3691. /* number of bits to clear out */
  3692.   e = NBITS - e;
  3693.   emov (f, y);
  3694.   if (e <= 0)
  3695.     return;
  3696.  
  3697.   p = &y[0];
  3698.   while (e >= 16)
  3699.     {
  3700.       *p++ = 0;
  3701.       e -= 16;
  3702.     }
  3703. /* clear the remaining bits */
  3704.   *p &= bmask[e];
  3705. /* truncate negatives toward minus infinity */
  3706. isitneg:
  3707.  
  3708.   if ((unsigned short) expon & (unsigned short) 0x8000)
  3709.     {
  3710.       for (i = 0; i < NE - 1; i++)
  3711.         {
  3712.           if (f[i] != y[i])
  3713.             {
  3714.               esub (eone, y, y, ldp);
  3715.               break;
  3716.             }
  3717.         }
  3718.     }
  3719. }
  3720.  
  3721.  
  3722.  
  3723. static void
  3724. eiremain (short unsigned int *den, short unsigned int *num, LDPARMS * ldp)
  3725. {
  3726.   long ld, ln;
  3727.   unsigned short j;
  3728.   unsigned short *equot = ldp->equot;
  3729.  
  3730.   ld = den[E];
  3731.   ld -= enormlz (den);
  3732.   ln = num[E];
  3733.   ln -= enormlz (num);
  3734.   ecleaz (equot);
  3735.   while (ln >= ld)
  3736.     {
  3737.       if (ecmpm (den, num) <= 0)
  3738.         {
  3739.           esubm (den, num);
  3740.           j = 1;
  3741.         }
  3742.       else
  3743.         {
  3744.           j = 0;
  3745.         }
  3746.       eshup1 (equot);
  3747.       equot[NI - 1] |= j;
  3748.       eshup1 (num);
  3749.       ln -= 1;
  3750.     }
  3751.   emdnorm (num, 0, 0, ln, 0, ldp);
  3752. }
  3753.  
  3754. /* NaN bit patterns
  3755.  */
  3756. #ifdef MIEEE
  3757. #if !defined(__mips)
  3758. static _CONST unsigned short nan113[8] = {
  3759.   0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  3760. };
  3761.  
  3762. static _CONST unsigned short nan64[6] = {
  3763.   0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  3764. };
  3765. static _CONST unsigned short nan53[4] = { 0x7fff, 0xffff, 0xffff, 0xffff };
  3766. static _CONST unsigned short nan24[2] = { 0x7fff, 0xffff };
  3767. #elif defined(__mips_nan2008)   /* __mips */
  3768. static _CONST unsigned short nan113[8] = { 0x7fff, 0x8000, 0, 0, 0, 0, 0, 0 };
  3769. static _CONST unsigned short nan64[6] = { 0x7fff, 0xc000, 0, 0, 0, 0 };
  3770. static _CONST unsigned short nan53[4] = { 0x7ff8, 0, 0, 0 };
  3771. static _CONST unsigned short nan24[2] = { 0x7fc0, 0 };
  3772. #else /* __mips && !__mips_nan2008 */
  3773. static _CONST unsigned short nan113[8] = {
  3774.   0x7fff, 0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  3775. };
  3776.  
  3777. static _CONST unsigned short nan64[6] = {
  3778.   0x7fff, 0xbfff, 0xffff, 0xffff, 0xffff, 0xffff
  3779. };
  3780. static _CONST unsigned short nan53[4] = { 0x7ff7, 0xffff, 0xffff, 0xffff };
  3781. static _CONST unsigned short nan24[2] = { 0x7fbf, 0xffff };
  3782. #endif /* __mips && !__mips_nan2008 */
  3783. #else /* !MIEEE */
  3784. #if !defined(__mips) || defined(__mips_nan2008)
  3785. static _CONST unsigned short nan113[8] = { 0, 0, 0, 0, 0, 0, 0x8000, 0x7fff };
  3786. static _CONST unsigned short nan64[6] = { 0, 0, 0, 0, 0xc000, 0x7fff };
  3787. static _CONST unsigned short nan53[4] = { 0, 0, 0, 0x7ff8 };
  3788. static _CONST unsigned short nan24[2] = { 0, 0x7fc0 };
  3789. #else /* __mips && !__mips_nan2008 */
  3790. static _CONST unsigned short nan113[8] = {
  3791.   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x7fff, 0x7fff
  3792. };
  3793.  
  3794. static _CONST unsigned short nan64[6] = {
  3795.   0xffff, 0xffff, 0xffff, 0xffff, 0xbfff, 0x7fff
  3796. };
  3797. static _CONST unsigned short nan53[4] = { 0xffff, 0xffff, 0xffff, 0x7ff7 };
  3798. static _CONST unsigned short nan24[2] = { 0xffff, 0x7fbf };
  3799. #endif /* __mips && !__mips_nan2008 */
  3800. #endif /* !MIEEE */
  3801.  
  3802.  
  3803. static void
  3804. enan (short unsigned int *nan, int size)
  3805. {
  3806.   int i, n;
  3807.   _CONST unsigned short *p;
  3808.  
  3809.   switch (size)
  3810.     {
  3811. #ifndef DEC
  3812.     case 113:
  3813.       n = 8;
  3814.       p = nan113;
  3815.       break;
  3816.  
  3817.     case 64:
  3818.       n = 6;
  3819.       p = nan64;
  3820.       break;
  3821.  
  3822.     case 53:
  3823.       n = 4;
  3824.       p = nan53;
  3825.       break;
  3826.  
  3827.     case 24:
  3828.       n = 2;
  3829.       p = nan24;
  3830.       break;
  3831.  
  3832.     case NBITS:
  3833. #if !defined(__mips) || defined(__mips_nan2008)
  3834.       for (i = 0; i < NE - 2; i++)
  3835.         *nan++ = 0;
  3836.       *nan++ = 0xc000;
  3837. #else /* __mips && !__mips_nan2008 */
  3838.       for (i = 0; i < NE - 2; i++)
  3839.         *nan++ = 0xffff;
  3840.       *nan++ = 0xbfff;
  3841. #endif /* __mips && !__mips_nan2008 */
  3842.       *nan++ = 0x7fff;
  3843.       return;
  3844.  
  3845.     case NI * 16:
  3846.       *nan++ = 0;
  3847.       *nan++ = 0x7fff;
  3848.       *nan++ = 0;
  3849. #if !defined(__mips) || defined(__mips_nan2008)
  3850.       *nan++ = 0xc000;
  3851.       for (i = 4; i < NI - 1; i++)
  3852.         *nan++ = 0;
  3853. #else /* __mips && !__mips_nan2008 */
  3854.       *nan++ = 0xbfff;
  3855.       for (i = 4; i < NI - 1; i++)
  3856.         *nan++ = 0xffff;
  3857. #endif /* __mips && !__mips_nan2008 */
  3858.       *nan++ = 0;
  3859.       return;
  3860. #endif
  3861.     default:
  3862.       mtherr ("enan", DOMAIN);
  3863.       return;
  3864.     }
  3865.   for (i = 0; i < n; i++)
  3866.     *nan++ = *p++;
  3867. }
  3868.