tlm_helpers.h revision 12027:1eb7dc7aa10b
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/* ---------------------------------------------------------------------------------------
21 @file tlm_helpers.h
22
23 @brief
24
25  Original Authors:
26    Charles Wilson, ESLX
27
28--------------------------------------------------------------------------------------- */
29
30#ifndef __TLM_HELPERS_H__
31#define __TLM_HELPERS_H__
32
33//#include <sys/param.h>
34//#include <cstring>
35
36namespace tlm {
37
38enum tlm_endianness { TLM_UNKNOWN_ENDIAN, TLM_LITTLE_ENDIAN, TLM_BIG_ENDIAN };
39
40inline tlm_endianness get_host_endianness(void)
41{
42  static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN;
43
44  if (host_endianness == TLM_UNKNOWN_ENDIAN) {
45    unsigned int number = 1;
46    unsigned char *p_msb_or_lsb = (unsigned char*)&number;
47
48    host_endianness = (p_msb_or_lsb[0] == 0) ? TLM_BIG_ENDIAN : TLM_LITTLE_ENDIAN;
49  }
50  return host_endianness;
51}
52
53inline bool host_has_little_endianness(void)
54{
55  static tlm_endianness host_endianness = TLM_UNKNOWN_ENDIAN;
56  static bool host_little_endian = false;
57
58  if (host_endianness == TLM_UNKNOWN_ENDIAN) {
59    unsigned int number = 1;
60    unsigned char *p_msb_or_lsb = (unsigned char*)&number;
61
62    host_little_endian = (p_msb_or_lsb[0] == 0) ? false : true;
63  }
64
65  return host_little_endian;
66}
67
68inline bool has_host_endianness(tlm_endianness endianness)
69{
70  if (host_has_little_endianness()) {
71    return endianness == TLM_LITTLE_ENDIAN;
72
73  } else {
74    return endianness == TLM_BIG_ENDIAN;
75  }
76}
77
78} // namespace tlm
79
80#endif /* __TLM_HELPERS_H__ */
81