Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / quickfactor.cpp
blob6fd21e812f423076338c9ac1b9e8087cb3505795
1 //-----------------------------------------------------------------------------
2 //
3 // Factor small numerical powers
4 //
5 // Input: tos-2 Base (positive integer < 2^31 - 1)
6 //
7 // tos-1 Exponent
8 //
9 // Output: Expr on stack
11 //-----------------------------------------------------------------------------
13 #include "stdafx.h"
14 #include "defs.h"
16 #define BASE p1
17 #define EXPO p2
19 static void quickpower(void);
21 void
22 quickfactor(void)
24 int h, i, n;
25 U **s;
27 save();
29 EXPO = pop();
30 BASE = pop();
32 h = tos;
34 push(BASE);
36 factor_small_number();
38 n = tos - h;
40 s = stack + h;
42 for (i = 0; i < n; i += 2) {
43 push(s[i]); // factored base
44 push(s[i + 1]); // factored exponent
45 push(EXPO);
46 multiply();
47 quickpower();
50 // stack has n results from factor_number_raw()
52 // on top of that are all the expressions from quickpower()
54 // multiply the quickpower() results
56 multiply_all(tos - h - n);
58 p1 = pop();
60 tos = h;
62 push(p1);
64 restore();
67 // BASE is a prime number so power is simpler
69 static void
70 quickpower(void)
72 int expo;
74 save();
76 EXPO = pop();
77 BASE = pop();
79 push(EXPO);
80 bignum_truncate();
81 p3 = pop();
83 push(EXPO);
84 push(p3);
85 subtract();
86 p4 = pop();
88 // fractional part of EXPO
90 if (!iszero(p4)) {
91 push_symbol(POWER);
92 push(BASE);
93 push(p4);
94 list(3);
97 push(p3);
98 expo = pop_integer();
100 if (expo == (int) 0x80000000) {
101 push_symbol(POWER);
102 push(BASE);
103 push(p3);
104 list(3);
105 restore();
106 return;
109 if (expo == 0) {
110 restore();
111 return;
114 push(BASE);
115 bignum_power_number(expo);
117 restore();