string.h is ANSI C include file
[unix-history] / usr / src / usr.bin / f77 / libI77 / fmtlib.c
CommitLineData
ae0d5425 1/*
161423a6
RE
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
ae0d5425 5 *
161423a6
RE
6 * @(#)fmtlib.c 5.1 %G%
7 */
8
9/*
ae0d5425 10 * integer to ascii conversion
c47956af
DW
11 *
12 * This code has been rearranged to produce optimized runtime code.
ae0d5425
DW
13 */
14
15#include "fio.h"
16
c47956af
DW
17static char _digit[] = "0123456789abcdefghijklmnopqrstuvwxyz";
18static char _icv_buf[MAXINTLENGTH+1];
19#define _mask 0x7fffffff
ae0d5425 20
c47956af
DW
21char *
22icvt(value, ndigit, sign)
23long value;
24int *ndigit;
25int *sign;
ae0d5425 26{
c47956af
DW
27 register long val = value;
28 register long rad = radix;
29 register char *b = &_icv_buf[MAXINTLENGTH];
30 register char *d = _digit;
31 register long tmp1;
32 register int tmp2;
33 long rem;
34 long kludge;
ae0d5425 35
c47956af
DW
36 if (val == 0)
37 {
38 *--b = '0';
39 *sign = 0;
40 *ndigit = 1;
41 return(b);
ae0d5425 42 }
c47956af
DW
43
44 if (signit && (*sign = (val < 0))) /* signed conversion */
ae0d5425 45 {
c47956af
DW
46 /*
47 * It is necessary to do the first divide
48 * before the absolute value, for the case -2^31
49 *
50 * This is actually what is being done...
51 * tmp1 = (int)(val % rad);
52 * val /= rad;
53 * val = -val
54 * *--b = d[-tmp1];
55 */
56 tmp1 = val / rad;
57 *--b = d[(tmp1 * rad) - val];
58 val = -tmp1;
ae0d5425 59 }
c47956af
DW
60 else /* unsigned conversion */
61 {
62 *sign = 0;
63 if (val < 0)
64 { /* ALL THIS IS TO SIMULATE UNSIGNED LONG MOD & DIV */
65 kludge = _mask - (rad - 1);
66 val &= _mask;
67 /*
68 * This is really what's being done...
69 * rem = (kludge % rad) + (val % rad);
70 * val = (kludge / rad) + (val / rad) + (rem / rad) + 1;
71 * *--b = d[rem % rad];
72 */
73 tmp1 = kludge / rad;
74 tmp2 = val / rad;
75 rem = (kludge - (tmp1 * rad)) + (val - (tmp2 * rad));
76 val = ++tmp1 + tmp2;
77 tmp1 = rem / rad;
78 val += tmp1;
79 *--b = d[rem - (tmp1 * rad)];
ae0d5425
DW
80 }
81 }
c47956af
DW
82
83 while (val)
ae0d5425 84 {
c47956af
DW
85 /*
86 * This is really what's being done ...
87 * *--b = d[val % rad];
88 * val /= rad;
89 */
90 tmp1 = val / rad;
91 *--b = d[val - (tmp1 * rad)];
92 val = tmp1;
ae0d5425 93 }
c47956af
DW
94
95 *ndigit = (&_icv_buf[MAXINTLENGTH] - b);
96 return(b);
ae0d5425 97}