From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id ECB473884F3B; Thu, 1 Sep 2022 10:06:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ECB473884F3B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-x434.google.com with SMTP id b16so14023409wru.7; Thu, 01 Sep 2022 03:06:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date; bh=8tgJaP0jxRQiHQpgELUi9kaGhAGj7Dv4zTu8WESUOx0=; b=q3RCB5go/YPOc6pUVu+bE2JhAWI6IV/u7I1o0TGVBH/Ov/p9n8ATWBV3Bwh2UiGmFi vC012u7/s9b3aeo/tNTiZ0ZHL0i8d6m22YZzW7nKMSNwcGfwYZAjtukhjYyDTjVv7nE/ w4I/WA+JNAhuJTbliqkeXvSoe1l+Xms2n0MsQ+OvnfdkFRg/yTbVyDF2QaxV89wuU7Sy tyBweNUE1qvdb9BiZBLXPeZ28GxwA6j5LGWapVVzAwqkA7fHULLL11WjXvsgrHqnDcBA jd/Dc5fr7JuFqbZJLNVX4YFs1Qmb5bQfl08XkRvuoSxDtw6P+D0EmZV5WEvdEG7ZlN1m hSgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date; bh=8tgJaP0jxRQiHQpgELUi9kaGhAGj7Dv4zTu8WESUOx0=; b=kk1WdpkzwAj5gcBQr9v25w9XJnwLUWcTywnLZFR8M9jDlYj0CBkur7PghxZ0P2xJQg 4fPqoGV+Qlj0sdznTvc58cR4WXINaced9kmlkJ8W48W7szPx5zlZkyTxoLmA/E+uG3/m zhEY3rTQhxnoskT8dzVG7dAwzunVkV3IBiYJwJXjzIWgSj65+uAOgV1n/G2o1VGkhjk2 FtR+IsJcjwO9lQ4ktsSVQKtheHrDTt5Qyvc+7TOhULhIjfG1NZBW86G/vZJMBEosY+Ii 0OyWw9PPfsJMw+mQ1KA5VjPTcrVSBmVpF6o+Sq05nGk17vZV+MUhkyCREVUTwK/n1GUJ 6mJw== X-Gm-Message-State: ACgBeo1Mq1r63xb8WsXIzL0f7VcMsroL7bCWpXsdmW0wo1tz8JG3xP6P tH2SXlLOibokAcjIqpmfySk= X-Google-Smtp-Source: AA6agR4fDJ0/NzMKSh5QQr3xGlCBwhNbRGIuPLyilB33cCftTi3sWzvrWWT0dF1i8g9CGlKm8A+low== X-Received: by 2002:adf:fdcf:0:b0:225:65c8:7b7f with SMTP id i15-20020adffdcf000000b0022565c87b7fmr14474892wrs.217.1662026776632; Thu, 01 Sep 2022 03:06:16 -0700 (PDT) Received: from smtpclient.apple (chp127.enscp.fr. [193.51.253.127]) by smtp.gmail.com with ESMTPSA id r7-20020a05600c35c700b003a5b6086381sm5526111wmq.48.2022.09.01.03.06.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Sep 2022 03:06:15 -0700 (PDT) From: FX Message-Id: Content-Type: multipart/mixed; boundary="Apple-Mail=_AB3E4E86-9040-48A0-9A18-7834232E5255" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Subject: Re: Floating-point comparisons in the middle-end Date: Thu, 1 Sep 2022 12:06:14 +0200 In-Reply-To: Cc: gcc@gcc.gnu.org, "Joseph S. Myers" , FX via Fortran To: Jakub Jelinek References: <8C6DDAA3-A40F-47C7-BE78-D56A3EC70C71@gmail.com> <3BD50DD7-5C9E-42CB-992C-A66584411A4F@gmail.com> X-Mailer: Apple Mail (2.3696.120.41.1.1) X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --Apple-Mail=_AB3E4E86-9040-48A0-9A18-7834232E5255 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, > Dunno if we really need a builtin for this, especially if it is = lowered > to that x >=3D y && x <=3D y early, will defer to Joseph. I think it=E2=80=99d be nice to have one for consistency, as the other = standard floating-point functions are there. It would also make things = slightly easier for our Fortran implementation, although admittedly we = can do without. A tentative patch is attached, it seems to work well on simple examples, = but for test coverage the hard part is going to be that the comparisons = seem to be optimised away very easily into their non-signaling versions. = Basically, if I do: float x =3D __builtin_nanf(""); printf("%d\n", __builtin_iseqsig(__builtin_nanf(""), = __builtin_inff())); printf("%d\n", __builtin_iseqsig(x, __builtin_inff())); With -O0 -fno-unsafe-math-optimizations -frounding-math = -fsignaling-nans: first one does not raise invalid, second one does. With -O2 -fno-unsafe-math-optimizations -frounding-math = -fsignaling-nans: no invalid raised at all. FX --Apple-Mail=_AB3E4E86-9040-48A0-9A18-7834232E5255 Content-Disposition: attachment; filename=iseqsig.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="iseqsig.diff" Content-Transfer-Encoding: 7bit diff --git a/gcc/builtins.cc b/gcc/builtins.cc index f1f7c0ce337..bf6bf2809d8 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -171,6 +171,7 @@ static tree fold_builtin_fabs (location_t, tree, tree); static tree fold_builtin_abs (location_t, tree, tree); static tree fold_builtin_unordered_cmp (location_t, tree, tree, tree, enum tree_code, enum tree_code); +static tree fold_builtin_iseqsig (location_t, tree, tree); static tree fold_builtin_varargs (location_t, tree, tree*, int); static tree fold_builtin_strpbrk (location_t, tree, tree, tree, tree); @@ -9404,6 +9405,42 @@ fold_builtin_unordered_cmp (location_t loc, tree fndecl, tree arg0, tree arg1, fold_build2_loc (loc, code, type, arg0, arg1)); } +/* Fold a call to __builtin_iseqsig(). ARG0 and ARG1 are the arguments. + After choosing the wider floating-point type for the comparison, + the code is folded to: + SAVE_EXPR >= SAVE_EXPR && SAVE_EXPR <= SAVE_EXPR */ + +static tree +fold_builtin_iseqsig (location_t loc, tree arg0, tree arg1) +{ + tree type0, type1; + enum tree_code code0, code1; + tree cmp1, cmp2, cmp_type = NULL_TREE; + + type0 = TREE_TYPE (arg0); + type1 = TREE_TYPE (arg1); + + code0 = TREE_CODE (type0); + code1 = TREE_CODE (type1); + + if (code0 == REAL_TYPE && code1 == REAL_TYPE) + /* Choose the wider of two real types. */ + cmp_type = TYPE_PRECISION (type0) >= TYPE_PRECISION (type1) + ? type0 : type1; + else if (code0 == REAL_TYPE && code1 == INTEGER_TYPE) + cmp_type = type0; + else if (code0 == INTEGER_TYPE && code1 == REAL_TYPE) + cmp_type = type1; + + arg0 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg0)); + arg1 = builtin_save_expr (fold_convert_loc (loc, cmp_type, arg1)); + + cmp1 = fold_build2_loc (loc, GE_EXPR, integer_type_node, arg0, arg1); + cmp2 = fold_build2_loc (loc, LE_EXPR, integer_type_node, arg0, arg1); + + return fold_build2_loc (loc, TRUTH_AND_EXPR, integer_type_node, cmp1, cmp2); +} + /* Fold __builtin_{,s,u}{add,sub,mul}{,l,ll}_overflow, either into normal arithmetics if it can never overflow, or into internal functions that return both result of arithmetics and overflowed boolean flag in @@ -9791,6 +9828,9 @@ fold_builtin_2 (location_t loc, tree expr, tree fndecl, tree arg0, tree arg1) arg0, arg1, UNORDERED_EXPR, NOP_EXPR); + case BUILT_IN_ISEQSIG: + return fold_builtin_iseqsig (loc, arg0, arg1); + /* We do the folding for va_start in the expander. */ case BUILT_IN_VA_START: break; @@ -11303,6 +11343,7 @@ is_inexpensive_builtin (tree decl) case BUILT_IN_ISLESSEQUAL: case BUILT_IN_ISLESSGREATER: case BUILT_IN_ISUNORDERED: + case BUILT_IN_ISEQSIG: case BUILT_IN_VA_ARG_PACK: case BUILT_IN_VA_ARG_PACK_LEN: case BUILT_IN_VA_COPY: diff --git a/gcc/builtins.def b/gcc/builtins.def index f0236316850..8fab9dc3f1b 100644 --- a/gcc/builtins.def +++ b/gcc/builtins.def @@ -908,6 +908,7 @@ DEF_GCC_BUILTIN (BUILT_IN_ISLESS, "isless", BT_FN_INT_VAR, ATTR_CONST_NOT DEF_GCC_BUILTIN (BUILT_IN_ISLESSEQUAL, "islessequal", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF) DEF_GCC_BUILTIN (BUILT_IN_ISLESSGREATER, "islessgreater", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF) DEF_GCC_BUILTIN (BUILT_IN_ISUNORDERED, "isunordered", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF) +DEF_GCC_BUILTIN (BUILT_IN_ISEQSIG, "iseqsig", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF) DEF_GCC_BUILTIN (BUILT_IN_ISSIGNALING, "issignaling", BT_FN_INT_VAR, ATTR_CONST_NOTHROW_TYPEGENERIC_LEAF) DEF_LIB_BUILTIN (BUILT_IN_LABS, "labs", BT_FN_LONG_LONG, ATTR_CONST_NOTHROW_LEAF_LIST) DEF_C99_BUILTIN (BUILT_IN_LLABS, "llabs", BT_FN_LONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST) diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 1eb842e1c7b..44d30436e47 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -6330,6 +6330,7 @@ check_builtin_function_arguments (location_t loc, vec arg_loc, case BUILT_IN_ISLESSEQUAL: case BUILT_IN_ISLESSGREATER: case BUILT_IN_ISUNORDERED: + case BUILT_IN_ISEQSIG: if (builtin_function_validate_nargs (loc, fndecl, nargs, 2)) { enum tree_code code0, code1; --Apple-Mail=_AB3E4E86-9040-48A0-9A18-7834232E5255--