remove conflict
[c11concurrency-benchmarks.git] / mabain / src / integer_4b_5b.h
1 /**
2  * Copyright (C) 2017 Cisco Inc.
3  *
4  * This program is free software: you can redistribute it and/or  modify
5  * it under the terms of the GNU General Public License, version 2,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 // @author Changxue Deng <chadeng@cisco.com>
18
19 #ifndef __INTEGER_4B_5B_H__
20 #define __INTEGER_4B_5B_H__
21
22 #include <stdint.h>
23 #include <string.h>
24
25 namespace mabain {
26
27 #define MAX_4B_OFFSET        0xFFFFFFFF
28 #define MAX_5B_OFFSET      0xFFFFFFFFFF
29 #define MAX_6B_OFFSET    0xFFFFFFFFFFFF
30
31 // write and read 5-byte 6-byte unsigned integer
32 // Note this is based on engianness.
33
34 inline void Write5BInteger(uint8_t *buffer, size_t offset)
35 {
36 #ifdef __DEBUG__
37     if(offset > MAX_5B_OFFSET)
38     {
39         std::cerr << "OFFSET " << offset << " TOO LARGE FOR 5 BYTES\n";
40         abort();
41     }
42 #endif
43
44     uint8_t *src = reinterpret_cast<uint8_t*>(&offset);
45 #ifndef __BIG__ENDIAN__
46     memcpy(buffer, src, 5);
47 #else
48     memcpy(buffer, src+3, 5);
49 #endif
50 }
51
52 inline size_t Get5BInteger(const uint8_t *buffer)
53 {
54     size_t offset = 0;
55     uint8_t *target = reinterpret_cast<uint8_t*>(&offset);
56 #ifndef __BIG__ENDIAN__
57     memcpy(target, buffer, 5);
58 #else
59     memcpy(target+3, buffer, 5);
60 #endif
61     return offset;
62 }
63
64 inline void Write6BInteger(uint8_t *buffer, size_t offset)
65 {
66 #ifdef __DEBUG__
67     if(offset > MAX_6B_OFFSET)
68     {
69         std::cerr << "OFFSET " << offset << "TOO LARGE FOR SIX BYTES\n";
70         abort();
71     }
72 #endif
73
74     uint8_t *src = reinterpret_cast<uint8_t*>(&offset);
75 #ifndef __BIG__ENDIAN__
76     memcpy(buffer, src, 6);
77 #else
78     memcpy(buffer, src+2, 6);
79 #endif
80 }
81
82 inline size_t Get6BInteger(const uint8_t *buffer)
83 {
84     size_t offset = 0;
85     uint8_t *target = reinterpret_cast<uint8_t*>(&offset);
86 #ifndef __BIG__ENDIAN__
87     memcpy(target, buffer, 6);
88 #else
89     memcpy(target+2, buffer, 6);
90 #endif
91     return offset;
92 }
93
94 }
95
96 #endif