|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level |
| 3 | +// directory of this distribution and at http://opencv.org/license.html. |
| 4 | +#ifndef OPENCV_HAL_RVV_SQRT_HPP_INCLUDED |
| 5 | +#define OPENCV_HAL_RVV_SQRT_HPP_INCLUDED |
| 6 | + |
| 7 | +#include <riscv_vector.h> |
| 8 | +#include <cmath> |
| 9 | +#include "hal_rvv_1p0/types.hpp" |
| 10 | + |
| 11 | +namespace cv { namespace cv_hal_rvv { |
| 12 | + |
| 13 | +#undef cv_hal_sqrt32f |
| 14 | +#undef cv_hal_sqrt64f |
| 15 | +#undef cv_hal_invSqrt32f |
| 16 | +#undef cv_hal_invSqrt64f |
| 17 | + |
| 18 | +#define cv_hal_sqrt32f cv::cv_hal_rvv::sqrt<cv::cv_hal_rvv::Sqrt32f<cv::cv_hal_rvv::RVV_F32M8>> |
| 19 | +#define cv_hal_sqrt64f cv::cv_hal_rvv::sqrt<cv::cv_hal_rvv::Sqrt64f<cv::cv_hal_rvv::RVV_F64M8>> |
| 20 | + |
| 21 | +#ifdef __clang__ |
| 22 | +// Strange bug in clang: invSqrt use 2 LMUL registers to store mask, which will cause memory access. |
| 23 | +// So a smaller LMUL is used here. |
| 24 | +# define cv_hal_invSqrt32f cv::cv_hal_rvv::invSqrt<cv::cv_hal_rvv::Sqrt32f<cv::cv_hal_rvv::RVV_F32M4>> |
| 25 | +# define cv_hal_invSqrt64f cv::cv_hal_rvv::invSqrt<cv::cv_hal_rvv::Sqrt64f<cv::cv_hal_rvv::RVV_F64M4>> |
| 26 | +#else |
| 27 | +# define cv_hal_invSqrt32f cv::cv_hal_rvv::invSqrt<cv::cv_hal_rvv::Sqrt32f<cv::cv_hal_rvv::RVV_F32M8>> |
| 28 | +# define cv_hal_invSqrt64f cv::cv_hal_rvv::invSqrt<cv::cv_hal_rvv::Sqrt64f<cv::cv_hal_rvv::RVV_F64M8>> |
| 29 | +#endif |
| 30 | + |
| 31 | +namespace detail { |
| 32 | + |
| 33 | +// Newton-Raphson method |
| 34 | +// Use 4 LMUL registers |
| 35 | +template <size_t iter_times, typename VEC_T> |
| 36 | +inline VEC_T sqrt(VEC_T x, size_t vl) |
| 37 | +{ |
| 38 | + auto x2 = __riscv_vfmul(x, 0.5, vl); |
| 39 | + auto y = __riscv_vfrsqrt7(x, vl); |
| 40 | +#pragma unroll |
| 41 | + for (size_t i = 0; i < iter_times; i++) |
| 42 | + { |
| 43 | + auto t = __riscv_vfmul(y, y, vl); |
| 44 | + t = __riscv_vfmul(t, x2, vl); |
| 45 | + t = __riscv_vfrsub(t, 1.5, vl); |
| 46 | + y = __riscv_vfmul(t, y, vl); |
| 47 | + } |
| 48 | + // just to prevent the compiler from calculating mask before the invSqrt, which will run out |
| 49 | + // of registers and cause memory access. |
| 50 | + asm volatile("" ::: "memory"); |
| 51 | + auto mask = __riscv_vmfne(x, 0.0, vl); |
| 52 | + mask = __riscv_vmfne_mu(mask, mask, x, INFINITY, vl); |
| 53 | + return __riscv_vfmul_mu(mask, x, x, y, vl); |
| 54 | +} |
| 55 | + |
| 56 | +// Newton-Raphson method |
| 57 | +// Use 3 LMUL registers and 1 mask register |
| 58 | +template <size_t iter_times, typename VEC_T> |
| 59 | +inline VEC_T invSqrt(VEC_T x, size_t vl) |
| 60 | +{ |
| 61 | + auto mask = __riscv_vmfne(x, 0.0, vl); |
| 62 | + mask = __riscv_vmfne_mu(mask, mask, x, INFINITY, vl); |
| 63 | + auto x2 = __riscv_vfmul(x, 0.5, vl); |
| 64 | + auto y = __riscv_vfrsqrt7(x, vl); |
| 65 | +#pragma unroll |
| 66 | + for (size_t i = 0; i < iter_times; i++) |
| 67 | + { |
| 68 | + auto t = __riscv_vfmul(y, y, vl); |
| 69 | + t = __riscv_vfmul(t, x2, vl); |
| 70 | + t = __riscv_vfrsub(t, 1.5, vl); |
| 71 | + y = __riscv_vfmul_mu(mask, y, t, y, vl); |
| 72 | + } |
| 73 | + return y; |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace detail |
| 77 | + |
| 78 | +template <typename RVV_T> |
| 79 | +struct Sqrt32f |
| 80 | +{ |
| 81 | + using T = RVV_T; |
| 82 | + static constexpr size_t iter_times = 2; |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename RVV_T> |
| 86 | +struct Sqrt64f |
| 87 | +{ |
| 88 | + using T = RVV_T; |
| 89 | + static constexpr size_t iter_times = 3; |
| 90 | +}; |
| 91 | + |
| 92 | +template <typename SQRT_T, typename Elem = typename SQRT_T::T::ElemType> |
| 93 | +inline int sqrt(const Elem* src, Elem* dst, int _len) |
| 94 | +{ |
| 95 | + size_t vl; |
| 96 | + for (size_t len = _len; len > 0; len -= vl, src += vl, dst += vl) |
| 97 | + { |
| 98 | + vl = SQRT_T::T::setvl(len); |
| 99 | + auto x = SQRT_T::T::vload(src, vl); |
| 100 | + SQRT_T::T::vstore(dst, detail::sqrt<SQRT_T::iter_times>(x, vl), vl); |
| 101 | + } |
| 102 | + |
| 103 | + return CV_HAL_ERROR_OK; |
| 104 | +} |
| 105 | + |
| 106 | +template <typename SQRT_T, typename Elem = typename SQRT_T::T::ElemType> |
| 107 | +inline int invSqrt(const Elem* src, Elem* dst, int _len) |
| 108 | +{ |
| 109 | + size_t vl; |
| 110 | + for (size_t len = _len; len > 0; len -= vl, src += vl, dst += vl) |
| 111 | + { |
| 112 | + vl = SQRT_T::T::setvl(len); |
| 113 | + auto x = SQRT_T::T::vload(src, vl); |
| 114 | + SQRT_T::T::vstore(dst, detail::invSqrt<SQRT_T::iter_times>(x, vl), vl); |
| 115 | + } |
| 116 | + |
| 117 | + return CV_HAL_ERROR_OK; |
| 118 | +} |
| 119 | + |
| 120 | +}} // namespace cv::cv_hal_rvv |
| 121 | + |
| 122 | +#endif // OPENCV_HAL_RVV_SQRT_HPP_INCLUDED |
0 commit comments