From patchwork Tue May 31 21:34:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54597 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3809D3834F0D for ; Tue, 31 May 2022 21:36:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3809D3834F0D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654033012; bh=riEUSm/U+5OE6hzZrj2dGO9+y48M6lQL72Jv1YDZlQA=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Fn9Wd6vqTQlcs0FIhpeVcMJvhMfMzSa8Pjvbr8ZZgZ+1ufadeOYBL7407Htb0+dQ5 I3FQdkT//VAyYsxoF4tK72MZ1x8XLiM0rVws/cR5W4Xd+WjFilBAPTWvmz6fDYvOEZ CU1AaqgdUoD/Iicw4nBC2zMTd1xwUtmKyr6EOqIY= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oa1-x30.google.com (mail-oa1-x30.google.com [IPv6:2001:4860:4864:20::30]) by sourceware.org (Postfix) with ESMTPS id 6EDB638356AA for ; Tue, 31 May 2022 21:35:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6EDB638356AA Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-f314077115so186464fac.1 for ; Tue, 31 May 2022 14:35:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=riEUSm/U+5OE6hzZrj2dGO9+y48M6lQL72Jv1YDZlQA=; b=drNzTk3qBgojHSZjbJN+Ep5k7NTxy09TaGG40TnoHGwWQMERAdxoLS8hY2w7JWhWLz dyzENkF6BvtYHysLx+2/yiqCQx53i7tgInKaFv0hL9jLOY+y3JeNYqztpRSFdQ1gY9Sp nZUXdS2vO8MEbfXDOPAYgH3iKdvR6kb2xokH5mK7TDsTg8+ow2MUXFKpCckUeMRU55ax YoheJDbGRNDqbGSl118BAUVsrfpHGKN1+ICHkPOskui28HRMJY0KT6moSS7ZAkju1A/e PDT7ptLWUc4JFOE92uOnCgvlJ/ZcxBI+J8ZHBT8ZDG3ZxqTBnEMScfSdfgiRhs2gdjl4 gsXQ== X-Gm-Message-State: AOAM5336pm92bAKKK1bjU/DMf7N5Ur8rOwhaGl+jwtHFniUxUapMsEts 9DfmYFNU5ryNRISvphpTQ6PiZ6VaMixacA== X-Google-Smtp-Source: ABdhPJx6LcZ+HDfBIJQqfs+SLekFwGxOaBF34Xos4f49QwwdGkSxHdSC5weAzGYHvJ8GIXK6EpUilw== X-Received: by 2002:a05:6870:45a5:b0:ee:1742:1be3 with SMTP id y37-20020a05687045a500b000ee17421be3mr14409464oao.170.1654032909290; Tue, 31 May 2022 14:35:09 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id s31-20020a056830439f00b0060613c844adsm6890802otv.10.2022.05.31.14.35.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 14:35:09 -0700 (PDT) To: libc-alpha@sourceware.org, "H . J . Lu" Subject: [PATCH 2/5] i686: Use generic cosf implementation for SSE2 version Date: Tue, 31 May 2022 18:34:59 -0300 Message-Id: <20220531213502.227895-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220531213502.227895-1-adhemerval.zanella@linaro.org> References: <20220531213502.227895-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Performance seems to be similar (gcc 11.2.1 on a Ryzen 9 5900X): * s_cosf-sse2.S: "cosf": { "workload-random": { "duration": 3.74987e+09, "iterations": 9.616e+07, "reciprocal-throughput": 15.8141, "latency": 62.1782, "max-throughput": 6.32346e+07, "min-throughput": 1.60828e+07 } } * generic s_cosf.c: "cosf": { "workload-random": { "duration": 3.87298e+09, "iterations": 1.00968e+08, "reciprocal-throughput": 18.3448, "latency": 58.3722, "max-throughput": 5.45113e+07, "min-throughput": 1.71314e+07 } } Checked on i686-linux-gnu. Reviewed-by: H.J. Lu --- sysdeps/i386/i686/fpu/multiarch/Makefile | 1 + .../i386/i686/fpu/multiarch/libm-test-ulps | 4 + sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.S | 552 ------------------ sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.c | 3 + sysdeps/ieee754/flt-32/s_cosf.c | 5 + 5 files changed, 13 insertions(+), 552 deletions(-) delete mode 100644 sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.S create mode 100644 sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.c diff --git a/sysdeps/i386/i686/fpu/multiarch/Makefile b/sysdeps/i386/i686/fpu/multiarch/Makefile index c0fa9761d3..b8f52c311f 100644 --- a/sysdeps/i386/i686/fpu/multiarch/Makefile +++ b/sysdeps/i386/i686/fpu/multiarch/Makefile @@ -7,4 +7,5 @@ CFLAGS-e_expf-sse2.c = -msse2 -mfpmath=sse CFLAGS-e_log2f-sse2.c = -msse2 -mfpmath=sse CFLAGS-e_logf-sse2.c = -msse2 -mfpmath=sse CFLAGS-e_powf-sse2.c = -msse2 -mfpmath=sse +CFLAGS-s_cosf-sse2.c = -msse2 -mfpmath=sse endif diff --git a/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps b/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps index cc21e6907f..1bfad77adc 100644 --- a/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps +++ b/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps @@ -746,21 +746,25 @@ ldouble: 1 Function: "cos": double: 1 +float: 1 float128: 2 ldouble: 1 Function: "cos_downward": double: 1 +float: 1 float128: 3 ldouble: 3 Function: "cos_towardzero": double: 1 +float: 1 float128: 1 ldouble: 2 Function: "cos_upward": double: 1 +float: 1 float128: 2 ldouble: 2 diff --git a/sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.S b/sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.S deleted file mode 100644 index 28cc68dbc7..0000000000 --- a/sysdeps/i386/i686/fpu/multiarch/s_cosf-sse2.S +++ /dev/null @@ -1,552 +0,0 @@ -/* Optimized with sse2 version of cosf - Copyright (C) 2012-2022 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - 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 - -/* Short algorithm description: - * - * 1) if |x| == 0: return 1.0-|x|. - * 2) if |x| < 2^-27: return 1.0-|x|. - * 3) if |x| < 2^-5 : return 1.0+x^2*DP_COS2_0+x^5*DP_COS2_1. - * 4) if |x| < Pi/4: return 1.0+x^2*(C0+x^2*(C1+x^2*(C2+x^2*(C3+x^2*C4)))). - * 5) if |x| < 9*Pi/4: - * 5.1) Range reduction: k=trunc(|x|/(Pi/4)), j=(k+1)&0x0e, n=k+3, - * t=|x|-j*Pi/4. - * 5.2) Reconstruction: - * s = (-1.0)^((n>>2)&1) - * if(n&2 != 0) { - * using cos(t) polynomial for |t|= 2^23, very large args: - * 7.1) Range reduction: k=trunc(|x|/(Pi/4)), j=(k+1)&0xfffffffe, n=k+3, - * t=|x|-j*Pi/4. - * 7.2) Reconstruction same as (5.2). - * 8) if x is Inf, return x-x, and set errno=EDOM. - * 9) if x is NaN, return x-x. - * - * Special cases: - * cos(+-0) = 1 not raising inexact, - * cos(subnormal) raises inexact, - * cos(min_normalized) raises inexact, - * cos(normalized) raises inexact, - * cos(Inf) = NaN, raises invalid, sets errno to EDOM, - * cos(NaN) = NaN. - */ - -#ifdef PIC -# define MO1(symbol) L(symbol)##@GOTOFF(%ebx) -# define MO2(symbol,reg2,_scale) L(symbol)##@GOTOFF(%ebx,reg2,_scale) -# define CFI_PUSH(REG) cfi_adjust_cfa_offset(4); cfi_rel_offset(REG,0) -# define CFI_POP(REG) cfi_adjust_cfa_offset(-4); cfi_restore(REG) -# define PUSH(REG) pushl REG; CFI_PUSH(REG) -# define POP(REG) popl REG; CFI_POP(REG) -# define ENTRANCE PUSH(%ebx); LOAD_PIC_REG(bx) -# define RETURN POP(%ebx); ret; CFI_PUSH(%ebx) -# define ARG_X 8(%esp) -#else -# define MO1(symbol) L(symbol) -# define MO2(symbol,reg2,_scale) L(symbol)(,reg2,_scale) -# define ENTRANCE -# define RETURN ret -# define ARG_X 4(%esp) -#endif - - .text -ENTRY(__cosf_sse2) - /* Input: single precision x on stack at address ARG_X */ - - ENTRANCE - movl ARG_X, %eax /* Bits of x */ - cvtss2sd ARG_X, %xmm0 /* DP x */ - andl $0x7fffffff, %eax /* |x| */ - - cmpl $0x3f490fdb, %eax /* |x|=Pi/4 */ - movd %eax, %xmm3 /* SP |x| */ - andpd MO1(DP_ABS_MASK),%xmm0 /* DP |x| */ - movss MO1(SP_INVPIO4), %xmm2 /* SP 1/(Pi/4) */ - - cmpl $0x40e231d6, %eax /* |x|<9*Pi/4? */ - jae L(large_args) - - /* Here if Pi/4<=|x|<9*Pi/4 */ - mulss %xmm3, %xmm2 /* SP |x|/(Pi/4) */ - cvttss2si %xmm2, %eax /* k, number of Pi/4 in x */ - addl $1, %eax /* k+1 */ - movl $0x0e, %edx - andl %eax, %edx /* j = (k+1)&0x0e */ - addl $2, %eax /* n */ - subsd MO2(PIO4J,%edx,8), %xmm0 /* t = |x| - j * Pi/4 */ - -L(reconstruction): - /* Input: %eax=n, %xmm0=t */ - testl $2, %eax /* n&2 != 0? */ - jz L(sin_poly) - -/*L(cos_poly):*/ - /* Here if cos(x) calculated using cos(t) polynomial for |t|>2)&1) - * result = s * (1.0+t^2*(C0+t^2*(C1+t^2*(C2+t^2*(C3+t^2*C4))))) - */ - shrl $2, %eax /* n>>2 */ - mulsd %xmm0, %xmm0 /* y=t^2 */ - andl $1, %eax /* (n>>2)&1 */ - movaps %xmm0, %xmm1 /* y */ - mulsd %xmm0, %xmm0 /* z=t^4 */ - - movsd MO1(DP_C4), %xmm4 /* C4 */ - mulsd %xmm0, %xmm4 /* z*C4 */ - movsd MO1(DP_C3), %xmm3 /* C3 */ - mulsd %xmm0, %xmm3 /* z*C3 */ - addsd MO1(DP_C2), %xmm4 /* C2+z*C4 */ - mulsd %xmm0, %xmm4 /* z*(C2+z*C4) */ - lea -8(%esp), %esp /* Borrow 4 bytes of stack frame */ - addsd MO1(DP_C1), %xmm3 /* C1+z*C3 */ - mulsd %xmm0, %xmm3 /* z*(C1+z*C3) */ - addsd MO1(DP_C0), %xmm4 /* C0+z*(C2+z*C4) */ - mulsd %xmm1, %xmm4 /* y*(C0+z*(C2+z*C4)) */ - - addsd %xmm4, %xmm3 /* y*(C0+y*(C1+y*(C2+y*(C3+y*C4)))) */ - /* 1.0+y*(C0+y*(C1+y*(C2+y*(C3+y*C4)))) */ - addsd MO1(DP_ONES), %xmm3 - - mulsd MO2(DP_ONES,%eax,8), %xmm3 /* DP result */ - movsd %xmm3, 0(%esp) /* Move result from sse... */ - fldl 0(%esp) /* ...to FPU. */ - /* Return back 4 bytes of stack frame */ - lea 8(%esp), %esp - RETURN - - .p2align 4 -L(sin_poly): - /* Here if cos(x) calculated using sin(t) polynomial for |t|>2)&1) - * result = s * t * (1.0+t^2*(S0+t^2*(S1+t^2*(S2+t^2*(S3+t^2*S4))))) - */ - - movaps %xmm0, %xmm4 /* t */ - shrl $2, %eax /* n>>2 */ - mulsd %xmm0, %xmm0 /* y=t^2 */ - andl $1, %eax /* (n>>2)&1 */ - movaps %xmm0, %xmm1 /* y */ - mulsd %xmm0, %xmm0 /* z=t^4 */ - - movsd MO1(DP_S4), %xmm2 /* S4 */ - mulsd %xmm0, %xmm2 /* z*S4 */ - movsd MO1(DP_S3), %xmm3 /* S3 */ - mulsd %xmm0, %xmm3 /* z*S3 */ - lea -8(%esp), %esp /* Borrow 4 bytes of stack frame */ - addsd MO1(DP_S2), %xmm2 /* S2+z*S4 */ - mulsd %xmm0, %xmm2 /* z*(S2+z*S4) */ - addsd MO1(DP_S1), %xmm3 /* S1+z*S3 */ - mulsd %xmm0, %xmm3 /* z*(S1+z*S3) */ - addsd MO1(DP_S0), %xmm2 /* S0+z*(S2+z*S4) */ - mulsd %xmm1, %xmm2 /* y*(S0+z*(S2+z*S4)) */ - /* t*s, where s = sign(x) * (-1.0)^((n>>2)&1) */ - mulsd MO2(DP_ONES,%eax,8), %xmm4 - addsd %xmm2, %xmm3 /* y*(S0+y*(S1+y*(S2+y*(S3+y*S4)))) */ - /* t*s*y*(S0+y*(S1+y*(S2+y*(S3+y*S4)))) */ - mulsd %xmm4, %xmm3 - /* t*s*(1.0+y*(S0+y*(S1+y*(S2+y*(S3+y*S4)))) */ - addsd %xmm4, %xmm3 - movsd %xmm3, 0(%esp) /* Move result from sse... */ - fldl 0(%esp) /* ...to FPU. */ - /* Return back 4 bytes of stack frame */ - lea 8(%esp), %esp - RETURN - - .p2align 4 -L(large_args): - /* Here if |x|>=9*Pi/4 */ - cmpl $0x7f800000, %eax /* x is Inf or NaN? */ - jae L(arg_inf_or_nan) - - /* Here if finite |x|>=9*Pi/4 */ - cmpl $0x4b000000, %eax /* |x|<2^23? */ - jae L(very_large_args) - - /* Here if 9*Pi/4<=|x|<2^23 */ - movsd MO1(DP_INVPIO4), %xmm1 /* 1/(Pi/4) */ - mulsd %xmm0, %xmm1 /* |x|/(Pi/4) */ - cvttsd2si %xmm1, %eax /* k=trunc(|x|/(Pi/4)) */ - addl $1, %eax /* k+1 */ - movl %eax, %edx - andl $0xfffffffe, %edx /* j=(k+1)&0xfffffffe */ - cvtsi2sdl %edx, %xmm4 /* DP j */ - movsd MO1(DP_PIO4HI), %xmm2 /* -PIO4HI = high part of -Pi/4 */ - mulsd %xmm4, %xmm2 /* -j*PIO4HI */ - movsd MO1(DP_PIO4LO), %xmm3 /* -PIO4LO = low part of -Pi/4 */ - addsd %xmm2, %xmm0 /* |x| - j*PIO4HI */ - addl $2, %eax /* n */ - mulsd %xmm3, %xmm4 /* j*PIO4LO */ - addsd %xmm4, %xmm0 /* t = |x| - j*PIO4HI - j*PIO4LO */ - jmp L(reconstruction) - - .p2align 4 -L(very_large_args): - /* Here if finite |x|>=2^23 */ - - /* bitpos = (ix>>23) - BIAS_32 + 59; */ - shrl $23, %eax /* eb = biased exponent of x */ - /* bitpos = eb - 0x7f + 59, where 0x7f is exponent bias */ - subl $68, %eax - movl $28, %ecx /* %cl=28 */ - movl %eax, %edx /* bitpos copy */ - - /* j = bitpos/28; */ - div %cl /* j in register %al=%ax/%cl */ - movapd %xmm0, %xmm3 /* |x| */ - /* clear unneeded remainder from %ah */ - andl $0xff, %eax - - imull $28, %eax, %ecx /* j*28 */ - movsd MO1(DP_HI_MASK), %xmm4 /* DP_HI_MASK */ - movapd %xmm0, %xmm5 /* |x| */ - mulsd -2*8+MO2(_FPI,%eax,8), %xmm3 /* tmp3 = FPI[j-2]*|x| */ - movapd %xmm0, %xmm1 /* |x| */ - mulsd -1*8+MO2(_FPI,%eax,8), %xmm5 /* tmp2 = FPI[j-1]*|x| */ - mulsd 0*8+MO2(_FPI,%eax,8), %xmm0 /* tmp0 = FPI[j]*|x| */ - addl $19, %ecx /* j*28+19 */ - mulsd 1*8+MO2(_FPI,%eax,8), %xmm1 /* tmp1 = FPI[j+1]*|x| */ - cmpl %ecx, %edx /* bitpos>=j*28+19? */ - jl L(very_large_skip1) - - /* Here if bitpos>=j*28+19 */ - andpd %xmm3, %xmm4 /* HI(tmp3) */ - subsd %xmm4, %xmm3 /* tmp3 = tmp3 - HI(tmp3) */ -L(very_large_skip1): - - movsd MO1(DP_2POW52), %xmm6 - movapd %xmm5, %xmm2 /* tmp2 copy */ - addsd %xmm3, %xmm5 /* tmp5 = tmp3 + tmp2 */ - movl $1, %edx - addsd %xmm5, %xmm6 /* tmp6 = tmp5 + 2^52 */ - movsd 8+MO1(DP_2POW52), %xmm4 - movd %xmm6, %eax /* k = I64_LO(tmp6); */ - addsd %xmm6, %xmm4 /* tmp4 = tmp6 - 2^52 */ - comisd %xmm5, %xmm4 /* tmp4 > tmp5? */ - jbe L(very_large_skip2) - - /* Here if tmp4 > tmp5 */ - subl $1, %eax /* k-- */ - addsd 8+MO1(DP_ONES), %xmm4 /* tmp4 -= 1.0 */ -L(very_large_skip2): - - andl %eax, %edx /* k&1 */ - subsd %xmm4, %xmm3 /* tmp3 -= tmp4 */ - addsd MO2(DP_ZERONE,%edx,8), %xmm3 /* t = DP_ZERONE[k&1] + tmp3 */ - addsd %xmm2, %xmm3 /* t += tmp2 */ - addsd %xmm3, %xmm0 /* t += tmp0 */ - addl $3, %eax /* n=k+3 */ - addsd %xmm1, %xmm0 /* t += tmp1 */ - mulsd MO1(DP_PIO4), %xmm0 /* t *= PI04 */ - - jmp L(reconstruction) /* end of very_large_args peth */ - - .p2align 4 -L(arg_less_pio4): - /* Here if |x| diff --git a/sysdeps/ieee754/flt-32/s_cosf.c b/sysdeps/ieee754/flt-32/s_cosf.c index ee9fa8565b..1236244244 100644 --- a/sysdeps/ieee754/flt-32/s_cosf.c +++ b/sysdeps/ieee754/flt-32/s_cosf.c @@ -23,6 +23,10 @@ #include "math_config.h" #include "s_sincosf.h" +#ifndef SECTION +# define SECTION +#endif + #ifndef COSF # define COSF_FUNC __cosf #else @@ -35,6 +39,7 @@ arithmetic. */ float +SECTION COSF_FUNC (float y) { double x = y;