* added compilers lcc and bcc (linux86)
[mascara-docs.git] / compilers / linux86-0.16.17 / ld / typeconv.c
blobd8c0c89c9fa0528e0ed384204e7402ddd5e8acf2
2 /*
3 * Type conversion routines, these have been rewritten for portability.
5 * The only requirement is now that the u2_t and u4_t must be big enough.
6 */
8 #include "syshead.h"
9 #include "const.h"
10 #include "type.h"
11 #include "globvar.h"
13 void xxerr P((char *));
14 void xxerr(x) char * x; { write(2, x, strlen(x)); }
16 static int no_swap = 1;
18 static int long_off[4] = {0,1,2,3};
19 static int int_off[2] = {0,1};
21 PUBLIC bool_pt typeconv_init(big_endian, long_big_endian)
22 bool_pt big_endian;
23 bool_pt long_big_endian;
25 int i;
26 no_swap = (!big_endian && !long_big_endian);
28 for(i=0; i<4; i++) long_off[i] = i;
29 for(i=0; i<2; i++) int_off[i] = i;
31 if( long_big_endian )
33 i = long_off[0]; long_off[0] = long_off[2]; long_off[2] = i;
34 i = long_off[1]; long_off[1] = long_off[3]; long_off[3] = i;
36 if( big_endian )
38 i = long_off[2]; long_off[2] = long_off[3]; long_off[3] = i;
39 i = long_off[0]; long_off[0] = long_off[1]; long_off[1] = i;
41 i = int_off[0]; int_off[0] = int_off[1]; int_off[1] = i;
43 return 1;
46 PUBLIC void u2c2(buf, offset)
47 char *buf;
48 u2_pt offset;
50 #ifdef __AS386_16__
51 if( no_swap )
53 *((unsigned short*)buf) = offset; /* UNALIGNED ACCESS! */
54 return;
56 #endif
57 buf[int_off[0]] = offset;
58 buf[int_off[1]] = (offset>>8);
61 PUBLIC void u4c4(buf, offset)
62 char *buf;
63 u4_t offset;
65 int i;
66 #ifdef __AS386_16__
67 if( no_swap )
69 *((unsigned long*)buf) = offset; /* UNALIGNED ACCESS! */
70 return;
72 #endif
73 for(i=0; i<4; i++)
75 buf[long_off[i]] = offset;
76 offset >>= 8;
80 PUBLIC void u4cn(buf, offset, count)
81 char *buf;
82 u4_t offset;
83 unsigned count;
85 switch(count)
87 case 1:
88 buf[0] = (char) offset;
89 return;
90 case 2:
91 u2c2(buf, (u2_pt) offset);
92 return;
93 case 4:
94 u4c4(buf, (u4_t) offset);
95 return;
96 default:
97 xxerr("WARNING: typeconv.c(u4cn) illegal count\n");
98 return;
102 PUBLIC void u2cn(buf, offset, count)
103 char *buf;
104 u2_pt offset;
105 unsigned count;
107 switch(count)
109 case 1:
110 buf[0] = (char) offset;
111 return;
112 case 2:
113 u2c2(buf, (u2_pt) offset);
114 return;
115 case 4:
116 u4c4(buf, (u4_t) offset);
117 return;
118 default:
119 xxerr("WARNING: typeconv.c(u2cn) illegal count\n");
120 return;
124 PUBLIC u2_pt c2u2(buf)
125 char *buf;
127 u2_pt res;
128 #ifdef __AS386_16__
129 if( no_swap ) return *((u2_pt *)buf); /* UNALIGNED ACCESS! */
130 #endif
132 res = ((unsigned char *)buf) [int_off[0]]
133 + ((((unsigned char *)buf) [int_off[1]]) << 8);
134 return res;
137 PUBLIC u4_t c4u4(buf)
138 char *buf;
140 u4_t res;
141 int i;
142 #ifdef __AS386_16__
143 if( no_swap ) return *((u4_t *)buf); /* UNALIGNED ACCESS! */
144 #endif
145 res = 0;
146 for(i=3; i>=0; i--)
148 res = (res<<8) + ((unsigned char *)buf) [long_off[i]];
150 return res;
153 PUBLIC u4_t cnu4(buf, count)
154 char *buf;
155 unsigned count;
157 switch (count)
159 case 0:
160 return 0;
161 case 1:
162 return buf[0] & 0xFF;
163 case 2:
164 return c2u2(buf);
165 case 4:
166 return c4u4(buf);
167 default:
168 xxerr("WARNING: typeconv.c(cnu4) illegal count\n");
169 return 0;
173 PUBLIC u2_pt cnu2(buf, count)
174 char *buf;
175 unsigned count;
177 switch (count)
179 case 0:
180 return 0;
181 case 1:
182 return buf[0] & 0xFF;
183 case 2:
184 return c2u2(buf);
185 case 4:
186 return (u2_pt) c4u4(buf);
187 default:
188 xxerr("WARNING: typeconv.c(cnu2) illegal count\n");
189 return 0;