home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / IODMath.cpp < prev    next >
C/C++ Source or Header  |  1997-03-21  |  6KB  |  198 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odutils
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //       
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.12 com/src/utils/IODMath.cpp, odutils, od96os2, odos29712d 8/2/96 18:24:47 [ 3/21/97 17:36:01 ]
  28. /********************************************************************/
  29. /*  Licensed Materials - Property of IBM                            */
  30. /*                                                                  */
  31. /*                                                                  */
  32. /* Copyright (C) International Business Machines Corp., 1994.       */
  33. /* Copyright (C) Apple Computer, Inc., 1994                         */
  34. /*                                                                  */
  35. /*  US Government Users Restricted Rights -                         */
  36. /*  Use, duplication, or disclosure restricted                      */
  37. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  38. /*                                                                  */
  39. /*  IBM Change History (most recent first):                         */
  40. /*  129861  7/18/95 ced  Reduce rounding error for ODFixedMultiply  */
  41. /*                       and ODFractMultiply.                       */
  42. /********************************************************************/
  43. #if defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_)
  44. #include <builtin.h>
  45. #endif // defined(_PLATFORM_WIN32_) || defined(_PLATFORM_OS2_)
  46. #include <math.h>
  47.  
  48. #ifndef _ODMATH_
  49. #include "ODMath.h"
  50. #endif
  51. #ifndef __ODPAGTUN__
  52. #include <ODPagTun.h>
  53. #endif
  54.  
  55. //=============================================================================
  56. // Constants
  57. //=============================================================================
  58.  
  59.  
  60. #define longSize    32
  61. #define wideSize    64
  62.  
  63. #define highBit      ((ODULong) 0x80000000)
  64. #define ODFloatToFract(a) ((ODFract)(((double)(a)) * kODFract1))
  65.  
  66.  
  67. ODFixed  ODFixedMultiply( register ODFixed a, register ODFixed b ) {
  68.    ODWide result;
  69.    ODWideMultiply(a, b, &result);
  70.    ODWideShift(&result, 16);
  71. #if !defined(_PLATFORM_UNIX_)
  72.     if (ODWideIsLong(&result))
  73.        return result.lo;
  74.     if (result.hi > 0)
  75.         return kODFixedInfinity;
  76.     else
  77.         return kODFixedMinusInfinity;
  78. #else // defined(_PLATFORM_UNIX_)
  79.     if (ODWideIsLong(&result))
  80.        return (unsigned long) result;
  81.     if (result > 0)
  82.         return kODFixedInfinity;
  83.     else
  84.         return kODFixedMinusInfinity;
  85. #endif // !defined(_PLATFORM_UNIX_)
  86. }
  87.  
  88. ODFixed  ODFixedDivide( ODFixed a, ODFixed b ) {
  89.    ODWide dividend;
  90.    ODSLong remainder;
  91. #if !defined(_PLATFORM_UNIX_)
  92.    dividend.lo = a;
  93.    dividend.hi = (a < 0) ? -1 : 0;
  94. #else // defined(_PLATFORM_UNIX_)
  95.    dividend = a;
  96. #endif // !defined(_PLATFORM_UNIX_)
  97.    ODWideShift(÷nd, -16);
  98.    return ODWideDivide(÷nd, b, &remainder);
  99. }
  100.  
  101. ODFract  ODFractMultiply( ODFract a, ODFract b ) {
  102.    ODWide result;
  103.    ODWideMultiply(a, b, &result);
  104.    ODWideShift(&result, 30);
  105. #if !defined(_PLATFORM_UNIX_)
  106.    return result.lo;
  107. #else // defined(_PLATFORM_UNIX_)
  108.    return (unsigned long)result;
  109. #endif // !defined(_PLATFORM_UNIX_)
  110. }
  111.  
  112. ODFract  ODFractDivide( ODFract a, ODFract b ) {
  113.    ODWide dividend;
  114.    ODSLong remainder;
  115. #if !defined(_PLATFORM_UNIX_)
  116.    dividend.lo = a;
  117.    dividend.hi = (a < 0) ? -1 : 0;
  118. #else // defined(_PLATFORM_UNIX_)
  119.    dividend = a;
  120. #endif // !defined(_PLATFORM_UNIX_)
  121.    ODWideShift(÷nd, -30);
  122.    return ODWideDivide(÷nd, b, &remainder);
  123. }
  124.  
  125. ODFract  ODFractSinCos( ODFixed angle, ODFract *cos ) {
  126.    double sine, cosine;
  127. #if defined(_PLATFORM_UNIX_)
  128.     float dAngle;
  129.    dAngle =  ODFixedToFloat(angle);
  130.     cosine = ::cos(dAngle);
  131.     sine = sin(dAngle);
  132. #else // defined(_PLATFORM_UNIX_)
  133.    sine = ODFloatToFract(_fsincos(ODFixedToFloat(angle), &cosine));
  134. #endif // defined(_PLATFORM_UNIX_)
  135.    *cos = ODFloatToFract(cosine);
  136.    return ODFloatToFract(sine);
  137. }
  138.  
  139. /*
  140.  * Approximates the square root of a positive ODWide number.
  141.  */
  142.  
  143. ODULong ODWideSquareRoot( const ODWide *src )
  144. {
  145. #if !defined(_PLATFORM_UNIX_)
  146.    if (src->hi < 0) return 0;      // Only works for positive numbers
  147.    ODULong hi = src->hi, number;
  148.    ODULong lo = src->lo;
  149. #else // defined(_PLATFORM_UNIX_)
  150.    if (*src < 0) return 0;         // Only works for positive numbers
  151.    ODULong hi = (*src >> 32), number;
  152.    ODULong lo = (long)*src;
  153. #endif // !defined(_PLATFORM_UNIX_)
  154.  
  155.  
  156.    if (hi <= 0x00007fff)
  157.        number = (((hi << 16) & 0xffff0000) + ((lo >> 16) & 0x0000ffff));
  158.    else {
  159.           if (hi <= 0x0000ffff) {
  160.               number  = (((hi << 16) & 0xffff0000) + ((lo >> 16) & 0x0000ffff));
  161.               number  = ((number >> 2) & 0x3fffffff);    /* 8000-FFFF */
  162.           }
  163.           else
  164.               number  = hi;
  165.    }
  166.    number = (ODULong)(sqrt( (double) number) * 256 );
  167.    if (hi <= 0x0000ffff) {
  168.       if (hi > 0x00007fff)
  169.           number = number << 1;
  170.    }
  171.    else
  172.       number = number << 8;
  173.  
  174.    return number;
  175. }
  176. #if defined(_PLATFORM_UNIX_)
  177. ODWide*  ODWideMultiply( ODSLong a, ODSLong b, ODWide *result )
  178. {
  179.     *result = (ODWide)a * (ODWide)b;
  180.     return(result);
  181. }
  182.  
  183. ODSLong  ODWideDivide( const ODWide *dividend,
  184.                      ODSLong divisor, ODSLong *remainder)
  185. {
  186.     ODWide quotient;
  187.  
  188.     quotient = *dividend / divisor;
  189.  
  190.     if (divisor == 0 || !ODWideIsLong("ient))
  191.         return(kODFixedMinusInfinity);
  192.  
  193.     *remainder = *dividend - (quotient * divisor);
  194.  
  195.     return (ODSLong) quotient;
  196. }
  197. #endif // defined(_PLATFORM_UNIX_)
  198.