Coverage Report

Created: 2025-04-30 13:04

/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
188
uint32_t ByteOrderValues::getUnsigned(const unsigned char* buf, int byteOrder) {
37
188
    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
188
    } else { // ENDIAN_LITTLE
41
188
        return ((uint32_t)(buf[3] & 0xff) << 24) | ((uint32_t)(buf[2] & 0xff) << 16) |
42
188
               ((uint32_t)(buf[1] & 0xff) << 8) | ((uint32_t)(buf[0] & 0xff));
43
188
    }
44
188
}
45
46
209
void ByteOrderValues::putInt(int32_t intValue, unsigned char* buf, int byteOrder) {
47
209
    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
209
    } else { // ENDIAN_LITTLE
53
209
        buf[3] = (unsigned char)(intValue >> 24);
54
209
        buf[2] = (unsigned char)(intValue >> 16);
55
209
        buf[1] = (unsigned char)(intValue >> 8);
56
209
        buf[0] = (unsigned char)intValue;
57
209
    }
58
209
}
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
600
int64_t ByteOrderValues::getLong(const unsigned char* buf, int byteOrder) {
75
600
    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
600
    } else { // ENDIAN_LITTLE
81
600
        return (int64_t)(buf[7]) << 56 | (int64_t)(buf[6] & 0xff) << 48 |
82
600
               (int64_t)(buf[5] & 0xff) << 40 | (int64_t)(buf[4] & 0xff) << 32 |
83
600
               (int64_t)(buf[3] & 0xff) << 24 | (int64_t)(buf[2] & 0xff) << 16 |
84
600
               (int64_t)(buf[1] & 0xff) << 8 | (int64_t)(buf[0] & 0xff);
85
600
    }
86
600
}
87
88
646
void ByteOrderValues::putLong(int64_t longValue, unsigned char* buf, int byteOrder) {
89
646
    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
646
    } else { // ENDIAN_LITTLE
99
646
        buf[0] = (unsigned char)longValue;
100
646
        buf[1] = (unsigned char)(longValue >> 8);
101
646
        buf[2] = (unsigned char)(longValue >> 16);
102
646
        buf[3] = (unsigned char)(longValue >> 24);
103
646
        buf[4] = (unsigned char)(longValue >> 32);
104
646
        buf[5] = (unsigned char)(longValue >> 40);
105
646
        buf[6] = (unsigned char)(longValue >> 48);
106
646
        buf[7] = (unsigned char)(longValue >> 56);
107
646
    }
108
646
}
109
110
600
double ByteOrderValues::getDouble(const unsigned char* buf, int byteOrder) {
111
600
    int64_t longValue = getLong(buf, byteOrder);
112
600
    double ret;
113
600
    std::memcpy(&ret, &longValue, sizeof(double));
114
600
    return ret;
115
600
}
116
117
646
void ByteOrderValues::putDouble(double doubleValue, unsigned char* buf, int byteOrder) {
118
646
    int64_t longValue;
119
646
    std::memcpy(&longValue, &doubleValue, sizeof(double));
120
646
    putLong(longValue, buf, byteOrder);
121
646
}
122
123
} // namespace doris