Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!uflorida!haven!mimsy!dftsrv!ames!pasteur!helios.ee.lbl.gov!nosc!humu!uhccux!mikem From: mikem@uhccux.uhcc.hawaii.edu (Mike Morton) Newsgroups: comp.sys.m68k Subject: Re: addq.w #n,sp and a pop quiz Keywords: subx Message-ID: <2840@uhccux.uhcc.hawaii.edu> Date: 16 Dec 88 22:15:53 GMT References: <5005@bsu-cs.UUCP> <5460@cbmvax.UUCP> <1100@ncar.ucar.edu> <191@spt.entity.com> Distribution: na Organization: University of Hawaii Lines: 29 Gail Zacharias recently contributed this 4-instruction signum function, for which I've counted cycles. word long add Dx,Dx 4 8 ; shift sign bit -> carry ; set Z bit only for Dx = 0, 0x8000 subx Dx,Dx 4 8 ; set Dx = 0xffff if Dx was < 0 ; (and set C bit) ; set Dx = 0 if sign Dx was >= 0 ; (and clear C bit) ; (and leave Z bit alone) bls .+4 10/8 (taken/not) ; branch on C bit (Dx < 0) ; (Dx now holds -1) ; ... or on Z bit (Dx = 0, 0x8000) ; (Dx now holds 0) moveq #1,Dx 4 4 ; none of the above: return 1 ---- ---- 18/20 26/28 Very elegant! It uses only one register and is faster than the Superoptimizer's solution for the longword case (30 cycles). However, the SO's solution is faster for the word case (16 cycles), mostly because it avoids the conditional branch. Takers? -- Mike Morton // P.O. Box 11378, Honolulu, HI 96828, (808) 676-6966 HST Internet: msm@ceta.ics.hawaii.edu (anagrams): Mr. Machine Tool; Ethical Mormon; Chosen Immortal; etc.