FFmpeg
intmath.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef AVUTIL_RISCV_INTMATH_H
20 #define AVUTIL_RISCV_INTMATH_H
21 
22 #include <stdint.h>
23 
24 #include "config.h"
25 #include "libavutil/attributes.h"
26 
27 /*
28  * The compiler is forced to sign-extend the result anyhow, so it is faster to
29  * compute it explicitly and use it.
30  */
31 #define av_clip_int8 av_clip_int8_rvi
33 {
34  union { uint8_t u; int8_t s; } u = { .u = a };
35 
36  if (a != u.s)
37  a = ((a >> 31) ^ 0x7F);
38  return a;
39 }
40 
41 #define av_clip_int16 av_clip_int16_rvi
43 {
44  union { uint16_t u; int16_t s; } u = { .u = a };
45 
46  if (a != u.s)
47  a = ((a >> 31) ^ 0x7FFF);
48  return a;
49 }
50 
51 #define av_clipl_int32 av_clipl_int32_rvi
53 {
54  union { uint32_t u; int32_t s; } u = { .u = a };
55 
56  if (a != u.s)
57  a = ((a >> 63) ^ 0x7FFFFFFF);
58  return a;
59 }
60 
61 #define av_clip_intp2 av_clip_intp2_rvi
63 {
64  const int shift = 31 - p;
65  int b = ((int)(((unsigned)a) << shift)) >> shift;
66 
67  if (a != b)
68  b = (a >> 31) ^ ((1 << p) - 1);
69  return b;
70 }
71 
72 #if defined (__riscv_zbb) && (__riscv_zbb > 0) && HAVE_INLINE_ASM
73 
74 #define av_popcount av_popcount_rvb
75 static av_always_inline av_const int av_popcount_rvb(uint32_t x)
76 {
77  int ret;
78 
79 #if (__riscv_xlen >= 64)
80  __asm__ ("cpopw %0, %1\n" : "=r" (ret) : "r" (x));
81 #else
82  __asm__ ("cpop %0, %1\n" : "=r" (ret) : "r" (x));
83 #endif
84  return ret;
85 }
86 
87 #if (__riscv_xlen >= 64)
88 #define av_popcount64 av_popcount64_rvb
89 static av_always_inline av_const int av_popcount64_rvb(uint64_t x)
90 {
91  int ret;
92 
93 #if (__riscv_xlen >= 128)
94  __asm__ ("cpopd %0, %1\n" : "=r" (ret) : "r" (x));
95 #else
96  __asm__ ("cpop %0, %1\n" : "=r" (ret) : "r" (x));
97 #endif
98  return ret;
99 }
100 #endif /* __riscv_xlen >= 64 */
101 #endif /* __riscv_zbb */
102 
103 #endif /* AVUTIL_RISCV_INTMATH_H */
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
av_const
#define av_const
Definition: attributes.h:84
b
#define b
Definition: input.c:41
av_clipl_int32_rvi
static av_always_inline av_const int32_t av_clipl_int32_rvi(int64_t a)
Definition: intmath.h:52
av_clip_int8_rvi
static av_always_inline av_const int8_t av_clip_int8_rvi(int a)
Definition: intmath.h:32
s
#define s(width, name)
Definition: cbs_vp9.c:256
shift
static int shift(int a, int b)
Definition: bonk.c:257
av_clip_intp2_rvi
static av_always_inline av_const int av_clip_intp2_rvi(int a, int p)
Definition: intmath.h:62
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
attributes.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
ret
ret
Definition: filter_design.txt:187
__asm__
__asm__(".macro parse_r var r\n\t" "\\var = -1\n\t" _IFC_REG(0) _IFC_REG(1) _IFC_REG(2) _IFC_REG(3) _IFC_REG(4) _IFC_REG(5) _IFC_REG(6) _IFC_REG(7) _IFC_REG(8) _IFC_REG(9) _IFC_REG(10) _IFC_REG(11) _IFC_REG(12) _IFC_REG(13) _IFC_REG(14) _IFC_REG(15) _IFC_REG(16) _IFC_REG(17) _IFC_REG(18) _IFC_REG(19) _IFC_REG(20) _IFC_REG(21) _IFC_REG(22) _IFC_REG(23) _IFC_REG(24) _IFC_REG(25) _IFC_REG(26) _IFC_REG(27) _IFC_REG(28) _IFC_REG(29) _IFC_REG(30) _IFC_REG(31) ".iflt \\var\n\t" ".error \"Unable to parse register name \\r\"\n\t" ".endif\n\t" ".endm")
av_clip_int16_rvi
static av_always_inline av_const int16_t av_clip_int16_rvi(int a)
Definition: intmath.h:42
int32_t
int32_t
Definition: audioconvert.c:56
int
int
Definition: ffmpeg_filter.c:156