Changeset 27b799 in git
- Timestamp:
- May 27, 1998, 7:14:09 PM (25 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', '0604212ebb110535022efecad887940825b97c3f')
- Children:
- 311499b3c35b89d435a5b5b44c51e0ace5b13c6c
- Parents:
- b911ac5ddf660513a9fa06698ab8ac4553c1ef65
- Location:
- Singular
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/ChangeLog
rb911ac r27b799 1 Wed May 27 19:13:10 MET DST 1998 hannes 2 * removed #define HAVE_GMP and non-gmp-parts 1 3 Wed May 20 15:03:28 1998 Jens Schmidt <schmidt@mathematik.uni-kl.de> 2 4 -
Singular/extra.cc
rb911ac r27b799 2 2 * Computer Algebra System SINGULAR * 3 3 *****************************************/ 4 /* $Id: extra.cc,v 1.5 0 1998-05-20 10:24:05 obachmanExp $ */4 /* $Id: extra.cc,v 1.51 1998-05-27 17:14:05 Singular Exp $ */ 5 5 /* 6 6 * ABSTRACT: general interface to internals of Singular ("system" command) … … 188 188 TEST_FOR("DLD") 189 189 #endif 190 #ifdef HAVE_GMP191 TEST_FOR("gmp")192 #endif193 190 #ifdef HAVE_FACTORY 194 191 TEST_FOR("factory") … … 357 354 else 358 355 #endif 359 /*==================== contributors =============================*/ 356 /*==================== contributors =============================*/ 360 357 if(strcmp((char*)(h->Data()),"contributors") == 0) 361 358 { -
Singular/longrat.cc
rb911ac r27b799 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: longrat.cc,v 1.1 6 1998-04-17 07:53:43Singular Exp $ */4 /* $Id: longrat.cc,v 1.17 1998-05-27 17:14:06 Singular Exp $ */ 5 5 /* 6 6 * ABSTRACT: computation with long rational numbers (Hubert Grassmann) … … 61 61 62 62 /*-----------------------------------------------------------------*/ 63 #ifdef HAVE_GMP64 63 /*3 65 64 * parameter s in number: … … 2102 2101 return iz; 2103 2102 } 2104 2105 #else2106 2107 /*0 --------------------------------------------implementation*/2108 2109 typedef unsigned int int32;2110 #define MAX_INT16 0XFFFF2111 #define SIGNUM 0X80002112 #define BASIS 0X100002113 #define MOD_BASIS 0XFFFF2114 #define SHIFT_BASIS 0X102115 2116 /*32117 * x%SIGNUM == length(x)2118 * x/SIGNUM == 1 : x < 02119 */2120 #define NLLENGTH(a) ((int)(*(a)&0X7FFF))2121 #define NLNEG(a) (*(a)^SIGNUM)2122 #define NLSIGN(a) (*(a)>SIGNUM)2123 2124 /*32125 *allocate memory for a long integer2126 */2127 static lint nlAllocLint (int L)2128 {2129 lint x;2130 2131 if (L < SIGNUM)2132 {2133 x = (lint)Alloc((L+1)*sizeof(int16));2134 *x = L;2135 return x;2136 }2137 else2138 {2139 WerrorS("number too large");2140 return NULL;2141 }2142 }2143 2144 /*32145 *deallocate Memory of a long integer2146 */2147 static void nlDeleteLint (lint x)2148 {2149 if (x)2150 {2151 Free((ADDRESS)x,(NLLENGTH(x)+1)*sizeof(int16));2152 }2153 }2154 2155 /*32156 *nlShrink the memory used by c2157 */2158 static void nlShrink (lint * c)2159 {2160 int j, i;2161 lint cc = *c;2162 2163 i = j = NLLENGTH(cc);2164 while ((i!=0) && (cc[i]==0))2165 {2166 i--;2167 }2168 if (i != j)2169 {2170 cc = nlAllocLint(i);2171 memcpy(cc+1,(*c)+1,i*sizeof(int16));2172 nlDeleteLint(*c);2173 *c = cc;2174 }2175 }2176 2177 /*32178 * x == 1 ?2179 */2180 static BOOLEAN nlIsOneLint (lint x)2181 {2182 return ((*x == 1) && (x[1] == 1));2183 }2184 2185 /*32186 *compares absolute values2187 *abs(x) > abs(y) nlCompAbs(x,y) = 1;2188 *abs(x) < abs(y) -1;2189 *abs(x) = abs(y) 0;2190 */2191 static int nlCompAbs (lint x, lint y)2192 {2193 int i = NLLENGTH(x);2194 2195 if (i != NLLENGTH(y))2196 {2197 if (i > NLLENGTH(y))2198 {2199 return 1;2200 }2201 else2202 {2203 return -1;2204 }2205 }2206 else2207 {2208 do2209 {2210 if (x[i] > y[i])2211 {2212 return 1;2213 }2214 if (x[i] < y[i])2215 {2216 return -1;2217 }2218 i--;2219 }2220 while (i!=0);2221 return 0;2222 }2223 }2224 2225 /*32226 *compares long integers2227 *(x) > (y) nlComp(x,y) = 1;2228 *(x) < (y) -1;2229 *(x) = (y) 0;2230 */2231 static int nlComp (lint x, lint y)2232 {2233 int i = NLSIGN(x);2234 2235 if (i != NLSIGN(y))2236 {2237 if (i != 0)2238 {2239 return -1;2240 }2241 else2242 {2243 return 1;2244 }2245 }2246 else if (i != 0)2247 {2248 return -nlCompAbs(x, y);2249 }2250 else2251 {2252 return nlCompAbs(x, y);2253 }2254 }2255 2256 /*32257 * copy f to t2258 */2259 static lint nlCopyLint (lint f)2260 {2261 int i;2262 lint t;2263 2264 if (f!=NULL)2265 {2266 i = (NLLENGTH(f)+1)*sizeof(int16);2267 t = (lint)Alloc(i);2268 memcpy(t, f, i);2269 return t;2270 }2271 else2272 {2273 return NULL;2274 }2275 }2276 2277 /*32278 * n := 12279 */2280 static lint nlInitOne()2281 {2282 lint n = (lint)Alloc(2*sizeof(int16));2283 *n = 1;2284 n[1] = 1;2285 return n;2286 }2287 2288 /*32289 * n := i2290 */2291 static lint nlToLint (int16 i)2292 {2293 if (i)2294 {2295 lint n = (lint)Alloc(2*sizeof(int16));2296 *n = 1;2297 n[1] = i;2298 return n;2299 }2300 else2301 {2302 return NULL;2303 }2304 }2305 2306 /*32307 * Add longs, signs are equal2308 */2309 static lint nlAddBasic (lint a, lint b)2310 {2311 int i, max, min;2312 int32 w;2313 lint xx,h;2314 2315 max = NLLENGTH(a);2316 min = NLLENGTH(b);2317 if ((min == 1) && (max == 1))2318 {2319 w = (int32) a[1] + (int32) b[1];2320 if (w < BASIS)2321 {2322 xx = (lint)Alloc(2*sizeof(int16));2323 *xx = 1;2324 xx[1] = (int16) w;2325 }2326 else2327 {2328 xx = (lint)Alloc(3 * sizeof(int16));2329 *xx = 2;2330 xx[1] = (int16) (MOD_BASIS&w);2331 xx[2] = 1;2332 }2333 if (NLSIGN(a))2334 {2335 *xx += SIGNUM;2336 }2337 return xx;2338 }2339 if(min>max)2340 {2341 i = max;2342 max = min;2343 min = i;2344 h = b;2345 }2346 else2347 {2348 h = a;2349 }2350 xx = (lint)Alloc((max+1)*sizeof(int16));2351 *xx = (int16)max;2352 w = (int32) a[1] + (int32) b[1];2353 xx[1] = (int16) (MOD_BASIS&w);2354 for (i=2; i<=min; i++)2355 {2356 w = (w>>SHIFT_BASIS) + (int32) a[i] + (int32) b[i];2357 xx[i] = (int16) (MOD_BASIS&w);2358 }2359 i = min;2360 loop2361 {2362 i++;2363 if ((w<BASIS) || (i>max))2364 {2365 break;2366 }2367 w = (w>>SHIFT_BASIS) + (int32) h[i];2368 xx[i] = (int16) (MOD_BASIS&w);2369 }2370 for(; i<=max; i++)2371 {2372 xx[i] = h[i];2373 }2374 if (w>=BASIS)2375 {2376 h = (lint)Alloc((max+2)*sizeof(int16));2377 memcpy(h+1,xx+1,max*sizeof(int16));2378 max++;2379 *h = (int16)max;2380 h[max] = 1;2381 nlDeleteLint(xx);2382 xx = h;2383 }2384 if (NLSIGN(a))2385 {2386 *xx += SIGNUM;2387 }2388 return xx;2389 }2390 2391 /*32392 * Subtract longs, signs are equal, a > b2393 */2394 static lint nlSubBasic (lint a, lint b)2395 {2396 int i,j1,j2;2397 int16 w;2398 lint xx;2399 j1 = NLLENGTH(a);2400 if (j1 == 1)2401 {2402 xx = (lint)Alloc(2*sizeof(int16));2403 *xx = 1;2404 xx[1] = a[1]-b[1];2405 if (NLSIGN(a))2406 {2407 *xx += SIGNUM;2408 }2409 return xx;2410 }2411 j2 = NLLENGTH(b);2412 xx = (lint)Alloc((j1+1)*sizeof(int16));2413 *xx = (int16)j1;2414 if(a[1] < b[1])2415 {2416 xx[1] = MAX_INT16-b[1]+a[1]+1;2417 w = 1;2418 }2419 else2420 {2421 xx[1] = a[1]-b[1];2422 w = 0;2423 }2424 for (i=2; i<=j2; i++)2425 {2426 if(a[i]>b[i])2427 {2428 xx[i] = a[i]-b[i]-w;2429 w = 0;2430 }2431 else if(a[i]<b[i])2432 {2433 if(w!=0)2434 {2435 xx[i] = MAX_INT16 - b[i] + a[i];2436 }2437 else2438 {2439 xx[i] = MAX_INT16 - b[i] + a[i] + 1;2440 }2441 w = 1;2442 }2443 else2444 {2445 if(w!=0)2446 {2447 xx[i] = MAX_INT16;2448 }2449 else2450 {2451 xx[i] = 0;2452 }2453 }2454 }2455 i = j2;2456 while(w!=0)2457 {2458 i++;2459 if(a[i])2460 {2461 xx[i] = a[i]-w;2462 w = 0;2463 }2464 else2465 {2466 xx[i] = MAX_INT16;2467 }2468 }2469 while(i < j1)2470 {2471 i++;2472 xx[i] = a[i];2473 }2474 nlShrink(&xx);2475 if (NLSIGN(a))2476 {2477 *xx += SIGNUM;2478 }2479 return xx;2480 }2481 2482 /*32483 * c:= a + b2484 */2485 static lint nlAddLong (lint a,lint b)2486 {2487 if (a==NULL)2488 return nlCopyLint(b);2489 else if (b==NULL)2490 return nlCopyLint(a);2491 if (NLSIGN(a) == NLSIGN(b))2492 return nlAddBasic(a,b);2493 else2494 {2495 switch(nlCompAbs(a, b))2496 {2497 case -1:2498 return nlSubBasic(b, a);2499 case 1:2500 return nlSubBasic(a, b);2501 default:2502 ;2503 }2504 }2505 return NULL;2506 }2507 2508 /*32509 * c:= a - b2510 */2511 static lint nlSubLong (lint a,lint b)2512 {2513 lint c;2514 if(b==NULL)2515 return nlCopyLint(a);2516 if(a==NULL)2517 {2518 c = nlCopyLint(b);2519 *c = NLNEG(c);2520 return c;2521 }2522 if (NLSIGN(a) != NLSIGN(b))2523 return nlAddBasic(a,b);2524 else2525 {2526 switch(nlCompAbs(a, b))2527 {2528 case 1:2529 return nlSubBasic(a, b);2530 case -1:2531 c = nlSubBasic(b, a);2532 *c = NLNEG(c);2533 return c;2534 default:2535 ;2536 }2537 }2538 return NULL;2539 }2540 2541 /*32542 * a[i] = a[i] + b[i]*x, i = 1 to length(b)2543 */2544 static void nlAddMult (lint a, lint b, int32 x)2545 {2546 int32 w;2547 int i, l = NLLENGTH(b);2548 w = (int32) a[1] + (int32) b[1]*x;2549 a[1] = (int16) (MOD_BASIS&w);2550 for (i=2; i<=l; i++)2551 {2552 w = (w>>SHIFT_BASIS) + (int32) a[i] + (int32) b[i]*x;2553 a[i] = (int16) (MOD_BASIS&w);2554 }2555 if (w>=BASIS)2556 a[l+1] = (int16)(w>>SHIFT_BASIS);2557 }2558 2559 /*32560 * a[i] = b[i]*x, i = 1 to length(b)2561 */2562 static void nlIniMult (lint a, lint b, int32 x)2563 {2564 int i, l = NLLENGTH(b);2565 int32 w;2566 w = (int32) b[1]*x;2567 a[1] = (int16) (MOD_BASIS&w);2568 for (i=2; i<=l; i++)2569 {2570 w = (w>>SHIFT_BASIS) + (int32) b[i]*x;2571 a[i] = (int16) (MOD_BASIS&w);2572 }2573 if (w>=BASIS)2574 a[l+1] = (int16)(w>>SHIFT_BASIS);2575 }2576 2577 /*32578 * mult long, c:= a * b2579 */2580 static lint nlMultLong (lint a,lint b)2581 {2582 int la, lb, i;2583 int32 h;2584 lint xx;2585 if ((a==NULL) || (b==NULL))2586 return NULL;2587 la = NLLENGTH(a);2588 lb = NLLENGTH(b);2589 if ((la == 1) && (lb == 1))2590 {2591 h = (int32) a[1] * (int32) b[1];2592 if (h < BASIS)2593 {2594 xx = (lint)Alloc(2*sizeof(int16));2595 *xx = 1;2596 xx[1] = (int16) h;2597 }2598 else2599 {2600 xx = (lint)Alloc(3 * sizeof(int16));2601 *xx = 2;2602 xx[1] = (int16) (MOD_BASIS&h);2603 xx[2] = (int16) (h>>SHIFT_BASIS);2604 }2605 if ((NLSIGN(a)) != (NLSIGN(b)))2606 *xx += SIGNUM;2607 return xx;2608 }2609 i = la+lb;2610 xx = (lint)Alloc((i+1)*sizeof(int16));2611 *xx = (int16)i;2612 memset(xx+1,0,i*sizeof(int16));2613 if (la < lb)2614 {2615 nlIniMult(xx, b, (int32) a[1]);2616 for (i=1; i<la; i++)2617 nlAddMult(xx+i, b, (int32) a[i+1]);2618 }2619 else2620 {2621 nlIniMult(xx, a, (int32) b[1]);2622 for (i=1; i<lb; i++)2623 nlAddMult(xx+i, a, (int32) b[i+1]);2624 }2625 nlShrink(&xx);2626 if ((NLSIGN(a)) != (NLSIGN(b)))2627 *xx += SIGNUM;2628 return xx;2629 }2630 2631 /*32632 * aq := aq div b, b small2633 */2634 static void nlQuotSmall (lint * aq, int16 b)2635 {2636 int al,j;2637 int16 a1,c1;2638 int32 db,da,dc;2639 lint a,cc;2640 db = (int32) b;2641 a = *aq;2642 al = NLLENGTH(a);2643 a1 = a[al];2644 if (al == 1)2645 {2646 a[1] = a1 / b;2647 return;2648 }2649 if (a1 >= b)2650 {2651 cc = a;2652 c1 = a1 / b;2653 cc[al] = c1;2654 a1 -= b*c1;2655 }2656 else2657 cc = nlAllocLint(al-1);2658 loop2659 {2660 al--;2661 if (al==0)2662 {2663 if(a != cc)2664 nlDeleteLint(a);2665 *aq = cc;2666 return;2667 }2668 if (a1==0)2669 {2670 j = al;2671 loop2672 {2673 a1 = a[j];2674 if (a1 >= b)2675 {2676 c1 = a1 / b;2677 cc[j] = c1;2678 a1 -= b*c1;2679 }2680 else2681 cc[j] = 0;2682 j--;2683 if (j==0)2684 {2685 if(a != cc)2686 nlDeleteLint(a);2687 *aq = cc;2688 return;2689 }2690 else if (a1!=0)2691 {2692 al = j;2693 break;2694 }2695 }2696 }2697 da = BASIS*(int32) a1 + (int32) a[al];2698 dc = da / db;2699 cc[al] = (int16) dc;2700 da -= db*dc;2701 a1 = (int16) da;2702 }2703 }2704 2705 /*32706 * a := a - b, b1...bj2707 */2708 static void nlSubSimple (lint a, lint b, int j)2709 {2710 int i;2711 int16 w;2712 if (a[1] < b[1])2713 {2714 a[1] += MAX_INT16-b[1]+1;2715 w = 1;2716 }2717 else2718 {2719 a[1] -= b[1];2720 w = 0;2721 }2722 for (i=2; i<=j; i++)2723 {2724 if(a[i] > b[i])2725 {2726 a[i] -= b[i]+w;2727 w = 0;2728 }2729 else if(a[i] < b[i])2730 {2731 if (w)2732 a[i] += MAX_INT16 - b[i];2733 else2734 a[i] += MAX_INT16 - b[i] + 1;2735 w = 1;2736 }2737 else2738 {2739 if(w)2740 a[i] = MAX_INT16;2741 else2742 a[i] = 0;2743 }2744 }2745 if(w!=0)2746 a[j+1]--;2747 }2748 2749 /*32750 * a := a - b*x, b1...bj2751 */2752 static void nlSubMult(lint a, lint b, int32 x, int l)2753 {2754 int i;2755 int16 k, w;2756 int32 y;2757 y = (int32)b[1]*x;2758 k = (int16)(MOD_BASIS&y);2759 if (a[1] < k)2760 {2761 a[1] += MAX_INT16-k+1;2762 w = 1;2763 }2764 else2765 {2766 a[1] -= k;2767 w = 0;2768 }2769 for(i=2; i<=l; i++)2770 {2771 y = (y>>SHIFT_BASIS) + (int32)b[i]*x;2772 k = (int16)(MOD_BASIS&y);2773 if(a[i] > k)2774 {2775 a[i] -= k+w;2776 w = 0;2777 }2778 else if(a[i] < k)2779 {2780 if (w!=0)2781 a[i] += MAX_INT16 - k;2782 else2783 a[i] += MAX_INT16 - k + 1;2784 w = 1;2785 }2786 else2787 {2788 if(w!=0)2789 a[i] = MAX_INT16;2790 else2791 a[i] = 0;2792 }2793 }2794 w += (int16)(y>>SHIFT_BASIS);2795 if(w!=0)2796 a[l+1] -= w;2797 }2798 2799 /*32800 * compare xi and yi from i=l to 12801 */2802 static BOOLEAN nlCompHigh (lint x, lint y, int l)2803 {2804 int i;2805 for (i=l; i!=0; i--)2806 {2807 if (x[i] > y[i])2808 return TRUE;2809 else if (x[i] < y[i])2810 return FALSE;2811 }2812 return TRUE;2813 }2814 2815 /*32816 * aq := aq div bn, bn long2817 */2818 static void nlQuotLong (lint * aq, lint bn)2819 {2820 int bl,al,si,j;2821 int16 b1,a1,a2;2822 int32 db,db1,dc,dc1,dc2;2823 lint an,cc,ansi;2824 BOOLEAN rel;2825 bl = NLLENGTH(bn);2826 b1 = bn[bl];2827 db = BASIS*(int32)b1 + (int32)bn[bl-1];2828 if ((bl > 2) && ((b1 < MAX_INT16) || (bn[bl-1] < MAX_INT16)))2829 db++;2830 db1 = (int32)(b1+1);2831 an = *aq;2832 al = NLLENGTH(an);2833 a1 = an[al];2834 a2 = an[al-1];2835 si = al-bl;2836 ansi = an+si;2837 rel = nlCompHigh(ansi, bn, bl);2838 if (rel)2839 {2840 cc = nlAllocLint(si+1);2841 dc = (BASIS*(int32)a1 + (int32)a2) / db;2842 if (dc > 1)2843 {2844 nlSubMult(ansi, bn, dc, bl);2845 rel = nlCompHigh(ansi, bn, bl);2846 }2847 else2848 dc = 0;2849 while (rel)2850 {2851 dc++;2852 nlSubSimple(ansi, bn, bl);2853 rel = nlCompHigh(ansi, bn, bl);2854 }2855 cc[si+1] = (int16)dc;2856 a1 = an[al];2857 a2 = an[al-1];2858 }2859 else2860 cc = nlAllocLint(si);2861 loop2862 {2863 al--;2864 if (al < bl)2865 {2866 nlDeleteLint(an);2867 *aq = cc;2868 return;2869 }2870 si--;2871 ansi--;2872 if (a1==0)2873 {2874 j = al;2875 loop2876 {2877 a1 = a2;2878 a2 = an[j-1];2879 rel = nlCompHigh(ansi, bn, bl);2880 if (rel)2881 {2882 dc = (BASIS*(int32)a1 + (int32)a2) / db;2883 if (dc > 1)2884 {2885 nlSubMult(ansi, bn, dc, bl);2886 rel = nlCompHigh(ansi, bn, bl);2887 }2888 else2889 dc = 0;2890 while (rel)2891 {2892 dc++;2893 nlSubSimple(ansi, bn, bl);2894 rel = nlCompHigh(ansi, bn, bl);2895 }2896 cc[si+1] = (int16)dc;2897 a1 = an[j];2898 a2 = an[j-1];2899 }2900 else2901 cc[si+1] = 0;2902 si--;2903 ansi--;2904 j--;2905 if (j < bl)2906 {2907 nlDeleteLint(an);2908 *aq = cc;2909 return;2910 }2911 else if(a1!=0)2912 {2913 al = j;2914 break;2915 }2916 }2917 }2918 dc2 = dc1 = (BASIS*(int32)a1+(int32)a2) / db1;2919 loop2920 {2921 nlSubMult(ansi, bn, dc1, bl);2922 a1 = an[al+1];2923 if (a1!=0)2924 {2925 dc1 = (BASIS*(int32)a1+(int32)an[al]) / db1;2926 dc2 += dc1;2927 }2928 else2929 break;2930 }2931 a1 = an[al];2932 a2 = an[al-1];2933 rel = nlCompHigh(ansi, bn, bl);2934 if (rel)2935 {2936 dc = (BASIS*(int32)a1 + (int32)a2) / db;2937 if (dc > 1)2938 {2939 nlSubMult(ansi, bn, dc, bl);2940 rel = nlCompHigh(ansi, bn, bl);2941 }2942 else2943 dc = 0;2944 while (rel)2945 {2946 dc++;2947 nlSubSimple(ansi, bn, bl);2948 rel = nlCompHigh(ansi, bn, bl);2949 }2950 a1 = an[al];2951 a2 = an[al-1];2952 }2953 else2954 dc = 0;2955 cc[si+1] = (int16) (dc2+dc);2956 }2957 }2958 2959 /*32960 * aq := aq div b2961 */2962 static void nlQuotient (lint * aq, lint b)2963 {2964 lint a;2965 int bl;2966 BOOLEAN neg;2967 a = *aq;2968 neg = (NLSIGN(a) != NLSIGN(b));2969 bl = NLLENGTH(b);2970 if(bl == 1)2971 nlQuotSmall(&a,b[1]);2972 else2973 nlQuotLong(&a,b);2974 if(neg && !NLSIGN(a))2975 *a += SIGNUM;2976 else if (!neg && NLSIGN(a))2977 *a -= SIGNUM;2978 *aq = a;2979 }2980 2981 /*32982 * r := a mod b, b small2983 * al = length(a)2984 */2985 static void nlRemSmall (lint a, int al, int16 b, int16 * r)2986 {2987 int j;2988 int16 a1,c1;2989 int32 db,da,dc;2990 db = (int32)b;2991 a1 = a[al];2992 if (al == 1)2993 {2994 c1 = a1 / b;2995 a1 -= b*c1;2996 *r = a1;2997 return;2998 }2999 if (a1 >= b)3000 {3001 c1 = a1 / b;3002 a1 -= b*c1;3003 }3004 loop3005 {3006 al--;3007 if (al==0)3008 {3009 *r = a1;3010 return;3011 }3012 if (a1==0)3013 {3014 j = al;3015 loop3016 {3017 a1 = a[j];3018 if (a1 >= b)3019 {3020 c1 = a1 / b;3021 a1 -= b*c1;3022 }3023 j--;3024 if (j==0)3025 {3026 *r = a1;3027 return;3028 }3029 else if (a1!=0)3030 {3031 al = j;3032 break;3033 }3034 }3035 }3036 da = BASIS*(int32)a1 + (int32)a[al];3037 dc = da / db;3038 da -= db*dc;3039 a1 = (int16)da;3040 }3041 }3042 3043 static void nlShrink0(lint c, int j, int * i)3044 {3045 while((j!=0) && (c[j]==0)) j--;3046 *i = j;3047 }3048 3049 /*33050 * rl := an mod bn, bn long, but <= an3051 * al = length(an)3052 */3053 static void nlRemLong(lint an, lint bn, int al, int bl, int * rl)3054 {3055 int si,j;3056 int16 b1,a1,a2;3057 int32 db,db1,dc;3058 lint ansi;3059 BOOLEAN rel;3060 b1 = bn[bl];3061 db = (int32)bn[bl-1];3062 db += (int32)b1<<SHIFT_BASIS;3063 if ((bl > 2) && ((b1 < MAX_INT16) || (bn[bl-1] < MAX_INT16)))3064 db++;3065 db1 = (int32)b1+1;3066 a1 = an[al];3067 a2 = an[al-1];3068 si = al-bl;3069 ansi = an+si;3070 rel = nlCompHigh(ansi, bn, bl);3071 if (rel)3072 {3073 dc = (BASIS*(int32)a1 + (int32)a2) / db;3074 if (dc > 1)3075 {3076 nlSubMult(ansi, bn, dc, bl);3077 rel = nlCompHigh(ansi, bn, bl);3078 }3079 while (rel)3080 {3081 nlSubSimple(ansi, bn, bl);3082 rel = nlCompHigh(ansi, bn, bl);3083 }3084 a1 = an[al];3085 a2 = an[al-1];3086 }3087 loop3088 {3089 al--;3090 if (al < bl)3091 {3092 nlShrink0(an,bl,rl);3093 return;3094 }3095 si--;3096 ansi--;3097 if(a1==0)3098 {3099 j = al;3100 loop3101 {3102 a1 = a2;3103 a2 = an[j-1];3104 rel = nlCompHigh(ansi, bn, bl);3105 if (rel)3106 {3107 dc = (BASIS*(int32)a1 + (int32)a2) / db;3108 if (dc > 1)3109 {3110 nlSubMult(ansi, bn, dc, bl);3111 rel = nlCompHigh(ansi, bn, bl);3112 }3113 while (rel)3114 {3115 nlSubSimple(ansi, bn, bl);3116 rel = nlCompHigh(ansi, bn, bl);3117 }3118 a1 = an[j];3119 a2 = an[j-1];3120 }3121 si--;3122 ansi--;3123 j--;3124 if (j < bl)3125 {3126 nlShrink0(an,bl,rl);3127 return;3128 }3129 else if (a1!=0)3130 {3131 al = j;3132 break;3133 }3134 }3135 }3136 dc = (BASIS*(int32)a1+(int32)a2) / db1;3137 loop3138 {3139 nlSubMult(ansi, bn, dc, bl);3140 a1 = an[al+1];3141 if (a1!=0)3142 dc = (BASIS*(int32)a1+(int32)an[al]) / db1;3143 else3144 break;3145 }3146 a1 = an[al];3147 a2 = an[al-1];3148 rel = nlCompHigh(ansi, bn, bl);3149 if (rel)3150 {3151 dc = (BASIS*(int32)a1 + (int32)a2) / db;3152 if (dc > 1)3153 {3154 nlSubMult(ansi, bn, dc, bl);3155 rel = nlCompHigh(ansi, bn, bl);3156 }3157 while (rel)3158 {3159 nlSubSimple(ansi, bn, bl);3160 rel = nlCompHigh(ansi, bn, bl);3161 }3162 a1 = an[al];3163 a2 = an[al-1];3164 }3165 }3166 }3167 3168 /*33169 * z = GCD(a, b)3170 */3171 static void nlGcdLong (lint a, lint b, lint * z)3172 {3173 lint h, x, y;3174 int xl,yl,hl;3175 int16 xc,yc,hc;3176 3177 x = nlCopyLint(a);3178 if (NLSIGN(x))3179 *x %= SIGNUM;3180 if (nlIsOneLint(x))3181 {3182 *z=x;3183 return;3184 }3185 y = nlCopyLint(b);3186 if (NLSIGN(y))3187 *y %= SIGNUM;3188 if (nlIsOneLint(y))3189 {3190 *z=y;3191 nlDeleteLint(x);3192 return;3193 }3194 switch (nlCompAbs(x,y))3195 {3196 case -1: h=x;x=y;y=h;h=NULL;3197 break;3198 case 0: *z=x;3199 nlDeleteLint(y);3200 return;3201 case 1: break;3202 }3203 xl = NLLENGTH(x);3204 yl = NLLENGTH(y);3205 /* we have: x > y >=0 as lint, xl>=yl>=0 as int*/3206 while (yl > 1)3207 {3208 nlRemLong(x,y,xl,yl,&hl);3209 h = x;3210 x = y;3211 y = h;3212 xl = yl;3213 yl = hl;3214 }3215 if(yl==0)3216 {3217 /*result is x with xl >1*/3218 nlDeleteLint(y);3219 nlShrink(&x);3220 *z = x;3221 return;3222 }3223 else3224 {3225 yc = y[1];3226 if (xl>1)3227 nlRemSmall(x,xl,yc,&hc);3228 else3229 {3230 hc = x[1];3231 if (hc > yc)3232 {3233 xc=yc;3234 yc=hc;3235 hc=xc;3236 }3237 }3238 }3239 nlDeleteLint(x);3240 nlDeleteLint(y);3241 /*we have now yc >hc*/3242 while(hc!=0)3243 {3244 xc = yc;3245 yc = hc;3246 hc = xc % yc;3247 }3248 *z = nlToLint(yc);3249 }3250 3251 /*-----------------------------------------------------------------*/3252 3253 /*03254 * Begin of operations with rational numbers3255 */3256 3257 /*33258 * parameter s in number:3259 * 0 : integer3260 * 1 : normalised rational3261 * -1 : not normalised rational3262 */3263 3264 #define NLISINT(a) !((a)->s)3265 #define NLISRAT(a) ((a)->s)3266 3267 #ifdef LDEBUG3268 #define nlTest(a) nlDBTest(a,__FILE__,__LINE__)3269 BOOLEAN nlDBTest(number a, char *f,int l)3270 {3271 BOOLEAN res;3272 if (a==NULL)3273 {3274 return TRUE;3275 }3276 else3277 {3278 if (a->z!=NULL)3279 {3280 res=mmDBTestBlock(a->z,(NLLENGTH(a->z)+1)*sizeof(int16),f,l);3281 }3282 if (NLISRAT(a))3283 {3284 res = (res && mmDBTestBlock(a->n,(NLLENGTH(a->n)+1)*sizeof(int16),f,l));3285 }3286 return res;3287 }3288 }3289 #endif3290 3291 /*23292 * delete a3293 */3294 #ifdef LDEBUG3295 void nlDBDelete (number *a, char *f, int l)3296 #else3297 void nlDelete (number *a)3298 #endif3299 {3300 number b=*a;3301 #ifdef LDEBUG3302 nlTest(b);3303 #endif3304 3305 if (b!=NULL)3306 {3307 nlDeleteLint(b->z);3308 if (NLISRAT(b))3309 {3310 nlDeleteLint(b->n);3311 }3312 #if defined(LDEBUG) && defined(MDEBUG)3313 mmDBFreeBlock((ADDRESS)b,sizeof(rnumber),f,l);3314 #else3315 Free((ADDRESS)b,sizeof(rnumber));3316 #endif3317 *a=NULL;3318 }3319 }3320 3321 /*23322 * r = 03323 */3324 void nlNew (number * r)3325 {3326 *r=NULL;3327 }3328 3329 /*23330 * result->z =gcd(a->z,b->z)3331 */3332 number nlGcd(number a, number b)3333 {3334 number result=(number)Alloc(sizeof(rnumber));3335 3336 result->s = 0;3337 nlGcdLong(a->z,b->z,&result->z);3338 #ifdef LDEBUG3339 nlTest(result);3340 #endif3341 return result;3342 }3343 3344 /*23345 * result->z = lcm(a->z,b->n)3346 */3347 number nlLcm(number a, number b)3348 {3349 lint t,bt;3350 number result=(number)Alloc(sizeof(rnumber));3351 3352 result->s = 0;3353 if (NLISINT(b))3354 {3355 result->z = nlCopyLint(a->z);3356 }3357 else3358 {3359 nlGcdLong(a->z,b->n,&t);3360 if (!nlIsOneLint(t))3361 {3362 bt = nlCopyLint(b->n);3363 nlQuotient(&bt,t);3364 result->z = nlMultLong(a->z,bt);3365 nlDeleteLint(bt);3366 }3367 else3368 {3369 result->z = nlMultLong(a->z,b->n);3370 }3371 nlDeleteLint(t);3372 }3373 #ifdef LDEBUG3374 nlTest(result);3375 #endif3376 return result;3377 }3378 3379 /*23380 * a == 0 ?3381 */3382 BOOLEAN nlIsZero (number a)3383 {3384 #ifdef LDEBUG3385 nlTest(a);3386 #endif3387 return (a == NULL);3388 }3389 3390 /*23391 * a = b ?3392 */3393 BOOLEAN nlEqual (number a, number b)3394 {3395 number h;3396 3397 if(a==NULL)3398 return (b==NULL);3399 if(b==NULL)3400 return(a==NULL);3401 if(NLISINT(a) && NLISINT(b))3402 {3403 return !nlComp(a->z,b->z);3404 }3405 h = nlSub(a, b);3406 if (nlIsZero(h))3407 {3408 nlDelete(&h);3409 return TRUE;3410 }3411 else3412 {3413 nlDelete(&h);3414 return FALSE;3415 }3416 }3417 3418 /*23419 * a == 1 ?3420 */3421 BOOLEAN nlIsOne (number a)3422 {3423 #ifdef LDEBUG3424 nlTest(a);3425 #endif3426 if(nlIsZero(a) || NLSIGN(a->z))3427 {3428 return FALSE;3429 }3430 if(NLISINT(a))3431 {3432 return ((a->z[1]==1) && (a->z[0]==1));3433 }3434 if (nlCompAbs(a->z, a->n))3435 {3436 return FALSE;3437 }3438 nlDeleteLint(a->z);3439 nlDeleteLint(a->n);3440 a->z = nlInitOne();3441 a->s = 0;3442 return TRUE;3443 }3444 3445 /*23446 * a == -1 ?3447 */3448 BOOLEAN nlIsMOne (number a)3449 {3450 #ifdef LDEBUG3451 nlTest(a);3452 #endif3453 if(nlIsZero(a) || !NLSIGN(a->z))3454 {3455 return FALSE;3456 }3457 if(NLISINT(a))3458 {3459 return ((a->z[1]==1) && (a->z[0]==SIGNUM+1));3460 }3461 if (nlCompAbs(a->z, a->n))3462 {3463 return FALSE;3464 }3465 nlDeleteLint(a->z);3466 nlDeleteLint(a->n);3467 a->z = nlInitOne();3468 a->z[0] += SIGNUM;3469 a->s = 0;3470 return TRUE;3471 }3472 3473 /*23474 * z := i3475 */3476 number nlInit (int i)3477 {3478 lint nom;3479 BOOLEAN neg = FALSE;3480 number z = NULL;3481 3482 if (i!=0)3483 {3484 z = (number)Alloc(sizeof(rnumber));3485 z->s = 0;3486 if (i < 0)3487 {3488 neg = TRUE;3489 i = -i;3490 }3491 if (i>=BASIS) // init a 4 byte number3492 {3493 nom = nlAllocLint(2);3494 nom[1] = (int16) (MOD_BASIS&i);3495 nom[2] = (int16) (i>>SHIFT_BASIS);3496 }3497 else // init a 2 byte number3498 {3499 nom = nlAllocLint(1);3500 nom[1] = (int16) i;3501 }3502 if (neg)3503 {3504 *nom += SIGNUM;3505 }3506 z->z = nom;3507 #ifdef LDEBUG3508 nlTest(z);3509 #endif3510 }3511 return z;3512 }3513 3514 /*23515 * convert number to int3516 */3517 int nlInt(number &a)3518 {3519 lint x;3520 int res, i;3521 3522 if (nlIsZero(a))3523 {3524 return 0;3525 }3526 nlNormalize(a);3527 if (NLISRAT(a))3528 {3529 return 0;3530 }3531 x = a->z;3532 i = NLLENGTH(x);3533 if (i > 2)3534 {3535 return 0;3536 }3537 res = x[1];3538 if (i == 2)3539 {3540 if (x[2] < SIGNUM)3541 {3542 res += BASIS*x[2];3543 }3544 else3545 {3546 return 0;3547 }3548 }3549 if (NLSIGN(x))3550 {3551 return -res;3552 }3553 else3554 {3555 return res;3556 }3557 }3558 3559 /*23560 * simplify x3561 */3562 void nlNormalize (number &x)3563 {3564 lint zh, nh, d;3565 3566 if ((x==NULL)||(x->s>=0))3567 {3568 return;3569 }3570 x->s = 1;3571 zh = x->z;3572 nh = x->n;3573 nlGcdLong(zh, nh, &d);3574 if (!nlIsOneLint(d))3575 {3576 nlQuotient(&zh,d);3577 x->z = zh;3578 nlQuotient(&nh,d);3579 if (nlIsOneLint(nh))3580 {3581 nlDeleteLint(nh);3582 x->s = 0;3583 }3584 else3585 {3586 x->n = nh;3587 }3588 }3589 nlDeleteLint(d);3590 #ifdef LDEBUG3591 nlTest(x);3592 #endif3593 }3594 3595 /*23596 * za >= 0 ?3597 */3598 BOOLEAN nlGreaterZero (number a)3599 {3600 if (nlIsZero(a))3601 {3602 return TRUE;3603 }3604 return !NLSIGN(a->z);3605 }3606 3607 /*23608 * a := -a3609 */3610 number nlNeg (number a)3611 {3612 if (!nlIsZero(a))3613 {3614 a->z[0] = NLNEG(a->z);3615 }3616 return a;3617 }3618 3619 /*3620 * 1/a3621 */3622 number nlInvers(number a)3623 {3624 number n=(number)Alloc(sizeof(rnumber));3625 3626 if (nlIsZero(a))3627 {3628 WerrorS("div. 1/0");3629 Free((ADDRESS)n,sizeof(rnumber));3630 return NULL;3631 }3632 nlNormalize(a);3633 if(NLISRAT(a))3634 {3635 n->z = nlCopyLint(a->n);3636 }3637 else3638 {3639 n->z = nlInitOne();3640 }3641 if((a->z[1]!=1) || (NLLENGTH(a->z)!=1))3642 {3643 n->n = nlCopyLint(a->z);3644 n->s = 1;3645 if(n->n[0]>SIGNUM)3646 {3647 n->n[0] -= SIGNUM;3648 n->z[0] += SIGNUM;3649 }3650 }3651 else3652 {3653 if (NLSIGN(a->z))3654 {3655 n->z[0] += SIGNUM;3656 }3657 n->s = 0;3658 }3659 #ifdef LDEBUG3660 nlTest(n);3661 #endif3662 return n;3663 }3664 3665 /*23666 * copy a to b3667 */3668 number nlCopy(number a)3669 {3670 number b=(number)Alloc0(sizeof(rnumber));3671 3672 if (!nlIsZero(a))3673 {3674 b->s = a->s;3675 b->z = nlCopyLint(a->z);3676 if (NLISRAT(a))3677 {3678 b->n = nlCopyLint(a->n);3679 }3680 #ifdef LDEBUG3681 nlTest(b);3682 #endif3683 return b;3684 }3685 else3686 {3687 Free((ADDRESS)b,sizeof(rnumber));3688 return NULL;3689 }3690 }3691 3692 /*23693 * lu:= la + li3694 */3695 number nlAdd (number la, number li)3696 {3697 number lu;3698 lint x, y;3699 BOOLEAN a0 = nlIsZero(la), i0 = nlIsZero(li);3700 3701 if (a0 || i0)3702 {3703 if (a0)3704 {3705 return nlCopy(li);3706 }3707 else3708 {3709 return nlCopy(la);3710 }3711 }3712 lu = (number)Alloc(sizeof(rnumber));3713 if (NLISINT(la))3714 {3715 if (NLISINT(li))3716 {3717 lu->z = nlAddLong(la->z, li->z);3718 if (lu->z==NULL)3719 {3720 Free((ADDRESS)lu,sizeof(rnumber));3721 return NULL;3722 }3723 lu->s = 0;3724 #ifdef LDEBUG3725 nlTest(lu);3726 #endif3727 return lu;3728 }3729 else3730 {3731 x = nlMultLong(la->z, li->n);3732 lu->z = nlAddLong(x, li->z);3733 nlDeleteLint(x);3734 lu->n = nlCopyLint(li->n);3735 }3736 }3737 else3738 {3739 if (NLISINT(li))3740 {3741 y = nlMultLong(li->z, la->n);3742 lu->z = nlAddLong(la->z, y);3743 nlDeleteLint(y);3744 lu->n = nlCopyLint(la->n);3745 }3746 else3747 {3748 x = nlMultLong(la->z, li->n);3749 y = nlMultLong(li->z, la->n);3750 lu->z = nlAddLong(x, y);3751 nlDeleteLint(x);3752 nlDeleteLint(y);3753 lu->n = nlMultLong(la->n, li->n);3754 }3755 }3756 if (lu->z==NULL)3757 {3758 nlDeleteLint(lu->n);3759 Free((ADDRESS)lu,sizeof(rnumber));3760 return NULL;3761 }3762 lu->s = -1;3763 #ifdef LDEBUG3764 nlTest(lu);3765 #endif3766 return lu;3767 }3768 3769 /*23770 * lu:= la - li3771 */3772 number nlSub (number la, number li)3773 {3774 number lu;3775 lint x, y;3776 BOOLEAN a0 = nlIsZero(la), i0 = nlIsZero(li);3777 3778 if (a0 || i0)3779 {3780 if (a0)3781 {3782 lu = nlCopy(li);3783 lu = nlNeg(lu);3784 return lu;3785 }3786 else3787 {3788 return nlCopy(la);3789 }3790 }3791 lu = (number)Alloc(sizeof(rnumber));3792 if (NLISINT(la))3793 {3794 if (NLISINT(li))3795 {3796 lu->z = nlSubLong(la->z, li->z);3797 if (lu->z==NULL)3798 {3799 Free((ADDRESS)lu,sizeof(rnumber));3800 return NULL;3801 }3802 lu->s = 0;3803 #ifdef LDEBUG3804 nlTest(lu);3805 #endif3806 return lu;3807 }3808 else3809 {3810 x = nlMultLong(la->z, li->n);3811 lu->z = nlSubLong(x, li->z);3812 nlDeleteLint(x);3813 lu->n = nlCopyLint(li->n);3814 }3815 }3816 else3817 {3818 if (NLISINT(li))3819 {3820 y = nlMultLong(li->z, la->n);3821 lu->z = nlSubLong(la->z, y);3822 nlDeleteLint(y);3823 lu->n = nlCopyLint(la->n);3824 }3825 else3826 {3827 x = nlMultLong(la->z, li->n);3828 y = nlMultLong(li->z, la->n);3829 lu->z = nlSubLong(x, y);3830 nlDeleteLint(x);3831 nlDeleteLint(y);3832 lu->n = nlMultLong(la->n, li->n);3833 }3834 }3835 if (lu->z==NULL)3836 {3837 nlDeleteLint(lu->n);3838 Free((ADDRESS)lu,sizeof(rnumber));3839 return NULL;3840 }3841 lu->s = -1;3842 #ifdef LDEBUG3843 nlTest(lu);3844 #endif3845 return lu;3846 }3847 3848 /*23849 * lo := la * li3850 */3851 number nlMult (number la, number li)3852 {3853 number lo=(number)Alloc(sizeof(rnumber));3854 3855 if (nlIsZero(la) || nlIsZero(li))3856 {3857 Free((ADDRESS)lo,sizeof(rnumber));3858 return NULL;3859 }3860 lo->z = nlMultLong(la->z,li->z);3861 if(NLISINT(la))3862 {3863 if(NLISINT(li))3864 {3865 lo->s = 0;3866 #ifdef LDEBUG3867 nlTest(lo);3868 #endif3869 return lo;3870 }3871 else3872 {3873 lo->n = nlCopyLint(li->n);3874 }3875 }3876 else3877 {3878 if(NLISINT(li))3879 {3880 lo->n = nlCopyLint(la->n);3881 }3882 else3883 {3884 lo->n = nlMultLong(la->n,li->n);3885 }3886 }3887 lo->s = -1;3888 #ifdef LDEBUG3889 nlTest(lo);3890 #endif3891 return lo;3892 }3893 3894 /*23895 * lo := la / li in Z3896 * with |li| > la - (lo * li) >= 03897 */3898 number nlIntDiv (number la, number li)3899 {3900 int c;3901 number lo=(number)Alloc(sizeof(rnumber));3902 3903 if (nlIsZero(la) || nlIsZero(li))3904 {3905 if (nlIsZero(li))3906 {3907 WerrorS("div. by 0");3908 }3909 Free((ADDRESS)lo,sizeof(rnumber));3910 return NULL;3911 }3912 lo->s = 0;3913 c = nlCompAbs(li->z, la->z);3914 if (c >= 0)3915 {3916 if (c == 0)3917 {3918 lo->z = nlInitOne();3919 if (NLSIGN(la->z) != NLSIGN(li->z))3920 {3921 lo->z[0] += SIGNUM;3922 }3923 }3924 else if (NLSIGN(la->z))3925 {3926 lo->z = nlInitOne();3927 if (!NLSIGN(li->z))3928 {3929 lo->z[0] += SIGNUM;3930 }3931 }3932 else3933 {3934 Free((ADDRESS)lo,sizeof(rnumber));3935 return NULL;3936 }3937 #ifdef LDEBUG3938 nlTest(lo);3939 #endif3940 return lo;3941 }3942 lo->z = nlCopyLint(la->z);3943 if (NLLENGTH(la->z)==1)3944 {3945 lo->z[1] /= li->z[1];3946 if (NLSIGN(la->z))3947 {3948 int16 h = la->z[1] - (lo->z[1]*li->z[1]);3949 if (h!=0)3950 {3951 lo->z[1]++;3952 }3953 }3954 if (NLSIGN(li->z))3955 {3956 lo->z[0] = NLNEG(lo->z);3957 }3958 #ifdef LDEBUG3959 nlTest(lo);3960 #endif3961 return lo;3962 }3963 if ((li->z[1]!=1) || (NLLENGTH(li->z)>1))3964 {3965 nlQuotient(&lo->z, li->z);3966 if (NLSIGN(la->z))3967 {3968 lint o1, t, h = nlMultLong(lo->z, li->z);3969 if (nlCompAbs(h, la->z))3970 {3971 o1 = nlInitOne();3972 if (NLSIGN(li->z))3973 {3974 t = nlAddLong(lo->z,o1);3975 }3976 else3977 {3978 t = nlSubLong(lo->z,o1);3979 }3980 nlDeleteLint(o1);3981 nlDeleteLint(lo->z);3982 lo->z = t;3983 }3984 nlDeleteLint(h);3985 }3986 }3987 else if (li->z[0] == (SIGNUM+1))3988 {3989 lo->z[0] = NLNEG(lo->z);3990 }3991 #ifdef LDEBUG3992 nlTest(lo);3993 #endif3994 return lo;3995 }3996 3997 /*23998 * lo := la mod li == la - ((la / li) * li)3999 * with |li| > lo >= 04000 */4001 number nlIntMod (number la, number li)4002 {4003 int16 xl, yl;4004 int c;4005 lint r, t;4006 number lo=(number)Alloc(sizeof(rnumber));4007 4008 if (nlIsZero(la) || nlIsZero(li))4009 {4010 if (nlIsZero(li))4011 {4012 WerrorS("mod. by 0");4013 }4014 Free((ADDRESS)lo,sizeof(rnumber));4015 return NULL;4016 }4017 c = nlCompAbs(li->z, la->z);4018 if (c == 0)4019 {4020 Free((ADDRESS)lo,sizeof(rnumber));4021 return NULL;4022 }4023 if (c > 0)4024 {4025 if (NLSIGN(la->z))4026 {4027 if (NLSIGN(li->z))4028 {4029 lo->z = nlSubLong(la->z,li->z);4030 }4031 else4032 {4033 lo->z = nlAddLong(la->z,li->z);4034 }4035 }4036 else4037 {4038 lo->z = nlCopyLint(la->z);4039 }4040 #ifdef LDEBUG4041 nlTest(lo);4042 #endif4043 return lo;4044 }4045 r = nlCopyLint(la->z);4046 xl = NLLENGTH(r);4047 if (xl == 1)4048 {4049 yl = r[1] / li->z[1];4050 r[1] -= yl * li->z[1];4051 if (r[1] != 0)4052 {4053 if (NLSIGN(la->z))4054 {4055 r[1] = li->z[1]-r[1];4056 }4057 lo->z = r;4058 #ifdef LDEBUG4059 nlTest(lo);4060 #endif4061 return lo;4062 }4063 else4064 {4065 nlDeleteLint(r);4066 Free((ADDRESS)lo,sizeof(rnumber));4067 return NULL;4068 }4069 }4070 yl = NLLENGTH(li->z);4071 if (yl > 1)4072 {4073 nlRemLong(r,li->z,xl,yl,&c);4074 xl = c;4075 if (xl != 0)4076 {4077 nlAllocLint(xl);4078 for (c=xl; c>0; c--)4079 {4080 t[c] = r[c];4081 }4082 }4083 }4084 else if (li->z[1]!=1)4085 {4086 nlRemSmall(r,xl,li->z[1],&xl);4087 if (xl != 0)4088 {4089 t = nlAllocLint(1);4090 t[1] = xl;4091 }4092 }4093 else4094 {4095 xl = 0;4096 }4097 nlDeleteLint(r);4098 if (xl == 0)4099 {4100 Free((ADDRESS)lo,sizeof(rnumber));4101 return NULL;4102 }4103 if (NLSIGN(la->z))4104 {4105 if (NLSIGN(li->z))4106 {4107 r = nlSubLong(t,li->z);4108 }4109 else4110 {4111 r = nlSubLong(li->z,t);4112 }4113 nlDeleteLint(t);4114 t = r;4115 }4116 lo->z = t;4117 #ifdef LDEBUG4118 nlTest(lo);4119 #endif4120 return lo;4121 }4122 4123 /*24124 * lo := la / li4125 */4126 number nlDiv (number la, number li)4127 {4128 number lo=(number)Alloc(sizeof(rnumber));4129 4130 if (nlIsZero(la) || nlIsZero(li))4131 {4132 if (nlIsZero(li))4133 {4134 WerrorS("div. by 0");4135 }4136 Free((ADDRESS)lo,sizeof(rnumber));4137 return NULL;4138 }4139 if (NLISINT(la))4140 {4141 lo->n = nlCopyLint(li->z);4142 }4143 else4144 {4145 lo->n = nlMultLong(la->n, li->z);4146 }4147 if (NLISINT(li))4148 {4149 lo->z = nlCopyLint(la->z);4150 }4151 else4152 {4153 lo->z = nlMultLong(la->z, li->n);4154 }4155 if(NLSIGN(lo->n))4156 {4157 lo->n[0] -= SIGNUM;4158 lo->z[0] = NLNEG(lo->z);4159 }4160 lo->s = -1;4161 if(nlIsOneLint(lo->n))4162 {4163 nlDeleteLint(lo->n);4164 lo->s = 0;4165 }4166 #ifdef LDEBUG4167 nlTest(lo);4168 #endif4169 return lo;4170 }4171 4172 /*34173 * Power of long; lu:= x^exp4174 */4175 static void nlPow (lint x,int exp,lint *lu)4176 {4177 lint yh, y, w;4178 y = nlInitOne();4179 w = nlCopyLint(x);4180 while (exp)4181 {4182 if (exp%2 != 0)4183 {4184 yh = nlMultLong(y,w);4185 nlDeleteLint(y);4186 y = yh;4187 }4188 if (exp > 1)4189 {4190 yh = nlMultLong(w,w);4191 nlDeleteLint(w);4192 w = yh;4193 }4194 exp = exp / 2;4195 }4196 *lu = y;4197 }4198 4199 /*24200 * lu:= x ^ exp4201 */4202 void nlPower (number x,int exp,number * lu)4203 {4204 number b=NULL;4205 4206 if (!nlIsZero(x))4207 {4208 b = (number)Alloc(sizeof(rnumber));4209 if (exp == 0)4210 {4211 b->z = nlInitOne();4212 *lu = b;4213 #ifdef LDEBUG4214 nlTest(*lu);4215 #endif4216 return;4217 }4218 nlNormalize(x);4219 if (exp > 0)4220 {4221 b->s = x->s;4222 if ((x->z[1]!=1) || (NLLENGTH(x->z)>1))4223 {4224 nlPow(x->z,exp,&b->z);4225 }4226 else4227 {4228 b->z = nlInitOne();4229 }4230 if (NLISRAT(x))4231 {4232 nlPow(x->n,exp,&b->n);4233 }4234 }4235 else4236 {4237 if ((x->z[1]!=1) || (NLLENGTH(x->z)>1))4238 {4239 b->s = 1;4240 nlPow(x->z,exp,&b->n);4241 if (NLSIGN(b->n))4242 {4243 b->n[0] -= SIGNUM;4244 }4245 }4246 else4247 {4248 b->s = 0;4249 }4250 if (NLISRAT(x))4251 {4252 nlPow(x->n,exp,&b->z);4253 }4254 else4255 {4256 b->z = nlInitOne();4257 }4258 }4259 if (exp%2 != 0) // make sign4260 {4261 if(NLSIGN(b->z) != NLSIGN(x->z))4262 {4263 b->z[0] = NLNEG(b->z);4264 }4265 }4266 else4267 {4268 if(NLSIGN(b->z))4269 {4270 b->z[0] -= SIGNUM;4271 }4272 }4273 }4274 *lu = b;4275 #ifdef LDEBUG4276 nlTest(*lu);4277 #endif4278 }4279 4280 /*24281 * a > b ?4282 */4283 BOOLEAN nlGreater (number a, number b)4284 {4285 number r;4286 BOOLEAN rr;4287 4288 r=nlSub(a,b);4289 rr=(!nlIsZero(r)) && (nlGreaterZero(r));4290 nlDelete(&r);4291 return rr;4292 }4293 4294 /*34295 * a := b * a + r4296 */4297 void nlDivMod(lint a, int16 al, int16 b, int16 * r)4298 {4299 int32 da;4300 const int32 db = (int32)b;4301 int16 a1 = a[al];4302 4303 a[al] /= b;4304 a1 -= b*a[al];4305 loop4306 {4307 al--;4308 if (al==0)4309 {4310 *r = a1;4311 return;4312 }4313 while (a1==0)4314 {4315 a1 = a[al];4316 a[al] /= b;4317 a1 -= b*a[al];4318 al--;4319 if (al==0)4320 {4321 *r = a1;4322 return;4323 }4324 }4325 da = BASIS*(int32) a1 + (int32) a[al];4326 a[al] = da / db;4327 da -= db*a[al];4328 a1 = (int16)da;4329 }4330 }4331 4332 /*24333 * returns (n->z mod p) / (n->n mod p)4334 */4335 int nlModP(number n, int p)4336 {4337 number a;4338 int16 iz, in;4339 int jz, jn, al;4340 4341 if (nlIsZero(n))4342 {4343 return 0;4344 }4345 a = nCopy(n);4346 al = NLLENGTH(a->z);4347 if (al == 1)4348 {4349 iz = (a->z[1])%p;4350 }4351 else4352 {4353 nlRemSmall(a->z,al,p,&iz);4354 }4355 jz = iz;4356 if (NLSIGN(a->z))4357 {4358 jz = p-jz;4359 }4360 if (NLISRAT(a))4361 {4362 al = *(a->n);4363 if (al == 1)4364 {4365 in = (a->n[1])%p;4366 }4367 else4368 {4369 nlRemSmall(a->n,al,p,&in);4370 }4371 jn = in;4372 jz = (int)npDiv((number)jz,(number)jn);4373 }4374 nlDelete(&a);4375 return jz;4376 }4377 4378 int nlSize(number a)4379 {4380 /*to be implemtented*/ return 0;4381 }4382 #endif4383 -
Singular/longrat.h
rb911ac r27b799 4 4 * Computer Algebra System SINGULAR * 5 5 ****************************************/ 6 /* $Id: longrat.h,v 1. 8 1997-08-12 17:14:40Singular Exp $ */6 /* $Id: longrat.h,v 1.9 1998-05-27 17:14:07 Singular Exp $ */ 7 7 /* 8 8 * ABSTRACT: computation with long rational numbers … … 10 10 #include "structs.h" 11 11 12 #ifdef HAVE_GMP13 12 extern "C" { 14 13 #include <gmp.h> … … 37 36 #endif 38 37 //#define mpz_size1(A) mpz_size(A) 39 40 #else41 42 typedef unsigned short int16;43 typedef int16 * lint;44 #endif45 38 46 39 struct snumber; … … 95 88 BOOLEAN nlSetMap(int c, char ** par, int nop, number minpol); 96 89 97 // internal use (longrat0) only:98 #ifndef HAVE_GMP99 void nlDivMod(lint a, int16 al, int16 b, int16 * r);100 #endif101 90 #endif 102 91 -
Singular/longrat0.cc
rb911ac r27b799 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: longrat0.cc,v 1. 5 1997-06-04 19:45:21 obachmanExp $ */4 /* $Id: longrat0.cc,v 1.6 1998-05-27 17:14:08 Singular Exp $ */ 5 5 /* 6 6 * ABSTRACT - … … 15 15 #include "febase.h" 16 16 #include "longrat.h" 17 18 #ifdef HAVE_GMP19 17 20 18 #define SR_HDL(A) ((long)(A)) … … 146 144 } 147 145 148 #else149 #define NLLENGTH(a) ((int)(*(a)&0X7FFF))150 #define NUM_BASIS 10000151 #define NLISRAT(a) ((a)->s)152 153 /*3154 * extracts a long integer from s, returns the rest155 */156 static char * nlEatLong(char *s, number *i)157 {158 number j, k, ten, n;159 j = nlInit(0);160 ten = nlInit(10);161 while (*s >= '0' && *s <= '9')162 {163 k = nlMult(j, ten);164 nlDelete(&j);165 j = nlInit((int)*s-(int)'0');166 s++;167 n = nlAdd(j, k);168 nlDelete(&j);169 nlDelete(&k);170 j = n;171 }172 nlDelete(&ten);173 *i = j;174 return s;175 }176 177 /*2178 * extracts the numberio a from s, returns the rest179 */180 char * nlRead (char *s, number *a)181 {182 number z, n;183 BOOLEAN neg = (*s == '-');184 185 if (*s == '+' || *s == '-')186 s++;187 if (*s<'0' || *s>'9')188 {189 if (neg)190 *a = nlInit(-1);191 else192 *a = nlInit(1);193 return s;194 }195 s = nlEatLong(s, &z);196 if (neg)197 nlNeg(z);198 *a = z;199 if (*s == '/')200 {201 s++;202 s = nlEatLong(s, &n);203 if (nlIsZero(n))204 {205 WerrorS("Zero Denominator");206 nlDelete(&n);207 nlDelete(a);208 return s;209 }210 if (nlIsOne(n))211 nlDelete(&n);212 else213 {214 *a = nlDiv(z, n);215 nlNormalize(*a);216 }217 }218 return s;219 }220 221 /*3222 * write long integer, assume n!=NULL223 */224 static void nlWriteLong(lint n)225 {226 char save;227 char *lonstr, *str1;228 int i, k, j;229 lint a;230 int16 al, w;231 al = NLLENGTH(n);232 k = al+1;233 i = k*sizeof(int16);234 a = (lint)Alloc(i);235 memcpy(a, n, i);236 j = k*5*sizeof(char);237 lonstr = (char *)Alloc(j);238 str1 = lonstr+(k*5-1);239 *str1 = '\0';240 do241 {242 nlDivMod(a, al, NUM_BASIS, &w);243 save = *str1;244 str1 -= 4;245 sprintf(str1, "%04u", w);246 str1[4] = save;247 if (a[al]==0)248 al--;249 } while (al!=0);250 Free((ADDRESS)a,i);251 while (*str1 == '0') str1++;252 StringAppend(str1);253 Free((ADDRESS)lonstr, j);254 }255 256 /*2257 * write a number258 */259 void nlWrite (number &a)260 {261 if (nlIsZero(a))262 {263 StringAppend("0");264 return;265 }266 if (!nlGreaterZero(a))267 {268 StringAppend("-");269 }270 if (a->s<0)271 nlNormalize(a);272 nlWriteLong(a->z);273 if (NLISRAT(a))274 {275 StringAppend("/");276 nlWriteLong(a->n);277 }278 }279 #endif280 -
Singular/misc.cc
rb911ac r27b799 42 42 extern const char * libfac_date; 43 43 #endif 44 #ifdef HAVE_GMP45 44 extern "C" { 46 45 #include <gmp.h> 47 46 } 48 #endif49 47 #ifdef HAVE_MPSR 50 48 #include <MP_Config.h> … … 670 668 StringAppend("libfac(%s,%s),\n\t",libfac_version,libfac_date); 671 669 #endif 672 #ifdef HAVE_GMP673 670 #if defined (__GNU_MP_VERSION) && defined (__GNU_MP_VERSION_MINOR) 674 671 StringAppend("GMP(%d.%d),",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR); … … 678 675 StringAppendS("GMP(1.3),"); 679 676 #endif 680 #endif681 677 #ifdef HAVE_MPSR 682 678 StringAppend("MP(%s),",MP_VERSION); -
Singular/mminit.cc
rb911ac r27b799 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: mminit.cc,v 1.1 1 1998-05-13 14:53:46Singular Exp $ */4 /* $Id: mminit.cc,v 1.12 1998-05-27 17:14:09 Singular Exp $ */ 5 5 /* 6 6 * ABSTRACT: init of memory management … … 18 18 #include "mmprivat.h" 19 19 extern "C" { /* begin of "C" */ 20 #ifdef HAVE_GMP21 20 #include <gmp.h> 22 #endif23 21 24 22 #ifdef ALIGN_8 … … 137 135 } 138 136 139 #if def HAVE_GMP137 #ifndef HAVE_SMALLGMP 140 138 #ifdef MDEBUG 141 139 void * mgAllocBlock( size_t t) … … 164 162 int mmInit( void ) 165 163 { 166 #if def HAVE_GMP164 #ifndef HAVE_SMALLGMP 167 165 if(mmIsInitialized==0) 168 166 {
Note: See TracChangeset
for help on using the changeset viewer.