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_DSP_AOM_DSP_COMMON_H_
13 #define AOM_AOM_DSP_AOM_DSP_COMMON_H_
14
15 #include "config/aom_config.h"
16
17 #include "aom/aom_integer.h"
18 #include "aom_ports/mem.h"
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #ifndef MAX_SB_SIZE
25 #define MAX_SB_SIZE 128
26 #endif // ndef MAX_SB_SIZE
27
28 #define AOMMIN(x, y) (((x) < (y)) ? (x) : (y))
29 #define AOMMAX(x, y) (((x) > (y)) ? (x) : (y))
30
31 #define IMPLIES(a, b) (!(a) || (b)) // Logical 'a implies b' (or 'a -> b')
32
33 #define IS_POWER_OF_TWO(x) (((x) & ((x)-1)) == 0)
34
35 /* Left shifting a negative value became undefined behavior in C99 (downgraded
36 from merely implementation-defined in C89). This should still compile to the
37 correct thing on any two's-complement machine, but avoid ubsan warnings.*/
38 #define AOM_SIGNED_SHL(x, shift) ((x) * (((x)*0 + 1) << (shift)))
39
40 // These can be used to give a hint about branch outcomes.
41 // This can have an effect, even if your target processor has a
42 // good branch predictor, as these hints can affect basic block
43 // ordering by the compiler.
44 #ifdef __GNUC__
45 #define LIKELY(v) __builtin_expect(v, 1)
46 #define UNLIKELY(v) __builtin_expect(v, 0)
47 #else
48 #define LIKELY(v) (v)
49 #define UNLIKELY(v) (v)
50 #endif
51
52 typedef uint8_t qm_val_t;
53 #define AOM_QM_BITS 5
54
55 // Note:
56 // tran_low_t is the datatype used for final transform coefficients.
57 // tran_high_t is the datatype used for intermediate transform stages.
58 typedef int64_t tran_high_t;
59 typedef int32_t tran_low_t;
60
clip_pixel(int val)61 static INLINE uint8_t clip_pixel(int val) {
62 return (val > 255) ? 255 : (val < 0) ? 0 : val;
63 }
64
clamp(int value,int low,int high)65 static INLINE int clamp(int value, int low, int high) {
66 return value < low ? low : (value > high ? high : value);
67 }
68
clamp64(int64_t value,int64_t low,int64_t high)69 static INLINE int64_t clamp64(int64_t value, int64_t low, int64_t high) {
70 return value < low ? low : (value > high ? high : value);
71 }
72
fclamp(double value,double low,double high)73 static INLINE double fclamp(double value, double low, double high) {
74 return value < low ? low : (value > high ? high : value);
75 }
76
clip_pixel_highbd(int val,int bd)77 static INLINE uint16_t clip_pixel_highbd(int val, int bd) {
78 switch (bd) {
79 case 8:
80 default: return (uint16_t)clamp(val, 0, 255);
81 case 10: return (uint16_t)clamp(val, 0, 1023);
82 case 12: return (uint16_t)clamp(val, 0, 4095);
83 }
84 }
85
86 // The result of this branchless code is equivalent to (value < 0 ? 0 : value)
87 // or max(0, value) and might be faster in some cases.
88 // Care should be taken since the behavior of right shifting signed type
89 // negative value is undefined by C standards and implementation defined,
negative_to_zero(int value)90 static INLINE unsigned int negative_to_zero(int value) {
91 return value & ~(value >> (sizeof(value) * 8 - 1));
92 }
93
94 #ifdef __cplusplus
95 } // extern "C"
96 #endif
97
98 #endif // AOM_AOM_DSP_AOM_DSP_COMMON_H_
99