Commit | Line | Data |
---|---|---|
ee035c83 | 1 | /* |
a7868ad7 RE |
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. | |
ce9c54b8 | 5 | * |
2129c98e | 6 | * @(#)pow_ii.c 5.3 %G% |
bb3ec388 | 7 | * |
ce9c54b8 DW |
8 | * Corrections by Robert P. Corbett, 1983 March 2 |
9 | * Revised to restore portability, 1983 March 4 | |
ee035c83 DW |
10 | */ |
11 | ||
ce9c54b8 | 12 | |
ee035c83 DW |
13 | long int pow_ii(ap, bp) |
14 | long int *ap, *bp; | |
15 | { | |
ce9c54b8 | 16 | long int pow, x, n; |
ce9c54b8 DW |
17 | |
18 | pow = 1; | |
19 | x = *ap; | |
20 | n = *bp; | |
21 | ||
22 | if (n == 0) | |
23 | return ( 1L ); | |
24 | ||
25 | if (x == 0) | |
2129c98e JB |
26 | { |
27 | if( n > 0 ) | |
28 | return ( 0L ); | |
29 | else | |
30 | return ( 1/x ); | |
31 | } | |
ee035c83 | 32 | |
ce9c54b8 DW |
33 | if (x == 1) |
34 | return ( 1L ); | |
ee035c83 | 35 | |
ce9c54b8 DW |
36 | if (x == -1) |
37 | { | |
38 | if (n < 0) | |
ee035c83 | 39 | { |
ce9c54b8 DW |
40 | if (n < -2) |
41 | n += 2; | |
42 | n = -n; | |
43 | } | |
44 | if (n % 2 == 0) | |
45 | return ( 1L ); | |
ee035c83 | 46 | else |
ce9c54b8 DW |
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; | |
ee035c83 | 59 | } |
ce9c54b8 DW |
60 | else |
61 | pow = 0; | |
62 | ||
63 | return(pow); | |
ee035c83 | 64 | } |