- Main Page
- Related Pages
- Classes
- Files

- File List

# vectorize8.h

    00001 #ifndef OPENMM_VECTORIZE8_H_
    00002 #define OPENMM_VECTORIZE8_H_
    00003
    00004 /* -------------------------------------------------------------------------- *
    00005  *                                   OpenMM                                   *
    00006  * -------------------------------------------------------------------------- *
    00007  * This is part of the OpenMM molecular simulation toolkit originating from   *
    00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
    00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
    00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
    00011  *                                                                            *
    00012  * Portions copyright (c) 2013-2014 Stanford University and the Authors.      *
    00013  * Authors: Peter Eastman                                                     *
    00014  * Contributors:                                                              *
    00015  *                                                                            *
    00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
    00017  * copy of this software and associated documentation files (the "Software"), *
    00018  * to deal in the Software without restriction, including without limitation  *
    00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
    00020  * and/or sell copies of the Software, and to permit persons to whom the      *
    00021  * Software is furnished to do so, subject to the following conditions:       *
    00022  *                                                                            *
    00023  * The above copyright notice and this permission notice shall be included in *
    00024  * all copies or substantial portions of the Software.                        *
    00025  *                                                                            *
    00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
    00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
    00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
    00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
    00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
    00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
    00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
    00033  * -------------------------------------------------------------------------- */
    00034
    00035 #include "vectorize.h"
    00036 #include <immintrin.h>
    00037
    00038 // This file defines classes and functions to simplify vectorizing code with AVX.
    00039
    00040 class ivec8;
    00041
    00045 class fvec8 {
    00046 public:
    00047     __m256 val;
    00048
    00049     fvec8() {}
    00050     fvec8(float v) : val(_mm256_set1_ps(v)) {}
    00051     fvec8(float v1, float v2, float v3, float v4, float v5, float v6, float v7, float v8) : val(_mm256_set_ps(v8, v7, v6, v5, v4, v3, v2, v1)) {}
    00052     fvec8(__m256 v) : val(v) {}
    00053     fvec8(const float* v) : val(_mm256_loadu_ps(v)) {}
    00054     operator __m256() const {
    00055         return val;
    00056     }
    00057     fvec4 lowerVec() const {
    00058         return _mm256_castps256_ps128(val);
    00059     }
    00060     fvec4 upperVec() const {
    00061         return _mm256_extractf128_ps(val, 1);
    00062     }
    00063     void store(float* v) const {
    00064         _mm256_storeu_ps(v, val);
    00065     }
    00066     fvec8 operator+(const fvec8& other) const {
    00067         return _mm256_add_ps(val, other);
    00068     }
    00069     fvec8 operator-(const fvec8& other) const {
    00070         return _mm256_sub_ps(val, other);
    00071     }
    00072     fvec8 operator*(const fvec8& other) const {
    00073         return _mm256_mul_ps(val, other);
    00074     }
    00075     fvec8 operator/(const fvec8& other) const {
    00076         return _mm256_div_ps(val, other);
    00077     }
    00078     void operator+=(const fvec8& other) {
    00079         val = _mm256_add_ps(val, other);
    00080     }
    00081     void operator-=(const fvec8& other) {
    00082         val = _mm256_sub_ps(val, other);
    00083     }
    00084     void operator*=(const fvec8& other) {
    00085         val = _mm256_mul_ps(val, other);
    00086     }
    00087     void operator/=(const fvec8& other) {
    00088         val = _mm256_div_ps(val, other);
    00089     }
    00090     fvec8 operator-() const {
    00091         return _mm256_sub_ps(_mm256_set1_ps(0.0f), val);
    00092     }
    00093     fvec8 operator&(const fvec8& other) const {
    00094         return _mm256_and_ps(val, other);
    00095     }
    00096     fvec8 operator|(const fvec8& other) const {
    00097         return _mm256_or_ps(val, other);
    00098     }
    00099     fvec8 operator==(const fvec8& other) const {
    00100         return _mm256_cmp_ps(val, other, _CMP_EQ_OQ);
    00101     }
    00102     fvec8 operator!=(const fvec8& other) const {
    00103         return _mm256_cmp_ps(val, other, _CMP_NEQ_OQ);
    00104     }
    00105     fvec8 operator>(const fvec8& other) const {
    00106         return _mm256_cmp_ps(val, other, _CMP_GT_OQ);
    00107     }
    00108     fvec8 operator<(const fvec8& other) const {
    00109         return _mm256_cmp_ps(val, other, _CMP_LT_OQ);
    00110     }
    00111     fvec8 operator>=(const fvec8& other) const {
    00112         return _mm256_cmp_ps(val, other, _CMP_GE_OQ);
    00113     }
    00114     fvec8 operator<=(const fvec8& other) const {
    00115         return _mm256_cmp_ps(val, other, _CMP_LE_OQ);
    00116     }
    00117     operator ivec8() const;
    00118 };
    00119
    00123 class ivec8 {
    00124 public:
    00125     __m256i val;
    00126
    00127     ivec8() {}
    00128     ivec8(int v) : val(_mm256_set1_epi32(v)) {}
    00129     ivec8(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8) : val(_mm256_set_epi32(v8, v7, v6, v5, v4, v3, v2, v1)) {}
    00130     ivec8(__m256i v) : val(v) {}
    00131     ivec8(const int* v) : val(_mm256_loadu_si256((const __m256i*) v)) {}
    00132     operator __m256i() const {
    00133         return val;
    00134     }
    00135     ivec4 lowerVec() const {
    00136         return _mm256_castsi256_si128(val);
    00137     }
    00138     ivec4 upperVec() const {
    00139         return _mm256_extractf128_si256(val, 1);
    00140     }
    00141     void store(int* v) const {
    00142         _mm256_storeu_si256((__m256i*) v, val);
    00143     }
    00144     ivec8 operator&(const ivec8& other) const {
    00145         return _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(val), _mm256_castsi256_ps(other.val)));
    00146     }
    00147     ivec8 operator|(const ivec8& other) const {
    00148         return _mm256_castps_si256(_mm256_or_ps(_mm256_castsi256_ps(val), _mm256_castsi256_ps(other.val)));
    00149     }
    00150     operator fvec8() const;
    00151 };
    00152
    00153 // Conversion operators.
    00154
    00155 inline fvec8::operator ivec8() const {
    00156     return _mm256_cvttps_epi32(val);
    00157 }
    00158
    00159 inline ivec8::operator fvec8() const {
    00160     return _mm256_cvtepi32_ps(val);
    00161 }
    00162
    00163 // Functions that operate on fvec8s.
    00164
    00165 static inline fvec8 floor(const fvec8& v) {
    00166     return fvec8(_mm256_round_ps(v.val, 0x09));
    00167 }
    00168
    00169 static inline fvec8 ceil(const fvec8& v) {
    00170     return fvec8(_mm256_round_ps(v.val, 0x0A));
    00171 }
    00172
    00173 static inline fvec8 round(const fvec8& v) {
    00174     return fvec8(_mm256_round_ps(v.val, _MM_FROUND_TO_NEAREST_INT));
    00175 }
    00176
    00177 static inline fvec8 min(const fvec8& v1, const fvec8& v2) {
    00178     return fvec8(_mm256_min_ps(v1.val, v2.val));
    00179 }
    00180
    00181 static inline fvec8 max(const fvec8& v1, const fvec8& v2) {
    00182     return fvec8(_mm256_max_ps(v1.val, v2.val));
    00183 }
    00184
    00185 static inline fvec8 abs(const fvec8& v) {
    00186     static const __m256 mask = _mm256_castsi256_ps(_mm256_set1_epi32(0x7FFFFFFF));
    00187     return fvec8(_mm256_and_ps(v.val, mask));
    00188 }
    00189
    00190 static inline fvec8 sqrt(const fvec8& v) {
    00191     return fvec8(_mm256_sqrt_ps(v.val));
    00192 }
    00193
    00194 static inline float dot8(const fvec8& v1, const fvec8& v2) {
    00195     fvec8 result = _mm256_dp_ps(v1, v2, 0xF1);
    00196     return _mm_cvtss_f32(result.lowerVec())+_mm_cvtss_f32(result.upperVec());
    00197 }
    00198
    00199 static inline void transpose(const fvec4& in1, const fvec4& in2, const fvec4& in3, const fvec4& in4, const fvec4& in5, const fvec4& in6, const fvec4& in7, const fvec4& in8, fvec8& out1, fvec8& out2, fvec8& out3, fvec8& out4) {
    00200     fvec4 i1 = in1, i2 = in2, i3 = in3, i4 = in4;
    00201     fvec4 i5 = in5, i6 = in6, i7 = in7, i8 = in8;
    00202     _MM_TRANSPOSE4_PS(i1, i2, i3, i4);
    00203     _MM_TRANSPOSE4_PS(i5, i6, i7, i8);
    00204 #ifdef _MSC_VER
    00205     // Visual Studio has a bug in _mm256_castps128_ps256, so we have to use the more expensive _mm256_insertf128_ps.
    00206     out1 = _mm256_insertf128_ps(out1, i1, 0);
    00207     out2 = _mm256_insertf128_ps(out2, i2, 0);
    00208     out3 = _mm256_insertf128_ps(out3, i3, 0);
    00209     out4 = _mm256_insertf128_ps(out4, i4, 0);
    00210 #else
    00211     out1 = _mm256_castps128_ps256(i1);
    00212     out2 = _mm256_castps128_ps256(i2);
    00213     out3 = _mm256_castps128_ps256(i3);
    00214     out4 = _mm256_castps128_ps256(i4);
    00215 #endif
    00216     out1 = _mm256_insertf128_ps(out1, i5, 1);
    00217     out2 = _mm256_insertf128_ps(out2, i6, 1);
    00218     out3 = _mm256_insertf128_ps(out3, i7, 1);
    00219     out4 = _mm256_insertf128_ps(out4, i8, 1);
    00220 }
    00221
    00222 static inline void transpose(const fvec8& in1, const fvec8& in2, const fvec8& in3, const fvec8& in4, fvec4& out1, fvec4& out2, fvec4& out3, fvec4& out4, fvec4& out5, fvec4& out6, fvec4& out7, fvec4& out8) {
    00223     out1 = in1.lowerVec();
    00224     out2 = in2.lowerVec();
    00225     out3 = in3.lowerVec();
    00226     out4 = in4.lowerVec();
    00227     _MM_TRANSPOSE4_PS(out1, out2, out3, out4);
    00228     out5 = in1.upperVec();
    00229     out6 = in2.upperVec();
    00230     out7 = in3.upperVec();
    00231     out8 = in4.upperVec();
    00232     _MM_TRANSPOSE4_PS(out5, out6, out7, out8);
    00233 }
    00234
    00235 // Functions that operate on ivec8s.
    00236
    00237 static inline bool any(const ivec8& v) {
    00238     return !_mm256_testz_si256(v, _mm256_set1_epi32(0xFFFFFFFF));
    00239 }
    00240
    00241 // Mathematical operators involving a scalar and a vector.
    00242
    00243 static inline fvec8 operator+(float v1, const fvec8& v2) {
    00244     return fvec8(v1)+v2;
    00245 }
    00246
    00247 static inline fvec8 operator-(float v1, const fvec8& v2) {
    00248     return fvec8(v1)-v2;
    00249 }
    00250
    00251 static inline fvec8 operator*(float v1, const fvec8& v2) {
    00252     return fvec8(v1)*v2;
    00253 }
    00254
    00255 static inline fvec8 operator/(float v1, const fvec8& v2) {
    00256     return fvec8(v1)/v2;
    00257 }
    00258
    00259 // Operations for blending fvec8s based on an ivec8.
    00260
    00261 static inline fvec8 blend(const fvec8& v1, const fvec8& v2, const ivec8& mask) {
    00262     return fvec8(_mm256_blendv_ps(v1.val, v2.val, _mm256_castsi256_ps(mask.val)));
    00263 }
    00264
    00265 #endif /*OPENMM_VECTORIZE8_H_*/