2 Copyright (C) 2001-2009, Parrot Foundation.
7 src/byteorder.c - Byteordering functions
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
18 =head2 Byte order handlers
20 Configure will have checked for supported word sizes.
28 #include "parrot/parrot.h"
29 #include "parrot/packfile.h"
31 /* HEADERIZER HFILE: include/parrot/packfile.h */
35 =item C<INTVAL fetch_iv_le(INTVAL w)>
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
45 PARROT_WARN_UNUSED_RESULT
50 ASSERT_ARGS(fetch_iv_le
)
55 return (w
<< 24) | ((w
& 0xff00) << 8) | ((w
& 0xff0000) >> 8) | (w
>> 24);
61 r
|= (w
& 0xff00) << 40;
62 r
|= (w
& 0xff0000) << 24;
63 r
|= (w
& 0xff000000) << 8;
64 r
|= (w
& 0xff00000000) >> 8;
65 r
|= (w
& 0xff0000000000) >> 24;
66 r
|= (w
& 0xff000000000000) >> 40;
67 r
|= (w
& 0xff00000000000000) >> 56;
70 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
79 =item C<INTVAL fetch_iv_be(INTVAL w)>
81 This function converts a 4 or 8 byte C<INTVAL> into big endian format.
82 If the native format is already big endian, then no conversion is done.
88 PARROT_WARN_UNUSED_RESULT
93 ASSERT_ARGS(fetch_iv_be
)
98 return (w
<< 24) | ((w
& 0xff00) << 8) | ((w
& 0xff0000) >> 8) | (w
>> 24);
100 # if INTVAL_SIZE == 8
103 r
|= (w
& 0xff00) << 40;
104 r
|= (w
& 0xff0000) << 24;
105 r
|= (w
& 0xff000000) << 8;
106 r
|= (w
& 0xff00000000) >> 8;
107 r
|= (w
& 0xff0000000000) >> 24;
108 r
|= (w
& 0xff000000000000) >> 40;
109 r
|= (w
& 0xff00000000000000) >> 56;
112 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
121 =item C<opcode_t fetch_op_be(opcode_t w)>
123 Same as C<fetch_iv_be> for opcode_t
129 PARROT_WARN_UNUSED_RESULT
130 PARROT_CONST_FUNCTION
132 fetch_op_be(opcode_t w
)
134 ASSERT_ARGS(fetch_op_be
)
138 # if OPCODE_T_SIZE == 4
139 return (w
<< 24) | ((w
& 0x0000ff00) << 8) | ((w
& 0x00ff0000) >> 8) |
140 ((w
& 0xff000000) >> 24);
145 r
|= (w
& 0xff00) << 40;
146 r
|= (w
& 0xff0000) << 24;
147 r
|= (w
& 0xff000000) << 8;
148 r
|= (w
& 0xff00000000) >> 8;
149 r
|= (w
& 0xff0000000000) >> 24;
150 r
|= (w
& 0xff000000000000) >> 40;
151 r
|= (w
& 0xff00000000000000) >> 56;
159 =item C<opcode_t fetch_op_le(opcode_t w)>
161 Same as C<fetch_iv_le> for opcode_t
167 PARROT_WARN_UNUSED_RESULT
168 PARROT_CONST_FUNCTION
170 fetch_op_le(opcode_t w
)
172 ASSERT_ARGS(fetch_op_le
)
173 #if !PARROT_BIGENDIAN
176 # if OPCODE_T_SIZE == 4
177 return (w
<< 24) | ((w
& 0x0000ff00) << 8) | ((w
& 0x00ff0000) >> 8) |
178 ((w
& 0xff000000) >> 24);
183 r
|= (w
& 0xff00) << 40;
184 r
|= (w
& 0xff0000) << 24;
185 r
|= (w
& 0xff000000) << 8;
186 r
|= (w
& 0xff00000000) >> 8;
187 r
|= (w
& 0xff0000000000) >> 24;
188 r
|= (w
& 0xff000000000000) >> 40;
189 r
|= (w
& 0xff00000000000000) >> 56;
199 Unrolled routines for swapping various sizes from 32-128 bits. These
200 should only be used if alignment is unknown or we are pulling something
201 out of a padded buffer.
209 =item C<void fetch_buf_be_4(unsigned char *rb, const unsigned char *b)>
211 Converts a 4-byte big-endian buffer C<b> into a little-endian C<rb>.
218 fetch_buf_be_4(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
220 ASSERT_ARGS(fetch_buf_be_4
)
233 =item C<void fetch_buf_le_4(unsigned char *rb, const unsigned char *b)>
235 Converts a 4-byte little-endian buffer C<b> into a big-endian buffer C<rb>.
242 fetch_buf_le_4(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
244 ASSERT_ARGS(fetch_buf_le_4
)
245 #if !PARROT_BIGENDIAN
257 =item C<void fetch_buf_be_8(unsigned char *rb, const unsigned char *b)>
259 Converts an 8-byte big-endian buffer C<b> into a little-endian buffer C<rb>
266 fetch_buf_be_8(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
268 ASSERT_ARGS(fetch_buf_be_8
)
285 =item C<void fetch_buf_le_8(unsigned char *rb, const unsigned char *b)>
287 Converts an 8-byte little-endian buffer C<b> into a big-endian buffer C<rb>.
294 fetch_buf_le_8(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
296 ASSERT_ARGS(fetch_buf_le_8
)
297 #if !PARROT_BIGENDIAN
313 =item C<void fetch_buf_le_12(unsigned char *rb, const unsigned char *b)>
315 Converts a 12-byte little-endian buffer C<b> into a big-endian buffer C<b>.
322 fetch_buf_le_12(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
324 ASSERT_ARGS(fetch_buf_le_12
)
325 #if !PARROT_BIGENDIAN
345 =item C<void fetch_buf_be_12(unsigned char *rb, const unsigned char *b)>
347 Converts a 12-byte big-endian buffer C<b> into a little-endian buffer C<b>.
354 fetch_buf_be_12(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
356 ASSERT_ARGS(fetch_buf_be_12
)
377 =item C<void fetch_buf_le_16(unsigned char *rb, const unsigned char *b)>
379 Converts a 16-byte little-endian buffer C<b> into a big-endian buffer C<b>.
386 fetch_buf_le_16(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
388 ASSERT_ARGS(fetch_buf_le_16
)
389 #if !PARROT_BIGENDIAN
413 =item C<void fetch_buf_be_16(unsigned char *rb, const unsigned char *b)>
415 Converts a 16-byte big-endian buffer C<b> into a little-endian buffer C<b>.
422 fetch_buf_be_16(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
424 ASSERT_ARGS(fetch_buf_be_16
)
449 =item C<void fetch_buf_le_32(unsigned char *rb, const unsigned char *b)>
451 Converts a 32-byte little-endian buffer C<b> into a big-endian buffer C<b>.
458 fetch_buf_le_32(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
460 ASSERT_ARGS(fetch_buf_le_32
)
461 #if !PARROT_BIGENDIAN
501 =item C<void fetch_buf_be_32(unsigned char *rb, const unsigned char *b)>
503 Converts a 32-byte big-endian buffer C<b> into a little-endian buffer C<b>.
510 fetch_buf_be_32(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
512 ASSERT_ARGS(fetch_buf_be_32
)
557 Initial version by Melvin on 2002/05/01
565 * c-file-style: "parrot"
567 * vim: expandtab shiftwidth=4: