From patchwork Mon Nov 25 12:29:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 845314 Delivered-To: patch@linaro.org Received: by 2002:a5d:688e:0:b0:382:43a8:7b94 with SMTP id h14csp1145273wru; Mon, 25 Nov 2024 04:30:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXAePYVRG7fl1g7YZPMTR5HxmYrpmxZ85UJ4yhBsKF351XuprCMnEfKiYyPQAuV6I3wGFIymQ==@linaro.org X-Google-Smtp-Source: AGHT+IFOMDqqhcZnZHheB/AJTeIlEGN7Ps0DO6lSaXcFNLRYR9/8cu//WTwcZNLrrWpqTGpFhUEw X-Received: by 2002:ac8:5984:0:b0:461:66e0:b676 with SMTP id d75a77b69052e-4653d580696mr190597201cf.13.1732537811404; Mon, 25 Nov 2024 04:30:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1732537811; cv=pass; d=google.com; s=arc-20240605; b=M+jr6E4om1PXP9j4fEmOJh6lbOgEq5z1zBjLY7Kr2Pia+2rMemD702av2l+W+ppPfN uQRw3zC/9EN9B8YQGa+0M8mLhx6J+3iP5NvmrYkqssdxUMPgEsgVElWoJNg0fpQfUrI3 Lx6Ll2iLsF0/M8x2wheQtsHLX83B9kVncOw8NjrbQX7JKY7N91gjXGePYDEFQpeIWoMm 1Ts/sEgbSRKaVEEkQCCpPdT5OdPn97UIi4czRLQ/85Ecnrlz1F5tXrp3FKP5Ju0nutci MO+MK5ItgvnlaDblyxDqIRQUNSFdnXHKTZ8phLC8p4wjkVg/FRi8NDWOAHtiGYQ3K6w4 g+DA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=aS8xjTnpsaVhdj+xTO7kFagdI52uyJs2AYcCD6IYS5k=; fh=zd4tYbwZdwnG/VrktjGKvNuUyaF8eE93MU2o8TRFN0c=; b=DbebOPyeA/csZgtzWCIG1WAvNo7jIzsfUOcINKgdl8XwsaQXbawBpMLN4SeD+4qI2q Ly6TP2toIYtc9T6sKyeql2MSc/t88MhRX4595W/Fnvpe9NlMasIGkGDGpOGbREbEfJM3 2B2aMrc30nfBJmi2Vk1XBTTQu5TguD2xmba6Jgg8+oN1NA13UPnCR7Y02Rzi5XCdyL3N f4ZLmOZFDlmGBfx2gRHqgBSLOZ1FvLLUdW8M1YgV3qfV7qnkzdX9iYwo8GXwlgv9G1Iz 3wiLNH4W+Hx8o7vnY1BlSHqub8oR1cNuu5RqTmDLsIWegIdQ3xHUvAkrZTbprU8ZO/OX A0+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bn9Bq56h; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d75a77b69052e-4669a357b19si10492041cf.372.2024.11.25.04.30.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Nov 2024 04:30:11 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bn9Bq56h; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBA533858D37 for ; Mon, 25 Nov 2024 12:30:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBA533858D37 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=bn9Bq56h X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id B0D643858D35 for ; Mon, 25 Nov 2024 12:29:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B0D643858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B0D643858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732537786; cv=none; b=q88OBfGaF93yghyPSiH4e0HK2Kp8lcvhcSs3bGOefC1PZGK2TjITCJAhIXARqd+mQK5xIyvl3DPxKqrFqID2sP22bV3SqXRRK/kS7dDvRegg8UoxZTqy8rVPp8A0GyQLZXYrEqetjVNXNBkankWjaHm5hTFFmh1DzNSG4XwnuQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732537786; c=relaxed/simple; bh=lx/RxUvAgjzUbIqJ4kA/1EPN7+0XzCDOhGy51zgj3CY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wszRXFsWzov4JooTyHm4K9TuWYQ4MU4Yo36lt7o3shYxA1vLlHkMgiQCHp/5d1Q9++/ELJlLWcOviy4E5wGxHRn6Ely94fElOV0yh6qJZ6QPZle4x51GVpp98xU10cR+3oFuT+3LlSeJ1Tx1F+DJ6NgAQQDcgBQaQJKhmfGxspY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B0D643858D35 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-211fd6a0a9cso31252085ad.3 for ; Mon, 25 Nov 2024 04:29:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1732537784; x=1733142584; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=aS8xjTnpsaVhdj+xTO7kFagdI52uyJs2AYcCD6IYS5k=; b=bn9Bq56hzC5tflpPLLJiOdhHrMig2TWF8vY0of4RI5655rOhjWt3M8a57hbDrhFWbY 2kurn4OQ7pDG2eoGWIvQHEGpnxB9NF3ZwHOI4zuF4eeu33loxVXlN1T4lHHNWQdVj/xu GX71RDpDvlx0hPJpmsqRjI5L1ZAErVTW4UFuYd6l6uGVumy5sZeJUQFV1OGzQKzuhfSF Q+shYJ2AJ9oTwy06qTZR+hn9TvTGYIhCoydoSWn5vEBmvXK+LgtoBk3IKZ0i04Ks73ZG NnpkYz5rgjFVXRtP0yQn6kYdLyRRlyku76qe9IyhkN/rX9rk3Qg3pgDTCBxqkth9yIYD v02w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732537784; x=1733142584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=aS8xjTnpsaVhdj+xTO7kFagdI52uyJs2AYcCD6IYS5k=; b=crs2IUEV16PwuTGURw3yku07gEX6naNopWtKwlXr9Y/xNIe13JXfkCaV1594fG1jTk K+iQLgUHWq7oI7Koexi+rlxy7TEHzGvGi8AMlDklV6ALHPlSWJghbAiy9YcBk4PVB7oJ DKIiGIcu61fSa8bGhHYt+6GXoRzH2OAyPhAQlalppBAHwUCa+tz15/qX2Ifv0rLls7uB AmRYgOemDuaK6XM6M/XRpp5IG6EeO8V+twK/kOTwjIPPGWZBm7SgJo+4sc1FG7QXeSQI qJRVgYBKNkVDIoYk+aLnRhFkdq1IMiZn6LYLSCaEpLnkY1IWVcz72cmC5UVkSRklieWE K0LA== X-Gm-Message-State: AOJu0YwiQM9zPFrOiihtHe/Og9A6j/SpgWRvUJotxcw8Qps0JHD0WRw2 CY4zhahEls+EwaICGNX0vm/ViBY6FaxEMPg5qlM9/USfj+NhP8cpBwxbuXbieluaVik0bmCQhX8 jbE+0dA== X-Gm-Gg: ASbGncvh4jT5oJ9uSz7jB7FWHm9EyrcYy+443LrWu8C/j0NlPbKZA9MtMcNn0B7a8T1 QmK2zsQZOY2pqyp0C5k2BqA7GiVOKwcctJOxrdUzMAEC2p+ZJN3AnbKx3f+JC3b4LfP5/P/NAk5 Fen2YPyanTHcPJz6NTu37w/u1NgT9fPDa3uTnmMKtDCmUSLVEoRu93M8azWrCChFgngts+qhieI N84fqbhV1O6NDh1kbwf9/GuvDLN0DIwlXbMgKwvyKEVX5a/0zKZrftGKN1Mnw== X-Received: by 2002:a17:902:e5cf:b0:20c:b810:13a5 with SMTP id d9443c01a7336-2129f22bc67mr166220275ad.21.1732537784381; Mon, 25 Nov 2024 04:29:44 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:68c8:538:9fa2:5db1:cdb5]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2129dbf9d2asm61644405ad.126.2024.11.25.04.29.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Nov 2024 04:29:44 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Vincent Lefevre Subject: [COMMITTED] math: Fix non-portability in the computation of signgam in lgammaf Date: Mon, 25 Nov 2024 09:29:34 -0300 Message-ID: <20241125122934.463747-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patch=linaro.org@sourceware.org From: Vincent Lefevre The k>>31 in signgam = 1 - (((k&(k>>31))&1)<<1); is not portable: * The ISO C standard says "If E1 has a signed type and a negative value, the resulting value is implementation-defined." (this is still in C23). * If the int type is larger than 32 bits (e.g. a 64-bit type), then k = INT_MAX; line 144 will make k>>31 put 1 in bit 0 (thus signgam will be -1) while 0 is expected. Moreover, instead of the fx >= 0x1p31f condition, testing fx >= 0 is probably better for 2 reasons: The signgam expression has more or less a condition on the sign of fx (the goal of k>>31, which can be dropped with this new condition). Since fx ≥ 0 should be the most common case, one can get signgam directly in this case (value 1). And this simplifies the expression for the other case (fx < 0). This new condition may be easier/faster to test on the processor (e.g. by avoiding a load of a constant from the memory). This is commit d41459c731865516318f813cf4c966dafa0eecbf from CORE-MATH. Checked on x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/e_lgammaf_r.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sysdeps/ieee754/flt-32/e_lgammaf_r.c b/sysdeps/ieee754/flt-32/e_lgammaf_r.c index cb65513056..447376dc55 100644 --- a/sysdeps/ieee754/flt-32/e_lgammaf_r.c +++ b/sysdeps/ieee754/flt-32/e_lgammaf_r.c @@ -181,12 +181,11 @@ __ieee754_lgammaf_r (float x, int *signgamp) Note that for a binary32 |x| >= 2^23, x is necessarily an integer, and we already dealed with negative integers, thus now: -2^23 < x < +Inf and x is not a negative integer nor 0, 1, 2. */ - int k; - if (__builtin_expect (fx >= 0x1p31f, 0)) - k = INT_MAX; + if (__glibc_unlikely (fx >= 0)) + *signgamp = 1; else - k = fx; - *signgamp = 1 - (((k & (k >> 31)) & 1) << 1); + /* gamma(x) is negative in (-2n-1,-2n), thus when fx is odd. */ + *signgamp = 1 - ((((int) fx) & 1) << 1); double z = ax, f; if (__glibc_unlikely (ax < 0x1.52p-1f))