From 51c06fb2bfe238bf76b0ec0da3175c04f4bea2f2 Mon Sep 17 00:00:00 2001 From: TAKAI Kousuke <62541129+t-a-k@users.noreply.github.com> Date: Fri, 8 Oct 2021 22:58:54 +0900 Subject: Simplify IV abs operation in pp_abs. Transforming -iv into "(UV)-(iv + 1) + 1" can avoid signed integer overflow even if iv was IV_MIN in 2's compelement representation (as long as iv < 0), so that it makes special treatment for IV_MIN unnecessary and saves one conditional jump. --- pp.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'pp.c') diff --git a/pp.c b/pp.c index 25d3a319cd..643a8758b7 100644 --- a/pp.c +++ b/pp.c @@ -3047,13 +3047,12 @@ PP(pp_abs) if (iv >= 0) { uv = (UV)iv; } else { - if (iv != IV_MIN) { - uv = (UV)-iv; - } else { - /* 2s complement assumption. Also, not really needed as - IV_MIN and -IV_MIN should both be %100...00 and NV-able */ - uv = (UV)IV_MIN; - } + /* "(UV)-(iv + 1) + 1" below is mathematically "-iv", but + transformed so that every subexpression will never trigger + overflows even on 2's complement representation (note that + iv is always < 0 here), and modern compilers could optimize + this to a single negation. */ + uv = (UV)-(iv + 1) + 1; } } set_uv: -- cgit v1.2.1