1 /*
2  * Copyright (c) 2001-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 /* clang-format off */
13 
14 #ifndef AOM_AV1_COMMON_ODINTRIN_H_
15 #define AOM_AV1_COMMON_ODINTRIN_H_
16 
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "aom/aom_integer.h"
21 #include "aom_dsp/aom_dsp_common.h"
22 #include "aom_ports/bitops.h"
23 #include "av1/common/enums.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 typedef int od_coeff;
30 
31 #define OD_DIVU_DMAX (1024)
32 
33 extern uint32_t OD_DIVU_SMALL_CONSTS[OD_DIVU_DMAX][2];
34 
35 /*Translate unsigned division by small divisors into multiplications.*/
36 #define OD_DIVU_SMALL(_x, _d)                                     \
37   ((uint32_t)((OD_DIVU_SMALL_CONSTS[(_d)-1][0] * (uint64_t)(_x) + \
38                OD_DIVU_SMALL_CONSTS[(_d)-1][1]) >>                \
39               32) >>                                              \
40    (OD_ILOG_NZ(_d) - 1))
41 
42 #define OD_DIVU(_x, _d) \
43   (((_d) < OD_DIVU_DMAX) ? (OD_DIVU_SMALL((_x), (_d))) : ((_x) / (_d)))
44 
45 #define OD_MINI AOMMIN
46 #define OD_MAXI AOMMAX
47 #define OD_CLAMPI(min, val, max) (OD_MAXI(min, OD_MINI(val, max)))
48 
49 /*Integer logarithm (base 2) of a nonzero unsigned 32-bit integer.
50   OD_ILOG_NZ(x) = (int)floor(log2(x)) + 1.*/
51 #define OD_ILOG_NZ(x) (1 + get_msb(x))
52 
53 /*Enable special features for gcc and compatible compilers.*/
54 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
55 #define OD_GNUC_PREREQ(maj, min, pat)                                \
56   ((__GNUC__ << 16) + (__GNUC_MINOR__ << 8) + __GNUC_PATCHLEVEL__ >= \
57    ((maj) << 16) + ((min) << 8) + pat)  // NOLINT
58 #else
59 #define OD_GNUC_PREREQ(maj, min, pat) (0)
60 #endif
61 
62 #if OD_GNUC_PREREQ(3, 4, 0)
63 #define OD_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
64 #else
65 #define OD_WARN_UNUSED_RESULT
66 #endif
67 
68 #if OD_GNUC_PREREQ(3, 4, 0)
69 #define OD_ARG_NONNULL(x) __attribute__((__nonnull__(x)))
70 #else
71 #define OD_ARG_NONNULL(x)
72 #endif
73 
74 /** Copy n elements of memory from src to dst. The 0* term provides
75     compile-time type checking  */
76 #if !defined(OVERRIDE_OD_COPY)
77 #define OD_COPY(dst, src, n) \
78   (memcpy((dst), (src), sizeof(*(dst)) * (n) + 0 * ((dst) - (src))))
79 #endif
80 
81 /** Copy n elements of memory from src to dst, allowing overlapping regions.
82     The 0* term provides compile-time type checking */
83 #if !defined(OVERRIDE_OD_MOVE)
84 # define OD_MOVE(dst, src, n) \
85  (memmove((dst), (src), sizeof(*(dst))*(n) + 0*((dst) - (src)) ))
86 #endif
87 
88 /*All of these macros should expect floats as arguments.*/
89 # define OD_SIGNMASK(a) (-((a) < 0))
90 # define OD_FLIPSIGNI(a, b) (((a) + OD_SIGNMASK(b)) ^ OD_SIGNMASK(b))
91 
92 #ifdef __cplusplus
93 }  // extern "C"
94 #endif
95 
96 #endif  // AOM_AV1_COMMON_ODINTRIN_H_
97