1 /* Host endianness self-testing object. -*- C++ -*- 2 Copyright 2004 Brian R. Gaeke. 3 4 This file is part of VMIPS. 5 6 VMIPS is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License as published by the 8 Free Software Foundation; either version 2 of the License, or (at your 9 option) any later version. 10 11 VMIPS is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 for more details. 15 16 You should have received a copy of the GNU General Public License along 17 with VMIPS; if not, write to the Free Software Foundation, Inc., 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 19 20 #ifndef _ENDIANTEST_H_ 21 #define _ENDIANTEST_H_ 22 23 #include "types.h" 24 #include <string> 25 26 class EndianSelfTester { 27 bool host_bigendian; 28 public: EndianSelfTester()29 EndianSelfTester () { 30 uint32 x; 31 char *p = (char *) &x; 32 p[0] = 0; 33 p[1] = 1; 34 p[2] = 2; 35 p[3] = 3; 36 if (x == 0x03020100) { 37 host_bigendian = false; 38 } else if (x == 0x00010203) { 39 host_bigendian = true; 40 } else { 41 throw std::string ("Unknown processor endianness."); 42 } 43 } 44 host_is_big_endian()45 bool host_is_big_endian () const { return host_bigendian; } 46 }; 47 48 #endif // _ENDIANTEST_H_ 49