revert - didn't do what I wanted
[parrot.git] / src / byteorder.c
blobeac19820438431ffa25fbce4206119fb97d39fe3
1 /*
2 Copyright (C) 2001-2009, Parrot 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 function 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"
30 /* HEADERIZER HFILE: include/parrot/packfile.h */
34 =item C<INTVAL fetch_iv_le(INTVAL w)>
36 This function converts a 4 or 8 byte C<INTVAL> into little endian
37 format. If the native format is already little endian, then no
38 conversion is done.
40 =cut
44 PARROT_WARN_UNUSED_RESULT
45 PARROT_CONST_FUNCTION
46 INTVAL
47 fetch_iv_le(INTVAL w)
49 ASSERT_ARGS(fetch_iv_le)
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 # if INTVAL_SIZE == 8
57 INTVAL r;
59 r = w << 56;
60 r |= (w & 0xff00) << 40;
61 r |= (w & 0xff0000) << 24;
62 r |= (w & 0xff000000) << 8;
63 r |= (w & 0xff00000000) >> 8;
64 r |= (w & 0xff0000000000) >> 24;
65 r |= (w & 0xff000000000000) >> 40;
66 r |= (w & 0xff00000000000000) >> 56;
67 return r;
68 # else
69 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
70 INTVAL_SIZE);
71 # endif
72 # endif
73 #endif
78 =item C<INTVAL fetch_iv_be(INTVAL w)>
80 This function converts a 4 or 8 byte C<INTVAL> into big endian format.
81 If the native format is already big endian, then no conversion is done.
83 =cut
87 PARROT_WARN_UNUSED_RESULT
88 PARROT_CONST_FUNCTION
89 INTVAL
90 fetch_iv_be(INTVAL w)
92 ASSERT_ARGS(fetch_iv_be)
93 #if PARROT_BIGENDIAN
94 return w;
95 #else
96 # if INTVAL_SIZE == 4
97 return (w << 24) | ((w & 0xff00) << 8) | ((w & 0xff0000) >> 8) | (w >> 24);
98 # else
99 # if INTVAL_SIZE == 8
100 INTVAL r;
101 r = w << 56;
102 r |= (w & 0xff00) << 40;
103 r |= (w & 0xff0000) << 24;
104 r |= (w & 0xff000000) << 8;
105 r |= (w & 0xff00000000) >> 8;
106 r |= (w & 0xff0000000000) >> 24;
107 r |= (w & 0xff000000000000) >> 40;
108 r |= (w & 0xff00000000000000) >> 56;
109 return r;
110 # else
111 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
112 INTVAL_SIZE);
113 # endif
114 # endif
115 #endif
120 =item C<opcode_t fetch_op_be(opcode_t w)>
122 Same as C<fetch_iv_be> for opcode_t
124 =cut
128 PARROT_WARN_UNUSED_RESULT
129 PARROT_CONST_FUNCTION
130 opcode_t
131 fetch_op_be(opcode_t w)
133 ASSERT_ARGS(fetch_op_be)
134 #if PARROT_BIGENDIAN
135 return w;
136 #else
137 # if OPCODE_T_SIZE == 4
138 return (w << 24) | ((w & 0x0000ff00) << 8) | ((w & 0x00ff0000) >> 8) |
139 ((w & 0xff000000) >> 24);
140 # else
141 opcode_t r;
143 r = w << 56;
144 r |= (w & 0xff00) << 40;
145 r |= (w & 0xff0000) << 24;
146 r |= (w & 0xff000000) << 8;
147 r |= (w & 0xff00000000) >> 8;
148 r |= (w & 0xff0000000000) >> 24;
149 r |= (w & 0xff000000000000) >> 40;
150 r |= (w & 0xff00000000000000) >> 56;
151 return r;
152 # endif
153 #endif
158 =item C<opcode_t fetch_op_le(opcode_t w)>
160 Same as C<fetch_iv_le> for opcode_t
162 =cut
166 PARROT_WARN_UNUSED_RESULT
167 PARROT_CONST_FUNCTION
168 opcode_t
169 fetch_op_le(opcode_t w)
171 ASSERT_ARGS(fetch_op_le)
172 #if !PARROT_BIGENDIAN
173 return w;
174 #else
175 # if OPCODE_T_SIZE == 4
176 return (w << 24) | ((w & 0x0000ff00) << 8) | ((w & 0x00ff0000) >> 8) |
177 ((w & 0xff000000) >> 24);
178 # else
179 opcode_t r;
181 r = w << 56;
182 r |= (w & 0xff00) << 40;
183 r |= (w & 0xff0000) << 24;
184 r |= (w & 0xff000000) << 8;
185 r |= (w & 0xff00000000) >> 8;
186 r |= (w & 0xff0000000000) >> 24;
187 r |= (w & 0xff000000000000) >> 40;
188 r |= (w & 0xff00000000000000) >> 56;
189 return r;
190 # endif
191 #endif
196 =pod
198 Unrolled routines for swapping various sizes from 32-128 bits. These
199 should only be used if alignment is unknown or we are pulling something
200 out of a padded buffer.
202 =cut
208 =item C<void fetch_buf_be_4(unsigned char *rb, const unsigned char *b)>
210 Converts a 4-byte big-endian buffer C<b> into a little-endian C<rb>.
212 =cut
216 void
217 fetch_buf_be_4(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
219 ASSERT_ARGS(fetch_buf_be_4)
220 #if PARROT_BIGENDIAN
221 memcpy(rb, b, 4);
222 #else
223 rb[0] = b[3];
224 rb[1] = b[2];
225 rb[2] = b[1];
226 rb[3] = b[0];
227 #endif
232 =item C<void fetch_buf_le_4(unsigned char *rb, const unsigned char *b)>
234 Converts a 4-byte little-endian buffer C<b> into a big-endian buffer C<rb>.
236 =cut
240 void
241 fetch_buf_le_4(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
243 ASSERT_ARGS(fetch_buf_le_4)
244 #if !PARROT_BIGENDIAN
245 memcpy(rb, b, 4);
246 #else
247 rb[0] = b[3];
248 rb[1] = b[2];
249 rb[2] = b[1];
250 rb[3] = b[0];
251 #endif
256 =item C<void fetch_buf_be_8(unsigned char *rb, const unsigned char *b)>
258 Converts an 8-byte big-endian buffer C<b> into a little-endian buffer C<rb>
260 =cut
264 void
265 fetch_buf_be_8(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
267 ASSERT_ARGS(fetch_buf_be_8)
268 #if PARROT_BIGENDIAN
269 memcpy(rb, b, 8);
270 #else
271 rb[0] = b[7];
272 rb[1] = b[6];
273 rb[2] = b[5];
274 rb[3] = b[4];
275 rb[4] = b[3];
276 rb[5] = b[2];
277 rb[6] = b[1];
278 rb[7] = b[0];
279 #endif
284 =item C<void fetch_buf_le_8(unsigned char *rb, const unsigned char *b)>
286 Converts an 8-byte little-endian buffer C<b> into a big-endian buffer C<rb>.
288 =cut
292 void
293 fetch_buf_le_8(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
295 ASSERT_ARGS(fetch_buf_le_8)
296 #if !PARROT_BIGENDIAN
297 memcpy(rb, b, 8);
298 #else
299 rb[0] = b[7];
300 rb[1] = b[6];
301 rb[2] = b[5];
302 rb[3] = b[4];
303 rb[4] = b[3];
304 rb[5] = b[2];
305 rb[6] = b[1];
306 rb[7] = b[0];
307 #endif
312 =item C<void fetch_buf_le_12(unsigned char *rb, const unsigned char *b)>
314 Converts a 12-byte little-endian buffer C<b> into a big-endian buffer C<b>.
316 =cut
320 void
321 fetch_buf_le_12(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
323 ASSERT_ARGS(fetch_buf_le_12)
324 #if !PARROT_BIGENDIAN
325 memcpy(rb, b, 12);
326 #else
327 rb[0] = b[11];
328 rb[1] = b[10];
329 rb[2] = b[9];
330 rb[3] = b[8];
331 rb[4] = b[7];
332 rb[5] = b[6];
333 rb[6] = b[5];
334 rb[7] = b[4];
335 rb[8] = b[3];
336 rb[9] = b[2];
337 rb[10] = b[1];
338 rb[11] = b[0];
339 #endif
344 =item C<void fetch_buf_be_12(unsigned char *rb, const unsigned char *b)>
346 Converts a 12-byte big-endian buffer C<b> into a little-endian buffer C<b>.
348 =cut
352 void
353 fetch_buf_be_12(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
355 ASSERT_ARGS(fetch_buf_be_12)
356 #if PARROT_BIGENDIAN
357 memcpy(rb, b, 12);
358 #else
359 rb[0] = b[11];
360 rb[1] = b[10];
361 rb[2] = b[9];
362 rb[3] = b[8];
363 rb[4] = b[7];
364 rb[5] = b[6];
365 rb[6] = b[5];
366 rb[7] = b[4];
367 rb[8] = b[3];
368 rb[9] = b[2];
369 rb[10] = b[1];
370 rb[11] = b[0];
371 #endif
376 =item C<void fetch_buf_le_16(unsigned char *rb, const unsigned char *b)>
378 Converts a 16-byte little-endian buffer C<b> into a big-endian buffer C<b>.
380 =cut
384 void
385 fetch_buf_le_16(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
387 ASSERT_ARGS(fetch_buf_le_16)
388 #if !PARROT_BIGENDIAN
389 memcpy(rb, b, 16);
390 #else
391 rb[0] = b[15];
392 rb[1] = b[14];
393 rb[2] = b[13];
394 rb[3] = b[12];
395 rb[4] = b[11];
396 rb[5] = b[10];
397 rb[6] = b[9];
398 rb[7] = b[8];
399 rb[8] = b[7];
400 rb[9] = b[6];
401 rb[10] = b[5];
402 rb[11] = b[4];
403 rb[12] = b[3];
404 rb[13] = b[2];
405 rb[14] = b[1];
406 rb[15] = b[0];
407 #endif
412 =item C<void fetch_buf_be_16(unsigned char *rb, const unsigned char *b)>
414 Converts a 16-byte big-endian buffer C<b> into a little-endian buffer C<b>.
416 =cut
420 void
421 fetch_buf_be_16(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
423 ASSERT_ARGS(fetch_buf_be_16)
424 #if PARROT_BIGENDIAN
425 memcpy(rb, b, 16);
426 #else
427 rb[0] = b[15];
428 rb[1] = b[14];
429 rb[2] = b[13];
430 rb[3] = b[12];
431 rb[4] = b[11];
432 rb[5] = b[10];
433 rb[6] = b[9];
434 rb[7] = b[8];
435 rb[8] = b[7];
436 rb[9] = b[6];
437 rb[10] = b[5];
438 rb[11] = b[4];
439 rb[12] = b[3];
440 rb[13] = b[2];
441 rb[14] = b[1];
442 rb[15] = b[0];
443 #endif
448 =item C<void fetch_buf_le_32(unsigned char *rb, const unsigned char *b)>
450 Converts a 32-byte little-endian buffer C<b> into a big-endian buffer C<b>.
452 =cut
456 void
457 fetch_buf_le_32(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
459 ASSERT_ARGS(fetch_buf_le_32)
460 #if !PARROT_BIGENDIAN
461 memcpy(rb, b, 32);
462 #else
463 rb[0] = b[31];
464 rb[1] = b[30];
465 rb[2] = b[29];
466 rb[3] = b[28];
467 rb[4] = b[27];
468 rb[5] = b[26];
469 rb[6] = b[25];
470 rb[7] = b[24];
471 rb[8] = b[23];
472 rb[9] = b[22];
473 rb[10] = b[21];
474 rb[11] = b[20];
475 rb[12] = b[19];
476 rb[13] = b[18];
477 rb[14] = b[17];
478 rb[15] = b[16];
479 rb[16] = b[15];
480 rb[17] = b[14];
481 rb[18] = b[13];
482 rb[19] = b[12];
483 rb[20] = b[11];
484 rb[21] = b[10];
485 rb[22] = b[9];
486 rb[23] = b[8];
487 rb[24] = b[7];
488 rb[25] = b[6];
489 rb[26] = b[5];
490 rb[27] = b[4];
491 rb[28] = b[3];
492 rb[29] = b[2];
493 rb[30] = b[1];
494 rb[31] = b[0];
495 #endif
500 =item C<void fetch_buf_be_32(unsigned char *rb, const unsigned char *b)>
502 Converts a 32-byte big-endian buffer C<b> into a little-endian buffer C<b>.
504 =cut
508 void
509 fetch_buf_be_32(ARGOUT(unsigned char *rb), ARGIN(const unsigned char *b))
511 ASSERT_ARGS(fetch_buf_be_32)
512 #if PARROT_BIGENDIAN
513 memcpy(rb, b, 32);
514 #else
515 rb[0] = b[31];
516 rb[1] = b[30];
517 rb[2] = b[29];
518 rb[3] = b[28];
519 rb[4] = b[27];
520 rb[5] = b[26];
521 rb[6] = b[25];
522 rb[7] = b[24];
523 rb[8] = b[23];
524 rb[9] = b[22];
525 rb[10] = b[21];
526 rb[11] = b[20];
527 rb[12] = b[19];
528 rb[13] = b[18];
529 rb[14] = b[17];
530 rb[15] = b[16];
531 rb[16] = b[15];
532 rb[17] = b[14];
533 rb[18] = b[13];
534 rb[19] = b[12];
535 rb[20] = b[11];
536 rb[21] = b[10];
537 rb[22] = b[9];
538 rb[23] = b[8];
539 rb[24] = b[7];
540 rb[25] = b[6];
541 rb[26] = b[5];
542 rb[27] = b[4];
543 rb[28] = b[3];
544 rb[29] = b[2];
545 rb[30] = b[1];
546 rb[31] = b[0];
547 #endif
552 =back
554 =head1 HISTORY
556 Initial version by Melvin on 2002/05/01
558 =cut
563 * Local variables:
564 * c-file-style: "parrot"
565 * End:
566 * vim: expandtab shiftwidth=4: