xref: /original-bsd/usr.bin/f77/libF77/pow_hh.c (revision 9ad5198e)
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_hh.c	5.3 (Berkeley) 04/12/91";
10 #endif /* not lint */
11 
12 short pow_hh(ap, bp)
13 short *ap, *bp;
14 {
15 	short int pow, x, n;
16 
17 	pow = 1;
18 	x = *ap;
19 	n = *bp;
20 
21 	if (n == 0)
22 		return ( 1L );
23 
24 	if (x == 0)
25 	{
26 		if( n > 0 )
27 			return ( 0L );
28 		else
29 			return ( 1/x );
30 	}
31 
32 	if (x == 1)
33 		return ( 1L );
34 
35 	if (x == -1)
36 	{
37 		if (n < 0)
38 		{
39 			if (n < -2)
40 				n += 2;
41 			n = -n;
42 		}
43 		if (n % 2 == 0)
44 			return ( 1L );
45 		else
46 			return ( -1L );
47 	}
48 
49 	if (n > 0)
50 		for( ; ; )
51 		{
52 			if(n & 01)
53 				pow *= x;
54 			if(n >>= 1)
55 				x *= x;
56 			else
57 				break;
58 		}
59 	else
60 		pow = 0;
61 
62 	return(pow);
63 }
64