Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
mastercore_convert.cpp
Go to the documentation of this file.
1 #include "mastercore_convert.h"
2 
3 #include <cmath>
4 #include <stdint.h>
5 
6 namespace mastercore
7 {
8 
9 // TODO: move to seperate file with checks
10 static bool isBigEndian()
11 {
12  union
13  {
14  uint32_t i;
15  char c[4];
16  } bint = {0x01020304};
17 
18  return 1 == bint.c[0];
19 }
20 
21 uint64_t rounduint64(long double ld)
22 {
23  return static_cast<uint64_t>(roundl(fabsl(ld)));
24 }
25 
26 void swapByteOrder16(uint16_t& us)
27 {
28  if (isBigEndian()) return;
29 
30  us = (us >> 8) |
31  (us << 8);
32 }
33 
34 void swapByteOrder32(uint32_t& ui)
35 {
36  if (isBigEndian()) return;
37 
38  ui = (ui >> 24) |
39  ((ui << 8) & 0x00FF0000) |
40  ((ui >> 8) & 0x0000FF00) |
41  (ui << 24);
42 }
43 
44 void swapByteOrder64(uint64_t& ull)
45 {
46  if (isBigEndian()) return;
47 
48  ull = (ull >> 56) |
49  ((ull << 40) & 0x00FF000000000000) |
50  ((ull << 24) & 0x0000FF0000000000) |
51  ((ull << 8) & 0x000000FF00000000) |
52  ((ull >> 8) & 0x00000000FF000000) |
53  ((ull >> 24) & 0x0000000000FF0000) |
54  ((ull >> 40) & 0x000000000000FF00) |
55  (ull << 56);
56 }
57 
58 } // namespace mastercore
void swapByteOrder16(uint16_t &us)
Swaps byte order on little-endian systems and does nothing otherwise.
void swapByteOrder32(uint32_t &ui)
static bool isBigEndian()
uint64_t rounduint64(long double ld)
Converts numbers to 64 bit wide unsigned integer whereby any signedness is ignored.
void swapByteOrder64(uint64_t &ull)