From patchwork Wed May 28 17:59:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 892975 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e0d:0:b0:3a4:ee3f:8f15 with SMTP id p13csp151492wrt; Wed, 28 May 2025 11:02:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWWSPOVw47lq+LF7FW2Flx/WBv3RJ7AaFn5FL+vGYMoqjPs+LPbH6NxHIxyDfAhvSXzBNfgNw==@linaro.org X-Google-Smtp-Source: AGHT+IEYLrm2R5jx5XWBoVG8RCmK93e565eqhhi2PbHToPrd7RIPSFY5tmxIA7QnHjiAcMY4r/q+ X-Received: by 2002:a05:6000:250c:b0:3a4:cfbf:51ae with SMTP id ffacd0b85a97d-3a4cfbf5352mr14174442f8f.4.1748455343352; Wed, 28 May 2025 11:02:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1748455343; cv=pass; d=google.com; s=arc-20240605; b=DXuisoIOCgYbAHE3Gt1C6j4oqiOP6BcCgDjKAjE+vec6qfx8QvRny6aeMj/dFdndC9 U1Gy7ZpRP3MtLj5EaYyTDyKbl00ciNvp2L7e559vXVzCR6/2D8KbKeKS89JMX/oIr+Jz 6sJdcMVhNOD5TxmgpPQGvi8Xhig2yFmWYfWTPrTaYxkoJvxUgWKuFEaqTdszDqJ2mOLH woTh/E0+1ySliVMK+sGFSTXnCT5XLQfq/PFoOgEPl2VFU29ur6+jU2bH4jZtuSg5RoM1 89dvXwx5NuK4kFVFKBp92PGMuXRQsO2VOqT2Sko8UgXEpX5Gyo9fGXOZdfGc47Yb3Yyr hrQA== 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=jx/NPfkDqEiiE7Nqvgn9oE6wboyzfsMLM5ptazllWu0=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=dFueUXIW47MnYxQwEBe/gFlJTJ1cA6MfZH7ZUdLWSLcUAEx0FcJEIwvs34HI0boGXz Gh889AzKqM2j1zRZc4Ar3jLm44WfK0CE+W25uC7E3LcMYtoyKZIMQO7grtVJl0zvXbFk a6A0TwCeit8g3E3yfHLdvM91bIC4hPlGRJhmAqixQuWk0deRLsvEOK4WhppoyD2d4dwS K58VKFmgNzpkahtAY4Ig+yh6QSh95VHu0ekFL2C40qNJEKrfiwFgs6pZLXHTLnWET3Jp uE8c96ZOxDdktq+xJqGmDUtgpaTCSLPGeA2LTAtEEBns26/zgonC7hx/d2uYfu48/M2C HtpQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VkDhuJBk; 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-3a4eacdd6e5si1219373f8f.768.2025.05.28.11.02.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 11:02:23 -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=VkDhuJBk; 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 8674E385B530 for ; Wed, 28 May 2025 18:02:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8674E385B530 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=VkDhuJBk X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by sourceware.org (Postfix) with ESMTPS id 6A079385B52F for ; Wed, 28 May 2025 18:01:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6A079385B52F 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 6A079385B52F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::729 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1748455283; cv=none; b=TcfLzhp2ZzJwZmfripE1rNaYMPtIQfEV4dlOPvwbS3t/nwNz4bhRz48RwHc4PpmW6txwbZ+9QzH6hf7jV29D3Eg+0+mN0O9OI5Nuz0quCQ5KJEl9irUYqPAZ7fC+OEmmaaLytPMJabaWfUziwpQGpb235uf6XVWVpDA2ckp3QJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1748455283; c=relaxed/simple; bh=4/af+osZxW2R7lHrw+lGtuZv9AO9Omci6/PWPlddNEk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kffLer6yaRxlGs0gs7OiOgubadM+armtIpY5JAXg82KK5Eq0fNIxUn8YAeXv5ICrISD1F8VxR7dbAspchpUWs25sdK4Of/ea4f1xrxLs1wmPq8QWMZpIggkJdVTMUGFdOd0Tf/6I9njWi7YEzT7jeNb7xmL4jS7X/1h7vHA09kA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A079385B52F Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-7c5e39d1db2so801085a.3 for ; Wed, 28 May 2025 11:01:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1748455282; x=1749060082; 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=jx/NPfkDqEiiE7Nqvgn9oE6wboyzfsMLM5ptazllWu0=; b=VkDhuJBk8L97h/mGDHyexncDEIIXZzsmgtnoz5pVKTSFnB4lWH2H6/F3/C9Uv+7vac YAWRLH5jaQLKRmOhCElsNjGKshsts5+aQGo0ieFJxBVOMwAsFFz0lv2utESsaETROsp6 nWLqvAnY1pJxbclP/SG4/FUUpYaTzltYinHH0ut/9g8lB9/Sznl25XXnp4JHH5OQ7ibb 4LWaiBmxUkSGJDACsUFlV0NBSq3wkLDgQxEunG8AWuPE2t7FyTnbTDXFkJ3kW67PXPJW JuEzVpWlpaz3tUOiFrBBjH7M/HvWGq5k+RjSFtr484PKvTXOyS8uCw4wR7MnGoqflrMr oeow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748455282; x=1749060082; 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=jx/NPfkDqEiiE7Nqvgn9oE6wboyzfsMLM5ptazllWu0=; b=TGeZgfKEW46TzaZg6bp+c/jVjobrSjWpTuMhFX74vfQE2pVl+C1eVXCzl0sNiB+IS9 TsAtQ4PGikLSARy9KkbXfdqEo7eRZ4DKaA0kkM1E0q0M086BTWyaTPTnmyg02VkXEA3S aMxV/++8rcT5hQhg/C7X6PyevttOeLb1oSxx2Ra39JeKxMFK8GxnxqWSgshn3vFxjDrM CKJBnYHrQzj9R7KVBEUN+wYn/bXfUteyf3pmJ2W6/oHDBHeHCRTfB46DHn/F+tPo6EXO n1TkYRyEKKHocXYer+2ESNjk1TrWHRRPT8ZjC5lcLpVmRC3c0l8zKNi374ujREcLBcyp kQ2w== X-Gm-Message-State: AOJu0Yw13fmBxTfAp7vrPpKZew4isZoBCgAV8ZsizcoBfQ0RjK3+VtV9 8OgS+ju6uhOe4cqxROryF/hJvsDJW0+k9zEeappbkNE9aFHNpUvzvBdc2lgenV/B8UznPhtVUrz nKgVC X-Gm-Gg: ASbGncuhWXt9UpY5Mp+oHKHqkfNZWr8/9zBA8pnRqbtosp5SwGQNOVDZBFuzfdtr3/0 H65unFVnIq+Vj0wAGGNCvpBTGE6XaUo1ICmc4iySQsGYbyJSHjy2Dex1hscTpDvYjhXO9gRclfp DAb7FMrxaIqCXJ7nUjbFgtEcbrsQIyMehuGzUAFmzLuXOV2Oed3BGNSrVdmKGWbpT1oJl1RhwwT Sdod9N3MyWXcgXAfQajQfR1lcAS6DGnV0UWpsZSvUl0UeFlmlaNV+Mgk8NaxGa8yMlMiGjEV+qz yOB2z5i4Ss7C+/4CtkiEckbeKUejO7T62uo+wH7XpS29p71DvNPaeFzHDsv7JnE= X-Received: by 2002:a05:6102:54a2:b0:4e2:872a:857b with SMTP id ada2fe7eead31-4e42409c7d8mr14970155137.7.1748455270503; Wed, 28 May 2025 11:01:10 -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.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 May 2025 11:01:10 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 3/7] math: Simplify and optimize modff implementation Date: Wed, 28 May 2025 14:59:29 -0300 Message-ID: <20250528180100.172042-4-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 for x86_64, while the optimization path aarch64 (where truncf is supported as a builtin) through frintz), the improvements are: reciprocal-throughput master patch difference workload-0_1 3.0740 3.0326 1.35% workload-1_maxint 5.2231 3.0436 41.73% workload-maxint_maxfloat 4.0962 3.0551 25.42% workload-integral 3.7093 3.0612 17.47% latency master patch difference workload-0_1 3.5521 4.7313 -33.20% workload-1_maxint 6.7148 4.7314 29.54% workload-maxint_maxfloat 4.0458 4.7518 -17.45% workload-integral 3.9719 4.7427 -19.40% Checked on aarch64-linux-gnu and x86_64-linux-gnu. --- sysdeps/ieee754/flt-32/math_config.h | 13 ++++ sysdeps/ieee754/flt-32/s_modff.c | 101 +++++++++++++++------------ 2 files changed, 70 insertions(+), 44 deletions(-) diff --git a/sysdeps/ieee754/flt-32/math_config.h b/sysdeps/ieee754/flt-32/math_config.h index 8d9c8ee3ad..e3e49835ce 100644 --- a/sysdeps/ieee754/flt-32/math_config.h +++ b/sysdeps/ieee754/flt-32/math_config.h @@ -165,6 +165,7 @@ issignalingf_inline (float x) #define BIT_WIDTH 32 #define MANTISSA_WIDTH 23 #define EXPONENT_WIDTH 8 +#define EXPONENT_BIAS 127 #define MANTISSA_MASK 0x007fffff #define EXPONENT_MASK 0x7f800000 #define EXP_MANT_MASK 0x7fffffff @@ -177,12 +178,24 @@ is_nan (uint32_t x) return (x & EXP_MANT_MASK) > EXPONENT_MASK; } +static inline bool +is_inf (uint32_t x) +{ + return (x << 1) == (EXPONENT_MASK << 1); +} + static inline uint32_t get_mantissa (uint32_t x) { return x & MANTISSA_MASK; } +static inline int +get_exponent (uint32_t x) +{ + return (int)(x >> MANTISSA_WIDTH & 0xff) - 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/flt-32/s_modff.c b/sysdeps/ieee754/flt-32/s_modff.c index ad2e91de25..6eb7fac989 100644 --- a/sysdeps/ieee754/flt-32/s_modff.c +++ b/sysdeps/ieee754/flt-32/s_modff.c @@ -1,54 +1,67 @@ -/* s_modff.c -- float version of s_modf.c. - */ +/* Extract signed integral and fractional values. + Copyright (C) 1993-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. -/* - * ==================================================== - * 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. - * ==================================================== - */ + 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 - -static const float one = 1.0; +#include "math_config.h" +#include float -__modff(float x, float *iptr) +__modff (float x, float *iptr) { - int32_t i0,j0; - uint32_t i; - GET_FLOAT_WORD(i0,x); - j0 = ((i0>>23)&0xff)-0x7f; /* exponent of x */ - if(__builtin_expect(j0<23, 1)) { /* integer part in x */ - if(j0<0) { /* |x|<1 */ - SET_FLOAT_WORD(*iptr,i0&0x80000000); /* *iptr = +-0 */ - return x; - } else { - i = (0x007fffff)>>j0; - if((i0&i)==0) { /* x is integral */ - uint32_t ix; - *iptr = x; - GET_FLOAT_WORD(ix,x); - SET_FLOAT_WORD(x,ix&0x80000000); /* return +-0 */ - return x; - } else { - SET_FLOAT_WORD(*iptr,i0&(~i)); - return x - *iptr; - } - } - } else { /* no fraction part */ - *iptr = x*one; - /* We must handle NaNs separately. */ - if (j0 == 0x80 && (i0 & 0x7fffff)) - return x*one; - SET_FLOAT_WORD(x,i0&0x80000000); /* return +-0 */ - return x; + uint32_t t = asuint (x); +#if USE_TRUNCF_BUILTIN + if (is_inf (t)) + { + *iptr = x; + return copysign (0.0, x); + } + *iptr = truncf (x); + return copysignf (x - *iptr, x); +#else + int e = get_exponent (t); + /* No fraction part. */ + if (e < MANTISSA_WIDTH) + { + if (e < 0) + { + /* |x|<1 -> *iptr = +-0 */ + *iptr = asfloat (t & SIGN_MASK);; + return x; } + + uint32_t i = 0x007fffff >> e; + if ((t & i) == 0) + { + /* x in integral, return +-0 */ + *iptr = x; + return asfloat (t & SIGN_MASK); + } + + *iptr = asfloat (t & ~i); + return x - *iptr; + } + + /* Set invalid operation for sNaN. */ + *iptr = x * 1.0f; + if ((e == 0x80) && (t & 0x7fffffu)) + return *iptr; + return asfloat (t & SIGN_MASK); +#endif } libm_alias_float (__modf, modf)