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 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
18 =head2 Byte order handlers
20 Configure will have checked for supported word sizes.
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
44 PARROT_WARN_UNUSED_RESULT
49 ASSERT_ARGS(fetch_iv_le
)
54 return (w
<< 24) | ((w
& 0xff00) << 8) | ((w
& 0xff0000) >> 8) | (w
>> 24);
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;
69 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
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.
87 PARROT_WARN_UNUSED_RESULT
92 ASSERT_ARGS(fetch_iv_be
)
97 return (w
<< 24) | ((w
& 0xff00) << 8) | ((w
& 0xff0000) >> 8) | (w
>> 24);
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;
111 exit_fatal(1, "Unsupported INTVAL_SIZE=%d\n",
120 =item C<opcode_t fetch_op_be(opcode_t w)>
122 Same as C<fetch_iv_be> for opcode_t
128 PARROT_WARN_UNUSED_RESULT
129 PARROT_CONST_FUNCTION
131 fetch_op_be(opcode_t w
)
133 ASSERT_ARGS(fetch_op_be
)
137 # if OPCODE_T_SIZE == 4
138 return (w
<< 24) | ((w
& 0x0000ff00) << 8) | ((w
& 0x00ff0000) >> 8) |
139 ((w
& 0xff000000) >> 24);
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;
158 =item C<opcode_t fetch_op_le(opcode_t w)>
160 Same as C<fetch_iv_le> for opcode_t
166 PARROT_WARN_UNUSED_RESULT
167 PARROT_CONST_FUNCTION
169 fetch_op_le(opcode_t w
)
171 ASSERT_ARGS(fetch_op_le
)
172 #if !PARROT_BIGENDIAN
175 # if OPCODE_T_SIZE == 4
176 return (w
<< 24) | ((w
& 0x0000ff00) << 8) | ((w
& 0x00ff0000) >> 8) |
177 ((w
& 0xff000000) >> 24);
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;
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.
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>.
217 fetch_buf_be_4(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
219 ASSERT_ARGS(fetch_buf_be_4
)
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>.
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
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>
265 fetch_buf_be_8(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
267 ASSERT_ARGS(fetch_buf_be_8
)
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>.
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
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>.
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
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>.
353 fetch_buf_be_12(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
355 ASSERT_ARGS(fetch_buf_be_12
)
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>.
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
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>.
421 fetch_buf_be_16(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
423 ASSERT_ARGS(fetch_buf_be_16
)
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>.
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
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>.
509 fetch_buf_be_32(ARGOUT(unsigned char *rb
), ARGIN(const unsigned char *b
))
511 ASSERT_ARGS(fetch_buf_be_32
)
556 Initial version by Melvin on 2002/05/01
564 * c-file-style: "parrot"
566 * vim: expandtab shiftwidth=4: