1: # include "../../symbol.h"
2:
3: /*
4: **
5: ** IIconvert -- Equel run-tme routine to convert
6: ** numeric values of one type and length, to a
7: ** (not necessarily) different type and length.
8: **
9: ** The source numeric can be i1, i2, i4, f4, or f8.
10: **
11: ** The source number will be converted to the
12: ** type and length specified in the destination.
13: ** It also must be one of i1, i2, i4, f4, or f8.
14: **
15: ** IIconvert returns 0 if no overflow occured,
16: ** otherwise it returns -1
17: **
18: */
19:
20:
21: IIconvert(inp, outp, sf, slen, df, dlen)
22: char *inp; /* input area */
23: char *outp; /* output area */
24: int sf; /* format of the source number */
25: int slen; /* length of the source number */
26: int df; /* format of the dest */
27: int dlen; /* length of the dest */
28: {
29: char number[8]; /* dummy buffer */
30: register char *num;
31: register int sl; /* refers to length
32: * of "number"
33: */
34: register int dl;
35:
36: # define i1deref(x) (*((char *)(x)))
37: # define i2deref(x) (*((int *)(x)))
38: # define i4deref(x) (*((long *)(x)))
39: # define f4deref(x) (*((float *)(x)))
40: # define f8deref(x) (*((double *)(x)))
41:
42: dl = dlen;
43: sl = slen;
44: num = number;
45: IIbmove(inp, num, sl); /* copy number into buffer */
46:
47: if (sf != df)
48: {
49: /* if the source and destination formats are
50: * different then the source must be converted
51: * to i4 if the dest is int, otherwise to f8
52: */
53:
54: if (df == FLOAT) /* {sf == INT} INT->f8 */
55: {
56: switch (sl)
57: {
58:
59: case 1:
60: f8deref(num) = i1deref(num); /* i1 to f8 */
61: break;
62:
63: case 2:
64: f8deref(num) = i2deref(num); /* i2 to f8 */
65: break;
66:
67: case 4:
68: f8deref(num) = i4deref(num); /* i4 to f8 */
69: }
70: sl = 8; /* df == INT && sf == FLOAT
71: * && sl == 8
72: */
73: }
74: else
75: {
76: /* {df == INT && sf == FLOAT} FLOAT->i4 */
77:
78: /* check if float > 2**31 */
79: if (sl == 8)
80: f4deref(num) = f8deref(num); /* f8 to f4 */
81:
82: if (f4deref(num) > 2147483647.0 || f4deref(num) < -2147483648.0)
83: return (-1);
84: i4deref(num) = f4deref(num);
85: sl = 4;
86: }
87: }
88:
89: /* number is now the same type as destination */
90: /* convert lengths to match */
91:
92: if (sl != dl)
93: {
94: /* lengths don't match. convert. */
95: if (df == FLOAT)
96: {
97: if (dl == 8)
98: f8deref(num) = f4deref(num); /* f4 to f8 */
99: else
100: f4deref(num) = f8deref(num); /* f8 to f4 with rounding */
101: }
102: else
103: {
104: switch (dl)
105: {
106:
107: case 1:
108: if (sl == 2) /* i2 to i1 */
109: {
110: if (i2deref(num) > 127 || i2deref(num) < -128)
111: return (-1);
112: i1deref(num) = i2deref(num);
113: }
114: else /* i4 to i1 */
115: {
116: if (i4deref(num) > 127 || i4deref(num) < -128)
117: return (-1);
118: i1deref(num) = i4deref(num);
119: }
120: break;
121:
122: case 2:
123: if (sl == 1) /* i1 to i2 */
124: {
125: i2deref(num) = i1deref(num);
126: }
127: else /* i4 to i2 */
128: {
129: if (i4deref(num) > 32767 || i4deref(num) < -32768)
130: return (-1);
131: i2deref(num) = i4deref(num);
132: }
133: break;
134:
135: case 4:
136: if (sl == 1) /* i1 to i4 */
137: i4deref(num) = i1deref(num);
138: else /* i2 to i4 */
139: i4deref(num) = i2deref(num);
140: }
141: }
142: }
143:
144: /* conversion is complete
145: * copy the result into outp
146: */
147:
148: IIbmove(num, outp, dl);
149: return (0);
150: }
Defined functions
Defined macros