Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!lll-crg!caip!princeton!allegra!ulysses!mhuxr!mhuxn!ihnp4!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: net.lang.c Subject: Re: non-built-in exponentiation (Re: What should be added to C) Message-ID: <1003@ttrdc.UUCP> Date: Sun, 22-Jun-86 00:44:09 EDT Article-I.D.: ttrdc.1003 Posted: Sun Jun 22 00:44:09 1986 Date-Received: Tue, 24-Jun-86 04:59:45 EDT References: <1356@brl-smoke.ARPA> <1371@brl-smoke.ARPA> Organization: AT&T, Computer Systems Division, Skokie, IL Lines: 55 In article <1371@brl-smoke.ARPA>, gwyn@brl-smoke.ARPA (Doug Gwyn ) writes: >/* > LPow -- long exponentiation (no overflow detection) > last edit: 86/06/16 D A Gwyn > SCCS ID: @(#)lpow.c 1.1 >*/ >long >LPow( base, exponent ) /* returns base^exponent */ > register long base; > register long exponent; > { > register long result; /* result accumulator */ > /* handle simple special cases separately: */ > if ( base == 0 ) > return 0; /* exp. < 0 should be EDOM */ > else if ( base == 1 ) > return 1; > else if ( base == -1 ) > return exponent % 2 == 0 ? 1 : -1; > else if ( exponent < 0 ) > return 0; > /* general case with exponent >= 0: */ > result = 1; > for ( ; ; ) /* LOOP INVARIANT: result*base^exponent */ > { > if ( exponent % 2 != 0 ) > result *= base; > if ( (exponent /= 2) == 0 ) > break; /* result now stable */ > base *= base; > } > return result; > } Question. Why not use exponent & 01 rather than exponent % 2 (other than consideration of portability to machines with one's complement integers, which could be #ifdef'd easily enough)? The latter is at least three times slower when I spot checked it on a 3B20 (which has one-instruction opcodes for both tests) and I doubt the VAX or other machines are much better with a general-purpose "modulo". -- ------------------------------- Disclaimer: The views contained herein are | dan levy | yvel nad | my own and are not at all those of my em- | an engihacker @ | ployer or the administrator of any computer | at&t computer systems division | upon which I may hack. | skokie, illinois | -------------------------------- Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, vax135}!ttrdc!levy