xref: /csrg-svn/usr.bin/f77/libF77/pow_ii.c (revision 23777)
110514Sdlw /*
222937Skre  * Copyright (c) 1980 Regents of the University of California.
322937Skre  * All rights reserved.  The Berkeley software License Agreement
422937Skre  * specifies the terms and conditions for redistribution.
511912Sdlw  *
6*23777Sjerry  *	@(#)pow_ii.c	5.3	06/26/85
723002Skre  *
811912Sdlw  *  Corrections by Robert P. Corbett, 1983 March 2
911912Sdlw  *  Revised to restore portability, 1983 March 4
1010514Sdlw  */
1110514Sdlw 
1211912Sdlw 
1310514Sdlw long int pow_ii(ap, bp)
1410514Sdlw long int *ap, *bp;
1510514Sdlw {
1611912Sdlw 	long int pow, x, n;
1710514Sdlw 
1811912Sdlw 	pow = 1;
1911912Sdlw 	x = *ap;
2011912Sdlw 	n = *bp;
2110514Sdlw 
2211912Sdlw 	if (n == 0)
2311912Sdlw 		return ( 1L );
2411912Sdlw 
2511912Sdlw 	if (x == 0)
26*23777Sjerry 	{
27*23777Sjerry 		if( n > 0 )
28*23777Sjerry 			return ( 0L );
29*23777Sjerry 		else
30*23777Sjerry 			return ( 1/x );
31*23777Sjerry 	}
3211912Sdlw 
3311912Sdlw 	if (x == 1)
3411912Sdlw 		return ( 1L );
3511912Sdlw 
3611912Sdlw 	if (x == -1)
3711912Sdlw 	{
3811912Sdlw 		if (n < 0)
3910514Sdlw 		{
4011912Sdlw 			if (n < -2)
4111912Sdlw 				n += 2;
4211912Sdlw 			n = -n;
4311912Sdlw 		}
4411912Sdlw 		if (n % 2 == 0)
4511912Sdlw 			return ( 1L );
4610514Sdlw 		else
4711912Sdlw 			return ( -1L );
4811912Sdlw 	}
4911912Sdlw 
5011912Sdlw 	if (n > 0)
5111912Sdlw 		for( ; ; )
5211912Sdlw 		{
5311912Sdlw 			if(n & 01)
5411912Sdlw 				pow *= x;
5511912Sdlw 			if(n >>= 1)
5611912Sdlw 				x *= x;
5711912Sdlw 			else
5811912Sdlw 				break;
5910514Sdlw 		}
6011912Sdlw 	else
6111912Sdlw 		pow = 0;
6211912Sdlw 
6311912Sdlw 	return(pow);
6410514Sdlw }
65