xref: /original-bsd/usr.bin/f77/libF77/pow_ii.c (revision a141c157)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)pow_ii.c	5.3	06/26/85
7  *
8  *  Corrections by Robert P. Corbett, 1983 March 2
9  *  Revised to restore portability, 1983 March 4
10  */
11 
12 
13 long int pow_ii(ap, bp)
14 long int *ap, *bp;
15 {
16 	long int pow, x, n;
17 
18 	pow = 1;
19 	x = *ap;
20 	n = *bp;
21 
22 	if (n == 0)
23 		return ( 1L );
24 
25 	if (x == 0)
26 	{
27 		if( n > 0 )
28 			return ( 0L );
29 		else
30 			return ( 1/x );
31 	}
32 
33 	if (x == 1)
34 		return ( 1L );
35 
36 	if (x == -1)
37 	{
38 		if (n < 0)
39 		{
40 			if (n < -2)
41 				n += 2;
42 			n = -n;
43 		}
44 		if (n % 2 == 0)
45 			return ( 1L );
46 		else
47 			return ( -1L );
48 	}
49 
50 	if (n > 0)
51 		for( ; ; )
52 		{
53 			if(n & 01)
54 				pow *= x;
55 			if(n >>= 1)
56 				x *= x;
57 			else
58 				break;
59 		}
60 	else
61 		pow = 0;
62 
63 	return(pow);
64 }
65