vspline 1.1.0
Generic C++11 Code for Uniform B-Splines
hwy_atan2.h
Go to the documentation of this file.
1// This file has a port af Vc's atan2 implementation to highway.
2// The implementation of atan2 which I have ported to highway was originally
3// found in https://github.com/VcDevel/Vc/blob/1.4/src/trigonometric.cpp
4// That code is licensensed like this:
5
6/* This file is part of the Vc library. {{{
7Copyright © 2012-2015 Matthias Kretz <kretz@kde.org>
8
9Redistribution and use in source and binary forms, with or without
10modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 * Neither the names of contributing organizations nor the
17 names of its contributors may be used to endorse or promote products
18 derived from this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
24DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31}}}*/
32
33// some code irrelevant to the port was deleted here.
34
35/* this is the original code of Vc's atan2 implementation, begin quote
36
37template <>
38template <>
39Vc::float_v Trigonometric<Vc::Detail::TrigonometricImplementation<
40 Vc::CurrentImplementation::current()>>::atan2(const Vc::float_v &y,
41 const Vc::float_v &x)
42{
43 using V = Vc::float_v;
44 typedef Const<float, V::abi> C;
45 typedef V::Mask M;
46
47 const M xZero = x == V::Zero();
48 const M yZero = y == V::Zero();
49 const M xMinusZero = xZero && isnegative(x);
50 const M yNeg = y < V::Zero();
51 const M xInf = !isfinite(x);
52 const M yInf = !isfinite(y);
53
54 V a = copysign(C::_pi(), y);
55 a.setZero(x >= V::Zero());
56
57 // setting x to any finite value will have atan(y/x) return sign(y/x)*pi/2, just in case x is inf
58 V _x = x;
59 _x(yInf) = copysign(V::One(), x);
60
61 a += atan(y / _x);
62
63 // if x is +0 and y is +/-0 the result is +0
64 a.setZero(xZero && yZero);
65
66 // for x = -0 we add/subtract pi to get the correct result
67 a(xMinusZero) += copysign(C::_pi(), y);
68
69 // atan2(-Y, +/-0) = -pi/2
70 a(xZero && yNeg) = -C::_pi_2();
71
72 // if both inputs are inf the output is +/- (3)pi/4
73 a(xInf && yInf) += copysign(C::_pi_4(), x ^ ~y);
74
75 // correct the sign of y if the result is 0
76 a(a == V::Zero()) = copysign(a, y);
77
78 // any NaN input will lead to NaN output
79 a.setQnan(isnan(y) || isnan(x));
80
81 return a;
82}
83template<> template<> Vc::double_v Trigonometric<Vc::Detail::TrigonometricImplementation<Vc::CurrentImplementation::current()>>::atan2 (const Vc::double_v &y, const Vc::double_v &x) {
84 typedef Vc::double_v V;
85 typedef Const<double, V::abi> C;
86 typedef V::Mask M;
87
88 const M xZero = x == V::Zero();
89 const M yZero = y == V::Zero();
90 const M xMinusZero = xZero && isnegative(x);
91 const M yNeg = y < V::Zero();
92 const M xInf = !isfinite(x);
93 const M yInf = !isfinite(y);
94
95 V a = copysign(V(C::_pi()), y);
96 a.setZero(x >= V::Zero());
97
98 // setting x to any finite value will have atan(y/x) return sign(y/x)*pi/2, just in case x is inf
99 V _x = x;
100 _x(yInf) = copysign(V::One(), x);
101
102 a += atan(y / _x);
103
104 // if x is +0 and y is +/-0 the result is +0
105 a.setZero(xZero && yZero);
106
107 // for x = -0 we add/subtract pi to get the correct result
108 a(xMinusZero) += copysign(C::_pi(), y);
109
110 // atan2(-Y, +/-0) = -pi/2
111 a(xZero && yNeg) = -C::_pi_2();
112
113 // if both inputs are inf the output is +/- (3)pi/4
114 a(xInf && yInf) += copysign(C::_pi_4(), x ^ ~y);
115
116 // correct the sign of y if the result is 0
117 a(a == V::Zero()) = copysign(a, y);
118
119 // any NaN input will lead to NaN output
120 a.setQnan(isnan(y) || isnan(x));
121
122 return a;
123}
124
125End quote original Vc code */
126
127// To affect the port, I have copied and pasted infrastructure code from
128// https://github.com/google/highway/blob/master/hwy/contrib/math/math-inl.h
129// in order to make the code 'slot into' highway. The highway code I have
130// copied and pasted from is licensed like this:
131
132// Copyright 2020 Google LLC
133// SPDX-License-Identifier: Apache-2.0
134//
135// Licensed under the Apache License, Version 2.0 (the "License");
136// you may not use this file except in compliance with the License.
137// You may obtain a copy of the License at
138//
139// http://www.apache.org/licenses/LICENSE-2.0
140//
141// Unless required by applicable law or agreed to in writing, software
142// distributed under the License is distributed on an "AS IS" BASIS,
143// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144// See the License for the specific language governing permissions and
145// limitations under the License.
146//
147
148// Begin code adapted from math-inl.h
149
150#include "hwy/highway.h"
151
153namespace hwy {
154namespace HWY_NAMESPACE {
155
156/**
157 * Highway SIMD version of std::atan2(x).
158 *
159 * Valid Lane Types: float32, float64
160 * Max Error: ULP = 3
161 * Valid Range: float32[-FLT_MAX, +FLT_MAX], float64[-DBL_MAX, +DBL_MAX]
162 * @return atan2 of 'y', 'x'
163 */
164template <class D, class V>
165HWY_INLINE V Atan(const D d, V y, V x);
166template <class D, class V>
167HWY_NOINLINE V CallAtan2(const D d, VecArg<V> y, VecArg<V> x) {
168 return Atan2(d, y, x);
169}
170
171template <class D, class V>
172HWY_INLINE V Atan2(const D d, V y, V x) {
173 using T = TFromD<D>;
174 typedef Mask < D > M ;
175
176// End of code adapted from math-inl.h
177
178// Begin port to highway. This derived work was made by
179// Kay F. Jahnke. Any aspect of this derived work which is not
180// covered by the licenses of either of the the original files
181// given above is copyrighted and licensed like this:
182//
183// Copyright Kay F. Jahnke 2022
184//
185/* Please direct questions, bug reports, and contributions to */
186/* */
187/* kfjahnke+vspline@gmail.com */
188/* */
189/* Permission is hereby granted, free of charge, to any person */
190/* obtaining a copy of this software and associated documentation */
191/* files (the "Software"), to deal in the Software without */
192/* restriction, including without limitation the rights to use, */
193/* copy, modify, merge, publish, distribute, sublicense, and/or */
194/* sell copies of the Software, and to permit persons to whom the */
195/* Software is furnished to do so, subject to the following */
196/* conditions: */
197/* */
198/* The above copyright notice and this permission notice shall be */
199/* included in all copies or substantial portions of the */
200/* Software. */
201/* */
202/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
203/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
204/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
205/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
206/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
207/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
208/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
209/* OTHER DEALINGS IN THE SOFTWARE. */
210/* */
211/************************************************************************/
212
213 const M xZero = Eq ( x , Set ( d , 0.0 ) ) ;
214 const M yZero = Eq ( y , Set ( d , 0.0 ) ) ;
215 const M xNeg = Lt ( x , Set ( d , 0.0 ) ) ;
216 const M xMinusZero = And ( xZero , xNeg ) ;
217 const M yNeg = Lt ( y , Set ( d , 0.0 ) ) ;
218 const M xInf = IsInf ( x ) ;
219 const M yInf = IsInf ( y ) ;
220// auto qnan = std::numeric_limits<T>::quiet_NaN() ;
221
222 V cpsy = CopySign ( Set ( d , M_PI ) , y ) ;
223 V a = IfThenElseZero ( xNeg , cpsy ) ;
224 V cpsx = CopySign ( Set ( d , 1.0 ) , x ) ;
225 // setting x to any finite value will have atan(y/x) return sign(y/x)*pi/2,
226 // just in case x is inf
227 V _x = IfThenElse ( yInf , cpsx , x ) ;
228 a = Add ( a , Atan ( d , Div ( y , _x ) ) ) ;
229 // if x is +0 and y is +/-0 the result is +0
230 a = IfThenZeroElse ( And ( xZero , yZero ) , a ) ;
231 // for x = -0 we add/subtract pi to get the correct result
232 a = IfThenElse ( xMinusZero , Add ( a , cpsy ) , a ) ;
233 // atan2(-Y, +/-0) = -pi/2
234 a = IfThenElse ( And ( xZero , yNeg ) , Set ( d , - M_PI_2 ) , a ) ;
235 V cpsp4 = CopySign ( Set ( d , M_PI_4 ) , Xor ( x , Not ( y ) ) ) ;
236 // if both inputs are inf the output is +/- (3)pi/4
237 a = IfThenElse ( And ( xInf , yInf ) , Add ( a , cpsp4 ) , a ) ;
238 M azero = Eq ( a , Set ( d , 0.0 ) ) ;
239 // correct the sign of y if the result is 0
240 a = IfThenElse ( azero , CopySign ( a , y ) , a ) ;
241 // any NaN input will lead to NaN output
242 a = IfThenElse ( Or ( IsNaN ( y ) , IsNaN ( x ) ) , NaN ( d ) , a ) ;
243 return a ;
244}
245
246} // namespace HWY_NAMESPACE
247} // namespace hwy
HWY_AFTER_NAMESPACE()
HWY_BEFORE_NAMESPACE()
HWY_NOINLINE V CallAtan2(const D d, VecArg< V > y, VecArg< V > x)
Definition: hwy_atan2.h:167
HWY_INLINE V Atan2(const D d, V y, V x)
Definition: hwy_atan2.h:172
HWY_INLINE V Atan(const D d, V y, V x)
Definition: hwy_atan2.h:153