xref: /csrg-svn/usr.bin/f77/libF77/pow_ii.c (revision 47940)
1*47940Sbostic /*-
2*47940Sbostic  * Copyright (c) 1980 The Regents of the University of California.
3*47940Sbostic  * All rights reserved.
411912Sdlw  *
5*47940Sbostic  * %sccs.include.proprietary.c%
6*47940Sbostic  */
7*47940Sbostic 
8*47940Sbostic #ifndef lint
9*47940Sbostic static char sccsid[] = "@(#)pow_ii.c	5.4 (Berkeley) 04/12/91";
10*47940Sbostic #endif /* not lint */
11*47940Sbostic 
12*47940Sbostic /*  Corrections by Robert P. Corbett, 1983 March 2
1311912Sdlw  *  Revised to restore portability, 1983 March 4
1410514Sdlw  */
1510514Sdlw 
pow_ii(ap,bp)1610514Sdlw long int pow_ii(ap, bp)
1710514Sdlw long int *ap, *bp;
1810514Sdlw {
1911912Sdlw 	long int pow, x, n;
2010514Sdlw 
2111912Sdlw 	pow = 1;
2211912Sdlw 	x = *ap;
2311912Sdlw 	n = *bp;
2410514Sdlw 
2511912Sdlw 	if (n == 0)
2611912Sdlw 		return ( 1L );
2711912Sdlw 
2811912Sdlw 	if (x == 0)
2923777Sjerry 	{
3023777Sjerry 		if( n > 0 )
3123777Sjerry 			return ( 0L );
3223777Sjerry 		else
3323777Sjerry 			return ( 1/x );
3423777Sjerry 	}
3511912Sdlw 
3611912Sdlw 	if (x == 1)
3711912Sdlw 		return ( 1L );
3811912Sdlw 
3911912Sdlw 	if (x == -1)
4011912Sdlw 	{
4111912Sdlw 		if (n < 0)
4210514Sdlw 		{
4311912Sdlw 			if (n < -2)
4411912Sdlw 				n += 2;
4511912Sdlw 			n = -n;
4611912Sdlw 		}
4711912Sdlw 		if (n % 2 == 0)
4811912Sdlw 			return ( 1L );
4910514Sdlw 		else
5011912Sdlw 			return ( -1L );
5111912Sdlw 	}
5211912Sdlw 
5311912Sdlw 	if (n > 0)
5411912Sdlw 		for( ; ; )
5511912Sdlw 		{
5611912Sdlw 			if(n & 01)
5711912Sdlw 				pow *= x;
5811912Sdlw 			if(n >>= 1)
5911912Sdlw 				x *= x;
6011912Sdlw 			else
6111912Sdlw 				break;
6210514Sdlw 		}
6311912Sdlw 	else
6411912Sdlw 		pow = 0;
6511912Sdlw 
6611912Sdlw 	return(pow);
6710514Sdlw }
68