From 73a4f2b263ab7f3cce0d3aeeba8a4a3d6fd650ed Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 13 Jul 2017 11:46:26 +1000 Subject: [PATCH] Remove the unnecessary PUFFS_LOW_BITS macro. --- cmd/puffs-gen-c/base-impl.h | 2 -- cmd/puffs-gen-c/data.go | 2 +- cmd/puffs-gen-c/func.go | 11 ++++++----- gen/c/std/flate.c | 2 -- gen/c/std/gif.c | 4 +--- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/cmd/puffs-gen-c/base-impl.h b/cmd/puffs-gen-c/base-impl.h index 6a718793..b4327daf 100644 --- a/cmd/puffs-gen-c/base-impl.h +++ b/cmd/puffs-gen-c/base-impl.h @@ -15,8 +15,6 @@ coro_susp_point = n; \ case n: -#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) - // Clang also defines "__GNUC__". #if defined(__GNUC__) #define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1)) diff --git a/cmd/puffs-gen-c/data.go b/cmd/puffs-gen-c/data.go index a18260fa..84874c42 100644 --- a/cmd/puffs-gen-c/data.go +++ b/cmd/puffs-gen-c/data.go @@ -12,5 +12,5 @@ const baseHeader = "" + "" const baseImpl = "" + - "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1))\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" diff --git a/cmd/puffs-gen-c/func.go b/cmd/puffs-gen-c/func.go index 983de1d7..bceb370b 100644 --- a/cmd/puffs-gen-c/func.go +++ b/cmd/puffs-gen-c/func.go @@ -1057,15 +1057,16 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro // n is a function call. // TODO: delete this hack that only matches "foo.low_bits(etc)". if isLowBits(g.tm, n) { - g.printf("PUFFS_LOW_BITS(") - if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, parenthesesMandatory, depth); err != nil { + // "x.low_bits(n:etc)" in C is "((x) & ((1 << (n)) - 1))". + g.writes("((") + if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, parenthesesOptional, depth); err != nil { return err } - g.writes(",") - if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, parenthesesMandatory, depth); err != nil { + g.writes(") & ((1 << (") + if err := g.writeExpr(n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil { return err } - g.writes(")") + g.writes(")) - 1))") return nil } // TODO. diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 032c1c87..7a750443 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -191,8 +191,6 @@ puffs_flate_status puffs_flate_decoder_decode_uncompressed( coro_susp_point = n; \ case n: -#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) - // Clang also defines "__GNUC__". #if defined(__GNUC__) #define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1)) diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index d2bdb4d0..1d2ad868 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -261,8 +261,6 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, coro_susp_point = n; \ case n: -#define PUFFS_LOW_BITS(x, n) ((x) & ((1 << (n)) - 1)) - // Clang also defines "__GNUC__". #if defined(__GNUC__) #define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1)) @@ -1136,7 +1134,7 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, v_bits |= (((uint32_t)(t_0)) << v_n_bits); v_n_bits += 8; } - v_code = PUFFS_LOW_BITS(v_bits, v_width); + v_code = ((v_bits) & ((1 << (v_width)) - 1)); v_bits >>= v_width; v_n_bits -= v_width; if (v_code < v_clear_code) {