FFmpeg
cpu.c
Go to the documentation of this file.
1 /*
2  * Copyright © 2022 Rémi Denis-Courmont.
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef _GNU_SOURCE
22 # define _GNU_SOURCE
23 #endif
24 #include "libavutil/cpu.h"
25 #include "libavutil/riscv/cpu.h"
26 #include "libavutil/cpu_internal.h"
27 #include "libavutil/macros.h"
28 #include "libavutil/log.h"
29 #include "config.h"
30 
31 #if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
32 #include <sys/auxv.h>
33 #define HWCAP_RV(letter) (1ul << ((letter) - 'A'))
34 #endif
35 #if HAVE_SYS_HWPROBE_H
36 #include <sys/hwprobe.h>
37 #elif HAVE_ASM_HWPROBE_H
38 #include <asm/hwprobe.h>
39 #include <sys/syscall.h>
40 #include <unistd.h>
41 
42 static int __riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
43  size_t cpu_count, unsigned long *cpus,
44  unsigned int flags)
45 {
46  return syscall(__NR_riscv_hwprobe, pairs, pair_count, cpu_count, cpus,
47  flags);
48 }
49 #endif
50 
52 {
53  int ret = 0;
54 #if HAVE_SYS_HWPROBE_H || HAVE_ASM_HWPROBE_H
55  struct riscv_hwprobe pairs[] = {
56  { RISCV_HWPROBE_KEY_BASE_BEHAVIOR, 0 },
57  { RISCV_HWPROBE_KEY_IMA_EXT_0, 0 },
58  { RISCV_HWPROBE_KEY_CPUPERF_0, 0 },
59  };
60 
61  if (__riscv_hwprobe(pairs, FF_ARRAY_ELEMS(pairs), 0, NULL, 0) == 0) {
62  if (pairs[0].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA)
64 #ifdef RISCV_HWPROBE_IMA_V
65  if (pairs[1].value & RISCV_HWPROBE_IMA_V)
68 #ifdef RISCV_HWPROBE_EXT_ZVE32X
69  else if ((pairs[1].value & RISCV_HWPROBE_EXT_ZVE32X) &&
70  ff_get_rv_vlenb() >= 16) { // runtime detect assumes 128+ bits
72 
73  if (pairs[1].value & RISCV_HWPROBE_EXT_ZVE32F)
75  if (pairs[1].value & RISCV_HWPROBE_EXT_ZVE64X) {
77 
78  if (pairs[1].value & RISCV_HWPROBE_EXT_ZVE64D)
80  }
81  }
82 #endif
83 #endif
84 #ifdef RISCV_HWPROBE_EXT_ZBB
85  if (pairs[1].value & RISCV_HWPROBE_EXT_ZBB)
87 #if defined (RISCV_HWPROBE_EXT_ZBA) && defined (RISCV_HWPROBE_EXT_ZBS)
88  if ((pairs[1].value & RISCV_HWPROBE_EXT_ZBA) &&
89  (pairs[1].value & RISCV_HWPROBE_EXT_ZBB) &&
90  (pairs[1].value & RISCV_HWPROBE_EXT_ZBS))
92 #endif
93 #endif
94 #ifdef RISCV_HWPROBE_EXT_ZVBB
95  if (pairs[1].value & RISCV_HWPROBE_EXT_ZVBB)
97 #endif
98  switch (pairs[2].value & RISCV_HWPROBE_MISALIGNED_MASK) {
99  case RISCV_HWPROBE_MISALIGNED_FAST:
101  break;
102  default:
103  ; /* do nothing */
104  }
105  }
106 #elif HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
107  {
108  const unsigned long hwcap = ff_getauxval(AT_HWCAP);
109 
110  if (hwcap & HWCAP_RV('I'))
111  ret |= AV_CPU_FLAG_RVI;
112  if (hwcap & HWCAP_RV('B'))
114 
115  /* The V extension implies all Zve* functional subsets */
116  if (hwcap & HWCAP_RV('V'))
119  }
120 #endif
121 
122 #ifdef __riscv_i
123  ret |= AV_CPU_FLAG_RVI;
124 #endif
125 
126 #ifdef __riscv_zbb
128 #endif
129 #if defined (__riscv_b) || \
130  (defined (__riscv_zba) && defined (__riscv_zbb) && defined (__riscv_zbs))
131  ret |= AV_CPU_FLAG_RVB;
132 #endif
133 
134  /* If RV-V is enabled statically at compile-time, check the details. */
135 #ifdef __riscv_vector
137 #if __riscv_v_elen >= 64
139 #endif
140 #if __riscv_v_elen_fp >= 32
142 #if __riscv_v_elen_fp >= 64
144 #endif
145 #endif
146 #endif
147 #ifdef __riscv_zvbb
149 #endif
150 
151  return ret;
152 }
flags
const SwsFlags flags[]
Definition: swscale.c:61
AT_HWCAP
#define AT_HWCAP
Definition: cpu.c:50
cpu_count
static atomic_int cpu_count
Definition: cpu.c:57
AV_CPU_FLAG_RVB_BASIC
#define AV_CPU_FLAG_RVB_BASIC
Basic bit-manipulations.
Definition: cpu.h:98
cpus
static const struct @544 cpus[]
AV_CPU_FLAG_RV_ZVBB
#define AV_CPU_FLAG_RV_ZVBB
Vector basic bit-manipulations.
Definition: cpu.h:102
AV_CPU_FLAG_RVV_F64
#define AV_CPU_FLAG_RVV_F64
Vectors of double's.
Definition: cpu.h:97
AV_CPU_FLAG_RVB
#define AV_CPU_FLAG_RVB
B (bit manipulations)
Definition: cpu.h:104
macros.h
AV_CPU_FLAG_RVV_F32
#define AV_CPU_FLAG_RVV_F32
Vectors of float's *‍/.
Definition: cpu.h:95
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
cpu_internal.h
cpu.h
ff_getauxval
unsigned long ff_getauxval(unsigned long type)
Definition: cpu.c:302
NULL
#define NULL
Definition: coverity.c:32
cpu.h
AV_CPU_FLAG_RV_MISALIGNED
#define AV_CPU_FLAG_RV_MISALIGNED
Fast misaligned accesses.
Definition: cpu.h:103
log.h
AV_CPU_FLAG_RVV_I32
#define AV_CPU_FLAG_RVV_I32
Vectors of 8/16/32-bit int's *‍/.
Definition: cpu.h:94
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
ret
ret
Definition: filter_design.txt:187
AV_CPU_FLAG_RVI
#define AV_CPU_FLAG_RVI
I (full GPR bank)
Definition: cpu.h:89
AV_CPU_FLAG_RVV_I64
#define AV_CPU_FLAG_RVV_I64
Vectors of 64-bit int's *‍/.
Definition: cpu.h:96
ff_get_cpu_flags_riscv
int ff_get_cpu_flags_riscv(void)
Definition: cpu.c:51