aboutsummaryrefslogtreecommitdiff
path: root/src/benchmarks/cfrac/atop.c
blob: 98f132a82413b74f6265839901aa6fc6946cde4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <ctype.h>
#include "pdefs.h"
#include "pcvt.h"
#include "precision.h"

/*
 * ascii to precision (modeled after atoi)
 *   leading whitespace skipped
 *   an optional leading '-' or '+' followed by digits '0'..'9' 
 *   leading 0's Ok
 *   stops at first unrecognized character
 *
 * Returns: pUndef if an invalid argument (pUndef or nondigit as 1st digit)
 */
precision atop(chp)
   register char *chp;
{
   precision res   = pUndef;
   precision clump = pUndef;
   int sign = 0;
   register int ch;
   register accumulator temp;
   accumulator x; 
   register int i;

   if (chp != (char *) 0) {
      while (isspace(*chp)) chp++;	/* skip whitespace */
      if (*chp == '-') {
	 sign = 1;
	 ++chp;
      } else if (*chp == '+') {
	 ++chp;
      }
      if (isdigit(ch = * (unsigned char *) chp)) {
	 pset(&res, pzero);
	 pset(&clump, utop(aDigit));
	 do {
	    i = aDigitLog-1;
	    temp = ch - '0';
	    do {
	       if (!isdigit(ch = * (unsigned char *) ++chp)) goto atoplast;
	       temp = temp * aBase + (ch - '0');
	    } while (--i > 0);
	    pset(&res, padd(pmul(res, clump), utop(temp)));
	 } while (isdigit(ch = * (unsigned char *) ++chp));
	 goto atopdone;
atoplast:
	 x = aBase;
	 while (i++ < aDigitLog-1) {
	    x *= aBase;
	 }
	 pset(&res, padd(pmul(res, utop(x)), utop(temp)));
atopdone:
	 if (sign) {
	    pset(&res, pneg(res));
	 }
      }
   }
   pdestroy(clump);
   return presult(res);
}