simple_number.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _KJS_SIMPLE_NUMBER_H_
00024 #define _KJS_SIMPLE_NUMBER_H_
00025
00026 #include <math.h>
00027 #include <string.h>
00028
00029 #define IS_NEGATIVE_ZERO(num) (num == 0.0 && !memcmp(&num, &SimpleNumber::negZero, sizeof(double)))
00030
00031 namespace KJS {
00032 class ValueImp;
00033
00034 class SimpleNumber {
00035 public:
00036 enum { tag = 1, shift = 2, mask = (1 << shift) - 1, sign = 1L << (sizeof(long) * 8 - 1 ), max = (1L << ((sizeof(long) * 8 - 1) - shift)) - 1, min = -max - 1, imax = (1L << ((sizeof(int) * 8 - 1) - shift)) - 1, imin = -imax - 1 };
00037
00038 static inline bool is(const ValueImp *imp) { return ((long)imp & mask) == tag; }
00039 static inline long value(const ValueImp *imp) { return ((long)imp >> shift) | (((long)imp & sign) ? ~max : 0); }
00040
00041 static inline bool fits(int i) { return i <= imax && i >= imin; }
00042 static inline bool fits(unsigned i) { return i <= (unsigned)max; }
00043 static inline bool fits(long i) { return i <= max && i >= min; }
00044 static inline bool fits(unsigned long i) { return i <= (unsigned)max; }
00045 static inline bool fits(double d) { return d >= min && d <= max && d == (double)(long)d && !IS_NEGATIVE_ZERO(d); }
00046 static inline ValueImp *make(long i) { return (ValueImp *)((i << shift) | tag); }
00047
00048 static double negZero;
00049 };
00050 }
00051
00052 #endif
|