xref: /aosp_15_r20/external/libvpx/vpx_dsp/vpx_dsp_common.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 /*
2  *  Copyright (c) 2015 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef VPX_VPX_DSP_VPX_DSP_COMMON_H_
12 #define VPX_VPX_DSP_VPX_DSP_COMMON_H_
13 
14 #include <limits.h>
15 
16 #include "./vpx_config.h"
17 #include "vpx/vpx_integer.h"
18 #include "vpx_ports/mem.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define VPXMIN(x, y) (((x) < (y)) ? (x) : (y))
25 #define VPXMAX(x, y) (((x) > (y)) ? (x) : (y))
26 
27 #define VPX_SWAP(type, a, b) \
28   do {                       \
29     type c = (b);            \
30     (b) = a;                 \
31     (a) = c;                 \
32   } while (0)
33 
34 #if CONFIG_VP9_HIGHBITDEPTH
35 // Note:
36 // tran_low_t  is the datatype used for final transform coefficients.
37 // tran_high_t is the datatype used for intermediate transform stages.
38 typedef int64_t tran_high_t;
39 typedef int32_t tran_low_t;
40 #else
41 // Note:
42 // tran_low_t  is the datatype used for final transform coefficients.
43 // tran_high_t is the datatype used for intermediate transform stages.
44 typedef int32_t tran_high_t;
45 typedef int16_t tran_low_t;
46 #endif  // CONFIG_VP9_HIGHBITDEPTH
47 
48 typedef int16_t tran_coef_t;
49 
50 // Visual Studio 2022 (cl.exe) targeting AArch64 with optimizations enabled
51 // produces invalid code for clip_pixel() when the return type is uint8_t.
52 // See:
53 // https://developercommunity.visualstudio.com/t/Misoptimization-for-ARM64-in-VS-2022-17/10363361
54 // TODO(jzern): check the compiler version after a fix for the issue is
55 // released.
56 #if defined(_MSC_VER) && defined(_M_ARM64) && !defined(__clang__)
clip_pixel(int val)57 static INLINE int clip_pixel(int val) {
58   return (val > 255) ? 255 : (val < 0) ? 0 : val;
59 }
60 #else
clip_pixel(int val)61 static INLINE uint8_t clip_pixel(int val) {
62   return (val > 255) ? 255 : (val < 0) ? 0 : val;
63 }
64 #endif
65 
clamp(int value,int low,int high)66 static INLINE int clamp(int value, int low, int high) {
67   return value < low ? low : (value > high ? high : value);
68 }
69 
fclamp(double value,double low,double high)70 static INLINE double fclamp(double value, double low, double high) {
71   return value < low ? low : (value > high ? high : value);
72 }
73 
lclamp(int64_t value,int64_t low,int64_t high)74 static INLINE int64_t lclamp(int64_t value, int64_t low, int64_t high) {
75   return value < low ? low : (value > high ? high : value);
76 }
77 
clip_pixel_highbd(int val,int bd)78 static INLINE uint16_t clip_pixel_highbd(int val, int bd) {
79   switch (bd) {
80     case 8:
81     default: return (uint16_t)clamp(val, 0, 255);
82     case 10: return (uint16_t)clamp(val, 0, 1023);
83     case 12: return (uint16_t)clamp(val, 0, 4095);
84   }
85 }
86 
87 // Returns the saturating cast of a double value to int.
saturate_cast_double_to_int(double d)88 static INLINE int saturate_cast_double_to_int(double d) {
89   if (d > INT_MAX) return INT_MAX;
90   return (int)d;
91 }
92 
93 #ifdef __cplusplus
94 }  // extern "C"
95 #endif
96 
97 #endif  // VPX_VPX_DSP_VPX_DSP_COMMON_H_
98