Permalink
Browse files

[GH #807] Refactor native_pbc endianness, bswap64. Add header argumen…

…t to converters

Convert endianness upfront does not work.
Some converters work on native floats, to do compiler casts. They
need to know the packfile byteorder.
Other bitfiddling converters work only on little-endian, so we also need
to know the packfile byteorder, and they also need to convert endianness
back to to the target format.

Refactor bswap64, as it only works with 64bit registers. Tested with HAS_INT64.
So there are two bswap64 API's, one for fast native conversion via a register
and one with two unsigned char * args, which might point to the same buffer,
for easier in-place conversion.

Added dummy ROUND_NUM_TO macro, which needs to round a long number down to the given
precision (when converting upwards). Can be done with sprintf, but need to find a
better way.

Simplify converter casts with unions.

Replace SWAB_12 with SWAB_10. TODO: Need to check the last two bytes.
  • Loading branch information...
1 parent ecd4ef1 commit f98d38b202d138b29fbb92b666d5ff4f03d9dc0c Reini Urban committed Sep 14, 2012
Showing with 508 additions and 278 deletions.
  1. +24 −19 include/parrot/bswap.h
  2. +3 −3 include/parrot/packfile.h
  3. +476 −256 src/packfile/pf_items.c
  4. +5 −0 t/native_pbc/number.t
View
@@ -70,7 +70,7 @@
(((Parrot_UInt4)(__x) & (Parrot_UInt4)0x00ff0000UL) >> 8) | \
(((Parrot_UInt4)(__x) & (Parrot_UInt4)0xff000000UL) >> 24) )); \
})
-# ifdef HAS_LONGLONG
+# if HAS_INT64
# define bswap_64(x) \
({ \
Parrot_UInt8 __x = (x); \
@@ -89,7 +89,7 @@
unsigned char rb[8]; \
const unsigned char *c = &x; \
SWAB_8(rb, c); \
- (Parrot_UInt8)rb; })
+ rb; })
# endif
#endif
@@ -109,19 +109,17 @@
rb[6] = b[1]; \
rb[7] = b[0]
-#define SWAB_12(rb,b) \
- rb[0] = b[11]; \
- rb[1] = b[10]; \
- rb[2] = b[9]; \
- rb[3] = b[8]; \
- rb[4] = b[7]; \
- rb[5] = b[6]; \
- rb[6] = b[5]; \
- rb[7] = b[4]; \
- rb[8] = b[3]; \
- rb[9] = b[2]; \
- rb[10] = b[1]; \
- rb[11] = b[0]
+#define SWAB_10(rb,b) \
+ rb[0] = b[9]; \
+ rb[1] = b[8]; \
+ rb[2] = b[7]; \
+ rb[3] = b[6]; \
+ rb[4] = b[5]; \
+ rb[5] = b[4]; \
+ rb[6] = b[3]; \
+ rb[7] = b[2]; \
+ rb[8] = b[1]; \
+ rb[9] = b[0];
#define SWAB_16(rb,b) \
rb[0] = b[15]; \
@@ -188,15 +186,22 @@ Parrot_UInt4 bswap32(Parrot_UInt4 x)
}
static inline
+#if HAS_INT64
Parrot_UInt8 bswap64(Parrot_UInt8 x)
+#else
+void bswap64(unsigned char *rb, const unsigned char *b)
+#endif
{
#if defined(bswap_64)
return bswap_64(x);
#else
- unsigned char rb[8];
- const unsigned char *c = &x;
- SWAB_8(rb, c);
- return (Parrot_UInt8)rb;
+ if (b == rb) {
+ unsigned char tmp[8];
+ memcpy(tmp, b, 8);
+ SWAB_8(rb, tmp);
+ } else {
+ SWAB_8(rb, b);
+ }
#endif
}
@@ -295,10 +295,11 @@ typedef struct PackFile_Directory {
PackFile_Segment **segments;
} PackFile_Directory;
-
typedef opcode_t (*packfile_fetch_op_t)(ARGIN(const unsigned char *));
typedef INTVAL (*packfile_fetch_iv_t)(ARGIN(const unsigned char *));
-typedef void (*packfile_fetch_nv_t)(ARGOUT(unsigned char *), ARGIN(const unsigned char *));
+typedef void (*packfile_fetch_nv_t)(ARGOUT(unsigned char *),
+ ARGIN(const unsigned char *),
+ ARGIN(const PackFile_Header *));
typedef struct PackFile {
/* the packfile is its own directory */
@@ -326,7 +327,6 @@ typedef struct PackFile {
packfile_fetch_nv_t fetch_nv;
} PackFile;
-
typedef enum {
PBC_MAIN = 1,
PBC_LOADED = 2,
Oops, something went wrong.

0 comments on commit f98d38b

Please sign in to comment.