home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 20 / AACD20.BIN / AACD / Programming / Jikes / Source / src / long.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-24  |  7.9 KB  |  263 lines

  1. // $Id: long.h,v 1.15 2001/02/17 06:26:55 mdejong Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #ifndef Long_INCLUDED
  11. #define Long_INCLUDED
  12.  
  13. #include "platform.h"
  14.  
  15. #ifdef    HAVE_JIKES_NAMESPACE
  16. namespace Jikes {    // Open namespace Jikes block
  17. #endif
  18.  
  19. class IEEEdouble;
  20. class IEEEfloat;
  21.  
  22. class LongInt;
  23. class ULongInt;
  24.  
  25. class BaseLong
  26. {
  27. protected:
  28.     union
  29.     {
  30.         double double_value;
  31. #ifndef HAVE_UNSIGNED_LONG_LONG
  32.         u4 word[2];
  33. #else
  34.         u8 words;
  35. #endif // HAVE_UNSIGNED_LONG_LONG
  36.     } value;
  37.  
  38.     // Set the high word only. Does not modify the low word!
  39.     inline void setHighWord(u4 high) {
  40. #ifndef HAVE_UNSIGNED_LONG_LONG
  41. # ifndef WORDS_BIGENDIAN
  42.         value.word[1] = high;
  43. # else
  44.         value.word[0] = high;
  45. # endif
  46. #else
  47.         setHighAndLowWords(high, LowWord());
  48. #endif // HAVE_UNSIGNED_LONG_LONG
  49.     }
  50.  
  51.     // Set the low word only. Does not modify the high word!
  52.     inline void setLowWord(u4 low) {
  53. #ifndef HAVE_UNSIGNED_LONG_LONG
  54. # ifndef WORDS_BIGENDIAN
  55.         value.word[0] = low;
  56. # else
  57.         value.word[1] = low;
  58. # endif
  59. #else
  60.         setHighAndLowWords(HighWord(), low);
  61. #endif // HAVE_UNSIGNED_LONG_LONG
  62.     }
  63.     
  64.     // Set the value for both words.
  65.     inline void setHighAndLowWords(u4 high, u4 low) {
  66. #ifndef HAVE_UNSIGNED_LONG_LONG
  67. # ifndef WORDS_BIGENDIAN
  68.         value.word[1] = high;
  69.         value.word[0] = low;
  70. # else
  71.         value.word[0] = high;
  72.         value.word[1] = low;
  73. # endif
  74. #else
  75.         value.words = (((u8) high) << 32) | low;
  76. #endif // HAVE_UNSIGNED_LONG_LONG
  77.     }
  78.  
  79.     // Set the value for both words.
  80.     inline void setHighAndLowWords(const BaseLong &op)
  81.     {
  82. #ifndef HAVE_UNSIGNED_LONG_LONG
  83.         value.word[0] = op.value.word[0];
  84.         value.word[1] = op.value.word[1];
  85. #else
  86.         value.words = op.value.words;
  87. #endif // HAVE_UNSIGNED_LONG_LONG
  88.     }
  89.  
  90. public:
  91.  
  92. #ifndef HAVE_UNSIGNED_LONG_LONG
  93. # ifndef WORDS_BIGENDIAN
  94.     inline u4 HighWord(void) { return value.word[1]; }
  95.     inline u4 LowWord(void)  { return value.word[0]; }
  96. # else
  97.     inline u4 HighWord(void) { return value.word[0]; }
  98.     inline u4 LowWord(void)  { return value.word[1]; }
  99. # endif
  100. #else
  101.     inline u4 HighWord(void) { return ((u4) (value.words >> 32)); }
  102.     inline u4 LowWord(void)  { return ((u4) value.words); }
  103.     inline u8 Words(void)    { return value.words; }
  104. #endif // HAVE_UNSIGNED_LONG_LONG
  105.  
  106.     double DoubleView(void) { return value.double_value; }
  107.  
  108.     BaseLong(u4 high, u4 low);
  109.     BaseLong(u4 a); // no sign extension
  110.     BaseLong(i4 a); // sign extends
  111.     inline BaseLong (void) {} // construct without initializing
  112. #ifdef HAVE_UNSIGNED_LONG_LONG
  113.     inline BaseLong(u8 a) { value.words = a; } // construct in one step
  114. #endif // HAVE_UNSIGNED_LONG_LONG
  115.  
  116.     BaseLong  operator+  (BaseLong); // binary addition
  117.     BaseLong  operator+  (void);     // unary plus
  118.     BaseLong &operator+= (BaseLong); // add and assign
  119.     BaseLong  operator++ (int);      // postfix increment
  120.     BaseLong  operator++ (void);     // prefix increment
  121.  
  122.     BaseLong  operator-  (BaseLong); // binary subtraction
  123.     BaseLong  operator-  (void);     // unary minus
  124.     BaseLong &operator-= (BaseLong); // subtract and assign
  125.     BaseLong  operator-- (int);      // postfix decrement
  126.     BaseLong  operator-- (void);     // prefix decrement
  127.  
  128.     BaseLong  operator*  (BaseLong); // multiplication
  129.     BaseLong &operator*= (BaseLong); // multiply and assign
  130.  
  131.     //
  132.     // NOTE: To match the JLS, mask the argument with 0x3f
  133.     //
  134.     BaseLong  operator<< (int);      // left shift
  135.     BaseLong &operator<<=(int);      // left shift and assign
  136.  
  137.     bool      operator== (BaseLong); // equal
  138.     bool      operator!= (BaseLong); // not equal
  139.     bool      operator!  (void);     // logical complement
  140.  
  141.     BaseLong  operator~  (void);     // bitwise complement
  142.     BaseLong  operator^  (BaseLong); // bitwise XOR
  143.     BaseLong &operator^= (BaseLong); // bitwise XOR and assign
  144.     BaseLong  operator|  (BaseLong); // bitwise OR
  145.     BaseLong &operator|= (BaseLong); // bitwise OR and assign
  146.     BaseLong  operator&  (BaseLong); // bitwise AND
  147.     BaseLong &operator&= (BaseLong); // bitwise AND and assign
  148.  
  149.     bool      operator&& (BaseLong); // logical AND (not short-circuit)
  150.     bool      operator|| (BaseLong); // logical OR (not short circuit)
  151.  
  152.     static void Divide(BaseLong &, BaseLong &, BaseLong &, BaseLong &);
  153.  
  154.     operator LongInt(void);          // Cast to LongInt
  155.     operator ULongInt(void);         // Cast to ULongInt
  156.  
  157.     // mirrors java.lang.Long, useful in hashing
  158.     inline i4 hashCode(void) { return (HighWord() ^ LowWord()); }
  159. };
  160.  
  161.  
  162. class LongInt : public BaseLong
  163. {
  164. public:
  165.  
  166.     inline LongInt (u4 a, u4 b) : BaseLong (a, b) {}
  167.     inline LongInt (u4 a) : BaseLong (a) {} // no sign extension
  168.     inline LongInt (i4 a) : BaseLong (a) {} // sign extends
  169.     inline LongInt (void) : BaseLong () {} // uninitialized
  170. #ifdef HAVE_EXPLICIT
  171.     explicit
  172. #endif
  173.            LongInt (IEEEdouble); // narrowing conversion of double to long
  174.  
  175. #ifdef HAVE_EXPLICIT
  176.     explicit 
  177. #endif
  178.     LongInt (IEEEfloat); // narrowing conversion of float to long
  179. #ifdef HAVE_UNSIGNED_LONG_LONG
  180.     inline LongInt(u8 a) : BaseLong (a) {} // construct in one step
  181. #endif // HAVE_UNSIGNED_LONG_LONG
  182.  
  183.     //
  184.     // These constants are generated when first used.  The memory they
  185.     // use can be reclaimed with ConstantCleanup().
  186.     //
  187.     static inline const LongInt MAX_LONG(void)
  188.     {
  189.         return max_long_const ? *max_long_const
  190.             : *(max_long_const = new LongInt(0x7FFFFFFF, 0xFFFFFFFF));
  191.     }
  192.     static inline const LongInt MIN_LONG(void)
  193.     {
  194.         return min_long_const ? *min_long_const
  195.             : *(min_long_const = new LongInt(0x80000000, 0x00000000));
  196.     }
  197.     static void ConstantCleanup(void)
  198.     {
  199.         if (max_long_const)
  200.             delete max_long_const;
  201.         if (min_long_const)
  202.             delete min_long_const;
  203.     }
  204.  
  205. private:
  206.     static LongInt *max_long_const, *min_long_const;
  207.  
  208. public:
  209.     LongInt  operator/  (LongInt); // divide
  210.     LongInt &operator/= (LongInt); // divide and assign
  211.  
  212.     LongInt  operator%  (LongInt); // modulus
  213.     LongInt &operator%= (LongInt); // modulus and assign
  214.  
  215.     //
  216.     // NOTE: To match the JLS, mask the argument with 0x3f
  217.     //
  218.     LongInt  operator>> (int);     // right shift
  219.     LongInt &operator>>=(int);     // right shift and assign
  220.  
  221.     bool     operator<  (LongInt); // less-than
  222.     bool     operator>  (LongInt); // greater-than
  223.     bool     operator<= (LongInt); // less-than or equal
  224.     bool     operator>= (LongInt); // greater-than or equal
  225. };
  226.  
  227.  
  228. class ULongInt : public BaseLong
  229. {
  230. public:
  231.  
  232.     inline ULongInt (u4 a, u4 b) : BaseLong (a, b) {}
  233.     inline ULongInt (u4 a) : BaseLong (a) {} // no sign extension
  234.     inline ULongInt (i4 a) : BaseLong (a) {} // sign extended
  235.     inline ULongInt (void) : BaseLong () {} // uninitialized
  236. #ifdef HAVE_UNSIGNED_LONG_LONG
  237.     inline ULongInt(u8 a) : BaseLong (a) {} // construct in one step
  238. #endif // HAVE_UNSIGNED_LONG_LONG
  239.  
  240.     ULongInt  operator/  (ULongInt); // divide
  241.     ULongInt &operator/= (ULongInt); // divide and assign
  242.  
  243.     ULongInt  operator%  (ULongInt); // modulus
  244.     ULongInt &operator%= (ULongInt); // modulus and assign
  245.  
  246.     //
  247.     // NOTE: To match the JLS, mask the argument with 0x3f
  248.     //
  249.     ULongInt  operator>> (int);      // right shift
  250.     ULongInt &operator>>=(int);      // right shift and assign
  251.  
  252.     bool      operator<  (ULongInt); // less-than
  253.     bool      operator>  (ULongInt); // greater-than
  254.     bool      operator<= (ULongInt); // less-than or equal
  255.     bool      operator>= (ULongInt); // greater-than or equal
  256. };
  257.  
  258. #ifdef    HAVE_JIKES_NAMESPACE
  259. }            // Close namespace Jikes block
  260. #endif
  261.  
  262. #endif // Long_INCLUDED
  263.