Commit | Line | Data |
---|---|---|
82492b51 KB |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
3 | * All rights reserved. | |
ce9c54b8 | 4 | * |
ad787160 C |
5 | * This module is believed to contain source code proprietary to AT&T. |
6 | * Use and redistribution is subject to the Berkeley Software License | |
7 | * Agreement and your Software Agreement with AT&T (Western Electric). | |
ee035c83 DW |
8 | */ |
9 | ||
82492b51 | 10 | #ifndef lint |
ad787160 | 11 | static char sccsid[] = "@(#)pow_ii.c 5.4 (Berkeley) 4/12/91"; |
82492b51 KB |
12 | #endif /* not lint */ |
13 | ||
14 | /* Corrections by Robert P. Corbett, 1983 March 2 | |
15 | * Revised to restore portability, 1983 March 4 | |
16 | */ | |
ce9c54b8 | 17 | |
ee035c83 DW |
18 | long int pow_ii(ap, bp) |
19 | long int *ap, *bp; | |
20 | { | |
ce9c54b8 | 21 | long int pow, x, n; |
ce9c54b8 DW |
22 | |
23 | pow = 1; | |
24 | x = *ap; | |
25 | n = *bp; | |
26 | ||
27 | if (n == 0) | |
28 | return ( 1L ); | |
29 | ||
30 | if (x == 0) | |
2129c98e JB |
31 | { |
32 | if( n > 0 ) | |
33 | return ( 0L ); | |
34 | else | |
35 | return ( 1/x ); | |
36 | } | |
ee035c83 | 37 | |
ce9c54b8 DW |
38 | if (x == 1) |
39 | return ( 1L ); | |
ee035c83 | 40 | |
ce9c54b8 DW |
41 | if (x == -1) |
42 | { | |
43 | if (n < 0) | |
ee035c83 | 44 | { |
ce9c54b8 DW |
45 | if (n < -2) |
46 | n += 2; | |
47 | n = -n; | |
48 | } | |
49 | if (n % 2 == 0) | |
50 | return ( 1L ); | |
ee035c83 | 51 | else |
ce9c54b8 DW |
52 | return ( -1L ); |
53 | } | |
54 | ||
55 | if (n > 0) | |
56 | for( ; ; ) | |
57 | { | |
58 | if(n & 01) | |
59 | pow *= x; | |
60 | if(n >>= 1) | |
61 | x *= x; | |
62 | else | |
63 | break; | |
ee035c83 | 64 | } |
ce9c54b8 DW |
65 | else |
66 | pow = 0; | |
67 | ||
68 | return(pow); | |
ee035c83 | 69 | } |