From patchwork Wed May 28 17:59:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 892978 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e0d:0:b0:3a4:ee3f:8f15 with SMTP id p13csp153488wrt; Wed, 28 May 2025 11:05:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUrF9C2x4CYZKB4qlGCQeU2jOxZaY287pxGVQ6qWZEt7Skm2NdEaFYtnsU33xzz2wl6rbqjWw==@linaro.org X-Google-Smtp-Source: AGHT+IFsoelreiJI3DSjl3ibZSzrFs0MnxkttDszffHWoc6+fWGJGT5bD5p3rGmAP/MnEyZi246r X-Received: by 2002:a05:6000:1a87:b0:3a4:d4cd:b0b with SMTP id ffacd0b85a97d-3a4d4cd0cd4mr13668375f8f.10.1748455544234; Wed, 28 May 2025 11:05:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1748455544; cv=pass; d=google.com; s=arc-20240605; b=gMvBSqE9i+1PcxRKdrL00haxq96bQCLhkq9Tjt0SpMka+4sbafLcHnCzdFZ5iJO8F6 M6deXRWiQaykC0fAJZzt56n7FL7kwvX7oySCayy4Pe1oeEDx76m9xOTDpkejslCvltKc XG8QLGmg+49pEYYw7CUIO5TWueI3TAgidRdQxYk5vpM0Ll0AmuIAhm2oebCXTNXFa8VM f0clehw5a7X/7EA+MWLmJS5asAFLHfGYspy3AqA5+AP34yTo/iaFonLX5ZN/QiDHHgH5 s8IETqi/OLH7B4BlmtF8fdUrS/++Xuo37SP8C4L/Q6YlOOfWh3XFqzyAdVjEksuBiP1J MlrQ== 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:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter:arc-filter:dmarc-filter:delivered-to :dkim-filter; bh=z282e9qYpgD0nMFzN6dlvQ2TjY1PRiVYctAn50c/610=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=dA1HZthuwnIeG5yd2SdInJ6kkGAtouIv4UcL85x8ek39jyzl3KIhVU5PjK3SvkuXl1 FZUaaTbnm/AGTGNmETRIR9aHuDuMDavkbMPlhWJ6xp1SJneYq7EikX6bvGPrx8kf1/Ih q0M6jBN+fhTPy55HW1ul0pX4XlyLClm0x11vIjnAU/G6H8TlUFAbl93Mu/Mg2PSdqONE VXo5oRUI9WGvNGhU8FmCXyMOP+N1YCewiyQwL4QGrhtDW6tM1MMQQg1OkrRPopu7hy+d 4jGhQMi2R1TiLRGsA0ocBmSPDnwCtJHAolYHrvJt2ZdIOIl24ids8PEHqMImeHwdcLVz BlgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=usfa81bX; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ffacd0b85a97d-3a4eacd1d5fsi1261554f8f.548.2025.05.28.11.05.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 11:05:44 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=usfa81bX; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 7A510385608A for ; Wed, 28 May 2025 18:05:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7A510385608A 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=usfa81bX X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by sourceware.org (Postfix) with ESMTPS id 4E7F7385B83D for ; Wed, 28 May 2025 18:01:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E7F7385B83D 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 4E7F7385B83D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::72e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1748455286; cv=none; b=OXO8UMHHBzMOZvl25YXePzJ6fY+5qM+9MIKA93VuSQWEaWcAWqY1QWqhFR2OlV2qw9EB3qArKSL158j45AcvYTS+fQyHmWPkWh0KBh03yykOCIKs16SpihQIyvlL+w9Kaaa5eZQVWVTAzg2kewTBIE8mi0e8hOvanzgR7euU0yY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1748455286; c=relaxed/simple; bh=sOcC+UAt+CDbEQTK/7tpnM4LAuiH0+uLavJJXMwk8zM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Jp3/HWTpoAVJgcBH0NMuus49q73sGc/UTSIg3rnwSowYi6FZCOjOrn7boqNQbcltojhD8vy9HSzf16K2zWmj858NJdFn8QbWRCJp6ClsM1EpfxSz7UPT4qk10hn66e8b6Nzi0CiLHsV6GzwDEWfSPnYQ7+OUQNJ/8yX4cbnAEUs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E7F7385B83D Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-7c597760323so956985a.3 for ; Wed, 28 May 2025 11:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1748455285; x=1749060085; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=z282e9qYpgD0nMFzN6dlvQ2TjY1PRiVYctAn50c/610=; b=usfa81bXWJ3vr83TMXwWOl8oFwIWQP1mf9TLKQXnidxMmhZHZ2xzpD4htfm8FCugXK QiXCHu/XXT76K1seEp9vXjdgOU9K6/2uBmXVwZrE62Gc5QG6n3CNIgUA9l8ZGnLCgg8F G5ThdkcgvGs1z5/z7Rm5AWtnGy6+LBrZz7UHpQ+oOmkTaPCqqrawW2G291yB1j6gIZzx RSbg+hy6Q9av8OPWlFSBEiGQI9lwRtxB4SHRKUysxgn6nKa+k0XUZ/Com5njfm/l6CpL NOjTLMmOCMA/rWJ4qgWIRWe0cLZMv+DS2CcoKu1MD1fM740WsF1GMEmjDLEWeDFA5hK7 gykw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748455285; x=1749060085; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z282e9qYpgD0nMFzN6dlvQ2TjY1PRiVYctAn50c/610=; b=xJLipCQZkQp5J8ihi6bsTsouZHj7CuXBRF+g8TKvWFbSZK38NDHCcW4ol5dlj3BCD3 AOonpZvydU2G8j7armufDmrI1DC39HBOKQOu0D0t0Q2FknhULyit/c+0jknMixDCGZQq JJC0XNXllBedx9aaB0HAin4ZqKK7ptYeQJd0e3nU1frwBCsPVgJF8D2kN8dgTIjJohTM +32gvzbwtZSLlO6YtCPKOt6R6GgeKuWSKBvuUlHo4DHSCbh3iEEAQhkBEz65u5fx139P DqblJuzu83wQeMGFrZyK7v/25EaXrxIBXG58stpFNod5sCRmdYTZntsMQL/+32c2LqnH 3OtA== X-Gm-Message-State: AOJu0Yw1Z7tlTX75vXyUTb+L3Uqdfr4QCBw9BT3sYZFQ1fjKKMyLPaVV YTKComWrSLGbajglTfg/q6A2/Iu0X3PA41PvKcNmYLruQI90UoqTTHFMzRWbJLmcaWHhpkhkUdl Sxw67 X-Gm-Gg: ASbGncsHV3U+bKHkSyeyv5AA8opszN2u0856eu6As3g7E+jM+ah1RnMWqDjvUc0V4+s KGelOIaloEtidL9nQdgIba0gpTgTUQ9ID1eq+sozE7JhKbvFaPZtHU43Cu+jSVnxnCg6bhyUnKl d4PUEy6q0cYdAw6XxSYNtL9LmAT78bRjrs52ZY5exr9tmgygmq34jp2sc5CIGM1Wa8hIIjBsVt3 d5OgHWMYSIeYAK9rUF5siiVvoExVs48IDus2y/GwjPD13OzRalpfurgSEOryTvDJt3iTEdwBJCY CQJDOwRysugwGr7wpH2boFL4kHpEH1W4PF06w4RSsSF/qNPTrcfp4jKOXsi+E4A= X-Received: by 2002:a05:6102:3f44:b0:4e2:a29d:ecb9 with SMTP id ada2fe7eead31-4e4240642ebmr15158975137.1.1748455272050; Wed, 28 May 2025 11:01:12 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:d17b:b9f7:b8cb:6df:3812]) by smtp.gmail.com with ESMTPSA id a1e0cc1a2514c-87e1f0db852sm1213969241.27.2025.05.28.11.01.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 11:01:11 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 4/7] math: Simplify and optimize modf implementation Date: Wed, 28 May 2025 14:59:30 -0300 Message-ID: <20250528180100.172042-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250528180100.172042-1-adhemerval.zanella@linaro.org> References: <20250528180100.172042-1-adhemerval.zanella@linaro.org> 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 Refactor the generic implementation to use math_config.h definitions, and add an alternative one if the ABI supports truncf instructions (gated through math-use-builtins-trunc.h). The generic implementation generates similar code on x86_64, while the optimization one for aarch64 (where truncf is supported as a builtin by through frintz), the improvements are: reciprocal-throughput master patch difference workload-0_1 3.0595 3.0698 -0.34% workload-1_maxint 5.1747 3.0542 40.98% workload-maxint_maxfloat 3.4391 3.0349 11.75% workload-integral 3.2732 3.0293 7.45% latency master patch difference workload-0_1 3.5267 4.7107 -33.57% workload-1_maxint 6.9074 4.7282 31.55% workload-maxint_maxfloat 3.7210 4.7506 -27.67% workload-integral 3.8634 4.8137 -24.60% Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/dbl-64/math_config.h | 13 ++++ sysdeps/ieee754/dbl-64/s_modf.c | 109 ++++++++++++++------------- 2 files changed, 70 insertions(+), 52 deletions(-) diff --git a/sysdeps/ieee754/dbl-64/math_config.h b/sysdeps/ieee754/dbl-64/math_config.h index 3382e385f9..10a73db95e 100644 --- a/sysdeps/ieee754/dbl-64/math_config.h +++ b/sysdeps/ieee754/dbl-64/math_config.h @@ -109,6 +109,7 @@ issignaling_inline (double x) #define BIT_WIDTH 64 #define MANTISSA_WIDTH 52 #define EXPONENT_WIDTH 11 +#define EXPONENT_BIAS 1023 #define MANTISSA_MASK UINT64_C(0x000fffffffffffff) #define EXPONENT_MASK UINT64_C(0x7ff0000000000000) #define EXP_MANT_MASK UINT64_C(0x7fffffffffffffff) @@ -121,12 +122,24 @@ is_nan (uint64_t x) return (x & EXP_MANT_MASK) > EXPONENT_MASK; } +static inline bool +is_inf (uint64_t x) +{ + return (x << 1) == (EXPONENT_MASK << 1); +} + static inline uint64_t get_mantissa (uint64_t x) { return x & MANTISSA_MASK; } +static inline int +get_exponent (uint64_t x) +{ + return (int)(x >> MANTISSA_WIDTH & 0x7ff) - EXPONENT_BIAS; +} + /* Convert integer number X, unbiased exponent EP, and sign S to double: result = X * 2^(EP+1 - exponent_bias) diff --git a/sysdeps/ieee754/dbl-64/s_modf.c b/sysdeps/ieee754/dbl-64/s_modf.c index 0de2084caf..3c5931dcc3 100644 --- a/sysdeps/ieee754/dbl-64/s_modf.c +++ b/sysdeps/ieee754/dbl-64/s_modf.c @@ -1,63 +1,68 @@ -/* - * ==================================================== - * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. - * - * Developed at SunPro, a Sun Microsystems, Inc. business. - * Permission to use, copy, modify, and distribute this - * software is freely granted, provided that this notice - * is preserved. - * ==================================================== - */ +/* Extract signed integral and fractional values. + Copyright (C) 1993-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. -/* - * modf(double x, double *iptr) - * return fraction part of x, and return x's integral part in *iptr. - * Method: - * Bit twiddling. - * - * Exception: - * No exception. - */ + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ #include -#include #include -#include - -static const double one = 1.0; +#include "math_config.h" +#include double -__modf(double x, double *iptr) +__modf (double x, double *iptr) { - int64_t i0; - int32_t j0; - EXTRACT_WORDS64(i0,x); - j0 = ((i0>>52)&0x7ff)-0x3ff; /* exponent of x */ - if(j0<52) { /* integer part in x */ - if(j0<0) { /* |x|<1 */ - /* *iptr = +-0 */ - INSERT_WORDS64(*iptr,i0&UINT64_C(0x8000000000000000)); - return x; - } else { - uint64_t i = UINT64_C(0x000fffffffffffff)>>j0; - if((i0&i)==0) { /* x is integral */ - *iptr = x; - /* return +-0 */ - INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); - return x; - } else { - INSERT_WORDS64(*iptr,i0&(~i)); - return x - *iptr; - } - } - } else { /* no fraction part */ - *iptr = x*one; - /* We must handle NaNs separately. */ - if (j0 == 0x400 && (i0 & UINT64_C(0xfffffffffffff))) - return x*one; - INSERT_WORDS64(x,i0&UINT64_C(0x8000000000000000)); /* return +-0 */ - return x; + uint64_t t = asuint64 (x); +#if USE_TRUNCF_BUILTIN + if (is_inf (t)) + { + *iptr = x; + return copysign (0.0, x); + } + *iptr = trunc (x); + return copysign (x - *iptr, x); +#else + int e = get_exponent (t); + /* No fraction part. */ + if (e < MANTISSA_WIDTH) + { + if (e < 0) + { + /* |x|<1 -> *iptr = +-0 */ + *iptr = asdouble (t & SIGN_MASK);; + return x; } + + uint64_t i = UINT64_C(0x000fffffffffffff) >> e; + if ((t & i) == 0) + { + /* x in integral, return +-0 */ + *iptr = x; + return asdouble (t & SIGN_MASK); + } + + *iptr = asdouble (t & ~i); + return x - *iptr; + } + + /* Set invalid operation for sNaN. */ + *iptr = x * 1.0; + if ((e == 0x400) && (t & UINT64_C(0xfffffffffffff))) + return *iptr; + return asdouble (t & SIGN_MASK); +#endif } #ifndef __modf libm_alias_double (__modf, modf)