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