alphalinux.org high-perf list (1998-2001)
 help / color / mirror / Atom feed
* fast sin/cos/tan routine
@ 1999-04-24 12:17 Kazushige Goto
  0 siblings, 0 replies; only message in thread
From: Kazushige Goto @ 1999-04-24 12:17 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 20806 bytes --]

Hi,

There are improved sin/cos/tan/cotan routine.

It's about 10% faster than libffm-0.28's version and good for random

values(such as +-+-...).  But I've not made sure enough whether it

works.

And dgemm runs about 10MFlops faster than yesterday's version(about

855 to 865MFlops, 21164A, 600MHz, size is 1000x1000 to 3000x3000).

I'll put at

 ftp://www.netstat.ne.jp/pub/Linux/Linux-Alpha-JP/BLAS

Thanks,

  goto@statabo.rim.or.jp

/*

   libffm	- Free, pretty fast replacement for some math (libm) routines 

                        on Linux/AXP, optimized for the 21164

   Copyright (C) 1998  Joachim Wesner <joachim.wesner@frankfurt.netsurf.de>

                  and  Kazushige Goto <goto@statabo.rim.or.jp>

   This library is free software; you can redistribute it and/or

   modify it under the terms of the GNU Library General Public

   License as published by the Free Software Foundation; either

   version 2 of the License, or (at your option) any later version.

   This 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

   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public

   License along with this library (see file COPYING.LIB); if not, write 

   to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 

   MA 02139, USA.

*/

/*

   Fast sin/cos approximation including range reduction by Joachim Wesner,

   <joachim.wesner@frankfurt.netsurf.de>, see also the work published in 

   mc 8/1991 p. 78-93. Done in June 1998.

*/

/*

  improved and modified by Kazushige Goto <goto@statabo.rim.or.jp>

  note:

   This program's target is Alpha 21164.

   It takes only 88 clocks at most in any parameters.

   But this reduction routine can not accept NaN, Inf or extra

   Huge values.  To get faster code(to fit 21164), I modified

   last routine.

  originally,

  x = (((((((C[0]*yy + C[1])*yy + C[2])*yy + C[3])*yy +

          C[4])*yy + C[5])*yy + C[6])*yy + C[7])*x;

  I modified to

  r1 = K[8] * yy;  r1 = r1 + K[12]; z1 = yy * yy;

  r2 = K[9] * yy;  r2 = r2 + K[13]; z2 = z1 * z1;

  r3 = K[10] * yy; r3 = r3 + K[14]; z3 = z2 * z1;

  r4 = K[11] * yy; r4 = r4 + K[15];

  r1 = z3 * r1;  r2 = z2 * r2;  r3 = z1 * r3;

  r1 = r1 + r2;  r1 = r1 + r3;  r1 = r1 + r4;

  x = r1 * x;

  This routine has slightly error.  My aim is to make routine as fast

  as I can.  If you worry about such error, please use glibc's generic

  sin/cos routine.  But its error is only 1-2 bit of LSB. Most people

  need not worry about.  This technique is provided by

  Naohiko Shimizu <nshimizu@et.u-tokai.ac.jp>).

  Also, I programmed to speed up for first access.  Different from

  hardware function routine(i.e. intel x86, Mips),  we must take

  into instruction and data movement from main memory.  At a first

  access,  alpha waits very-very long time(about 1000 to 10000 clocks,

  it depends on the coding).  This is not owing to me, the users must

  take consideration. You must try to call continuously.

 sin(+-Inf), cos(+-Inf) = NaN

 sin(NaN) = NaN

*/

/*

   further modified to use a higher order approximation by Joachim Wesner

   (joachim.wesner@frankfurt.netsurf.de).

*/

/*

    entirely revised by Kazushige Goto <goto@statabo.rim.or.jp>

*/

        .set noreorder

        .set noat

#ifdef __ELF__

        .section .rodata

#else

        .rdata

#endif

        .align 5

$K:

        .t_floating  1.34930377e10

        .t_floating  3.18309886183790691216e-1

        .t_floating  0.5000000000000000000

        .t_floating  3.1415920257568359375

        .t_floating  6.2783283283351920545101165771e-7

        .t_floating  1.2446744343793226784516935342e-13

        /* Derived from Hart & Cheney, "Computer Approximations" */

        /* Approximation #3345, rescaled,  18.6 digits precision */

        /*   old # 3344 only had Precision 15.9 digits */

        .t_floating  8.33333333333316687e-03

        .t_floating  2.75573192101527649e-06

        .t_floating  1.60589364903715968e-10

        .t_floating  2.72047909578888632e-15

        .t_floating -1.66666666666666665e-01

        .t_floating -1.98412698412018475e-04

        .t_floating -2.50521067982745961e-08

        .t_floating -7.64291780689105074e-13

        .t_floating  1.00000000000000000e+00

.text

        .align 5

        .globl sin

        .ent sin

sin:

        lda	$30, -16($30)

        ldgp	$29,.-sin($27)

        stt	$f16,  0($30)

#ifdef PROF

        lda	$28, _mcount

        jsr	$28, ($28), _mcount

        unop

        unop

#endif

        .prologue 1

        lda	$28,  $K

        lda	$6, 0x7ff

        lda	$8, 0x420

        sll	$6, 52, $6

        ldq	$5, 0($30)

        sll	$8, 52, $8

        ldt	$f1,    0($28)		# K[ 0]

        ldt	$f17,   8($28)		# K[ 1]

        fabs	$f16, $f0

        and	$5, $6, $7

        unop

        beq	$7, $SinSubNormal

        cmplt	$8, $7, $0

        bne	$0, $Inf_and_NaN

# Now we can calculate.

        mult	$f16, $f17, $f10	# z = x * K[1]

        ldt	$f19,  24($28)		# K[ 3]

        ldt	$f20,  32($28)		# K[ 4]

        ldt	$f21,  40($28)		# K[ 5]

        ldt	$f22,  48($28)		# K[ 6]

        ldt	$f23,  56($28)		# K[ 7]

        ldt	$f24,  64($28)		# K[ 8]

        ldt	$f25,  72($28)		# K[ 9]

        cmptlt  $f1, $f0, $f1		# |arg| >= max. |arg| ?

        cvttq	$f10, $f11		# float -> quad

        mult	$f16, $f16, $f10	# yy = x * x

        ldt	$f26,  80($28)		# K[10]

        ldt	$f27,  88($28)		# K[11]

        ldt	$f28,  96($28)		# K[12]

        unop

        stt	$f11,   8($30)

        cvtqt	$f11, $f12		# quad  -> float

        fbeq	$f11,  $Common

        .align 4

        mult	$f19, $f12, $f19	# z * K[3]

        mult	$f20, $f12, $f20	# z * K[4]

        mult	$f21, $f12, $f21	# z * K[5]

        subt	$f16, $f19, $f16

        subt	$f16, $f20, $f16

        subt	$f16, $f21, $f16

        mult	$f16, $f16, $f10	# yy = x * x

        br	$Common

        .end sin

        .align 5

        .globl cos

        .ent cos

cos:

        lda	$30, -16($30)

        ldgp	$29,.-cos($27)

        stt	$f16,  0($30)

#ifdef PROF

        lda	$28, _mcount

        jsr	$28, ($28), _mcount

        unop

        unop

#endif

        .prologue 1

        fabs	$f16, $f0

        lda	$28,  $K

        unop

        lda	$6, 0x7ff

        cpysn	$f31, $f16, $f16

        lda	$8, 0x420

        unop

        sll	$6, 52, $6

        ldq	$5, 0($30)

        sll	$8, 52, $8

        ldt	$f1,    0($28)		# K[ 0]

        ldt	$f17,   8($28)		# K[ 1]

        and	$5, $6, $7

        beq	$7, $CosSubNormal

        cmplt	$8, $7, $0

        bne	$0, $Inf_and_NaN

# Now we can calculate.

        mult	$f16, $f17, $f10	# z = x * K[1]

        ldt	$f18,  16($28)		# K[ 2]

        cmptlt  $f1, $f0, $f1		# |arg| >= max. |arg| ?

        ldt	$f19,  24($28)		# K[ 3]

        ldt	$f20,  32($28)		# K[ 4]

        ldt	$f21,  40($28)		# K[ 5]

        ldt	$f22,  48($28)		# K[ 6]

        ldt	$f23,  56($28)		# K[ 7]

        ldt	$f24,  64($28)		# K[ 8]

        ldt	$f25,  72($28)		# K[ 9]

        cvttq/c	$f10, $f11		# float -> quad

        ldt	$f26,  80($28)		# K[10]

        ldt	$f27,  88($28)		# K[11]

        ldt	$f28,  96($28)		# K[12]

        stt	$f11,   8($30)

        cvtqt	$f11, $f12		# quad  -> float

        subt	$f12, $f18, $f12

        mult	$f19, $f12, $f19	# z * K[3]

        mult	$f20, $f12, $f20	# z * K[4]

        mult	$f21, $f12, $f21	# z * K[5]

        subt	$f16, $f19, $f16

        subt	$f16, $f20, $f16

        subt	$f16, $f21, $f16

        mult	$f16, $f16, $f10	# yy = x * x

        .align 4

$Common:

        mult	$f10, $f10, $f12	# z1 = yy * yy

        ldt	$f29, 104($28)		# K[13]

        ldt	$f30, 112($28)		# K[14]

        ldq	$1,   8($30)

        mult	$f12, $f10, $f13	# z2 = z1 * yy

        mult	$f22, $f10, $f22	# r4 = K[6] * yy

        mult	$f23, $f10, $f23	# r3 = K[7] * yy

        mult	$f24, $f10, $f24	# r2 = K[8] * yy

        mult	$f13, $f12, $f14	# z3 = z2 * z1

        mult	$f25, $f10, $f25	# r1 = K[9] * yy

        addt	$f22, $f26, $f22	# r4 += K[10]

        addt	$f23, $f27, $f23	# r3 += K[11]

        mult	$f14, $f12, $f15	# z4 = z3 * z1

        addt	$f24, $f28, $f24	# r2 += K[12]

        addt	$f25, $f29, $f25	# r1 += K[13]

        mult	$f10, $f22, $f22	# r4 = yy * r4

        mult	$f13, $f23, $f23	# r3 = z2 * r3

        and	$1, 1, $1

        stq	$1,   8($30)

        fneg	$f16, $f10

        mult	$f14, $f24, $f24	# r2 = z3 * r2

        fbne	$f1, $Inf_and_NaN

        mult	$f15, $f25, $f25	# r1 = z4 * r1

        addt	$f22, $f30, $f22	# r4 += K[14]

        ldt	$f1,  8($30)

        addt	$f25, $f24, $f25	# r1 = r1 + r2

        fcmovne	$f1,  $f10, $f16

        addt	$f25, $f23, $f25	# r1 = r1 + r3

        addt	$f25, $f22, $f25	# r1 = r1 + r4

        mult	$f25, $f16, $f0

        lda	$30, 16($30)

        ret

        .align 4

$SinSubNormal:

        fmov	$f16, $f0

        lda	$30, 16($30)

        ret

        .align 4

$CosSubNormal:

        ldt	$f0, 112($28)

        lda	$30, 16($30)

        ret

        .align 4

$Inf_and_NaN:

        lda	$0, -1

        stq	$0,   0($30)

        ldt	$f0,  0($30)

        lda	$30, 16($30)

        ret

        .end cos

/*

   libffm	- Free, pretty fast replacement for some math (libm) routines 

                        on Linux/AXP, optimized for the 21164

   Copyright (C) 1998  Joachim Wesner <joachim.wesner@frankfurt.netsurf.de>

                  and  Kazushige Goto <goto@statabo.rim.or.jp>

   This library is free software; you can redistribute it and/or

   modify it under the terms of the GNU Library General Public

   License as published by the Free Software Foundation; either

   version 2 of the License, or (at your option) any later version.

   This 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

   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public

   License along with this library (see file COPYING.LIB); if not, write 

   to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 

   MA 02139, USA.

*/

/*

   Fast tan/cotan approximation including range reduction by Joachim Wesner,

   <joachim.wesner@frankfurt.netsurf.de>, see also mc 8/1991 p. 78-93.

   Done in June/July 1998.

*/

/* improved and modified by Kazushige Goto <goto@statabo.rim.or.jp> */

/*

   now including illegal/denormal argument handling by Kazushige Goto and

   Joachim Wesner (Nov 1998).

*/

/*

    entirely revised by Kazushige Goto <goto@statabo.rim.or.jp>

*/

        .set noat

        .set noreorder

#ifdef __ELF__

        .section .rodata

#else

        .rdata

#endif

        .align 5

$K:

        .t_floating  6.746518852e9			 # 2**32*pi/2

        .t_floating  6.36619772367581382433e-1		 # 2./M_PI

        .t_floating  1.57079601287841796875e0

        .t_floating  3.13916416416759602726e-7

        .t_floating  6.22337217189661338378e-14

        .t_floating -3.5852891647471419219e+001

        .t_floating  5.1513699203375195243e+004

        .t_floating -2.6773388479715725433e+005

        .t_floating -6.2589095005702739694e+002

        .t_floating  6.8746022970978239588e+003

        .t_floating -9.3681685518878501841e+005

        .t_floating  2.0072489111748832058e+006

.text

        .align 5

        .globl	tan

        .ent	tan

tan:

        lda	$30, -32($30)

        ldgp	$29,.-tan($27)

        stt	$f16,  0($30)

#ifdef PROF

        lda	$28, _mcount

        jsr	$28, ($28), _mcount

        unop

        unop

#endif

        .prologue 1	

        lda	$28,  $K

        lda	$6, 0x7ff

        lda	$8, 0x41f

        sll	$6, 52, $6

        ldq	$5, 0($30)

        sll	$8, 52, $8

        ldt	$f18,   0($28)		# K[0]

        ldt	$f19,   8($28)		# K[1]

        fabs	$f16, $f0

        and	$5, $6, $7

        unop

        beq	$7, $TanSubNormal

        cmplt	$8, $7, $0

        bne	$0, $Inf_and_NaN

# Now we can calculate.

        mult	$f16, $f19, $f19	# z = x * K[1]

        ldt	$f20,  16($28)		# K[2]

        ldt	$f21,  24($28)		# K[3]

        ldt	$f22,  32($28)		# K[4]

        ldt	$f23,  40($28)		# K[5]

        ldt	$f24,  48($28)		# K[6]

        ldt	$f25,  56($28)		# K[7]

        cmptlt  $f18, $f0, $f18		# |arg| >= max. |arg| ?

        ldt	$f26,  64($28)		# K[8]

        mult	$f16, $f16, $f12	# y  = x * x

        cvttq	$f19, $f17		# float -> quad

        ldt	$f27,  72($28)		# K[9]

        ldt	$f28,  80($28)		# K[10]

        cvtqt	$f17, $f10		# quad  -> float

        fbeq	$f17, $SkipReduction

        mult	$f10,  $f20, $f20	# z * K[2]

        mult	$f10,  $f21, $f21	# z * K[3]

        mult	$f10,  $f22, $f22	# z * K[4]

        subt	$f16, $f20, $f16

        subt	$f16, $f21, $f16

        subt	$f16, $f22, $f16

        mult	$f16, $f16, $f12	# y  = x * x

        .align 4

$SkipReduction:

        mult	$f12, $f12, $f13	# y1 = y * y

        stt	$f17,   8($30)

        ldt	$f29,  88($28)		# K[11]

        mult	$f23, $f12, $f23	# r1 = K[5] * y

        mult	$f24, $f12, $f24	# r4 = K[6] * y

        fneg	$f16, $f10

        mult	$f25, $f12, $f25	# r2 = K[7] * y

        ldq	$0, 8($30)

        mult	$f13, $f12, $f14	# y2 = y1 * y

        addt	$f12, $f26, $f26	# r3 = y  + K[8]

        addt	$f23, $f27, $f23	# r1 = r1 + K[9]

        and	$0, 1, $0

        addt	$f24, $f28, $f24	# r4 = r4 + K[10]

        stq	$0, 16($30)

        addt	$f25, $f29, $f25	# r2 = r2 + K[11]

        mult	$f26, $f14, $f26	# r3 = r3 * y2

        mult	$f24, $f12, $f24	# r4 = r4 * y

        ldt	$f11, 16($30)

        mult	$f23, $f13, $f23	# r1 = r1 * y1

        fbne	$f18, $Inf_and_NaN

        fcmovne	$f11, $f10, $f16

        lda	$30, 32($30)

        addt	$f26, $f24, $f26	# r3 = r3 + r4

        addt	$f23, $f25, $f23	# r1 = r1 + r2

        addt	$f26, $f29, $f26	# r3 = r3 + K[12]

        unop

        mult	$f23, $f16, $f23	# r1 = r1 * x

        beq	$0, $Skip

        divt	$f26, $f23, $f0

        ret

$Skip:

        divt	$f23, $f26, $f0

        ret

        .align 4

$TanSubNormal:

        fmov	$f16, $f0

        lda	$30, 32($30)

        ret

        .end tan

        .align 5

        .globl	cotan

        .ent	cotan

cotan:

        lda	$30, -32($30)

        ldgp	$29,.-cotan($27)

        stt	$f16,  0($30)

#ifdef PROF

        lda	$28, _mcount

        jsr	$28, ($28), _mcount

        unop

        unop

#endif

        .prologue 1	

        lda	$28,  $K

        lda	$6, 0x7ff

        lda	$8, 0x41f

        sll	$6, 52, $6

        ldq	$5, 0($30)

        sll	$8, 52, $8

        ldt	$f18,   0($28)		# K[0]

        ldt	$f19,   8($28)		# K[1]

        fabs	$f16, $f0

        and	$5, $6, $7

        unop

        beq	$7, $CotanSubNormal

        cmplt	$8, $7, $0

        bne	$0, $Inf_and_NaN

# Now we can calculate.

        mult	$f16, $f19, $f19	# z = x * K[1]

        ldt	$f20,  16($28)		# K[2]

        ldt	$f21,  24($28)		# K[3]

        ldt	$f22,  32($28)		# K[4]

        ldt	$f23,  40($28)		# K[5]

        ldt	$f24,  48($28)		# K[6]

        ldt	$f25,  56($28)		# K[7]

        cmptlt  $f18, $f0, $f18		# |arg| >= max. |arg| ?

        ldt	$f26,  64($28)		# K[8]

        mult	$f16, $f16, $f12	# y  = x * x

        cvttq	$f19, $f17		# float -> quad

        ldt	$f27,  72($28)		# K[9]

        ldt	$f28,  80($28)		# K[10]

        cvtqt	$f17, $f10		# quad  -> float

        fbeq	$f17, $CotanSkipReduction

        mult	$f10,  $f20, $f20	# z * K[2]

        mult	$f10,  $f21, $f21	# z * K[3]

        mult	$f10,  $f22, $f22	# z * K[4]

        subt	$f16, $f20, $f16

        subt	$f16, $f21, $f16

        subt	$f16, $f22, $f16

        mult	$f16, $f16, $f12	# y  = x * x

        .align 4

$CotanSkipReduction:

        mult	$f12, $f12, $f13	# y1 = y * y

        stt	$f17,   8($30)

        ldt	$f29,  88($28)		# K[11]

        mult	$f23, $f12, $f23	# r1 = K[5] * y

        mult	$f24, $f12, $f24	# r4 = K[6] * y

        fneg	$f16, $f10

        mult	$f25, $f12, $f25	# r2 = K[7] * y

        ldq	$0, 8($30)

        mult	$f13, $f12, $f14	# y2 = y1 * y

        addt	$f12, $f26, $f26	# r3 = y  + K[8]

        addt	$f23, $f27, $f23	# r1 = r1 + K[9]

        and	$0, 1, $0

        addt	$f24, $f28, $f24	# r4 = r4 + K[10]

        stq	$0, 16($30)

        addt	$f25, $f29, $f25	# r2 = r2 + K[11]

        mult	$f26, $f14, $f26	# r3 = r3 * y2

        mult	$f24, $f12, $f24	# r4 = r4 * y

        ldt	$f11, 16($30)

        mult	$f23, $f13, $f23	# r1 = r1 * y1

        fbne	$f18, $Inf_and_NaN

        fcmovne	$f11, $f10, $f16

        lda	$30, 32($30)

        addt	$f26, $f24, $f26	# r3 = r3 + r4

        addt	$f23, $f25, $f23	# r1 = r1 + r2

        addt	$f26, $f29, $f26	# r3 = r3 + K[12]

        unop

        mult	$f23, $f16, $f23	# r1 = r1 * x

        beq	$0, $CotanSkip

        divt	$f23, $f26, $f0

        ret

$CotanSkip:

        divt	$f26, $f23, $f0

        ret

        .align 4

$CotanSubNormal:

        stq	$6,  0($30)

        ldt	$f0, 0($30)

        bge	$2,$Non_Negative

        fneg	$f0, $f0

$Non_Negative:

        lda	$30, 32($30)

        ret

        .align 4

$Inf_and_NaN:

        lda	$0, -1

        stq	$0,   0($30)

        ldt	$f0,  0($30)

        lda	$30, 32($30)

        ret

        .end cotan

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1999-04-24 12:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-04-24 12:17 fast sin/cos/tan routine Kazushige Goto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox