BRL-CAD
endian.c
Go to the documentation of this file.
1 /* E N D I A N . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include "bu/endian.h"
24 
25 
28 {
29  const union bob {
30  unsigned long i;
31  unsigned char c[sizeof(unsigned long)];
32  } b = {1};
33 
34 /* give run-time test preference to compile-time endian, tested much
35  * faster than stashing in a static.
36  */
37 #ifdef WORDS_BIGENDIAN
38  if (LIKELY(b.c[sizeof(unsigned long)-1]))
39  return BU_BIG_ENDIAN;
40  if (UNLIKELY(b.c[0]))
41  return BU_LITTLE_ENDIAN;
42 #else
43  if (LIKELY(b.c[0]))
44  return BU_LITTLE_ENDIAN;
45  if (UNLIKELY(b.c[sizeof(unsigned long)-1]))
46  return BU_BIG_ENDIAN;
47 #endif
48  if (UNLIKELY(b.c[1]))
49  return BU_PDP_ENDIAN;
50 
51  return (bu_endian_t)0;
52 }
53 
54 
55 /*
56  * Local Variables:
57  * tab-width: 8
58  * mode: C
59  * indent-tabs-mode: t
60  * c-file-style: "stroustrup"
61  * End:
62  * ex: shiftwidth=4 tabstop=8
63  */
#define LIKELY(expression)
Definition: common.h:261
bu_endian_t bu_byteorder(void)
Definition: endian.c:27
Header file for the BRL-CAD common definitions.
bu_endian_t
Definition: endian.h:41
#define UNLIKELY(expression)
Definition: common.h:282