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