diff --git a/cmd/wuffs-c/internal/cgen/base/base-private.h b/cmd/wuffs-c/internal/cgen/base/base-private.h index 77b826cd..9f61b440 100644 --- a/cmd/wuffs-c/internal/cgen/base/base-private.h +++ b/cmd/wuffs-c/internal/cgen/base/base-private.h @@ -329,6 +329,18 @@ wuffs_base__store_u64le(uint8_t* p, uint64_t x) { p[7] = x >> 56; } + // -------- + + // TODO: do we need to care about (1 << 32) being re-interpreted as (1 << 0) + // on some architectures?? + // + // Should the first "1" be "(uint32_t)1"? + +#define WUFFS_BASE__LOW_BITS_MASK__U8(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U16(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U32(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U64(n) ((1 << (n)) - 1) + // -------- static inline void // diff --git a/cmd/wuffs-c/internal/cgen/builtin.go b/cmd/wuffs-c/internal/cgen/builtin.go index 84dfb78b..fe580daf 100644 --- a/cmd/wuffs-c/internal/cgen/builtin.go +++ b/cmd/wuffs-c/internal/cgen/builtin.go @@ -17,6 +17,8 @@ package cgen import ( "errors" "fmt" + "math/big" + "strings" a "github.com/google/wuffs/lang/ast" t "github.com/google/wuffs/lang/token" @@ -253,16 +255,33 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args [] func (g *gen) writeBuiltinNumType(b *buffer, recv *a.Expr, method t.ID, args []*a.Node, rp replacementPolicy, depth uint32) error { switch method { case t.IDLowBits: - // "recv.low_bits(n:etc)" in C is "((recv) & ((1 << (n)) - 1))". + // "recv.low_bits(n:etc)" in C is one of: + // - "((recv) & constant)" + // - "((recv) & WUFFS_BASE__LOW_BITS_MASK__UXX(n))" b.writes("((") if err := g.writeExpr(b, recv, rp, depth); err != nil { return err } - b.writes(") & ((1 << (") - if err := g.writeExpr(b, args[0].AsArg().Value(), rp, depth); err != nil { - return err + b.writes(") & ") + + if cv := args[0].AsArg().Value().ConstValue(); cv != nil && cv.Sign() >= 0 && cv.Cmp(sixtyFour) <= 0 { + mask := big.NewInt(0) + mask.Lsh(one, uint(cv.Uint64())) + mask.Sub(mask, one) + b.printf("0x%s", strings.ToUpper(mask.Text(16))) + } else { + if sz, err := g.sizeof(recv.MType()); err != nil { + return err + } else { + b.printf("WUFFS_BASE__LOW_BITS_MASK__U%d(", 8*sz) + } + if err := g.writeExpr(b, args[0].AsArg().Value(), rp, depth); err != nil { + return err + } + b.writes(")") } - b.writes(")) - 1))") + + b.writes(")") return nil case t.IDHighBits: diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index e7143efd..fbeb4ed8 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -40,9 +40,10 @@ import ( ) var ( - zero = big.NewInt(0) - one = big.NewInt(1) - eight = big.NewInt(8) + zero = big.NewInt(0) + one = big.NewInt(1) + eight = big.NewInt(8) + sixtyFour = big.NewInt(64) mibi = big.NewInt(1 << 20) ) diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index a00931d2..b52c801a 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -88,7 +88,9 @@ const baseBasePrivateH = "" + "" + "// --------\n\nstatic inline void //\nwuffs_base__store_u8be(uint8_t* p, uint8_t x) {\n p[0] = x;\n}\n\nstatic inline void //\nwuffs_base__store_u16be(uint8_t* p, uint16_t x) {\n p[0] = x >> 8;\n p[1] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u16le(uint8_t* p, uint16_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n}\n\nstatic inline void //\nwuffs_base__store_u24be(uint8_t* p, uint32_t x) {\n p[0] = x >> 16;\n p[1] = x >> 8;\n p[2] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u24le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n}\n\nstatic inline void //\nwuffs_base__store_u32be(uint8_t* p, uint32_t x) {\n p[0] = x >> 24;\n p[1] = x >> 16;\n p[2] = x >> 8;\n p[3] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u32le(uint8_t* p, uint32_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n}\n\nstatic inline void //\nwuffs_base__store_u40be(uint8_t* p, uint64_t x) {\n p[0] = x >> 32;\n p[1] = x >> 24;\n p[2] = x >> 16;\n p[3] = x >> 8;\n p[4] = x >>" + " 0;\n}\n\nstatic inline void //\nwuffs_base__store_u40le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n}\n\nstatic inline void //\nwuffs_base__store_u48be(uint8_t* p, uint64_t x) {\n p[0] = x >> 40;\n p[1] = x >> 32;\n p[2] = x >> 24;\n p[3] = x >> 16;\n p[4] = x >> 8;\n p[5] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u48le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n}\n\nstatic inline void //\nwuffs_base__store_u56be(uint8_t* p, uint64_t x) {\n p[0] = x >> 48;\n p[1] = x >> 40;\n p[2] = x >> 32;\n p[3] = x >> 24;\n p[4] = x >> 16;\n p[5] = x >> 8;\n p[6] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u56le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n}\n\nstatic inline void //\nwuffs_base__store_u64be(uint8_t* p, uint64_t x) {\n p[0] = x >> 56" + - ";\n p[1] = x >> 48;\n p[2] = x >> 40;\n p[3] = x >> 32;\n p[4] = x >> 24;\n p[5] = x >> 16;\n p[6] = x >> 8;\n p[7] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u64le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n p[7] = x >> 56;\n}\n\n" + + ";\n p[1] = x >> 48;\n p[2] = x >> 40;\n p[3] = x >> 32;\n p[4] = x >> 24;\n p[5] = x >> 16;\n p[6] = x >> 8;\n p[7] = x >> 0;\n}\n\nstatic inline void //\nwuffs_base__store_u64le(uint8_t* p, uint64_t x) {\n p[0] = x >> 0;\n p[1] = x >> 8;\n p[2] = x >> 16;\n p[3] = x >> 24;\n p[4] = x >> 32;\n p[5] = x >> 40;\n p[6] = x >> 48;\n p[7] = x >> 56;\n}\n\n " + + "" + + "// --------\n\n // TODO: do we need to care about (1 << 32) being re-interpreted as (1 << 0)\n // on some architectures??\n //\n // Should the first \"1\" be \"(uint32_t)1\"?\n\n#define WUFFS_BASE__LOW_BITS_MASK__U8(n) ((1 << (n)) - 1)\n#define WUFFS_BASE__LOW_BITS_MASK__U16(n) ((1 << (n)) - 1)\n#define WUFFS_BASE__LOW_BITS_MASK__U32(n) ((1 << (n)) - 1)\n#define WUFFS_BASE__LOW_BITS_MASK__U64(n) ((1 << (n)) - 1)\n\n" + "" + "// --------\n\nstatic inline void //\nwuffs_base__u8__sat_add_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u8__sat_sub_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_add_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_sub_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_add_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_sub_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_add_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_sub(*x, y);\n}\n\n" + "" + diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 622bce08..f590810d 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -3419,6 +3419,18 @@ wuffs_base__store_u64le(uint8_t* p, uint64_t x) { p[7] = x >> 56; } + // -------- + + // TODO: do we need to care about (1 << 32) being re-interpreted as (1 << 0) + // on some architectures?? + // + // Should the first "1" be "(uint32_t)1"? + +#define WUFFS_BASE__LOW_BITS_MASK__U8(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U16(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U32(n) ((1 << (n)) - 1) +#define WUFFS_BASE__LOW_BITS_MASK__U64(n) ((1 << (n)) - 1) + // -------- static inline void // @@ -3888,7 +3900,7 @@ wuffs_adler32__hasher__update(wuffs_adler32__hasher* self, self->private_impl.f_started = true; self->private_impl.f_state = 1; } - v_s1 = ((self->private_impl.f_state) & ((1 << (16)) - 1)); + v_s1 = ((self->private_impl.f_state) & 0xFFFF); v_s2 = ((self->private_impl.f_state) >> (32 - (16))); while (((uint64_t)(a_x.len)) > 0) { v_remaining = ((wuffs_base__slice_u8){}); @@ -5365,12 +5377,11 @@ wuffs_deflate__decoder__decode_uncompressed(wuffs_deflate__decoder* self, } v_length = t_1; } - if ((((v_length) & ((1 << (16)) - 1)) + ((v_length) >> (32 - (16)))) != - 65535) { + if ((((v_length)&0xFFFF) + ((v_length) >> (32 - (16)))) != 65535) { status = wuffs_deflate__error__inconsistent_stored_block_length; goto exit; } - v_length = ((v_length) & ((1 << (16)) - 1)); + v_length = ((v_length)&0xFFFF); while (true) { v_n_copied = wuffs_base__io_writer__copy_n_from_reader( &iop_a_dst, io1_a_dst, v_length, &iop_a_src, io1_a_src); @@ -5552,19 +5563,19 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, } v_n_bits += 8; } - v_n_lit = (((v_bits) & ((1 << (5)) - 1)) + 257); + v_n_lit = (((v_bits)&0x1F) + 257); if (v_n_lit > 286) { status = wuffs_deflate__error__bad_literal_length_code_count; goto exit; } v_bits >>= 5; - v_n_dist = (((v_bits) & ((1 << (5)) - 1)) + 1); + v_n_dist = (((v_bits)&0x1F) + 1); if (v_n_dist > 30) { status = wuffs_deflate__error__bad_distance_code_count; goto exit; } v_bits >>= 5; - v_n_clen = (((v_bits) & ((1 << (4)) - 1)) + 4); + v_n_clen = (((v_bits)&0xF) + 4); v_bits >>= 4; v_n_bits -= 14; v_i = 0; @@ -5668,7 +5679,7 @@ wuffs_deflate__decoder__init_dynamic_huffman(wuffs_deflate__decoder* self, } v_n_bits += 8; } - v_rep_count += ((v_bits) & ((1 << (v_n_extra_bits)) - 1)); + v_rep_count += ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_n_extra_bits)); v_bits >>= v_n_extra_bits; v_n_bits -= v_n_extra_bits; while (v_rep_count > 0) { @@ -5922,7 +5933,7 @@ label_1_break:; v_tmp = (v_cl - 9); v_cl = v_tmp; v_redirect_key = ((v_key >> v_tmp) & 511); - v_key = ((v_key) & ((1 << (v_tmp)) - 1)); + v_key = ((v_key)&WUFFS_BASE__LOW_BITS_MASK__U32(v_tmp)); if (v_prev_redirect_key != v_redirect_key) { v_prev_redirect_key = v_redirect_key; v_remaining = (((uint32_t)(1)) << v_cl); @@ -6176,7 +6187,8 @@ label_0_continue:; } else { } v_length = - ((v_length + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & + ((v_length + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; @@ -6244,7 +6256,8 @@ label_0_continue:; v_n_bits += 8; } v_dist_minus_1 = - ((v_dist_minus_1 + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & + ((v_dist_minus_1 + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; @@ -6559,7 +6572,8 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, v_n_bits += 8; } v_length = - ((v_length + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & + ((v_length + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; @@ -6639,9 +6653,10 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, } v_n_bits += 8; } - v_dist_minus_1 = ((v_dist_minus_1 + - ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & - 32767); + v_dist_minus_1 = + ((v_dist_minus_1 + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & + 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } @@ -9573,7 +9588,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, label_0_break:; } } - v_code = ((v_bits) & ((1 << (v_width)) - 1)); + v_code = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_width)); v_bits >>= v_width; v_n_bits -= v_width; if (v_code < v_clear_code) { @@ -9669,7 +9684,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, goto exit; } } - v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1)); + v_bits = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_n_bits)); self->private_impl.f_flush_j = v_j; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); status = wuffs_lzw__decoder__flush(self, a_dst); @@ -9690,7 +9705,7 @@ wuffs_lzw__decoder__decode(wuffs_lzw__decoder* self, goto exit; } } - v_bits = ((v_bits) & ((1 << (v_n_bits)) - 1)); + v_bits = ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_n_bits)); self->private_impl.f_flush_j = v_j; WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); status = wuffs_lzw__decoder__flush(self, a_dst);