113521Sgabeblack@google.com/*****************************************************************************
213521Sgabeblack@google.com
313521Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
413521Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
513521Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
613521Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
713521Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
813521Sgabeblack@google.com  License.  You may obtain a copy of the License at
913521Sgabeblack@google.com
1013521Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1113521Sgabeblack@google.com
1213521Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1313521Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1413521Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1513521Sgabeblack@google.com  implied.  See the License for the specific language governing
1613521Sgabeblack@google.com  permissions and limitations under the License.
1713521Sgabeblack@google.com
1813521Sgabeblack@google.com *****************************************************************************/
1913521Sgabeblack@google.com
2013521Sgabeblack@google.com#ifndef __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__
2113521Sgabeblack@google.com#define __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__
2213521Sgabeblack@google.com
2313521Sgabeblack@google.comnamespace tlm
2413521Sgabeblack@google.com{
2513521Sgabeblack@google.com
2613521Sgabeblack@google.comenum tlm_endianness { TLM_UNKNOWN_ENDIAN, TLM_LITTLE_ENDIAN, TLM_BIG_ENDIAN };
2713521Sgabeblack@google.com
2813521Sgabeblack@google.cominline tlm_endianness
2913521Sgabeblack@google.comget_host_endianness()
3013521Sgabeblack@google.com{
3113521Sgabeblack@google.com    static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN;
3213521Sgabeblack@google.com
3313521Sgabeblack@google.com    if (host_endianness == TLM_UNKNOWN_ENDIAN) {
3413521Sgabeblack@google.com        unsigned int number = 1;
3513521Sgabeblack@google.com        unsigned char *p_msb_or_lsb = (unsigned char *)&number;
3613521Sgabeblack@google.com        host_endianness = (p_msb_or_lsb[0] == 0) ?
3713521Sgabeblack@google.com            TLM_BIG_ENDIAN : TLM_LITTLE_ENDIAN;
3813521Sgabeblack@google.com    }
3913521Sgabeblack@google.com    return host_endianness;
4013521Sgabeblack@google.com}
4113521Sgabeblack@google.com
4213521Sgabeblack@google.cominline bool
4313521Sgabeblack@google.comhost_has_little_endianness()
4413521Sgabeblack@google.com{
4513521Sgabeblack@google.com    static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN;
4613521Sgabeblack@google.com    static bool host_little_endian = false;
4713521Sgabeblack@google.com
4813521Sgabeblack@google.com    if (host_endianness == TLM_UNKNOWN_ENDIAN) {
4913521Sgabeblack@google.com        unsigned int number = 1;
5013521Sgabeblack@google.com        unsigned char *p_msb_or_lsb = (unsigned char *)&number;
5113521Sgabeblack@google.com
5213521Sgabeblack@google.com        host_little_endian = (p_msb_or_lsb[0] == 0) ? false : true;
5313521Sgabeblack@google.com    }
5413521Sgabeblack@google.com
5513521Sgabeblack@google.com    return host_little_endian;
5613521Sgabeblack@google.com}
5713521Sgabeblack@google.com
5813521Sgabeblack@google.cominline bool
5913521Sgabeblack@google.comhas_host_endianness(tlm_endianness endianness)
6013521Sgabeblack@google.com{
6113521Sgabeblack@google.com    if (host_has_little_endianness()) {
6213521Sgabeblack@google.com        return endianness == TLM_LITTLE_ENDIAN;
6313521Sgabeblack@google.com    } else {
6413521Sgabeblack@google.com        return endianness == TLM_BIG_ENDIAN;
6513521Sgabeblack@google.com    }
6613521Sgabeblack@google.com}
6713521Sgabeblack@google.com
6813521Sgabeblack@google.com} // namespace tlm
6913521Sgabeblack@google.com
7013521Sgabeblack@google.com#endif /* __SYSTEMC_EXT_TLM_CORE_2_GENERIC_PAYLOAD_HELPERS_HH__ */
71