Patchwork [RFC,2/2] aarch64: Add optimized ASIMD version of cosf

login
register
mail settings
Submitter Ashwin Sekhar T K
Date June 13, 2017, 7:17 a.m.
Message ID <20170613071707.43396-3-ashwin.sekhar@caviumnetworks.com>
Download mbox | patch
Permalink /patch/20979/
State New
Headers show

Comments

Ashwin Sekhar T K - June 13, 2017, 7:17 a.m.
This patch adds the optimized ASIMD version of cosf for Aarch64.
The algorithm and code flow is based on the SSE version of cosf
implementation for x86_64 in sysdeps/x86_64/fpu/s_cosf.S.

        * sysdeps/aarch64/fpu/multiarch/Makefile: Add s_cosf-asimd.
        * sysdeps/aarch64/fpu/multiarch/s_cosf.c: New file.
        * sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S: Likewise.
---
 sysdeps/aarch64/fpu/multiarch/Makefile       |   2 +-
 sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S | 367 +++++++++++++++++++++++++++
 sysdeps/aarch64/fpu/multiarch/s_cosf.c       |  31 +++
 3 files changed, 399 insertions(+), 1 deletion(-)
 create mode 100644 sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S
 create mode 100644 sysdeps/aarch64/fpu/multiarch/s_cosf.c

Patch

diff --git a/sysdeps/aarch64/fpu/multiarch/Makefile b/sysdeps/aarch64/fpu/multiarch/Makefile
index 2092e9a885..3711b71805 100644
--- a/sysdeps/aarch64/fpu/multiarch/Makefile
+++ b/sysdeps/aarch64/fpu/multiarch/Makefile
@@ -1,3 +1,3 @@ 
 ifeq ($(subdir),math)
-libm-sysdep_routines += s_sinf-asimd
+libm-sysdep_routines += s_sinf-asimd s_cosf-asimd
 endif
diff --git a/sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S b/sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S
new file mode 100644
index 0000000000..d052cfad7c
--- /dev/null
+++ b/sysdeps/aarch64/fpu/multiarch/s_cosf-asimd.S
@@ -0,0 +1,367 @@ 
+/* Optimized ASIMD version of cosf
+   Copyright (C) 2017 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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#define __need_Emath
+#include <bits/errno.h>
+
+/* 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|<Pi/4, result is
+ *              s     * (1.0+t^2*(C0+t^2*(C1+t^2*(C2+t^2*(C3+t^2*C4))))).
+ *          } else {
+ *              using sin(t) polynomial for |t|<Pi/4, result is
+ *              s * t * (1.0+t^2*(S0+t^2*(S1+t^2*(S2+t^2*(S3+t^2*S4))))).
+ *          }
+ *  6) if |x| < 2^23, large args:
+ *      6.1) Range reduction: k=trunc(|x|/(Pi/4)), j=(k+1)&0xfffffffe, n=k+3,
+ *           t=|x|-j*Pi/4.
+ *      6.2) Reconstruction same as (5.2).
+ *  7) if |x| >= 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.
+ */
+
+sp_x		.req	s0			/* SP x */
+dp_x		.req	d1			/* DP x */
+sp_abs_x	.req	s2			/* SP |x| */
+dp_abs_x	.req	d3			/* DP |x| */
+t_val		.req	d4			/* DP t */
+dp_pio4		.req	d5			/* DP Pi/4 */
+dp_zero		.req	d6			/* DP 0.0 */
+dp_one		.req	d7			/* DP 1.0 */
+sp_one		.req	s7			/* SP 1.0 */
+
+bits_abs_x	.req	w0			/* Bits of SP |x| */
+n_val		.req	w1			/* n */
+bits_x		.req	w2			/* Bits of SP x */
+
+ENTRY_ALIGN(__cosf_asimd, 6)
+	/* Input: single precision x in s0 */
+	ldr	w9, L(SP_PIO4)			/* Pi/4 */
+	fmov	dp_one, #1.0			/* DP 1.0 */
+	fmov	bits_x, sp_x			/* Bits of x */
+	ubfx	bits_abs_x, bits_x, #0, #31	/* Bits of |x| */
+	cmp	bits_abs_x, w9			/* |x|<Pi/4?  */
+	fmov	sp_abs_x, bits_abs_x		/* SP |x| */
+	blt	L(arg_less_pio4)
+
+	/* Here if |x|>=Pi/4 */
+	ldr	w10, L(SP_9PIO4)		/* 9*Pi/4 */
+	cmp	bits_abs_x, w10			/* |x|>9*Pi/4?  */
+	bge	L(large_args)
+
+	/* Here if Pi/4<=|x|<9*Pi/4 */
+	ldr	s16, L(SP_INVPIO4)		/* SP 1/(Pi/4) */
+	fcvt	dp_abs_x, sp_abs_x		/* DP |x| */
+	ldr	dp_pio4, L(DP_PIO4)
+	fmul	s16, sp_abs_x, s16		/* SP |x|/(Pi/4) */
+	fcvtzu	w12, s16			/* k=trunc(|x|/(Pi/4)) */
+	add	w12, w12, #1			/* k+1*/
+	and	w12, w12, #0x0e			/* j=(k+1)&0x0e */
+	add	n_val, w12, #2			/* n=k+3 */
+	ucvtf	d16, w12			/* DP j */
+	fmsub	t_val, d16, dp_pio4, dp_abs_x	/* t=|x|-j*Pi/4 */
+
+	.p2align	3
+L(reconstruction):
+	/* Input: w1=n, d4=t */
+	tst	n_val, #2			/* n&2? */
+	adr	x14, L(DP_C)			/* Cos Poly Coefficients */
+	adr	x15, L(DP_S)			/* Sin Poly Coefficients */
+	fcsel	d17, t_val, dp_one, EQ		/* q=t or 1.0 */
+	lsr	w9, n_val, #2			/* (n>>2) */
+	lsl	x9, x9, #63			/* sign_cos=(n>>2)<<63 */
+	fmov	d20, x9				/* sign_cos */
+	csel	x14, x15, x14, EQ		/* K=Sin or Cos Coefficients */
+	eor	v17.8b, v17.8b, v20.8b		/* r=sign_cos XOR (1.0 or t) */
+
+	.p2align	3
+L(sin_cos_poly):
+	/*
+	 * Here if cos(x) is evalutaed by sin(t)/cos(t) polynomial for |t|<Pi/4:
+	 * y = t*t; z = y*y;
+	 * s = (-1.0)^((n>>2)&1)
+	 * result = r * (1.0+t^2*(K0+t^2*(K1+t^2*(K2+t^2*(K3+t^2*K4)))))
+	 * where r=s*t, Kx=Sx (Sin Polynomial Coefficients) if n&2==0
+	 *	 r=s,   Kx=Cx (Cos Polynomial Coefficients) otherwise
+	 */
+	fmul	d18, t_val, t_val		/* y=t^2 */
+	fmul	d19, d18, d18			/* z=t^4 */
+	ldr	d21, [x14, #0*8]		/* K0 */
+	ldp	d22, d23, [x14, #1*8]		/* K1,K2 */
+	ldp	d24, d25, [x14, #3*8]		/* K3,K4 */
+	fmadd	d23, d25, d19, d23		/* K2+z*K4 */
+	fmadd	d22, d24, d19, d22		/* K1+z*K3 */
+	fmadd	d21, d23, d19, d21		/* K0+z*(K2+z*K4) */
+	fmul	d22, d22, d19			/* z*(K1+z*K3) */
+	/* y*(K0+y*(K1+y*(K2+y*(K3+y*K4)))) */
+	fmadd	d22, d21, d18, d22
+	fmadd	d22, d22, d17, d17		/* DP result */
+	fcvt	s0, d22				/* SP result */
+	ret
+
+	.p2align	3
+L(large_args):
+	/* Here if |x|>=9*Pi/4 */
+	mov	w8, #0x7f8			/* InfNan>>20 */
+	cmp	bits_abs_x, w8, LSL #20		/* x is Inf or NaN?  */
+	bge	L(arg_inf_or_nan)
+
+	/* Here if finite |x|>=9*Pi/4 */
+	fcvt	dp_abs_x, sp_abs_x		/* DP |x| */
+	mov	w11, #0x4b0			/* 2^23>>20 */
+	cmp	bits_abs_x, w11, LSL #20	/* |x|>=2^23?  */
+	bge	L(very_large_args)
+
+	/* Here if 9*Pi/4<=|x|<2^23 */
+	adr	x14, L(DP_PIO4HILO)
+	ldr	d16, L(DP_INVPIO4)		/* 1/(Pi/4) */
+	ldp	d17, d18, [x14] 		/* -PIO4HI,-PIO4LO */
+	fmadd	d16, dp_abs_x, d16, dp_one	/* |x|/(Pi/4)+1.0 */
+	fcvtzu	w10, d16			/* k+1=trunc(|x|/(Pi/4)+1.0) */
+	and	w10, w10, #0xfffffffe		/* j=(k+1)&0xfffffffe */
+	add	n_val, w10, #2			/* n=k+3 */
+	ucvtf	d16, w10			/* DP j */
+	fmadd	d17, d16, d17, dp_abs_x		/* |x|-j*PIO4HI */
+	fmadd	t_val, d16, d18, d17		/* t=|x|-j*PIO4HI-j*PIO4LO */
+	b	L(reconstruction)
+
+L(very_large_args):
+	/* Here if finite |x|>=2^23 */
+	movz	x11, #0x4330, LSL #48		/* 2^52 */
+	fmov	d21, x11			/* DP 2^52 */
+	ldr	dp_pio4, L(DP_PIO4)		/* Pi/4 */
+	fmov	dp_zero, xzr			/* 0.0 */
+	adr	x14, L(_FPI)
+	lsr	w8, bits_abs_x, #23		/* eb = biased exponent of x */
+	add	w8, w8, #-0x7f+59		/* bitpos=eb-BIAS_32+59 */
+	mov	w9, #28				/* =28 */
+	udiv	w10, w8, w9			/* j=bitpos/28 */
+	mov	x11, #0xffffffff00000000	/* DP_HI_MASK */
+	add	x14, x14, x10, LSL #3
+	ldr	d16, [x14, #-2*8]		/* FPI[j-2] */
+	ldr	d17, [x14, #-1*8]		/* FPI[j-1] */
+	ldr	q18, [x14]			/* FPI[j+1]|FPI[j] */
+	mul	w10, w10, w9			/* j*28 */
+	add	w10, w10, #19			/* j*28+19 */
+	fmov	d20, x11			/* DP_HI_MASK */
+	fmul	d16, dp_abs_x, d16		/* tmp3 */
+	fmul	d17, dp_abs_x, d17		/* tmp2 */
+	fmul	v18.2d, v18.2d, v3.d[0]		/* tmp1|tmp0 */
+	cmp	w8, w10				/* bitpos>=j*28+19?  */
+	and	v20.8b, v16.8b, v20.8b		/* HI(tmp3) */
+	fcsel	d20, dp_zero, d20, LT		/* d=0.0 OR HI(tmp3) */
+	fsub	d16, d16, d20			/* tmp3=tmp3-d */
+	fadd	d22, d16, d17			/* tmp5=tmp3+tmp2 */
+	fadd	d20, d22, d21			/* tmp6=tmp5+2^52 */
+	fsub	d21, d20, d21			/* tmp4=tmp6-2^52 */
+	faddp	d18, v18.2d			/* tmp7=tmp0+tmp1 */
+	fmov	w10, s20			/* k=I64_LO(tmp6) */
+	fcmp	d21, d22			/* tmp4>tmp5?  */
+	cset	w9, GT				/* c=1 or 0 */
+	fcsel	d22, dp_one, dp_zero, GT	/* d=1.0 or 0.0 */
+	sub	w10, w10, w9			/* k-=c */
+	fsub	d21, d21, d22			/* tmp4-=d */
+	and	w11, w10, #1			/* k&1 */
+	ucvtf	d20, w11			/* DP k&1 */
+	fsub	d16, d16, d21			/* tmp3-=tmp4 */
+	fmsub	d20, d20, dp_one, d16		/* t=-1.0*[k&1]+tmp3 */
+	fadd	d20, d20, d17			/* t+=tmp2 */
+	add	n_val, w10, #3			/* n=k+3 */
+	fadd	d20, d20, d18			/* t+=tmp7 */
+	fmul	t_val, d20, dp_pio4		/* t*=PI/4 */
+	b	L(reconstruction)
+
+	.p2align	3
+L(arg_less_pio4):
+	/* Here if |x|<Pi/4 */
+	fcvt	dp_x, sp_x			/* DP x */
+	mov	w10, #0x3d0			/* 2^-5>>20 */
+	cmp	bits_abs_x, w10, LSL #20	/* |x|<2^-5? */
+	blt	L(arg_less_2pn5)
+
+	/* Here if 2^-5<=|x|<Pi/4 */
+	fmov	t_val, dp_x			/* DP t=x */
+	adr	x14, L(DP_C)			/* Kx=Cx */
+	fmov	d17, dp_one			/* r=1.0 */
+	b	L(sin_cos_poly)
+
+L(arg_less_2pn5):
+	/* Here if |x|<2^-5 */
+	mov	w11, #0x320			/* 2^-27>>20 */
+	cmp	bits_abs_x, w11, LSL #20	/* |x|<2^-27? */
+	blt	L(arg_less_2pn27)
+
+	/* Here if 2^-27<=|x|<2^-5 */
+	adr	x14, L(DP_COS2)
+	ldp	d16, d17, [x14]			/* DP COS2_0,COS2_1 */
+	fmul	d18, dp_x, dp_x			/* y=x^2 */
+	fmadd	d16, d17, d18, d16		/* DP COS2_0+x^2*COS2_1 */
+	fmadd	d16, d16, d18, dp_one		/* DP 1+x^2*COS2_0+x^4*COS2_1 */
+	fcvt	s0, d16				/* SP result */
+	ret
+
+L(arg_less_2pn27):
+	/* Here if |x|<2^-27 */
+	fmov	sp_one, #1.0			/* 1.0 */
+	fsub	s0, sp_one, sp_abs_x		/* result is 1.0-|x| */
+	ret
+
+	.p2align	3
+L(arg_inf_or_nan):
+	/* Here if |x| is Inf or NAN */
+	bne	L(skip_errno_setting)		/* in case of x is NaN */
+
+	/* Here if x is Inf. Set errno to EDOM.  */
+	adrp	x14, :gottprel: errno
+	ldr	PTR_REG(14), [x14, #:gottprel_lo12:errno]
+	mrs	x15, tpidr_el0
+	mov	w8, #EDOM			/* EDOM */
+	str	w8, [x15, x14]			/* Store EDOM in errno */
+
+L(skip_errno_setting):
+	/* Here if |x| is Inf or NAN. Continued.  */
+	fsub	s0, sp_x, sp_x			/* Result is NaN */
+	ret
+
+END(__cosf_asimd)
+
+	.section .rodata, "a"
+	.p2align 3
+L(_FPI): /* 4/Pi broken into sum of positive DP values */
+	.long	0x00000000,0x00000000
+	.long	0x6c000000,0x3ff45f30
+	.long	0x2a000000,0x3e3c9c88
+	.long	0xa8000000,0x3c54fe13
+	.long	0xd0000000,0x3aaf47d4
+	.long	0x6c000000,0x38fbb81b
+	.long	0xe0000000,0x3714acc9
+	.long	0x7c000000,0x3560e410
+	.long	0x56000000,0x33bca2c7
+	.long	0xac000000,0x31fbd778
+	.long	0xe0000000,0x300b7246
+	.long	0xe8000000,0x2e5d2126
+	.long	0x48000000,0x2c970032
+	.long	0xe8000000,0x2ad77504
+	.long	0xe0000000,0x290921cf
+	.long	0xb0000000,0x274deb1c
+	.long	0xe0000000,0x25829a73
+	.long	0xbe000000,0x23fd1046
+	.long	0x10000000,0x2224baed
+	.long	0x8e000000,0x20709d33
+	.long	0x80000000,0x1e535a2f
+	.long	0x64000000,0x1cef904e
+	.long	0x30000000,0x1b0d6398
+	.long	0x24000000,0x1964ce7d
+	.long	0x16000000,0x17b908bf
+	.type L(_FPI), @object
+	ASM_SIZE_DIRECTIVE(L(_FPI))
+
+/* Coefficients of polynomial
+ for cos(x)~=1.0+x^2*DP_COS2_0+x^4*DP_COS2_1, |x|<2^-5.  */
+	.p2align 3
+L(DP_COS2):
+	.long	0xff5cc6fd,0xbfdfffff	/* DP_COS2_0 */
+	.long	0xb178dac5,0x3fa55514	/* DP_COS2_1 */
+	.type L(DP_COS2), @object
+	ASM_SIZE_DIRECTIVE(L(DP_COS2))
+
+/* Coefficients of polynomial
+   for sin(t)~=t+t^3*(S0+t^2*(S1+t^2*(S2+t^2*(S3+t^2*S4)))), |t|<Pi/4.  */
+	.p2align 3
+L(DP_S):
+	.long	0x55551cd9,0xbfc55555		/* S0 */
+	.long	0x10c2688b,0x3f811111		/* S1 */
+	.long	0x8b4bd1f9,0xbf2a019f		/* S2 */
+	.long	0x64e6b5b4,0x3ec71d72		/* S3 */
+	.long	0x1674b58a,0xbe5a947e		/* S4 */
+	.type L(DP_S), @object
+	ASM_SIZE_DIRECTIVE(L(DP_S))
+
+	.p2align 3
+/* Coefficients of polynomial
+   for cos(t)~=1.0+t^2*(C0+t^2*(C1+t^2*(C2+t^2*(C3+t^2*C4)))), |t|<Pi/4.  */
+L(DP_C):
+	.long	0xfffe98ae,0xbfdfffff		/* C0 */
+	.long	0x545c50c7,0x3fa55555		/* C1 */
+	.long	0x348b6874,0xbf56c16b		/* C2 */
+	.long	0x9ac43cc0,0x3efa00eb		/* C3 */
+	.long	0xdd8844d7,0xbe923c97		/* C4 */
+	.type L(DP_C4), @object
+	ASM_SIZE_DIRECTIVE(L(DP_C))
+
+	.p2align 3
+L(DP_PIO4):
+	.long	0x54442d18,0x3fe921fb		/* Pi/4 */
+	.type L(DP_PIO4), @object
+	ASM_SIZE_DIRECTIVE(L(DP_PIO4))
+
+	.p2align 3
+L(DP_INVPIO4):
+	.long	0x6dc9c883,0x3ff45f30		/* 4/Pi */
+	.type L(DP_INVPIO4), @object
+	ASM_SIZE_DIRECTIVE(L(DP_INVPIO4))
+
+	.p2align 3
+L(DP_PIO4HILO):
+	.long	0x54000000,0xbfe921fb		/* High part of Pi/4 */
+	.long	0x11A62633,0xbe010b46		/* Low part of Pi/4 */
+	.type L(DP_PIO4HILO), @object
+	ASM_SIZE_DIRECTIVE(L(DP_PIO4HILO))
+
+	.p2align 2
+L(SP_PIO4):
+	.long	0x3f490fdb			/* Pi/4 */
+	.type L(SP_PIO4), @object
+	ASM_SIZE_DIRECTIVE(L(SP_PIO4))
+
+	.p2align 2
+L(SP_9PIO4):
+	.long	0x40e231d6			/* 9*Pi/4 */
+	.type L(SP_9PIO4), @object
+	ASM_SIZE_DIRECTIVE(L(SP_9PIO4))
+
+	.p2align 2
+L(SP_INVPIO4):
+	.long	0x3fa2f983			/* 4/Pi */
+	.type L(SP_INVPIO4), @object
+	ASM_SIZE_DIRECTIVE(L(SP_INVPIO4))
+
+weak_alias (__cosf, cosf)
diff --git a/sysdeps/aarch64/fpu/multiarch/s_cosf.c b/sysdeps/aarch64/fpu/multiarch/s_cosf.c
new file mode 100644
index 0000000000..05fb61d278
--- /dev/null
+++ b/sysdeps/aarch64/fpu/multiarch/s_cosf.c
@@ -0,0 +1,31 @@ 
+/* Multiple versions of cosf. AARCH64 Version.
+   Copyright (C) 2017 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
+   <http://www.gnu.org/licenses/>.  */
+
+#include <init-arch.h>
+#include <sys/auxv.h>
+
+extern float __cosf_asimd (float);
+extern float __cosf_aarch64 (float);
+float __cosf (float);
+
+libm_ifunc (__cosf,
+	    (GLRO(dl_hwcap) & HWCAP_ASIMD) ? __cosf_asimd : __cosf_aarch64);
+weak_alias (__cosf, cosf);
+
+#define COSF __cosf_aarch64
+#include <sysdeps/ieee754/flt-32/s_cosf.c>