xref: /aosp_15_r20/external/libaom/aom_ports/mem.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_PORTS_MEM_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_PORTS_MEM_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
16*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #if defined(__GNUC__) || defined(__SUNPRO_C)
19*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_ALIGNED(n, typ, val) typ val __attribute__((aligned(n)))
20*77c1e3ccSAndroid Build Coastguard Worker #elif defined(_MSC_VER)
21*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_ALIGNED(n, typ, val) __declspec(align(n)) typ val
22*77c1e3ccSAndroid Build Coastguard Worker #else
23*77c1e3ccSAndroid Build Coastguard Worker #warning No alignment directives known for this compiler.
24*77c1e3ccSAndroid Build Coastguard Worker #define DECLARE_ALIGNED(n, typ, val) typ val
25*77c1e3ccSAndroid Build Coastguard Worker #endif
26*77c1e3ccSAndroid Build Coastguard Worker 
27*77c1e3ccSAndroid Build Coastguard Worker #if defined(__has_builtin)
28*77c1e3ccSAndroid Build Coastguard Worker #define AOM_HAS_BUILTIN(x) __has_builtin(x)
29*77c1e3ccSAndroid Build Coastguard Worker #else
30*77c1e3ccSAndroid Build Coastguard Worker #define AOM_HAS_BUILTIN(x) 0
31*77c1e3ccSAndroid Build Coastguard Worker #endif
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker #if !AOM_HAS_BUILTIN(__builtin_prefetch) && !defined(__GNUC__)
34*77c1e3ccSAndroid Build Coastguard Worker #define __builtin_prefetch(x)
35*77c1e3ccSAndroid Build Coastguard Worker #endif
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker /* Shift down with rounding for use when n >= 0. Usually value >= 0, but the
38*77c1e3ccSAndroid Build Coastguard Worker  * macro can be used with a negative value if the direction of rounding is
39*77c1e3ccSAndroid Build Coastguard Worker  * acceptable.
40*77c1e3ccSAndroid Build Coastguard Worker  */
41*77c1e3ccSAndroid Build Coastguard Worker #define ROUND_POWER_OF_TWO(value, n) (((value) + (((1 << (n)) >> 1))) >> (n))
42*77c1e3ccSAndroid Build Coastguard Worker 
43*77c1e3ccSAndroid Build Coastguard Worker /* Shift down with rounding for signed integers, for use when n >= 0 */
44*77c1e3ccSAndroid Build Coastguard Worker #define ROUND_POWER_OF_TWO_SIGNED(value, n)           \
45*77c1e3ccSAndroid Build Coastguard Worker   (((value) < 0) ? -ROUND_POWER_OF_TWO(-(value), (n)) \
46*77c1e3ccSAndroid Build Coastguard Worker                  : ROUND_POWER_OF_TWO((value), (n)))
47*77c1e3ccSAndroid Build Coastguard Worker 
48*77c1e3ccSAndroid Build Coastguard Worker /* Shift down with rounding for use when n >= 0 (64-bit value). Usually
49*77c1e3ccSAndroid Build Coastguard Worker  * value >= 0, but the macro can be used with a negative value if the direction
50*77c1e3ccSAndroid Build Coastguard Worker  * of rounding is acceptable.
51*77c1e3ccSAndroid Build Coastguard Worker  */
52*77c1e3ccSAndroid Build Coastguard Worker #define ROUND_POWER_OF_TWO_64(value, n) \
53*77c1e3ccSAndroid Build Coastguard Worker   (((value) + ((((int64_t)1 << (n)) >> 1))) >> (n))
54*77c1e3ccSAndroid Build Coastguard Worker /* Shift down with rounding for signed integers, for use when n >= 0 (64-bit
55*77c1e3ccSAndroid Build Coastguard Worker  * value)
56*77c1e3ccSAndroid Build Coastguard Worker  */
57*77c1e3ccSAndroid Build Coastguard Worker #define ROUND_POWER_OF_TWO_SIGNED_64(value, n)           \
58*77c1e3ccSAndroid Build Coastguard Worker   (((value) < 0) ? -ROUND_POWER_OF_TWO_64(-(value), (n)) \
59*77c1e3ccSAndroid Build Coastguard Worker                  : ROUND_POWER_OF_TWO_64((value), (n)))
60*77c1e3ccSAndroid Build Coastguard Worker 
61*77c1e3ccSAndroid Build Coastguard Worker /* Shift down with ceil() for use when n >= 0 and value >= 0.*/
62*77c1e3ccSAndroid Build Coastguard Worker #define CEIL_POWER_OF_TWO(value, n) (((value) + (1 << (n)) - 1) >> (n))
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker /* shift right or left depending on sign of n */
65*77c1e3ccSAndroid Build Coastguard Worker #define RIGHT_SIGNED_SHIFT(value, n) \
66*77c1e3ccSAndroid Build Coastguard Worker   ((n) < 0 ? ((value) << (-(n))) : ((value) >> (n)))
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker #define ALIGN_POWER_OF_TWO(value, n) \
69*77c1e3ccSAndroid Build Coastguard Worker   (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
70*77c1e3ccSAndroid Build Coastguard Worker #define ALIGN_POWER_OF_TWO_UNSIGNED(value, n) \
71*77c1e3ccSAndroid Build Coastguard Worker   (((value) + ((1u << (n)) - 1)) & ~((1u << (n)) - 1))
72*77c1e3ccSAndroid Build Coastguard Worker 
73*77c1e3ccSAndroid Build Coastguard Worker #define DIVIDE_AND_ROUND(x, y) (((x) + ((y) >> 1)) / (y))
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker #define CONVERT_TO_SHORTPTR(x) ((uint16_t *)(((uintptr_t)(x)) << 1))
76*77c1e3ccSAndroid Build Coastguard Worker #define CONVERT_TO_BYTEPTR(x) ((uint8_t *)(((uintptr_t)(x)) >> 1))
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be unsigned 1 byte*/
79*77c1e3ccSAndroid Build Coastguard Worker #define UENUM1BYTE(enumvar) \
80*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
81*77c1e3ccSAndroid Build Coastguard Worker   typedef uint8_t enumvar
82*77c1e3ccSAndroid Build Coastguard Worker 
83*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be signed 1 byte*/
84*77c1e3ccSAndroid Build Coastguard Worker #define SENUM1BYTE(enumvar) \
85*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
86*77c1e3ccSAndroid Build Coastguard Worker   typedef int8_t enumvar
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be unsigned 2 byte*/
89*77c1e3ccSAndroid Build Coastguard Worker #define UENUM2BYTE(enumvar) \
90*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
91*77c1e3ccSAndroid Build Coastguard Worker   typedef uint16_t enumvar
92*77c1e3ccSAndroid Build Coastguard Worker 
93*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be signed 2 byte*/
94*77c1e3ccSAndroid Build Coastguard Worker #define SENUM2BYTE(enumvar) \
95*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
96*77c1e3ccSAndroid Build Coastguard Worker   typedef int16_t enumvar
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be unsigned 4 byte*/
99*77c1e3ccSAndroid Build Coastguard Worker #define UENUM4BYTE(enumvar) \
100*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
101*77c1e3ccSAndroid Build Coastguard Worker   typedef uint32_t enumvar
102*77c1e3ccSAndroid Build Coastguard Worker 
103*77c1e3ccSAndroid Build Coastguard Worker /*!\brief force enum to be unsigned 4 byte*/
104*77c1e3ccSAndroid Build Coastguard Worker #define SENUM4BYTE(enumvar) \
105*77c1e3ccSAndroid Build Coastguard Worker   ;                         \
106*77c1e3ccSAndroid Build Coastguard Worker   typedef int32_t enumvar
107*77c1e3ccSAndroid Build Coastguard Worker 
108*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_PORTS_MEM_H_
109