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 const unsigned char endian_array[2] = { 1, 0 }; 00044 unsigned short endian_short = *(unsigned short *)endian_array; 00045 00046 host_endianness = (endian_short == 1) ? TLM_LITTLE_ENDIAN : TLM_BIG_ENDIAN; 00047 } 00048 00049 return host_endianness; 00050 } 00051 00052 inline bool host_has_little_endianness(void) 00053 { 00054 static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN; 00055 static bool host_little_endian = false; 00056 00057 if (host_endianness == TLM_UNKNOWN_ENDIAN) { 00058 const unsigned char endian_array[2] = { 1, 0 }; 00059 unsigned short endian_short = *(unsigned short *)endian_array; 00060 00061 host_little_endian = (endian_short == 1); 00062 } 00063 00064 return host_little_endian; 00065 } 00066 00067 inline bool has_host_endianness(tlm_endianness endianness) 00068 { 00069 if (host_has_little_endianness()) { 00070 return endianness == TLM_LITTLE_ENDIAN; 00071 00072 } else { 00073 return endianness == TLM_BIG_ENDIAN; 00074 } 00075 } 00076 00077 } // namespace tlm 00078 00079 #endif /* __TLM_HELPERS_H__ */