1 # Copyright (C) 2001-2006, Parrot Foundation.
6 docs/dev/byteorder.pod - Byteorder Conversion Functions
10 The platform byteorder is stored for C code in F<include/parrot/config.h>
12 #define PARROT_BYTEORDER 0x1234
19 .include 'iglobals.pasm'
20 $P1 = $P0[.IGLOBALS_CONFIG_HASH]
21 $P2 = $P1["byteorder"]
30 The byteorder values are analog to perl, see L<perlfunc/pack>:
32 1234 little-endian 32-bit, 12345678 little-endian 64-bit
33 4321 big-endian 32-bit, 87654321 big-endian 64-bit
35 When reading a pbc stored in a different architecture, the pbc header defines
36 the pbc byteorder for the architecture which stored the pbc, and the
37 F<src/packfile/pf_items.c> functions are used to convert the values to the
38 native endianness, wordsize and ptrsize.
40 The byteorder code will check the endianness of an C<INTVAL> or an C<opcode_t>
41 value and swap from little to big, or big to little when appropriate. Functions
42 also exist to convert a 4, 8, 12, or 16 byte character buffer to big or little
43 endian. The functions are be placed in the PackFile vtable and are be called
44 when necessary. The Parrot interpreter is be smart enough to avoid calling
45 these functions when converting from and to the same byteorder.
47 =head1 Data Structures and Algorithms
49 The algorithm to change from one endianness to another is identical and simple
50 to understand. Basically, the size of an C<INTVAL> or C<opcode_t> is used to
51 determine at compile time how many bits should be shifted around. Then the
52 correct bits are shifted by the correct amounts (please look at source code for
53 exact amounts). The buffer change functions are implemented by a straight
54 forward algorithm that assigns swaps all of the bytes.
56 =head1 Important Functions
62 This function will convert an C<INTVAL> into little endian format. It is a
63 no-op if the native format is already little endian.
67 This function will convert an C<INTVAL> into big endian format. It is a no-op
68 if the native format is already big endian.
72 This function will convert an C<opcode_t> into little endian format. It is a
73 no-op if the native format is already little endian.
77 This function will convert an C<opcode_t> into big endian format. It is a no-op
78 if the native format is already big endian.
80 =item C<fetch_buf_le_>(4,8,12,16)
82 This set of functions will convert an unsigned character buffer into little
83 endian format. Only a C<memcpy> is performed if the native format is already
86 =item C<fetch_buf_be_>(4,8,12,16)
88 This set of functions will convert an unsigned character buffer into big endian
89 format. Only a C<memcpy> is performed if the native format is already big
94 =head1 Low level FLOATVAL fetch and convert functions
96 We support two different floattypes, stored in the pbc header as 0 or 1.
98 Floattype 0 = IEEE-754 8 byte double
99 Floattype 1 = x86 little endian 12 byte long double
103 =item C<cvt_num12_num8>
105 Converts i386 LE 12-byte long double to IEEE 754 8 byte double.
107 =item C<cvt_num12_num8_be>
109 Converts a 12-byte i386 long double into a big-endian IEEE 754 8-byte double.
110 Converting to BE is not yet implemented (throws internal_exception).
112 =item C<cvt_num12_num8_le>
114 Converts a 12-byte i386 long double into a little-endian IEEE 754
119 =head1 Unimplemented Functions
123 =item C<endianize_fetch_int>
125 Fetch an C<INTVAL> directly from a bytestream
127 =item C<endianize_put_int>
129 Put an C<INTVAL> directly on a bytestream
135 Initial version by Melvin on 2002-01-05,
136 more byteorder explanations by Reini Urban 2009-02-02
140 This assumes big or little endianness...other, more esoteric forms (such as
141 middle endian) are not supported. Also, an assumption of 4 or 8 byte
142 C<INTVAL>'s and C<opcode_t>'s is made.
146 The fetch and transformer functions are implemented in F<src/packfile/pf_items.c>
154 vim: expandtab shiftwidth=2 textwidth=70: