MySQL  8.0.27
Source Code Documentation
big_endian.h
Go to the documentation of this file.
1 /* Copyright (c) 2012, 2021, Oracle and/or its affiliates.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 /**
24  @file include/big_endian.h
25 
26  Endianness-independent definitions (little_endian.h contains optimized
27  versions if you know you are on a little-endian platform).
28 */
29 
30 // IWYU pragma: private, include "my_byteorder.h"
31 
32 #ifndef MY_BYTEORDER_INCLUDED
33 #error This file should never be #included directly; use my_byteorder.h.
34 #endif
35 
36 #include <string.h>
37 
38 #include "my_inttypes.h"
39 
40 static inline int16 sint2korr(const uchar *A) {
41  return (int16)(((int16)(A[0])) + ((int16)(A[1]) << 8));
42 }
43 
44 static inline int32 sint4korr(const uchar *A) {
45  return (int32)(((int32)(A[0])) + (((int32)(A[1]) << 8)) +
46  (((int32)(A[2]) << 16)) + (((int32)(A[3]) << 24)));
47 }
48 
49 static inline uint16 uint2korr(const uchar *A) {
50  return (uint16)(((uint16)(A[0])) + ((uint16)(A[1]) << 8));
51 }
52 
53 static inline uint32 uint4korr(const uchar *A) {
54  return (uint32)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
55  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24));
56 }
57 
58 static inline ulonglong uint8korr(const uchar *A) {
59  return ((ulonglong)(((uint32)(A[0])) + (((uint32)(A[1])) << 8) +
60  (((uint32)(A[2])) << 16) + (((uint32)(A[3])) << 24)) +
61  (((ulonglong)(((uint32)(A[4])) + (((uint32)(A[5])) << 8) +
62  (((uint32)(A[6])) << 16) + (((uint32)(A[7])) << 24)))
63  << 32));
64 }
65 
66 static inline longlong sint8korr(const uchar *A) {
67  return (longlong)uint8korr(A);
68 }
69 
70 static inline void int2store(uchar *T, uint16 A) {
71  uint def_temp = A;
72  *(T) = (uchar)(def_temp);
73  *(T + 1) = (uchar)(def_temp >> 8);
74 }
75 
76 static inline void int4store(uchar *T, uint32 A) {
77  *(T) = (uchar)(A);
78  *(T + 1) = (uchar)(A >> 8);
79  *(T + 2) = (uchar)(A >> 16);
80  *(T + 3) = (uchar)(A >> 24);
81 }
82 
83 static inline void int7store(uchar *T, ulonglong A) {
84  *(T) = (uchar)(A);
85  *(T + 1) = (uchar)(A >> 8);
86  *(T + 2) = (uchar)(A >> 16);
87  *(T + 3) = (uchar)(A >> 24);
88  *(T + 4) = (uchar)(A >> 32);
89  *(T + 5) = (uchar)(A >> 40);
90  *(T + 6) = (uchar)(A >> 48);
91 }
92 
93 static inline void int8store(uchar *T, ulonglong A) {
94  uint def_temp = (uint)A, def_temp2 = (uint)(A >> 32);
95  int4store(T, def_temp);
96  int4store(T + 4, def_temp2);
97 }
98 
99 /*
100  Data in big-endian format.
101 */
102 static inline void float4store(uchar *T, float A) {
103  *(T) = ((uchar *)&A)[3];
104  *((T) + 1) = (char)((uchar *)&A)[2];
105  *((T) + 2) = (char)((uchar *)&A)[1];
106  *((T) + 3) = (char)((uchar *)&A)[0];
107 }
108 
109 static inline float float4get(const uchar *M) {
110  float def_temp;
111  ((uchar *)&def_temp)[0] = (M)[3];
112  ((uchar *)&def_temp)[1] = (M)[2];
113  ((uchar *)&def_temp)[2] = (M)[1];
114  ((uchar *)&def_temp)[3] = (M)[0];
115  return def_temp;
116 }
117 
118 static inline void float8store(uchar *T, double V) {
119  *(T) = ((uchar *)&V)[7];
120  *((T) + 1) = (char)((uchar *)&V)[6];
121  *((T) + 2) = (char)((uchar *)&V)[5];
122  *((T) + 3) = (char)((uchar *)&V)[4];
123  *((T) + 4) = (char)((uchar *)&V)[3];
124  *((T) + 5) = (char)((uchar *)&V)[2];
125  *((T) + 6) = (char)((uchar *)&V)[1];
126  *((T) + 7) = (char)((uchar *)&V)[0];
127 }
128 
129 static inline double float8get(const uchar *M) {
130  double def_temp;
131  ((uchar *)&def_temp)[0] = (M)[7];
132  ((uchar *)&def_temp)[1] = (M)[6];
133  ((uchar *)&def_temp)[2] = (M)[5];
134  ((uchar *)&def_temp)[3] = (M)[4];
135  ((uchar *)&def_temp)[4] = (M)[3];
136  ((uchar *)&def_temp)[5] = (M)[2];
137  ((uchar *)&def_temp)[6] = (M)[1];
138  ((uchar *)&def_temp)[7] = (M)[0];
139  return def_temp;
140 }
static double float8get(const uchar *M)
Definition: big_endian.h:129
static void int8store(uchar *T, ulonglong A)
Definition: big_endian.h:93
static void int4store(uchar *T, uint32 A)
Definition: big_endian.h:76
static void float8store(uchar *T, double V)
Definition: big_endian.h:118
static float float4get(const uchar *M)
Definition: big_endian.h:109
static void float4store(uchar *T, float A)
Definition: big_endian.h:102
static void int7store(uchar *T, ulonglong A)
Definition: big_endian.h:83
static int16 sint2korr(const uchar *A)
Definition: big_endian.h:40
static void int2store(uchar *T, uint16 A)
Definition: big_endian.h:70
static uint16 uint2korr(const uchar *A)
Definition: big_endian.h:49
static ulonglong uint8korr(const uchar *A)
Definition: big_endian.h:58
static uint32 uint4korr(const uchar *A)
Definition: big_endian.h:53
static int32 sint4korr(const uchar *A)
Definition: big_endian.h:44
static longlong sint8korr(const uchar *A)
Definition: big_endian.h:66
#define M
Definition: ctype-tis620.cc:73
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
long long int longlong
Definition: my_inttypes.h:54
int16_t int16
Definition: my_inttypes.h:63
int32_t int32
Definition: my_inttypes.h:65
uint16_t uint16
Definition: my_inttypes.h:64
uint32_t uint32
Definition: my_inttypes.h:66
unsigned int uint
Definition: uca-dump.cc:29