aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libmysql_r/include/little_endian.h
blob: c67a6ee03e461768f378c6d0bff9ff20bcd47020 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef LITTLE_ENDIAN_INCLUDED
#define LITTLE_ENDIAN_INCLUDED
/* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is also distributed with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have included with MySQL.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file include/little_endian.h
  Data in little-endian format.
*/

// IWYU pragma: private, include "my_byteorder.h"

#ifndef MY_BYTEORDER_INCLUDED
#error This file should never be #included directly; use my_byteorder.h.
#endif

#include <string.h>

#include "my_inttypes.h"

/*
  Since the pointers may be misaligned, we cannot do a straight read out of
  them. (It usually works-by-accident on x86 and on modern ARM, but not always
  when the compiler chooses unusual instruction for the read, e.g. LDM on ARM
  or most SIMD instructions on x86.) memcpy is safe and gets optimized to a
  single operation, since the size is small and constant.
*/

static inline int16 sint2korr(const uchar *A) {
  int16 ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline int32 sint4korr(const uchar *A) {
  int32 ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline uint16 uint2korr(const uchar *A) {
  uint16 ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline uint32 uint4korr(const uchar *A) {
  uint32 ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline ulonglong uint8korr(const uchar *A) {
  ulonglong ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline longlong sint8korr(const uchar *A) {
  longlong ret;
  memcpy(&ret, A, sizeof(ret));
  return ret;
}

static inline void int2store(uchar *T, uint16 A) { memcpy(T, &A, sizeof(A)); }

static inline void int4store(uchar *T, uint32 A) { memcpy(T, &A, sizeof(A)); }

static inline void int7store(uchar *T, ulonglong A) { memcpy(T, &A, 7); }

static inline void int8store(uchar *T, ulonglong A) {
  memcpy(T, &A, sizeof(A));
}

static inline void float4get(float *V, const uchar *M) {
  memcpy(V, (M), sizeof(float));
}

static inline void float4store(uchar *V, float M) {
  memcpy(V, (&M), sizeof(float));
}

static inline void float8get(double *V, const uchar *M) {
  memcpy(V, M, sizeof(double));
}

static inline void float8store(uchar *V, double M) {
  memcpy(V, &M, sizeof(double));
}

static inline void floatget(float *V, const uchar *M) { float4get(V, M); }
static inline void floatstore(uchar *V, float M) { float4store(V, M); }

static inline void doublestore(uchar *T, double V) {
  memcpy(T, &V, sizeof(double));
}
static inline void doubleget(double *V, const uchar *M) {
  memcpy(V, M, sizeof(double));
}

static inline void ushortget(uint16 *V, const uchar *pM) { *V = uint2korr(pM); }
static inline void shortget(int16 *V, const uchar *pM) { *V = sint2korr(pM); }
static inline void longget(int32 *V, const uchar *pM) { *V = sint4korr(pM); }
static inline void ulongget(uint32 *V, const uchar *pM) { *V = uint4korr(pM); }
static inline void shortstore(uchar *T, int16 V) { int2store(T, V); }
static inline void longstore(uchar *T, int32 V) { int4store(T, V); }

static inline void longlongget(longlong *V, const uchar *M) {
  memcpy(V, (M), sizeof(ulonglong));
}
static inline void longlongstore(uchar *T, longlong V) {
  memcpy((T), &V, sizeof(ulonglong));
}

#endif /* LITTLE_ENDIAN_INCLUDED */