diff options
author | Nathan Moinvaziri <nathan@nathanm.com> | 2020-01-26 12:34:54 -0800 |
---|---|---|
committer | Hans Kristian Rosbach <hk-github@circlestorm.org> | 2020-02-07 18:36:08 +0100 |
commit | d06d965ff0fe143b54f162a7970285c88460f34f (patch) | |
tree | 3ca34efda351322d363b1864d1017e25afbe8c00 /deflate.h | |
parent | 840e931ad341169830733a35387c14c6559f2829 (diff) |
Added better aligned access support for put_short.
Renamed putShortMSB to put_short_msb and moved to deflate.h with the rest of the put functions.
Added put_uint32 and put_uint32_msb and replaced instances of put_byte with put_short or put_uint32.
Diffstat (limited to 'deflate.h')
-rw-r--r-- | deflate.h | 53 |
1 files changed, 48 insertions, 5 deletions
@@ -306,14 +306,57 @@ typedef enum { /* =========================================================================== * Output a short LSB first on the stream. - * IN assertion: there is enough room in pendingBuf. + * IN assertion: there is enough room in pending_buf. */ static inline void put_short(deflate_state *s, uint16_t w) { -#if BYTE_ORDER == BIG_ENDIAN - w = ZSWAP16(w); -#endif - memcpy(&(s->pending_buf[s->pending]), &w, sizeof(uint16_t)); +#if defined(UNALIGNED_OK) + *(uint16_t *)(&s->pending_buf[s->pending]) = w; s->pending += 2; +#else + put_byte(s, (w & 0xff)); + put_byte(s, ((w >> 8) & 0xff)); +#endif +} + +/* =========================================================================== + * Output a short MSB first on the stream. + * IN assertion: there is enough room in pending_buf. + */ +static inline void put_short_msb(deflate_state *s, uint16_t w) { + put_byte(s, ((w >> 8) & 0xff)); + put_byte(s, (w & 0xff)); +} + +/* =========================================================================== + * Output a 32-bit unsigned int LSB first on the stream. + * IN assertion: there is enough room in pending_buf. + */ +static inline void put_uint32(deflate_state *s, uint32_t dw) { +#if defined(UNALIGNED_OK) + *(uint32_t *)(&s->pending_buf[s->pending]) = dw; + s->pending += 4; +#else + put_byte(s, (dw & 0xff)); + put_byte(s, ((dw >> 8) & 0xff)); + put_byte(s, ((dw >> 16) & 0xff)); + put_byte(s, ((dw >> 24) & 0xff)); +#endif +} + +/* =========================================================================== + * Output a 32-bit unsigned int MSB first on the stream. + * IN assertion: there is enough room in pending_buf. + */ +static inline void put_uint32_msb(deflate_state *s, uint32_t dw) { +#if defined(UNALIGNED_OK) + *(uint32_t *)(&s->pending_buf[s->pending]) = ZSWAP32(dw); + s->pending += 4; +#else + put_byte(s, ((dw >> 24) & 0xff)); + put_byte(s, ((dw >> 16) & 0xff)); + put_byte(s, ((dw >> 8) & 0xff)); + put_byte(s, (dw & 0xff)); +#endif } #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) |