|
| 1 | +/***************************************************************************** |
| 2 | +Copyright (c) 2024, The OpenBLAS Project |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions are |
| 7 | +met: |
| 8 | +
|
| 9 | + 1. Redistributions of source code must retain the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | + 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in |
| 14 | + the documentation and/or other materials provided with the |
| 15 | + distribution. |
| 16 | + 3. Neither the name of the OpenBLAS project nor the names of |
| 17 | + its contributors may be used to endorse or promote products |
| 18 | + derived from this software without specific prior written |
| 19 | + permission. |
| 20 | +
|
| 21 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 30 | +USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +**********************************************************************************/ |
| 32 | + |
| 33 | +#include <stdint.h> |
| 34 | + |
| 35 | +#ifdef __riscv_v_intrinsic |
| 36 | +#include <riscv_vector.h> |
| 37 | +#endif |
| 38 | + |
| 39 | +unsigned detect_riscv64_get_vlenb(void) { |
| 40 | +#ifdef __riscv_v_intrinsic |
| 41 | + return __riscv_vlenb(); |
| 42 | +#else |
| 43 | + return 0; |
| 44 | +#endif |
| 45 | +} |
| 46 | + |
| 47 | +/* |
| 48 | + * Based on the approach taken here: |
| 49 | + * https://code.videolan.org/videolan/dav1d/-/merge_requests/1629 |
| 50 | + * |
| 51 | + * Only to be called after we've determined we have some sort of |
| 52 | + * RVV support. |
| 53 | + */ |
| 54 | + |
| 55 | +uint64_t detect_riscv64_rvv100(void) |
| 56 | +{ |
| 57 | + uint64_t rvv10_supported; |
| 58 | + |
| 59 | + /* |
| 60 | + * After the vsetvli statement vtype will either be a value > 0 if the |
| 61 | + * vsetvli succeeded or less than 0 if it failed. If 0 < vtype |
| 62 | + * we're good and the function will return 1, otherwise there's no |
| 63 | + * RVV 1.0 and we return 0. |
| 64 | + */ |
| 65 | + |
| 66 | + asm volatile("vsetvli x0, x0, e8, m1, ta, ma\n\t" |
| 67 | + "csrr %0, vtype\n\t" |
| 68 | + "slt %0, x0, %0\n" |
| 69 | + : "=r" (rvv10_supported) |
| 70 | + : |
| 71 | + :); |
| 72 | + |
| 73 | + return rvv10_supported; |
| 74 | +} |
| 75 | + |
0 commit comments