simple_number.h

00001 // -*- c-basic-offset: 2 -*-
00002 /*
00003  *  This file is part of the KDE libraries
00004  *  Copyright (C) 2003 Apple Computer, Inc
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Library General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU Library General Public License
00017  *  along with this library; see the file COPYING.LIB.  If not, write to
00018  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA 02110-1301, USA.
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
KDE Home | KDE Accessibility Home | Description of Access Keys