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