00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2008 by all Contributors. 00005 All Rights reserved. 00006 00007 The contents of this file are subject to the restrictions and limitations 00008 set forth in the SystemC Open Source License Version 3.0 (the "License"); 00009 You may not use this file except in compliance with such restrictions and 00010 limitations. You may obtain instructions on how to receive a copy of the 00011 License at http://www.systemc.org/. Software distributed by Contributors 00012 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 00013 ANY KIND, either express or implied. See the License for the specific 00014 language governing rights and limitations under the License. 00015 00016 *****************************************************************************/ 00017 00018 /* --------------------------------------------------------------------------------------- 00019 @file tlm_helpers.h 00020 00021 @brief 00022 00023 Original Authors: 00024 Charles Wilson, ESLX 00025 00026 --------------------------------------------------------------------------------------- */ 00027 00028 #ifndef __TLM_HELPERS_H__ 00029 #define __TLM_HELPERS_H__ 00030 00031 //#include <sys/param.h> 00032 //#include <cstring> 00033 00034 namespace tlm { 00035 00036 enum tlm_endianness { TLM_UNKNOWN_ENDIAN, TLM_LITTLE_ENDIAN, TLM_BIG_ENDIAN }; 00037 00038 inline tlm_endianness get_host_endianness(void) 00039 { 00040 static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN; 00041 00042 if (host_endianness == TLM_UNKNOWN_ENDIAN) { 00043 unsigned int number = 1; 00044 unsigned char *p_msb_or_lsb = (unsigned char*)&number; 00045 00046 host_endianness = (p_msb_or_lsb[0] == 0) ? TLM_BIG_ENDIAN : TLM_LITTLE_ENDIAN; 00047 } 00048 return host_endianness; 00049 } 00050 00051 inline bool host_has_little_endianness(void) 00052 { 00053 static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN; 00054 static bool host_little_endian = false; 00055 00056 if (host_endianness == TLM_UNKNOWN_ENDIAN) { 00057 unsigned int number = 1; 00058 unsigned char *p_msb_or_lsb = (unsigned char*)&number; 00059 00060 host_little_endian = (p_msb_or_lsb[0] == 0) ? false : true; 00061 } 00062 00063 return host_little_endian; 00064 } 00065 00066 inline bool has_host_endianness(tlm_endianness endianness) 00067 { 00068 if (host_has_little_endianness()) { 00069 return endianness == TLM_LITTLE_ENDIAN; 00070 00071 } else { 00072 return endianness == TLM_BIG_ENDIAN; 00073 } 00074 } 00075 00076 } // namespace tlm 00077 00078 #endif /* __TLM_HELPERS_H__ */