* Re: sqrt
1999-02-27 8:46 ` sqrt Kazushige Goto
@ 1999-02-28 5:14 ` Kazushige Goto
0 siblings, 0 replies; 6+ messages in thread
From: Kazushige Goto @ 1999-02-28 5:14 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 8857 bytes --]
Hi,
goto> From: Robert Harley <Robert.Harley@inria.fr>
goto> Subject: sqrt
goto>
goto> Robert> The total time seems to be 101 or 103 cycles or thereabouts. Anyone
goto> Robert> want to knock of a few cycles to get under the 100 barrier?
goto>
goto> I attached sample sqrt routine which I made before. This sqrt is
goto> heavy table versions(8kB), but it runs only 66 clocks(libffm's is 82
goto> clocks). It's possible to archive about 90 clocks evenif last bit
goto> correction instruction is added.
I modified Mr. Robert's routine, but I can improve only few
clocks(original:105clocks, improved:101clocks). I think it's fatal
to use "mulq" instruction, it takes 12 clocks. I recommend to use
glibc's technique(I attached modified and glibc's routine).
Thanks,
goto@statabo.rim.or.jp
/* > sqrt.S
* Purpose: Fast double precision square root in Alpha ASM.
* Copyright: (c) Robert J. Harley, 19-Oct-1998
* Contact: Robert.Harley@inria.fr
* Legalese: This code is subject to the GNU General Public License (v2).
*/
/* Integer registers. */
#define ea $1
#define ta $2
#define wm $3
#define wp $4
#define pt $5
#define ux $6
#define ua $7
#define pc $8
#define v $22
#define v2 $23
#define tmp $24
#define exp $25
#define ra $26
#define pv $27
#define at $28
#define gp $29
#define sp $30
#define zero $31
/* Floating-point registers. */
#define dX $f0
#define d0_5 $f10
#define d1_5 $f11
#define dB $f12
#define dT $f13
#define dZ $f14
#define dTmp $f15
#define dA $f16
#define dZero $f31
/* Delay one integer slot (E0 or E1). */
#define NOP bis $31, $31, $31
/* Delay one floating-point slot (FA or FM). */
#define FNOP cpys $f31, $f31, $f31
/* No delay, for alignment (invalidated at slotting). */
/* #define UNOP ldq_u $31, 0($30) */
.set noreorder
.section .rodata
.align 5
consts:
.s_floating 1.5
.s_floating 0.5
.t_floating 0.70710678118654757273731092936941422522068023681640625
#define BLA(n) .long n<<16 | 126<<23
.type tab, @object
.size tab, 256
tab:
BLA(127)
BLA(125)
BLA(123)
BLA(121)
BLA(119)
BLA(118)
BLA(116)
BLA(114)
BLA(113)
BLA(111)
BLA(109)
BLA(108)
BLA(106)
BLA(105)
BLA(103)
BLA(102)
BLA(100)
BLA(99)
BLA(97)
BLA(96)
BLA(95)
BLA(93)
BLA(92)
BLA(91)
BLA(90)
BLA(88)
BLA(87)
BLA(86)
BLA(85)
BLA(84)
BLA(83)
BLA(82)
BLA(80)
BLA(79)
BLA(78)
BLA(77)
BLA(76)
BLA(75)
BLA(74)
BLA(73)
BLA(72)
BLA(71)
BLA(70)
BLA(70)
BLA(69)
BLA(68)
BLA(67)
BLA(66)
BLA(65)
BLA(64)
BLA(63)
BLA(63)
BLA(62)
BLA(61)
BLA(60)
BLA(59)
BLA(59)
BLA(58)
BLA(57)
BLA(56)
BLA(56)
BLA(55)
BLA(54)
BLA(53)
#undef BLA
.text
.globl sqrt
.ent sqrt
.align 5
sqrt:
.frame sp, 32, ra, 0
subq sp, 32, sp
ldgp gp, .-sqrt(pv)
stt dA, 0(sp)
.prologue 1
.align 4
lda pc, consts
lda exp, 1022
sll exp, 52, exp
nop
ldq ua, 0(sp)
lds d1_5, 0(pc)
lds d0_5, 4(pc)
srl ua, 46, ta
and ta, 63, ta
sll ua, 12, ux
s4addq ta, pc, pt
srl ux, 12, ux
lds dX, 16(pt)
bis ux, exp, ux
stq ux, 0(sp)
ble ua, pm_zero_or_negative
mult dX, dX, dTmp
srl ua, 52, ea
addq ea, 1, ta
lda tmp, -2047(ea)
ldt dZ, 0(sp)
beq ea, positive_denormal
bic ta, 1, ta
beq tmp, pos_nan_or_inf
addt dZ, dZ, dB
lda ta, -1024(ta)
mult dZ, dTmp, dZ
ldt dTmp, 8(pc)
subt d1_5, dZ, dT
mult dZ, dT, dZ
mult dX, dT, dX
sll ua, 52, wp
mult dZ, dT, dZ
subt d1_5, dZ, dT
mult dX, dT, dX
blbs ea, skip
addt dB, dB, dB
sll ua, 53, wp
mult dX, dTmp, dX
unop
.align 4
skip:
mult dB, dX, dT
sll ta, 51, ta
mult dX, d0_5, dX
mult dT, dT, dZ
subt dB, dZ, dZ
mult dX, dZ, dX
addt dX, dT, dX
stt dX, 0(sp)
nop # avoid flashing pipeline
nop # wait 1 clock(actually 2 clocks)
ldq ux, 0(sp)
subq ux, exp, v
mulq v, v, v2 # takes too long. difficult scheduling
addq ux, ta, ux
stq ux, 8(sp)
addq ux, 1, ux
stq ux, 0(sp)
subq ux, 2, ux
stq ux, 16(sp)
subq wp, v, wm
addq wp, v, wp
nop
subq v2, wm, wm # stopping pipeline, wait 8 clocks
subq v2, wp, wp
sra wm, 63, wm
cmple zero, wp, wp
addq wm, wp, wm
s8addq wm, sp, wm
ldt dX, 8(wm)
addq sp, 32, sp
ret zero, (ra), 1
.align 4
pm_zero_or_negative:
positive_denormal:
pos_nan_or_inf:
cpys dZero, dZero, dX /* Junk for now. */
addq sp, 32, sp
ret zero, (ra), 1
.end sqrt
/* This proguram is from glibc/sysdeps/alpha/w_sqrt.S */
/* Conberted to C by Kazushige Goto for a test */
#include <stdio.h>
static unsigned int table[] = {
0x1500, 0x2ef8, 0x4d67, 0x6b02, 0x87be, 0xa395, 0xbe7a, 0xd866,
0xf14a, 0x1091b, 0x11fcd, 0x13552, 0x14999, 0x15c98, 0x16e34, 0x17e5f,
0x18d03, 0x19a01, 0x1a545, 0x1ae8a, 0x1b5c4, 0x1bb01, 0x1bfde, 0x1c28d,
0x1c2de, 0x1c0db, 0x1ba73, 0x1b11c, 0x1a4b5, 0x1953d, 0x18266, 0x16be0,
0x1683e, 0x179d8, 0x18a4d, 0x19992, 0x1a789, 0x1b445, 0x1bf61, 0x1c989,
0x1d16d, 0x1d77b, 0x1dddf, 0x1e2ad, 0x1e5bf, 0x1e6e8, 0x1e654, 0x1e3cd,
0x1df2a, 0x1d635, 0x1cb16, 0x1be2c, 0x1ae4e, 0x19bde, 0x1868e, 0x16e2e,
0x1527f, 0x1334a, 0x11051, 0xe951, 0xbe01, 0x8e0d, 0x5924, 0x1edd,
};
double sqrt(double x){
unsigned long dnI = 0x3fefffffffffffffUL;
unsigned long upI = 0x3ff0000000000001UL;
unsigned long hfI = 0x3fe0000000000000UL;
unsigned long alI = 0x3ff7ffffffc00000UL;
double dn = *(double *)(&dnI);
double up = *(double *)(&upI);
double half = *(double *)(&hfI);
double al = *(double *)(&alI);
double y, yp, ypp;
double z, zp;
double zmi, zpl, zmii, zpll;
unsigned long t0, t1, t2;
t1 = 0x5fe80000;
t0 = *(unsigned long *)(&x);
t0 = (t0 >> 33);
t1 = t1 - t0;
t2 = (t1 << 33);
t0 = (t1 >> 12);
t0 = (t0 & 0xfc);
t0 = table[(t0>>2)];
t1 = (int)(t1 - t0);
t1 = (t1 << 32);
y = *(double *)(&t1);
yp = y * (1.5 - x * 0.5 * y * y);
ypp = yp * (al - x * 0.5 * yp * yp);
z = x * ypp;
zp = z + z * 0.5 * (1.0 - z * ypp);
asm volatile("mult/c %1, %2, %0" : "=&f"(zmi) : "f"(zp), "f"(dn));
asm volatile("mult/c %1, %2, %0" : "=&f"(zpl) : "f"(zp), "f"(up));
asm volatile("mult/c %1, %2, %0" : "=&f"(zmii) : "f"(zp), "f"(zmi));
asm volatile("mult/c %1, %2, %0" : "=&f"(zpll) : "f"(zp), "f"(zpl));
if (zmii - x >= 0.) return zmi;
if (zpll - x < 0.) return zpl;
return zp;
}
^ permalink raw reply [flat|nested] 6+ messages in thread