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