• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*!
2  * \copy
3  *     Copyright (c)  2013, Cisco Systems
4  *     All rights reserved.
5  *
6  *     Redistribution and use in source and binary forms, with or without
7  *     modification, are permitted provided that the following conditions
8  *     are met:
9  *
10  *        * Redistributions of source code must retain the above copyright
11  *          notice, this list of conditions and the following disclaimer.
12  *
13  *        * Redistributions in binary form must reproduce the above copyright
14  *          notice, this list of conditions and the following disclaimer in
15  *          the documentation and/or other materials provided with the
16  *          distribution.
17  *
18  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *     COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  *     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  *     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  *     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  *     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *     POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #ifndef ___LD_ST_MACROS___
34 #define ___LD_ST_MACROS___
35 
36 #include <string.h>
37 #include "typedefs.h"
38 
39 #ifdef __GNUC__
40 
41 struct tagUnaligned_64 {
42   uint64_t l;
43 } __attribute__ ((packed)) __attribute__ ((may_alias));
44 struct tagUnaligned_32 {
45   uint32_t l;
46 } __attribute__ ((packed)) __attribute__ ((may_alias));
47 struct tagUnaligned_16 {
48   uint16_t l;
49 } __attribute__ ((packed)) __attribute__ ((may_alias));
50 
51 #define LD16(a) (((struct tagUnaligned_16 *) (a))->l)
52 #define LD32(a) (((struct tagUnaligned_32 *) (a))->l)
53 #define LD64(a) (((struct tagUnaligned_64 *) (a))->l)
54 
55 #define STRUCTA(size, align) struct tagUnaligned_##size##_##align {\
56     uint##size##_t l; \
57 } __attribute__ ((aligned(align))) __attribute__ ((may_alias))
58 STRUCTA (16, 2);
59 STRUCTA (32, 2);
60 STRUCTA (32, 4);
61 STRUCTA (64, 2);
62 STRUCTA (64, 4);
63 STRUCTA (64, 8);
64 //#define _USE_STRUCT_INT_CVT
65 //#ifdef _USE_STRUCT_INT_CVT
66 #define ST16(a, b) (((struct tagUnaligned_16 *) (a))->l) = (b)
67 #define ST32(a, b) (((struct tagUnaligned_32 *) (a))->l) = (b)
68 #define ST64(a, b) (((struct tagUnaligned_64 *) (a))->l) = (b)
69 
70 #define LDA(a, size, align) (((struct tagUnaligned_##size##_##align *) (a))->l)
71 #define STA(a, b, size, align) (((struct tagUnaligned_##size##_##align *) (a))->l) = (b)
72 #define LD16A2(a) LDA(a, 16, 2)
73 #define LD32A2(a) LDA(a, 32, 2)
74 #define LD32A4(a) LDA(a, 32, 4)
75 #define LD64A2(a) LDA(a, 64, 2)
76 #define LD64A4(a) LDA(a, 64, 4)
77 #define LD64A8(a) LDA(a, 64, 8)
78 #define ST16A2(a, b) STA(a, b, 16, 2)
79 #define ST32A2(a, b) STA(a, b, 32, 2)
80 #define ST32A4(a, b) STA(a, b, 32, 4)
81 #define ST64A2(a, b) STA(a, b, 64, 2)
82 #define ST64A4(a, b) STA(a, b, 64, 4)
83 #define ST64A8(a, b) STA(a, b, 64, 8)
84 //#else
85 //inline void __ST16(void *dst, uint16_t v) { memcpy(dst, &v, 2); }
86 //inline void __ST32(void *dst, uint32_t v) { memcpy(dst, &v, 4); }
87 //inline void __ST64(void *dst, uint64_t v) { memcpy(dst, &v, 8); }
88 //#endif
89 
90 #else
91 
92 //#define INTD16(a) (*((int16_t*)(a)))
93 //#define INTD32(a) (*((int32_t*)(a)))
94 //#define INTD64(a) (*((int64_t*)(a)))
95 
96 #define LD16(a) (*((uint16_t*)(a)))
97 #define LD32(a) (*((uint32_t*)(a)))
98 #define LD64(a) (*((uint64_t*)(a)))
99 
100 #define ST16(a, b) *((uint16_t*)(a)) = (b)
101 #define ST32(a, b) *((uint32_t*)(a)) = (b)
102 #define ST64(a, b) *((uint64_t*)(a)) = (b)
103 #define LD16A2 LD16
104 #define LD32A2 LD32
105 #define LD32A4 LD32
106 #define LD64A2 LD64
107 #define LD64A4 LD64
108 #define LD64A8 LD64
109 #define ST16A2 ST16
110 #define ST32A2 ST32
111 #define ST32A4 ST32
112 #define ST64A2 ST64
113 #define ST64A4 ST64
114 #define ST64A8 ST64
115 
116 #endif /* !__GNUC__ */
117 
118 #ifndef INTD16
119 #define INTD16 LD16
120 #endif//INTD16
121 
122 #ifndef INTD32
123 #define INTD32 LD32
124 #endif//INTD32
125 
126 #ifndef INTD64
127 #define INTD64 LD64
128 #endif//INTD64
129 
130 #endif//___LD_ST_MACROS___
131