|
||||
File indexing completed on 2025-01-30 10:25:46
0001 /* This file is part of the Vc library. {{{ 0002 Copyright © 2012-2015 Matthias Kretz <kretz@kde.org> 0003 0004 Redistribution and use in source and binary forms, with or without 0005 modification, are permitted provided that the following conditions are met: 0006 * Redistributions of source code must retain the above copyright 0007 notice, this list of conditions and the following disclaimer. 0008 * Redistributions in binary form must reproduce the above copyright 0009 notice, this list of conditions and the following disclaimer in the 0010 documentation and/or other materials provided with the distribution. 0011 * Neither the names of contributing organizations nor the 0012 names of its contributors may be used to endorse or promote products 0013 derived from this software without specific prior written permission. 0014 0015 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 0016 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 0017 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 0018 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 0019 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 0020 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 0021 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 0022 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0023 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 0024 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0025 0026 }}}*/ 0027 0028 #ifndef VC_COMMON_IIF_H_ 0029 #define VC_COMMON_IIF_H_ 0030 0031 #include "../type_traits" 0032 #include "macros.h" 0033 0034 namespace Vc_VERSIONED_NAMESPACE 0035 { 0036 /** 0037 * \ingroup Utilities 0038 * 0039 * Function to mimic the ternary operator '?:' (inline-if). 0040 * 0041 * \param condition Determines which values are returned. This is analog to the first argument to 0042 * the ternary operator. 0043 * \param trueValue The values to return where \p condition is \c true. 0044 * \param falseValue The values to return where \p condition is \c false. 0045 * \return A combination of entries from \p trueValue and \p falseValue, according to \p condition. 0046 * 0047 * So instead of the scalar variant 0048 * \code 0049 * float x = a > 1.f ? b : b + c; 0050 * \endcode 0051 * you'd write 0052 * \code 0053 * float_v x = Vc::iif (a > 1.f, b, b + c); 0054 * \endcode 0055 * 0056 * Assuming \c a has the values [0, 3, 5, 1], \c b is [1, 1, 1, 1], and \c c is [1, 2, 3, 4], then x 0057 * will be [2, 2, 3, 5]. 0058 */ 0059 template <typename Mask, typename T> 0060 Vc_ALWAYS_INLINE enable_if<is_simd_mask<Mask>::value && is_simd_vector<T>::value, T> iif( 0061 const Mask &condition, const T &trueValue, const T &falseValue) 0062 { 0063 T result(falseValue); 0064 Vc::where(condition) | result = trueValue; 0065 return result; 0066 } 0067 0068 /**\internal 0069 * The following declaration makes it explicit that `iif (Mask, non-vector, non-vector)` 0070 * is not supposed to work. Doing the same thing with \c static_assert would break SFINAE. 0071 */ 0072 template <typename Mask, typename T> 0073 enable_if<is_simd_mask<Mask>::value && !is_simd_vector<T>::value, T> iif( 0074 const Mask &, const T &, const T &) = delete; 0075 0076 /** 0077 * \ingroup Utilities 0078 * 0079 * Overload of the above for boolean conditions. 0080 * 0081 * This typically results in direct use of the ternary operator. This function makes it easier to 0082 * switch from a Vc type to a builtin type. 0083 * 0084 * \param condition Determines which value is returned. This is analog to the first argument to 0085 * the ternary operator. 0086 * \param trueValue The value to return if \p condition is \c true. 0087 * \param falseValue The value to return if \p condition is \c false. 0088 * \return Either \p trueValue or \p falseValue, depending on \p condition. 0089 */ 0090 template<typename T> constexpr T iif (bool condition, const T &trueValue, const T &falseValue) 0091 { 0092 return condition ? trueValue : falseValue; 0093 } 0094 0095 } // namespace Vc 0096 0097 #endif // VC_COMMON_IIF_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |