tagged release 0.6.4
[parrot.git] / src / byteorder.c
blobc46e7d32a6c74a2cc79fef0dd83a83c29ebe6f47
1 /*
2 Copyright (C) 2001-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/byteorder.c - Byteordering functions
9 =head1 DESCRIPTION
11 These are assigned to a vtable when the PBC file is loaded.
13 If the vtable method for conversion from the native byteorder is called,
14 it is a I<no op> and will work, but the caller should know if the
15 byteorder in the PBC file is native and skip the conversion and just map
16 it in.
18 =head2 Byte order handlers
20 Configure will have checked for supported word sizes.
22 =over 4
24 =cut
28 #include "parrot/parrot.h"
29 #include "parrot/packfile.h"
31 /* HEADERIZER HFILE: include/parrot/packfile.h */
35 =item C<INTVAL fetch_iv_le>
37 This function converts a 4 or 8 byte C<INTVAL> into little endian
38 format. If the native format is already little endian, then no
39 conversion is done.
41 =cut
45 PARROT_WARN_UNUSED_RESULT
46 PARROT_CONST_FUNCTION
47 INTVAL
48 fetch_iv_le(INTVAL w)
50 #if !PARROT_BIGENDIAN
51 return w;
52 #else
53 # if INTVAL_SIZE == 4
54 return (w << 24) | ((w & 0xff00) << 8) | ((w & 0xff0000) >> 8) | (w >> 24);
55 # else
56 INTVAL r;
58 r = w << 56;
59 r |= (w & 0xff00) << 40;
60 r |= (w & 0xff0000) << 24;
61 r |= (w & 0xff000000) << 8;
62 r |= (w & 0xff00000000) >> 8;
63 r |= (w & 0xff0000000000) >> 24;
64 r |= (w & 0xff000000000000) >> 40;
65 r |= (w & 0xff00000000000000) >> 56;
66 return r;
67 # endif
68 #endif
73 =item C<INTVAL fetch_iv_be>
75 This function converts a 4 or 8 byte C<INTVAL> into big endian format.
76 If the native format is already big endian, then no conversion is done.
78 =cut
82 PARROT_WARN_UNUSED_RESULT
83 PARROT_CONST_FUNCTION
84 INTVAL
85 fetch_iv_be(INTVAL w)
87 #if PARROT_BIGENDIAN
88 return w;
89 #else
90 # if INTVAL_SIZE == 4
91 return (w << 24) | ((w & 0xff00) << 8) | ((w & 0xff0000) >> 8) | (w >> 24);
92 # else
93 INTVAL r;
94 r = w << 56;
95 r |= (w & 0xff00) << 40;
96 r |= (w & 0xff0000) << 24;
97 r |= (w & 0xff000000) << 8;
98 r |= (w & 0xff00000000) >> 8;
99 r |= (w & 0xff0000000000) >> 24;
100 r |= (w & 0xff000000000000) >> 40;
101 r |= (w & 0xff00000000000000) >> 56;
102 return r;
103 # endif
104 #endif
109 =item C<opcode_t fetch_op_be>
111 Same as C<fetch_iv_be> for opcode_t
113 =cut
117 PARROT_WARN_UNUSED_RESULT
118 PARROT_CONST_FUNCTION
119 opcode_t
120 fetch_op_be(opcode_t w)
122 #if PARROT_BIGENDIAN
123 return w;
124 #else
125 # if OPCODE_T_SIZE == 4
126 return (w << 24) | ((w & 0x0000ff00) << 8) | ((w & 0x00ff0000) >> 8) |
127 ((w & 0xff000000) >> 24);
128 # else
129 opcode_t r;
131 r = w << 56;
132 r |= (w & 0xff00) << 40;
133 r |= (w & 0xff0000) << 24;
134 r |= (w & 0xff000000) << 8;
135 r |= (w & 0xff00000000) >> 8;
136 r |= (w & 0xff0000000000) >> 24;
137 r |= (w & 0xff000000000000) >> 40;
138 r |= (w & 0xff00000000000000) >> 56;
139 return r;
140 # endif
141 #endif
146 =item C<opcode_t fetch_op_le>
148 Same as C<fetch_iv_le> for opcode_t
150 =cut
154 PARROT_WARN_UNUSED_RESULT
155 PARROT_CONST_FUNCTION
156 opcode_t
157 fetch_op_le(opcode_t w)
159 #if !PARROT_BIGENDIAN
160 return w;
161 #else
162 # if OPCODE_T_SIZE == 4
163 return (w << 24) | ((w & 0x0000ff00) << 8) | ((w & 0x00ff0000) >> 8) |
164 ((w & 0xff000000) >> 24);
165 # else
166 opcode_t r;
168 r = w << 56;
169 r |= (w & 0xff00) << 40;
170 r |= (w & 0xff0000) << 24;
171 r |= (w & 0xff000000) << 8;
172 r |= (w & 0xff00000000) >> 8;
173 r |= (w & 0xff0000000000) >> 24;
174 r |= (w & 0xff000000000000) >> 40;
175 r |= (w & 0xff00000000000000) >> 56;
176 return r;
177 # endif
178 #endif
183 =pod
185 Unrolled routines for swapping various sizes from 32-128 bits. These
186 should only be used if alignment is unknown or we are pulling something
187 out of a padded buffer.
189 =cut
195 =item C<void fetch_buf_be_4>
197 RT#48260: Not yet documented!!!
199 =cut
203 void
204 fetch_buf_be_4(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
206 #if PARROT_BIGENDIAN
207 memcpy(rb, b, 4);
208 #else
209 rb[0] = b[3];
210 rb[1] = b[2];
211 rb[2] = b[1];
212 rb[3] = b[0];
213 #endif
218 =item C<void fetch_buf_le_4>
220 RT#48260: Not yet documented!!!
222 =cut
226 void
227 fetch_buf_le_4(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
229 #if !PARROT_BIGENDIAN
230 memcpy(rb, b, 4);
231 #else
232 rb[0] = b[3];
233 rb[1] = b[2];
234 rb[2] = b[1];
235 rb[3] = b[0];
236 #endif
241 =item C<void fetch_buf_be_8>
243 RT#48260: Not yet documented!!!
245 =cut
249 void
250 fetch_buf_be_8(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
252 #if PARROT_BIGENDIAN
253 memcpy(rb, b, 8);
254 #else
255 rb[0] = b[7];
256 rb[1] = b[6];
257 rb[2] = b[5];
258 rb[3] = b[4];
259 rb[4] = b[3];
260 rb[5] = b[2];
261 rb[6] = b[1];
262 rb[7] = b[0];
263 #endif
268 =item C<void fetch_buf_le_8>
270 RT#48260: Not yet documented!!!
272 =cut
276 void
277 fetch_buf_le_8(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
279 #if !PARROT_BIGENDIAN
280 memcpy(rb, b, 8);
281 #else
282 rb[0] = b[7];
283 rb[1] = b[6];
284 rb[2] = b[5];
285 rb[3] = b[4];
286 rb[4] = b[3];
287 rb[5] = b[2];
288 rb[6] = b[1];
289 rb[7] = b[0];
290 #endif
295 =item C<void fetch_buf_le_12>
297 RT#48260: Not yet documented!!!
299 =cut
303 void
304 fetch_buf_le_12(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
306 #if !PARROT_BIGENDIAN
307 memcpy(rb, b, 12);
308 #else
309 rb[0] = b[11];
310 rb[1] = b[10];
311 rb[2] = b[9];
312 rb[3] = b[8];
313 rb[4] = b[7];
314 rb[5] = b[6];
315 rb[6] = b[5];
316 rb[7] = b[4];
317 rb[8] = b[3];
318 rb[9] = b[2];
319 rb[10] = b[1];
320 rb[11] = b[0];
321 #endif
326 =item C<void fetch_buf_be_12>
328 RT#48260: Not yet documented!!!
330 =cut
334 void
335 fetch_buf_be_12(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
337 #if PARROT_BIGENDIAN
338 memcpy(rb, b, 12);
339 #else
340 rb[0] = b[11];
341 rb[1] = b[10];
342 rb[2] = b[9];
343 rb[3] = b[8];
344 rb[4] = b[7];
345 rb[5] = b[6];
346 rb[6] = b[5];
347 rb[7] = b[4];
348 rb[8] = b[3];
349 rb[9] = b[2];
350 rb[10] = b[1];
351 rb[11] = b[0];
352 #endif
357 =item C<void fetch_buf_le_16>
359 RT#48260: Not yet documented!!!
361 =cut
365 void
366 fetch_buf_le_16(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
368 #if !PARROT_BIGENDIAN
369 memcpy(rb, b, 16);
370 #else
371 rb[0] = b[15];
372 rb[1] = b[14];
373 rb[2] = b[13];
374 rb[3] = b[12];
375 rb[4] = b[11];
376 rb[5] = b[10];
377 rb[6] = b[9];
378 rb[7] = b[8];
379 rb[8] = b[7];
380 rb[9] = b[6];
381 rb[10] = b[5];
382 rb[11] = b[4];
383 rb[12] = b[3];
384 rb[13] = b[2];
385 rb[14] = b[1];
386 rb[15] = b[0];
387 #endif
392 =item C<void fetch_buf_be_16>
394 RT#48260: Not yet documented!!!
396 =cut
400 void
401 fetch_buf_be_16(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
403 #if PARROT_BIGENDIAN
404 memcpy(rb, b, 16);
405 #else
406 rb[0] = b[15];
407 rb[1] = b[14];
408 rb[2] = b[13];
409 rb[3] = b[12];
410 rb[4] = b[11];
411 rb[5] = b[10];
412 rb[6] = b[9];
413 rb[7] = b[8];
414 rb[8] = b[7];
415 rb[9] = b[6];
416 rb[10] = b[5];
417 rb[11] = b[4];
418 rb[12] = b[3];
419 rb[13] = b[2];
420 rb[14] = b[1];
421 rb[15] = b[0];
422 #endif
427 =back
429 =head1 HISTORY
431 Initial version by Melvin on 2002/05/01
433 =cut
438 * Local variables:
439 * c-file-style: "parrot"
440 * End:
441 * vim: expandtab shiftwidth=4: