File indexing completed on 2024-05-12 15:43:20

0001 /*
0002  *  Copyright (C) 2003, 2006 Apple Computer, Inc.
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Lesser General Public
0006  *  License as published by the Free Software Foundation; either
0007  *  version 2 of the License, or (at your option) any later version.
0008  *
0009  *  This library is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  *  Lesser General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU Lesser General Public
0015  *  License along with this library; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
0017  *  USA.
0018  *
0019  */
0020 
0021 #include <math.h>
0022 #include "global.h"
0023 
0024 namespace KJS
0025 {
0026 
0027 // This file exists because JavaScriptCore needs to define the NaN and Inf globals in a way
0028 // that does not use a static initializer so we don't have a framework initialization routine.
0029 
0030 // The trick is to define the NaN and Inf globals with a different type than the declaration.
0031 // This trick works because the mangled name of the globals does not include the type, although
0032 // I'm not sure that's guaranteed. There could be alignment issues with this, since arrays of
0033 // characters don't necessarily need the same alignment doubles do, but for now it seems to work.
0034 // It would be good to figure out a 100% clean way that still avoids code that runs at init time.
0035 
0036 #if (defined(AVOID_STATIC_CONSTRUCTORS) && !AVOID_STATIC_CONSTRUCTORS)
0037 KJS_EXPORT extern const double NaN = NAN;
0038 KJS_EXPORT extern const double Inf = INFINITY;
0039 #elif PLATFORM(DARWIN)
0040 
0041 #if PLATFORM(BIG_ENDIAN)
0042 KJS_EXPORT extern const unsigned char NaN[sizeof(double)] = { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 };
0043 KJS_EXPORT extern const unsigned char Inf[sizeof(double)] = { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 };
0044 #elif PLATFORM(MIDDLE_ENDIAN)
0045 KJS_EXPORT extern const unsigned char NaN[] = { 0, 0, 0xf8, 0x7f, 0, 0, 0, 0 };
0046 KJS_EXPORT extern const unsigned char Inf[] = { 0, 0, 0xf0, 0x7f, 0, 0, 0, 0 };
0047 #else
0048 KJS_EXPORT extern const unsigned char NaN[sizeof(double)] = { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f };
0049 KJS_EXPORT extern const unsigned char Inf[sizeof(double)] = { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f };
0050 #endif // PLATFORM(MIDDLE_ENDIAN)
0051 
0052 #else // !PLATFORM(DARWIN)
0053 
0054 // Note, we have to use union to ensure alignment. Otherwise, NaN_Bytes can start anywhere,
0055 // while NaN_double has to be 4-byte aligned for 32-bits.
0056 // With -fstrict-aliasing enabled, unions are the only safe way to do type masquerading.
0057 
0058 static const union {
0059     struct {
0060         unsigned char NaN_Bytes[8];
0061         unsigned char Inf_Bytes[8];
0062     } bytes;
0063 
0064     struct {
0065         double NaN_Double;
0066         double Inf_Double;
0067     } doubles;
0068 
0069 } NaNInf = { {
0070 #if PLATFORM(BIG_ENDIAN)
0071         { 0x7f, 0xf8, 0, 0, 0, 0, 0, 0 },
0072         { 0x7f, 0xf0, 0, 0, 0, 0, 0, 0 }
0073 #elif PLATFORM(MIDDLE_ENDIAN)
0074         { 0, 0, 0xf8, 0x7f, 0, 0, 0, 0 },
0075         { 0, 0, 0xf0, 0x7f, 0, 0, 0, 0 }
0076 #else
0077         { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f },
0078         { 0, 0, 0, 0, 0, 0, 0xf0, 0x7f }
0079 #endif
0080     }
0081 };
0082 
0083 KJS_EXPORT extern const double NaN = NaNInf.doubles.NaN_Double;
0084 KJS_EXPORT extern const double Inf = NaNInf.doubles.Inf_Double;
0085 
0086 #endif // !PLATFORM(DARWIN)
0087 
0088 } // namespace KJS