1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19 20#ifndef __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__ 21#define __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__ 22 23namespace tlm 24{ 25 26enum tlm_endianness { TLM_UNKNOWN_ENDIAN, TLM_LITTLE_ENDIAN, TLM_BIG_ENDIAN }; 27 28inline tlm_endianness 29get_host_endianness() 30{ 31 static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN; 32 33 if (host_endianness == TLM_UNKNOWN_ENDIAN) { 34 unsigned int number = 1; 35 unsigned char *p_msb_or_lsb = (unsigned char *)&number; 36 host_endianness = (p_msb_or_lsb[0] == 0) ? 37 TLM_BIG_ENDIAN : TLM_LITTLE_ENDIAN; 38 } 39 return host_endianness; 40} 41 42inline bool 43host_has_little_endianness() 44{ 45 static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN; 46 static bool host_little_endian = false; 47 48 if (host_endianness == TLM_UNKNOWN_ENDIAN) { 49 unsigned int number = 1; 50 unsigned char *p_msb_or_lsb = (unsigned char *)&number; 51 52 host_little_endian = (p_msb_or_lsb[0] == 0) ? false : true; 53 } 54 55 return host_little_endian; 56} 57 58inline bool 59has_host_endianness(tlm_endianness endianness) 60{ 61 if (host_has_little_endianness()) { 62 return endianness == TLM_LITTLE_ENDIAN; 63 } else { 64 return endianness == TLM_BIG_ENDIAN; 65 } 66} 67 68} // namespace tlm 69 70#endif /* __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__ */ 71