VCTR
Loading...
Searching...
No Matches
SignedSqrt.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2026 - by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr::expressions
24{
25
26template <size_t extent, class SrcType>
27requires is::realFloatNumber<ValueType<SrcType>>
29{
30public:
31 using value_type = ValueType<SrcType>;
32
33 VCTR_COMMON_UNARY_EXPRESSION_MEMBERS (SignedSqrt, src)
34
35 VCTR_FORCEDINLINE constexpr value_type operator[] (size_t i) const
36 {
37 const auto in = src[i];
38
39#if VCTR_USE_GCEM
40 if (std::is_constant_evaluated())
41 {
42 if (in == value_type (0))
43 return in; // preserves -0
44
45 return in < value_type (0) ? -gcem::sqrt (-in) : gcem::sqrt (in);
46 }
47 #endif
48
49 return std::copysign (std::sqrt (std::abs (in)), in);
50 }
51
52 //==============================================================================
53 // Platform Vector Operation Implementation
54 VCTR_FORCEDINLINE const value_type* evalNextVectorOpInExpressionChain (value_type* dst) const
56 {
57 const auto* s = src.evalNextVectorOpInExpressionChain (dst);
58
59 if (s != dst)
60 {
61 const auto len = sizeToInt (size());
62 Expression::Accelerate::abs (s, dst, len);
63 Expression::Accelerate::sqrt (dst, dst, len);
64 Expression::Accelerate::copysign (dst, s, dst, len);
65 return dst;
66 }
67
68 // The vectorised approach does not work for in-place evaluation, so we fall back to a scalar implementation here
69 for (size_t i = 0; i < size(); ++i)
70 dst[i] = std::copysign (std::sqrt (std::abs (dst[i])), dst[i]);
71
72 return dst;
73 }
74};
75
76} // namespace vctr::expressions
77
78namespace vctr
79{
80
88
89} // namespace vctr
90
91
92
Definition: SignedSqrt.h:29
A combined concept to check if Apple Accelerate is a suitable option for a real valued floating point...
Definition: ContainerAndExpressionConcepts.h:291
constexpr ExpressionChainBuilder< expressions::SignedSqrt > signedSqrt
Computes the sign-preserving square root of the source values.
Definition: SignedSqrt.h:87
The main namespace of the VCTR project.
Definition: Array.h:24
typename detail::ValueType< std::remove_cvref_t< T > >::Type ValueType
If T is an expression template, it equals its return type, if it's a type that defines value_type as ...
Definition: Traits.h:201
int sizeToInt(size_t size)
Casts the size_t argument to an int.
Definition: PlatformVectorOpsHelpers.h:27
An expression chain builder is an object which supplies various operator<< overloads which build chai...
Definition: ExpressionChainBuilder.h:170
The base class to every expression template.
Definition: ExpressionTemplate.h:37