home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / MPMATH_C.C < prev    next >
C/C++ Source or Header  |  1991-07-07  |  11KB  |  435 lines

  1. /* MPMath_c.c (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2.      All rights reserved.
  3.  
  4.    Code may be used in any program provided the author is credited
  5.      either during program execution or in the documentation.  Source
  6.      code may be distributed only in combination with public domain or
  7.      shareware source code.  Source code may be modified provided the
  8.      copyright notice and this message is left unchanged and all
  9.      modifications are clearly documented.
  10.  
  11.      I would appreciate a copy of any work which incorporates this code,
  12.      however this is optional.
  13.  
  14.      Mark C. Peterson
  15.      405-C Queen St. Suite #181
  16.      Southington, CT 06489
  17.      (203) 276-9721
  18. */
  19.  
  20. #include "mpmath.h"
  21.  
  22. int MPaccuracy = 32;
  23.  
  24. struct MP *MPsub(struct MP x, struct MP y) {
  25.    y.Exp ^= 0x8000;
  26.    return(MPadd(x, y));
  27. }
  28.  
  29. /* added by TW */
  30. struct MP *MPsub086(struct MP x, struct MP y) {
  31.    y.Exp ^= 0x8000;
  32.    return(MPadd086(x, y));
  33. }
  34.  
  35. /* added by TW */
  36. struct MP *MPsub386(struct MP x, struct MP y) {
  37.    y.Exp ^= 0x8000;
  38.    return(MPadd386(x, y));
  39. }
  40.  
  41. struct MP *MPabs(struct MP x) {
  42.    x.Exp &= 0x7fff;
  43.    return(&x);
  44. }
  45.  
  46. struct MPC MPCsqr(struct MPC x) {
  47.    struct MPC z;
  48.  
  49.         z.x = *pMPsub(*pMPmul(x.x, x.x), *pMPmul(x.y, x.y));
  50.         z.y = *pMPmul(x.x, x.y);
  51.         z.y.Exp++;
  52.    return(z);
  53. }
  54.  
  55. struct MP MPCmod(struct MPC x) {
  56.         return(*pMPadd(*pMPmul(x.x, x.x), *pMPmul(x.y, x.y)));
  57. }
  58.  
  59. struct MPC MPCmul(struct MPC x, struct MPC y) {
  60.    struct MPC z;
  61.  
  62.         z.x = *pMPsub(*pMPmul(x.x, y.x), *pMPmul(x.y, y.y));
  63.         z.y = *pMPadd(*pMPmul(x.x, y.y), *pMPmul(x.y, y.x));
  64.    return(z);
  65. }
  66.  
  67. struct MPC MPCdiv(struct MPC x, struct MPC y) {
  68.    struct MP mod;
  69.  
  70.    mod = MPCmod(y);
  71.         y.y.Exp ^= 0x8000;
  72.         y.x = *pMPdiv(y.x, mod);
  73.         y.y = *pMPdiv(y.y, mod);
  74.    return(MPCmul(x, y));
  75. }
  76.  
  77. struct MPC MPCadd(struct MPC x, struct MPC y) {
  78.    struct MPC z;
  79.  
  80.         z.x = *pMPadd(x.x, y.x);
  81.         z.y = *pMPadd(x.y, y.y);
  82.    return(z);
  83. }
  84.  
  85. struct MPC MPCsub(struct MPC x, struct MPC y) {
  86.    struct MPC z;
  87.  
  88.         z.x = *pMPsub(x.x, y.x);
  89.         z.y = *pMPsub(x.y, y.y);
  90.    return(z);
  91. }
  92.  
  93. struct MPC MPCone = { 0x3fff, 0x80000000l, 0, 0l };
  94.  
  95. struct MPC MPCpow(struct MPC x, int exp) {
  96.    struct MPC z;
  97.    struct MPC zz;
  98.  
  99.    if(exp & 1)
  100.       z = x;
  101.    else
  102.       z = MPCone;
  103.    exp >>= 1;
  104.    while(exp) {
  105.                 zz.x = *pMPsub(*pMPmul(x.x, x.x), *pMPmul(x.y, x.y));
  106.                 zz.y = *pMPmul(x.x, x.y);
  107.                 zz.y.Exp++;
  108.       x = zz;
  109.       if(exp & 1) {
  110.                         zz.x = *pMPsub(*pMPmul(z.x, x.x), *pMPmul(z.y, x.y));
  111.                         zz.y = *pMPadd(*pMPmul(z.x, x.y), *pMPmul(z.y, x.x));
  112.          z = zz;
  113.       }
  114.       exp >>= 1;
  115.    }
  116.    return(z);
  117. }
  118.  
  119. int MPCcmp(struct MPC x, struct MPC y) {
  120.    struct MPC z;
  121.  
  122.         if(pMPcmp(x.x, y.x) || pMPcmp(x.y, y.y)) {
  123.                 z.x = MPCmod(x);
  124.                 z.y = MPCmod(y);
  125.                 return(pMPcmp(z.x, z.y));
  126.    }
  127.    else
  128.       return(0);
  129. }
  130.  
  131. struct complex MPC2cmplx(struct MPC x) {
  132.    struct complex z;
  133.  
  134.         z.x = *pMP2d(x.x);
  135.         z.y = *pMP2d(x.y);
  136.    return(z);
  137. }
  138.  
  139. struct MPC cmplx2MPC(struct complex z) {
  140.    struct MPC x;
  141.  
  142.         x.x = *pd2MP(z.x);
  143.         x.y = *pd2MP(z.y);
  144.    return(x);
  145. }
  146.  
  147. /* function pointer versions added by Tim Wegner 12/07/89 */
  148. int        (*ppMPcmp)() = MPcmp086;
  149. int        (*pMPcmp)(struct MP x, struct MP y) = MPcmp086;
  150. struct MP  *(*pMPmul)(struct MP x, struct MP y) = MPmul086;
  151. struct MP  *(*pMPdiv)(struct MP x, struct MP y) = MPdiv086;
  152. struct MP  *(*pMPadd)(struct MP x, struct MP y) = MPadd086;
  153. struct MP  *(*pMPsub)(struct MP x, struct MP y) = MPsub086;
  154. struct MP  *(*pd2MP)(double x)                 = d2MP086 ;
  155. double *(*pMP2d)(struct MP m)          = MP2d086 ;
  156. struct MP  *(*pfg2MP)(long x, int fg)          = fg2MP086;
  157.  
  158. void setMPfunctions(void) {
  159.    if(cpu == 386)
  160.    {
  161.       pMPmul = MPmul386;
  162.       pMPdiv = MPdiv386;
  163.       pMPadd = MPadd386;
  164.       pMPsub = MPsub386;
  165.       pMPcmp = MPcmp386;
  166.       pd2MP  = d2MP386 ;
  167.       pMP2d  = MP2d386 ;
  168.       pfg2MP = fg2MP386;
  169.    }
  170.    else
  171.    {
  172.       pMPmul = MPmul086;
  173.       pMPdiv = MPdiv086;
  174.       pMPadd = MPadd086;
  175.       pMPsub = MPsub086;
  176.       pMPcmp = MPcmp086;
  177.       pd2MP  = d2MP086 ;
  178.       pMP2d  = MP2d086 ;
  179.       pfg2MP = fg2MP086;
  180.    }
  181. }
  182.  
  183. #define sqr(x) ((x) * (x))
  184.  
  185. extern int debugflag, fpu;
  186.  
  187. struct complex ComplexPower(struct complex x, struct complex y) {
  188.    struct complex z, cLog, t;
  189.    double e2x, siny, cosy;
  190.  
  191.    FPUcplxlog(&x, &cLog);
  192.    FPUcplxmul(&cLog, &y, &t);
  193.  
  194.    if(fpu == 387)
  195.       FPUcplxexp387(&t, &z);
  196.    else {
  197.       e2x = exp(t.x);
  198.       FPUsincos(&t.y, &siny, &cosy);
  199.       z.x = e2x * cosy;
  200.       z.y = e2x * siny;
  201.    }
  202.    return(z);
  203. }
  204.  
  205.  
  206. /***** FRACTINT specific routines and variables *****/
  207.  
  208. #ifndef TESTING_MATH
  209.  
  210. #include "fractint.h"
  211.  
  212. extern double param[];
  213. extern struct complex old, new, init;
  214. extern double threshold, roverd, d1overd, dx0[], dy0[];
  215. extern int periodicitycheck, row, col, debugflag;
  216.  
  217.  
  218. #include <float.h>
  219. #include <stdlib.h>
  220.  
  221. extern int  xdots, ydots;     /* coordinates of dots on the screen  */
  222. extern int  colors;           /* maximum colors available */
  223. extern int  maxit;
  224.  
  225. unsigned char far *LogTable = (unsigned char far *)0;
  226. extern int LogFlag;
  227.    /* LogFlag == 1  -- standard log palettes
  228.       LogFlag == -1 -- 'old' log palettes
  229.       LogFlag >  1  -- compress counts < LogFlag into color #1
  230.       LogFlag < -1  -- use quadratic palettes based on square roots && compress
  231.    */
  232.  
  233. void SetupLogTable(void) {
  234.    float l, f, c, m;
  235.    unsigned n, prev, limit, lf;
  236.  
  237.    if (LogFlag > -2) {
  238.       lf = (LogFlag > 1) ? LogFlag : 0;
  239.       if (lf >= maxit)
  240.          lf = maxit - 1;
  241.       Fg2Float((long)(maxit-lf), 0, m);
  242.       fLog14(m, m);
  243.       Fg2Float((long)(colors-(lf?2:1)), 0, c);
  244.       fDiv(m, c, m);
  245.       for (prev = 1; prev <= lf; prev++)
  246.          LogTable[prev] = 1;
  247.       for (n = (lf?2:1); n < colors; n++) {
  248.          Fg2Float((long)n, 0, f);
  249.          fMul16(f, m, f);
  250.          fExp14(f, l);
  251.          limit = Float2Fg(l, 0) + lf;
  252.          if (limit > maxit || n == colors-1)
  253.             limit = maxit;
  254.          while (prev <= limit)
  255.             LogTable[prev++] = n;
  256.       }
  257.    } else {
  258.       if ((lf = 0 - LogFlag) >= maxit)
  259.          lf = maxit - 1;
  260.       Fg2Float((long)(maxit-lf), 0, m);
  261.       fSqrt14(m, m);
  262.       Fg2Float((long)(colors-2), 0, c);
  263.       fDiv(m, c, m);
  264.       for (prev = 1; prev <= lf; prev++)
  265.          LogTable[prev] = 1;
  266.       for (n = 2; n < colors; n++) {
  267.          Fg2Float((long)n, 0, f);
  268.          fMul16(f, m, f);
  269.          fMul16(f, f, l);
  270.          limit = Float2Fg(l, 0) + lf;
  271.          if (limit > maxit || n == colors-1)
  272.             limit = maxit;
  273.          while (prev <= limit)
  274.             LogTable[prev++] = n;
  275.       }
  276.    }
  277.    LogTable[0] = 0;
  278.    if (LogFlag != -1)
  279.       for (n = 1; n < maxit; n++) /* spread top to incl unused colors */
  280.          if (LogTable[n] > LogTable[n-1])
  281.             LogTable[n] = LogTable[n-1]+1;
  282. }
  283.  
  284. long far ExpFloat14(long x) {
  285.    static float fLogTwo = (float)0.6931472;
  286.    int f;
  287.    long Ans;
  288.  
  289.    f = 23 - (int)RegFloat2Fg(RegDivFloat(x, *(long*)&fLogTwo), 0);
  290.    Ans = ExpFudged(RegFloat2Fg(x, 16), f);
  291.    return(RegFg2Float(Ans, (char)f));
  292. }
  293.  
  294. extern struct complex tmp;
  295. extern int color, colors;
  296. double TwoPi;
  297. struct complex temp, t2, BaseLog;
  298. struct complex cdegree = { 3.0, 0.0 },
  299.                croot   = { 1.0, 0.0 };
  300.  
  301. int ComplexNewtonSetup(void) {
  302.    threshold = .001;
  303.    periodicitycheck = 0;
  304.    if(param[0] != 0.0 || param[1] != 0.0 || param[2] != 0.0 ||
  305.       param[3] != 0.0) {
  306.       croot.x = param[2];
  307.       croot.y = param[3];
  308.       cdegree.x = param[0];
  309.       cdegree.y = param[1];
  310.       FPUcplxlog(&croot, &BaseLog);
  311.       TwoPi = asin(1.0) * 4;
  312.    }
  313.    return(1);
  314. }
  315.  
  316. int ComplexNewton(void) {
  317.    struct complex cd1;
  318.  
  319.  
  320.    /* new = ((cdegree-1) * old**cdegree) + croot
  321.             ----------------------------------
  322.                  cdegree * old**(cdegree-1)         */
  323.  
  324.    cd1.x = cdegree.x - 1.0;
  325.    cd1.y = cdegree.y;
  326.  
  327.    temp = ComplexPower(old, cd1);
  328.    FPUcplxmul(&temp, &old, &new);
  329.  
  330.    tmp.x = new.x - croot.x;
  331.    tmp.y = new.y - croot.y;
  332.    if((sqr(tmp.x) + sqr(tmp.y)) < threshold)
  333.       return(1);
  334.  
  335.    FPUcplxmul(&new, &cd1, &tmp);
  336.    tmp.x += croot.x;
  337.    tmp.y += croot.y;
  338.  
  339.    FPUcplxmul(&temp, &cdegree, &t2);
  340.    FPUcplxdiv(&tmp, &t2, &old);
  341.    new = old;
  342.    return(0);
  343. }
  344.  
  345. int ComplexBasin(void) {
  346.    struct complex cd1;
  347.    double mod;
  348.  
  349.    /* new = ((cdegree-1) * old**cdegree) + croot
  350.             ----------------------------------
  351.                  cdegree * old**(cdegree-1)         */
  352.  
  353.    cd1.x = cdegree.x - 1.0;
  354.    cd1.y = cdegree.y;
  355.  
  356.    temp = ComplexPower(old, cd1);
  357.    FPUcplxmul(&temp, &old, &new);
  358.  
  359.    tmp.x = new.x - croot.x;
  360.    tmp.y = new.y - croot.y;
  361.    if((sqr(tmp.x) + sqr(tmp.y)) < threshold) {
  362.       if(fabs(old.y) < .01)
  363.          old.y = 0.0;
  364.       FPUcplxlog(&old, &temp);
  365.       FPUcplxmul(&temp, &cdegree, &tmp);
  366.       mod = tmp.y/TwoPi;
  367.       color = (int)mod;
  368.       if(fabs(mod - color) > 0.5) {
  369.          if(mod < 0.0)
  370.             color--;
  371.          else
  372.             color++;
  373.       }
  374.       color += 2;
  375.       if(color < 0)
  376.          color += 128;
  377.       return(1);
  378.    }
  379.  
  380.    FPUcplxmul(&new, &cd1, &tmp);
  381.    tmp.x += croot.x;
  382.    tmp.y += croot.y;
  383.  
  384.    FPUcplxmul(&temp, &cdegree, &t2);
  385.    FPUcplxdiv(&tmp, &t2, &old);
  386.    new = old;
  387.    return(0);
  388. }
  389.  
  390. extern int Distribution, Offset, Slope;
  391. extern long con;
  392.  
  393. /*** PB, commented this out, it was unused, actual work is in prompts.c
  394. int Starfield(void) {
  395.    int c;
  396.  
  397.    plasma();
  398.    Distribution = (int)param[1];
  399.    con = (long)(param[2] / 100 * (1L << 16));
  400.    Slope = (int)param[3];
  401.    for(row = 0; row < ydots; row++) {
  402.       for(col = 0; col < xdots; col++) {
  403.          if(check_key())
  404.             return(-1);
  405.          c = getcolor(col, row);
  406.          putcolor(col, row, GausianNumber(c, colors));
  407.       }
  408.    }
  409.    return(0);
  410. }
  411.   ***/
  412.  
  413. int GausianNumber(int Probability, int Range) {
  414.    int n, r;
  415.    long Accum = 0, p;
  416.  
  417.    p = divide((long)Probability << 16, (long)Range << 16, 16);
  418.    p = multiply(p, con, 16);
  419.    p = multiply((long)Distribution << 16, p, 16);
  420.    if(!(rand() % (Distribution - (int)(p >> 16) + 1))) {
  421.       for(n = 0; n < Slope; n++)
  422.          Accum += rand();
  423.       Accum /= Slope;
  424.       r = (int)(multiply((long)Range << 15, Accum, 15) >> 14);
  425.       r = r - Range;
  426.       if(r < 0)
  427.          r = -r;
  428.       return(Range - r + Offset);
  429.    }
  430.    return(Offset);
  431. }
  432.  
  433. #endif
  434.  
  435.