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