1 #ifndef LITTLE_ENDIAN_INCLUDED
2 #define LITTLE_ENDIAN_INCLUDED
3 // Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License, version 2.0, as
7 // published by the Free Software Foundation.
8 //
9 // This program is also distributed with certain software (including
10 // but not limited to OpenSSL) that is licensed under separate terms,
11 // as designated in a particular file or component or in included license
12 // documentation. The authors of MySQL hereby grant you an
13 // additional permission to link the program and your derivative works
14 // with the separately licensed software that they have included with
15 // MySQL.
16 //
17 // Without limiting anything contained in the foregoing, this file,
18 // which is part of MySQL Server, is also subject to the
19 // Universal FOSS Exception, version 1.0, a copy of which can be found at
20 // http://oss.oracle.com/licenses/universal-foss-exception.
21 //
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25 // See the GNU General Public License, version 2.0, for more details.
26 //
27 // You should have received a copy of the GNU General Public License
28 // along with this program; if not, write to the Free Software Foundation, Inc.,
29 // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 
31 /*
32   Data in little-endian format.
33 */
34 
35 #include <string.h>
36 
float4get(float * V,const uchar * M)37 static inline void float4get  (float  *V, const uchar *M)
38 {
39   memcpy(V, (M), sizeof(float));
40 }
41 
float4store(uchar * V,float M)42 static inline void float4store(uchar  *V, float  M)
43 {
44   memcpy(V, (&M), sizeof(float));
45 }
46 
float8get(double * V,const uchar * M)47 static inline void float8get  (double *V, const uchar *M)
48 {
49   memcpy(V,  M, sizeof(double));
50 }
51 
float8store(uchar * V,double M)52 static inline void float8store(uchar  *V, double M)
53 {
54   memcpy(V, &M, sizeof(double));
55 }
56 
floatget(float * V,const uchar * M)57 static inline void floatget   (float  *V, const uchar *M) { float4get(V, M); }
floatstore(uchar * V,float M)58 static inline void floatstore (uchar  *V, float M)        { float4store(V, M); }
59 
60 /* Bi-endian hardware.... */
61 #if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
doublestore(uchar * T,double V)62 static inline void doublestore(uchar *T, double V)
63 { *(((char*)T)+0)=(char) ((uchar *) &V)[4];
64   *(((char*)T)+1)=(char) ((uchar *) &V)[5];
65   *(((char*)T)+2)=(char) ((uchar *) &V)[6];
66   *(((char*)T)+3)=(char) ((uchar *) &V)[7];
67   *(((char*)T)+4)=(char) ((uchar *) &V)[0];
68   *(((char*)T)+5)=(char) ((uchar *) &V)[1];
69   *(((char*)T)+6)=(char) ((uchar *) &V)[2];
70   *(((char*)T)+7)=(char) ((uchar *) &V)[3]; }
doubleget(double * V,const uchar * M)71 static inline void doubleget(double *V, const uchar *M)
72 { double def_temp;
73   ((uchar*) &def_temp)[0]=(M)[4];
74   ((uchar*) &def_temp)[1]=(M)[5];
75   ((uchar*) &def_temp)[2]=(M)[6];
76   ((uchar*) &def_temp)[3]=(M)[7];
77   ((uchar*) &def_temp)[4]=(M)[0];
78   ((uchar*) &def_temp)[5]=(M)[1];
79   ((uchar*) &def_temp)[6]=(M)[2];
80   ((uchar*) &def_temp)[7]=(M)[3];
81   (*V) = def_temp; }
82 
83 #else /* Bi-endian hardware.... */
84 
doublestore(uchar * T,double V)85 static inline void doublestore(uchar  *T, double V)       { memcpy(T, &V, sizeof(double)); }
doubleget(double * V,const uchar * M)86 static inline void doubleget  (double *V, const uchar *M) { memcpy(V, M, sizeof(double)); }
87 
88 #endif /* Bi-endian hardware.... */
89 
ushortget(uint16 * V,const uchar * pM)90 static inline void ushortget(uint16 *V, const uchar *pM) { *V= uint2korr(pM); }
shortget(int16 * V,const uchar * pM)91 static inline void shortget (int16  *V, const uchar *pM) { *V= sint2korr(pM); }
longget(int32 * V,const uchar * pM)92 static inline void longget  (int32  *V, const uchar *pM) { *V= sint4korr(pM); }
ulongget(uint32 * V,const uchar * pM)93 static inline void ulongget (uint32 *V, const uchar *pM) { *V= uint4korr(pM); }
shortstore(uchar * T,int16 V)94 static inline void shortstore(uchar *T, int16 V) { int2store(T, V); }
longstore(uchar * T,int32 V)95 static inline void longstore (uchar *T, int32 V) { int4store(T, V); }
96 
longlongget(longlong * V,const uchar * M)97 static inline void longlongget(longlong *V, const uchar *M)
98 {
99   memcpy(V, (M), sizeof(ulonglong));
100 }
longlongstore(uchar * T,longlong V)101 static inline void longlongstore(uchar *T, longlong V)
102 {
103   memcpy((T), &V, sizeof(ulonglong));
104 }
105 
106 #endif /* LITTLE_ENDIAN_INCLUDED */
107