xref: /aosp_15_r20/external/libopenapv/src/oapv_port.h (revision abb65b4b03b69e1d508d4d9a44dcf199df16e7c3)
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the copyright owner, nor the names of its contributors
16  *   may be used to endorse or promote products derived from this software
17  *   without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _OAPV_PORT_H_
33 #define _OAPV_PORT_H_
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <stdint.h>
40 #include <stdbool.h>
41 
42 /*****************************************************************************
43  * types
44  *****************************************************************************/
45 typedef int8_t   s8;
46 typedef uint8_t  u8;
47 typedef int16_t  s16;
48 typedef uint16_t u16;
49 typedef int32_t  s32;
50 typedef uint32_t u32;
51 typedef int64_t  s64;
52 typedef uint64_t u64;
53 
54 typedef s16      pel;
55 typedef s32      dpel;
56 
57 /*****************************************************************************
58  * inline attribute
59  *****************************************************************************/
60 #ifndef force_inline
61 #if defined(_MSC_VER)
62 #define force_inline __forceinline
63 #elif defined(__GNUC__)
64 #define force_inline __attribute__((always_inline)) inline
65 #else
66 #define force_inline inline
67 #endif
68 #endif
69 
70 /*****************************************************************************
71  * memory operations
72  *****************************************************************************/
73 #define oapv_malloc(size)      malloc((size))
74 #define oapv_malloc_fast(size) oapv_malloc((size))
75 
76 #define oapv_mfree(m) \
77     {                 \
78         if(m) {       \
79             free(m);  \
80         }             \
81     }
82 #define oapv_mfree_fast(m) \
83     {                      \
84         if(m) {            \
85             oapv_mfree(m); \
86         }                  \
87     }
88 
89 void *oapv_malloc_align32(int size);
90 void oapv_mfree_align32(void *p);
91 
92 #define oapv_mcpy(dst, src, size)    memcpy((dst), (src), (size))
93 #define oapv_mset(dst, v, size)      memset((dst), (v), (size))
94 #define oapv_mset_x64a(dst, v, size) memset((dst), (v), (size))
95 #define oapv_mset_x128(dst, v, size) memset((dst), (v), (size))
96 #define oapv_mcmp(dst, src, size)    memcmp((dst), (src), (size))
97 
oapv_mset_16b(s16 * dst,s16 v,int cnt)98 static __inline void oapv_mset_16b(s16 *dst, s16 v, int cnt)
99 {
100     int i;
101     for(i = 0; i < cnt; i++)
102         dst[i] = v;
103 }
104 
105 /*****************************************************************************
106  * trace and assert
107  *****************************************************************************/
108 void oapv_trace0(char *filename, int line, const char *fmt, ...);
109 void oapv_trace_line(char *pre);
110 #ifndef OAPV_TRACE
111 #define OAPV_TRACE 0
112 #endif
113 
114 /* trace function */
115 #if OAPV_TRACE
116 #if defined(__GNUC__)
117 #define __FILENAME__ \
118     (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
119 #define oapv_trace(args...) oapv_trace0(__FILENAME__, __LINE__, args)
120 #else
121 #define __FILENAME__ \
122     (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
123 #define oapv_trace(args, ...) oapv_trace0(__FILENAME__, __LINE__, args, __VA_ARGS__)
124 #endif
125 #else
126 #define oapv_trace(args, ...) \
127     {}
128 #endif
129 #if defined(__GNUC__)
130 #define oapv_print(args, ...) oapv_trace0(NULL, -1, args)
131 #else
132 #define oapv_print(args, ...) oapv_trace0(NULL, -1, args, __VA_ARGS__)
133 #endif
134 
135 /* assert function */
136 #include <assert.h>
137 #define oapv_assert(x) assert(x)
138 #define oapv_assert_r(x)     \
139     {                        \
140         if(!(x)) {           \
141             oapv_assert(x);  \
142             return;          \
143         }                    \
144     }
145 #define oapv_assert_rv(x, r) \
146     {                        \
147         if(!(x)) {           \
148             oapv_assert(x);  \
149             return (r);      \
150         }                    \
151     }
152 #define oapv_assert_g(x, g)  \
153     {                        \
154         if(!(x)) {           \
155             oapv_assert(x);  \
156             goto g;          \
157         }                    \
158     }
159 #define oapv_assert_gv(x, r, v, g) \
160     {                              \
161         if(!(x)) {                 \
162             oapv_assert(x);        \
163             (r) = (v);             \
164             goto g;                \
165         }                          \
166     }
167 
168 #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || \
169     defined(_M_X64) || defined(__amd64__) || defined(_M_AMD64) ||   \
170     defined(__i386__)
171 #define X86_SSE 1
172 #elif defined(__aarch64__)
173 #define ARM_NEON 1
174 #endif
175 
176 #if X86_SSE
177 #ifdef _WIN32
178 #include <emmintrin.h>
179 #include <xmmintrin.h>
180 #include <tmmintrin.h>
181 #include <smmintrin.h>
182 #else
183 #include <x86intrin.h>
184 #endif
185 #endif
186 
187 #if ARM_NEON
188 #include <arm_neon.h>
189 #endif
190 
191 /* Buffer Alignement */
192 #if defined(_WIN32) && !defined(__GNUC__)
193 #define DECLARE_ALIGNED(var, n) __declspec(align(n)) var
194 #else
195 #define DECLARE_ALIGNED(var, n) var __attribute__((aligned(n)))
196 #endif
197 
198 #define ALIGNED_16(var)  DECLARE_ALIGNED(var, 16)
199 #define ALIGNED_32(var)  DECLARE_ALIGNED(var, 32)
200 #define ALIGNED_128(var) DECLARE_ALIGNED(var, 128)
201 
202 #endif /* _OAPV_PORT_H_ */
203