Coverage Report

Created: 2024-11-20 12:06

/root/doris/be/src/geo/ByteOrderValues.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "ByteOrderValues.h"
19
20
#include <stdint.h>
21
22
#include <cstring>
23
24
namespace doris {
25
26
0
int32_t ByteOrderValues::getInt(const unsigned char* buf, int byteOrder) {
27
0
    if (byteOrder == ENDIAN_BIG) {
28
0
        return ((int32_t)(buf[0] & 0xff) << 24) | ((int32_t)(buf[1] & 0xff) << 16) |
29
0
               ((int32_t)(buf[2] & 0xff) << 8) | ((int32_t)(buf[3] & 0xff));
30
0
    } else { // ENDIAN_LITTLE
31
0
        return ((int32_t)(buf[3] & 0xff) << 24) | ((int32_t)(buf[2] & 0xff) << 16) |
32
0
               ((int32_t)(buf[1] & 0xff) << 8) | ((int32_t)(buf[0] & 0xff));
33
0
    }
34
0
}
35
36
0
uint32_t ByteOrderValues::getUnsigned(const unsigned char* buf, int byteOrder) {
37
0
    if (byteOrder == ENDIAN_BIG) {
38
0
        return ((uint32_t)(buf[0] & 0xff) << 24) | ((uint32_t)(buf[1] & 0xff) << 16) |
39
0
               ((uint32_t)(buf[2] & 0xff) << 8) | ((uint32_t)(buf[3] & 0xff));
40
0
    } else { // ENDIAN_LITTLE
41
0
        return ((uint32_t)(buf[3] & 0xff) << 24) | ((uint32_t)(buf[2] & 0xff) << 16) |
42
0
               ((uint32_t)(buf[1] & 0xff) << 8) | ((uint32_t)(buf[0] & 0xff));
43
0
    }
44
0
}
45
46
0
void ByteOrderValues::putInt(int32_t intValue, unsigned char* buf, int byteOrder) {
47
0
    if (byteOrder == ENDIAN_BIG) {
48
0
        buf[0] = (unsigned char)(intValue >> 24);
49
0
        buf[1] = (unsigned char)(intValue >> 16);
50
0
        buf[2] = (unsigned char)(intValue >> 8);
51
0
        buf[3] = (unsigned char)intValue;
52
0
    } else { // ENDIAN_LITTLE
53
0
        buf[3] = (unsigned char)(intValue >> 24);
54
0
        buf[2] = (unsigned char)(intValue >> 16);
55
0
        buf[1] = (unsigned char)(intValue >> 8);
56
0
        buf[0] = (unsigned char)intValue;
57
0
    }
58
0
}
59
60
0
void ByteOrderValues::putUnsigned(uint32_t intValue, unsigned char* buf, int byteOrder) {
61
0
    if (byteOrder == ENDIAN_BIG) {
62
0
        buf[0] = (unsigned char)(intValue >> 24);
63
0
        buf[1] = (unsigned char)(intValue >> 16);
64
0
        buf[2] = (unsigned char)(intValue >> 8);
65
0
        buf[3] = (unsigned char)intValue;
66
0
    } else { // ENDIAN_LITTLE
67
0
        buf[3] = (unsigned char)(intValue >> 24);
68
0
        buf[2] = (unsigned char)(intValue >> 16);
69
0
        buf[1] = (unsigned char)(intValue >> 8);
70
0
        buf[0] = (unsigned char)intValue;
71
0
    }
72
0
}
73
74
0
int64_t ByteOrderValues::getLong(const unsigned char* buf, int byteOrder) {
75
0
    if (byteOrder == ENDIAN_BIG) {
76
0
        return (int64_t)(buf[0]) << 56 | (int64_t)(buf[1] & 0xff) << 48 |
77
0
               (int64_t)(buf[2] & 0xff) << 40 | (int64_t)(buf[3] & 0xff) << 32 |
78
0
               (int64_t)(buf[4] & 0xff) << 24 | (int64_t)(buf[5] & 0xff) << 16 |
79
0
               (int64_t)(buf[6] & 0xff) << 8 | (int64_t)(buf[7] & 0xff);
80
0
    } else { // ENDIAN_LITTLE
81
0
        return (int64_t)(buf[7]) << 56 | (int64_t)(buf[6] & 0xff) << 48 |
82
0
               (int64_t)(buf[5] & 0xff) << 40 | (int64_t)(buf[4] & 0xff) << 32 |
83
0
               (int64_t)(buf[3] & 0xff) << 24 | (int64_t)(buf[2] & 0xff) << 16 |
84
0
               (int64_t)(buf[1] & 0xff) << 8 | (int64_t)(buf[0] & 0xff);
85
0
    }
86
0
}
87
88
0
void ByteOrderValues::putLong(int64_t longValue, unsigned char* buf, int byteOrder) {
89
0
    if (byteOrder == ENDIAN_BIG) {
90
0
        buf[0] = (unsigned char)(longValue >> 56);
91
0
        buf[1] = (unsigned char)(longValue >> 48);
92
0
        buf[2] = (unsigned char)(longValue >> 40);
93
0
        buf[3] = (unsigned char)(longValue >> 32);
94
0
        buf[4] = (unsigned char)(longValue >> 24);
95
0
        buf[5] = (unsigned char)(longValue >> 16);
96
0
        buf[6] = (unsigned char)(longValue >> 8);
97
0
        buf[7] = (unsigned char)longValue;
98
0
    } else { // ENDIAN_LITTLE
99
0
        buf[0] = (unsigned char)longValue;
100
0
        buf[1] = (unsigned char)(longValue >> 8);
101
0
        buf[2] = (unsigned char)(longValue >> 16);
102
0
        buf[3] = (unsigned char)(longValue >> 24);
103
0
        buf[4] = (unsigned char)(longValue >> 32);
104
0
        buf[5] = (unsigned char)(longValue >> 40);
105
0
        buf[6] = (unsigned char)(longValue >> 48);
106
0
        buf[7] = (unsigned char)(longValue >> 56);
107
0
    }
108
0
}
109
110
0
double ByteOrderValues::getDouble(const unsigned char* buf, int byteOrder) {
111
0
    int64_t longValue = getLong(buf, byteOrder);
112
0
    double ret;
113
0
    std::memcpy(&ret, &longValue, sizeof(double));
114
0
    return ret;
115
0
}
116
117
0
void ByteOrderValues::putDouble(double doubleValue, unsigned char* buf, int byteOrder) {
118
0
    int64_t longValue;
119
0
    std::memcpy(&longValue, &doubleValue, sizeof(double));
120
0
    putLong(longValue, buf, byteOrder);
121
0
}
122
123
} // namespace doris