- Main Page
- Related Pages
- Classes
- Files

- File List

# vectorize_sse.h

    00001 #ifndef OPENMM_VECTORIZE_SSE_H_
    00002 #define OPENMM_VECTORIZE_SSE_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 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 <smmintrin.h>
    00036 #include "hardware.h"
    00037
    00038 // This file defines classes and functions to simplify vectorizing code with SSE.
    00039
    00043 static bool isVec4Supported() {
    00044     int cpuInfo[4];
    00045     cpuid(cpuInfo, 0);
    00046     if (cpuInfo[0] >= 1) {
    00047         cpuid(cpuInfo, 1);
    00048         return ((cpuInfo[2] & ((int) 1 << 19)) != 0);
    00049     }
    00050     return false;
    00051 }
    00052
    00053 class ivec4;
    00054
    00058 class fvec4 {
    00059 public:
    00060     __m128 val;
    00061
    00062     fvec4() {}
    00063     fvec4(float v) : val(_mm_set1_ps(v)) {}
    00064     fvec4(float v1, float v2, float v3, float v4) : val(_mm_set_ps(v4, v3, v2, v1)) {}
    00065     fvec4(__m128 v) : val(v) {}
    00066     fvec4(const float* v) : val(_mm_loadu_ps(v)) {}
    00067     operator __m128() const {
    00068         return val;
    00069     }
    00070     float operator[](int i) const {
    00071         float result[4];
    00072         store(result);
    00073         return result[i];
    00074     }
    00075     void store(float* v) const {
    00076         _mm_storeu_ps(v, val);
    00077     }
    00078     fvec4 operator+(const fvec4& other) const {
    00079         return _mm_add_ps(val, other);
    00080     }
    00081     fvec4 operator-(const fvec4& other) const {
    00082         return _mm_sub_ps(val, other);
    00083     }
    00084     fvec4 operator*(const fvec4& other) const {
    00085         return _mm_mul_ps(val, other);
    00086     }
    00087     fvec4 operator/(const fvec4& other) const {
    00088         return _mm_div_ps(val, other);
    00089     }
    00090     void operator+=(const fvec4& other) {
    00091         val = _mm_add_ps(val, other);
    00092     }
    00093     void operator-=(const fvec4& other) {
    00094         val = _mm_sub_ps(val, other);
    00095     }
    00096     void operator*=(const fvec4& other) {
    00097         val = _mm_mul_ps(val, other);
    00098     }
    00099     void operator/=(const fvec4& other) {
    00100         val = _mm_div_ps(val, other);
    00101     }
    00102     fvec4 operator-() const {
    00103         return _mm_sub_ps(_mm_set1_ps(0.0f), val);
    00104     }
    00105     fvec4 operator&(const fvec4& other) const {
    00106         return _mm_and_ps(val, other);
    00107     }
    00108     fvec4 operator|(const fvec4& other) const {
    00109         return _mm_or_ps(val, other);
    00110     }
    00111     fvec4 operator==(const fvec4& other) const {
    00112         return _mm_cmpeq_ps(val, other);
    00113     }
    00114     fvec4 operator!=(const fvec4& other) const {
    00115         return _mm_cmpneq_ps(val, other);
    00116     }
    00117     fvec4 operator>(const fvec4& other) const {
    00118         return _mm_cmpgt_ps(val, other);
    00119     }
    00120     fvec4 operator<(const fvec4& other) const {
    00121         return _mm_cmplt_ps(val, other);
    00122     }
    00123     fvec4 operator>=(const fvec4& other) const {
    00124         return _mm_cmpge_ps(val, other);
    00125     }
    00126     fvec4 operator<=(const fvec4& other) const {
    00127         return _mm_cmple_ps(val, other);
    00128     }
    00129     operator ivec4() const;
    00130 };
    00131
    00135 class ivec4 {
    00136 public:
    00137     __m128i val;
    00138
    00139     ivec4() {}
    00140     ivec4(int v) : val(_mm_set1_epi32(v)) {}
    00141     ivec4(int v1, int v2, int v3, int v4) : val(_mm_set_epi32(v4, v3, v2, v1)) {}
    00142     ivec4(__m128i v) : val(v) {}
    00143     ivec4(const int* v) : val(_mm_loadu_si128((const __m128i*) v)) {}
    00144     operator __m128i() const {
    00145         return val;
    00146     }
    00147     int operator[](int i) const {
    00148         int result[4];
    00149         store(result);
    00150         return result[i];
    00151     }
    00152     void store(int* v) const {
    00153         _mm_storeu_si128((__m128i*) v, val);
    00154     }
    00155     ivec4 operator+(const ivec4& other) const {
    00156         return _mm_add_epi32(val, other);
    00157     }
    00158     ivec4 operator-(const ivec4& other) const {
    00159         return _mm_sub_epi32(val, other);
    00160     }
    00161     ivec4 operator*(const ivec4& other) const {
    00162         return _mm_mullo_epi32(val, other);
    00163     }
    00164     void operator+=(const ivec4& other) {
    00165         val = _mm_add_epi32(val, other);
    00166     }
    00167     void operator-=(const ivec4& other) {
    00168         val = _mm_sub_epi32(val, other);
    00169     }
    00170     void operator*=(const ivec4& other) {
    00171         val = _mm_mullo_epi32(val, other);
    00172     }
    00173     ivec4 operator-() const {
    00174         return _mm_sub_epi32(_mm_set1_epi32(0), val);
    00175     }
    00176     ivec4 operator&(const ivec4& other) const {
    00177         return _mm_and_si128(val, other);
    00178     }
    00179     ivec4 operator|(const ivec4& other) const {
    00180         return _mm_or_si128(val, other);
    00181     }
    00182     ivec4 operator==(const ivec4& other) const {
    00183         return _mm_cmpeq_epi32(val, other);
    00184     }
    00185     ivec4 operator!=(const ivec4& other) const {
    00186         return _mm_xor_si128(*this==other, _mm_set1_epi32(0xFFFFFFFF));
    00187     }
    00188     ivec4 operator>(const ivec4& other) const {
    00189         return _mm_cmpgt_epi32(val, other);
    00190     }
    00191     ivec4 operator<(const ivec4& other) const {
    00192         return _mm_cmplt_epi32(val, other);
    00193     }
    00194     ivec4 operator>=(const ivec4& other) const {
    00195         return _mm_xor_si128(_mm_cmplt_epi32(val, other), _mm_set1_epi32(0xFFFFFFFF));
    00196     }
    00197     ivec4 operator<=(const ivec4& other) const {
    00198         return _mm_xor_si128(_mm_cmpgt_epi32(val, other), _mm_set1_epi32(0xFFFFFFFF));
    00199     }
    00200     operator fvec4() const;
    00201 };
    00202
    00203 // Conversion operators.
    00204
    00205 inline fvec4::operator ivec4() const {
    00206     return _mm_cvttps_epi32(val);
    00207 }
    00208
    00209 inline ivec4::operator fvec4() const {
    00210     return _mm_cvtepi32_ps(val);
    00211 }
    00212
    00213 // Functions that operate on fvec4s.
    00214
    00215 static inline fvec4 floor(const fvec4& v) {
    00216     return fvec4(_mm_floor_ps(v.val));
    00217 }
    00218
    00219 static inline fvec4 ceil(const fvec4& v) {
    00220     return fvec4(_mm_ceil_ps(v.val));
    00221 }
    00222
    00223 static inline fvec4 round(const fvec4& v) {
    00224     return fvec4(_mm_round_ps(v.val, _MM_FROUND_TO_NEAREST_INT));
    00225 }
    00226
    00227 static inline fvec4 min(const fvec4& v1, const fvec4& v2) {
    00228     return fvec4(_mm_min_ps(v1.val, v2.val));
    00229 }
    00230
    00231 static inline fvec4 max(const fvec4& v1, const fvec4& v2) {
    00232     return fvec4(_mm_max_ps(v1.val, v2.val));
    00233 }
    00234
    00235 static inline fvec4 abs(const fvec4& v) {
    00236     static const __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
    00237     return fvec4(_mm_and_ps(v.val, mask));
    00238 }
    00239
    00240 static inline fvec4 sqrt(const fvec4& v) {
    00241     return fvec4(_mm_sqrt_ps(v.val));
    00242 }
    00243
    00244 static inline float dot3(const fvec4& v1, const fvec4& v2) {
    00245     return _mm_cvtss_f32(_mm_dp_ps(v1, v2, 0x71));
    00246 }
    00247
    00248 static inline float dot4(const fvec4& v1, const fvec4& v2) {
    00249     return _mm_cvtss_f32(_mm_dp_ps(v1, v2, 0xF1));
    00250 }
    00251
    00252 static inline fvec4 cross(const fvec4& v1, const fvec4& v2) {
    00253     fvec4 temp = fvec4(_mm_mul_ps(v1, _mm_shuffle_ps(v2, v2, _MM_SHUFFLE(3, 0, 2, 1)))) -
    00254                  fvec4(_mm_mul_ps(v2, _mm_shuffle_ps(v1, v1, _MM_SHUFFLE(3, 0, 2, 1))));
    00255     return _mm_shuffle_ps(temp, temp, _MM_SHUFFLE(3, 0, 2, 1));
    00256 }
    00257
    00258 static inline void transpose(fvec4& v1, fvec4& v2, fvec4& v3, fvec4& v4) {
    00259     _MM_TRANSPOSE4_PS(v1, v2, v3, v4);
    00260 }
    00261
    00262 // Functions that operate on ivec4s.
    00263
    00264 static inline ivec4 min(const ivec4& v1, const ivec4& v2) {
    00265     return ivec4(_mm_min_epi32(v1.val, v2.val));
    00266 }
    00267
    00268 static inline ivec4 max(const ivec4& v1, const ivec4& v2) {
    00269     return ivec4(_mm_max_epi32(v1.val, v2.val));
    00270 }
    00271
    00272 static inline ivec4 abs(const ivec4& v) {
    00273     return ivec4(_mm_abs_epi32(v.val));
    00274 }
    00275
    00276 static inline bool any(const ivec4& v) {
    00277     return !_mm_test_all_zeros(v, _mm_set1_epi32(0xFFFFFFFF));
    00278 }
    00279
    00280 // Mathematical operators involving a scalar and a vector.
    00281
    00282 static inline fvec4 operator+(float v1, const fvec4& v2) {
    00283     return fvec4(v1)+v2;
    00284 }
    00285
    00286 static inline fvec4 operator-(float v1, const fvec4& v2) {
    00287     return fvec4(v1)-v2;
    00288 }
    00289
    00290 static inline fvec4 operator*(float v1, const fvec4& v2) {
    00291     return fvec4(v1)*v2;
    00292 }
    00293
    00294 static inline fvec4 operator/(float v1, const fvec4& v2) {
    00295     return fvec4(v1)/v2;
    00296 }
    00297
    00298 // Operations for blending fvec4s based on an ivec4.
    00299
    00300 static inline fvec4 blend(const fvec4& v1, const fvec4& v2, const ivec4& mask) {
    00301     return fvec4(_mm_blendv_ps(v1.val, v2.val, _mm_castsi128_ps(mask.val)));
    00302 }
    00303
    00304 #endif /*OPENMM_VECTORIZE_SSE_H_*/
    00305