00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef _Random123_sse_dot_h__
00033 #define _Random123_sse_dot_h__
00034
00035 #if R123_USE_SSE
00036
00037 #if R123_USE_X86INTRIN_H
00038 #include <x86intrin.h>
00039 #endif
00040 #if R123_USE_IA32INTRIN_H
00041 #include <ia32intrin.h>
00042 #endif
00043 #if R123_USE_EMMINTRIN_H
00044 #include <emmintrin.h>
00045 #endif
00046 #if R123_USE_SMMINTRIN_H
00047 #include <smmintrin.h>
00048 #endif
00049 #if R123_USE_WMMINTRIN_H
00050 #include <wmmintrin.h>
00051 #endif
00052 #if R123_USE_INTRIN_H
00053 #include <intrin.h>
00054 #endif
00055 #ifdef __cplusplus
00056 #include <iostream>
00057 #include <limits>
00058 #include <stdexcept>
00059 #endif
00060
00061 #if R123_USE_ASM_GNU
00062
00063
00064 R123_STATIC_INLINE int haveAESNI(){
00065 unsigned int eax, ebx, ecx, edx;
00066 __asm__ __volatile__ ("cpuid": "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) :
00067 "a" (1));
00068 return (ecx>>25) & 1;
00069 }
00070 #elif R123_USE_CPUID_MSVC
00071 R123_STATIC_INLINE int haveAESNI(){
00072 int CPUInfo[4];
00073 __cpuid(CPUInfo, 1);
00074 return (CPUInfo[2]>>25)&1;
00075 }
00076 #else
00077 #warning "No R123_USE_CPUID_XXX method chosen. haveAESNI will always return false"
00078 R123_STATIC_INLINE int haveAESNI(){
00079 return 0;
00080 }
00081 #endif
00082
00083
00084
00085
00086
00087
00088
00089
00090 #if defined(__ICC) || (defined(_MSC_VER) && !defined(_WIN64))
00091
00092
00093 R123_STATIC_INLINE __m128i _mm_set_epi64x(uint64_t v1, uint64_t v0){
00094 union{
00095 uint64_t u64;
00096 uint32_t u32[2];
00097 } u1, u0;
00098 u1.u64 = v1;
00099 u0.u64 = v0;
00100 return _mm_set_epi32(u1.u32[1], u1.u32[0], u0.u32[1], u0.u32[0]);
00101 }
00102 #endif
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #if !defined(__x86_64__) || defined(_MSC_VER) || defined(__OPEN64__)
00114 R123_STATIC_INLINE uint64_t _mm_extract_lo64(__m128i si){
00115 union{
00116 uint64_t u64[2];
00117 __m128i m;
00118 }u;
00119 _mm_store_si128(&u.m, si);
00120 return u.u64[0];
00121 }
00122 #elif defined(__llvm__) || defined(__ICC)
00123 R123_STATIC_INLINE uint64_t _mm_extract_lo64(__m128i si){
00124 return (uint64_t)_mm_cvtsi128_si64(si);
00125 }
00126 #else
00127
00128
00129
00130 R123_STATIC_INLINE uint64_t _mm_extract_lo64(__m128i si){
00131 return (uint64_t)_mm_cvtsi128_si64x(si);
00132 }
00133 #endif
00134 #if defined(__GNUC__) && __GNUC__ < 4
00135
00136 R123_STATIC_INLINE __m128 _mm_castsi128_ps(__m128i si){
00137 return (__m128)si;
00138 }
00139 #endif
00140
00141 #ifdef __cplusplus
00142
00143 struct r123m128i{
00144 __m128i m;
00145 #if R123_USE_CXX0X
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 r123m128i() = default;
00156 r123m128i(__m128i _m): m(_m){}
00157 #endif
00158 r123m128i& operator=(const __m128i& rhs){ m=rhs; return *this;}
00159 r123m128i& operator=(R123_ULONG_LONG n){ m = _mm_set_epi64x(0, n); return *this;}
00160 #if R123_USE_CXX0X
00161
00162
00163
00164 explicit operator bool() const {return _bool();}
00165 #else
00166
00167
00168 operator const void*() const{return _bool()?this:0;}
00169 #endif
00170 operator __m128i() const {return m;}
00171
00172 private:
00173 #if R123_USE_SSE4_1
00174 bool _bool() const{ return !_mm_testz_si128(m,m); }
00175 #else
00176 bool _bool() const{ return 0xf != _mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(m, _mm_setzero_si128()))); }
00177 #endif
00178 };
00179
00180 R123_STATIC_INLINE r123m128i& operator++(r123m128i& v){
00181 __m128i& c = v.m;
00182 __m128i zeroone = _mm_set_epi64x(R123_64BIT(0), R123_64BIT(1));
00183 c = _mm_add_epi64(c, zeroone);
00184
00185 #if R123_USE_SSE4_1
00186 __m128i zerofff = _mm_set_epi64x(0, ~(R123_64BIT(0)));
00187 if( R123_BUILTIN_EXPECT(_mm_testz_si128(c,zerofff), 0) ){
00188 __m128i onezero = _mm_set_epi64x(R123_64BIT(1), R123_64BIT(0));
00189 c = _mm_add_epi64(c, onezero);
00190 }
00191 #else
00192 unsigned mask = _mm_movemask_ps( _mm_castsi128_ps(_mm_cmpeq_epi32(c, _mm_setzero_si128())));
00193
00194
00195 if( R123_BUILTIN_EXPECT((mask&0x3) == 0x3, 0) ){
00196 __m128i onezero = _mm_set_epi64x(1,0);
00197 c = _mm_add_epi64(c, onezero);
00198 }
00199 #endif
00200 return v;
00201 }
00202
00203 R123_STATIC_INLINE r123m128i& operator+=(r123m128i& lhs, R123_ULONG_LONG n){
00204 __m128i c = lhs.m;
00205 __m128i incr128 = _mm_set_epi64x(0, n);
00206 c = _mm_add_epi64(c, incr128);
00207
00208
00209 int64_t lo64 = _mm_extract_lo64(c);
00210 if((uint64_t)lo64 < n)
00211 c = _mm_add_epi64(c, _mm_set_epi64x(1,0));
00212 lhs.m = c;
00213 return lhs;
00214 }
00215
00216
00217 R123_STATIC_INLINE bool operator<=(R123_ULONG_LONG lhs, const r123m128i &rhs){
00218 throw std::runtime_error("operator<=(unsigned long long, r123m128i) is unimplemented.");}
00219
00220
00221
00222
00223 R123_STATIC_INLINE bool operator<(const r123m128i& lhs, const r123m128i& rhs){
00224 throw std::runtime_error("operator<(r123m128i, r123m128i) is unimplemented.");}
00225 R123_STATIC_INLINE bool operator<=(const r123m128i& lhs, const r123m128i& rhs){
00226 throw std::runtime_error("operator<=(r123m128i, r123m128i) is unimplemented.");}
00227 R123_STATIC_INLINE bool operator>(const r123m128i& lhs, const r123m128i& rhs){
00228 throw std::runtime_error("operator>(r123m128i, r123m128i) is unimplemented.");}
00229 R123_STATIC_INLINE bool operator>=(const r123m128i& lhs, const r123m128i& rhs){
00230 throw std::runtime_error("operator>=(r123m128i, r123m128i) is unimplemented.");}
00231
00232 R123_STATIC_INLINE bool operator==(const r123m128i &lhs, const r123m128i &rhs){
00233 return 0xf==_mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(lhs, rhs))); }
00234 R123_STATIC_INLINE bool operator!=(const r123m128i &lhs, const r123m128i &rhs){
00235 return !(lhs==rhs);}
00236 R123_STATIC_INLINE bool operator==(R123_ULONG_LONG lhs, const r123m128i &rhs){
00237 r123m128i LHS; LHS.m=_mm_set_epi64x(0, lhs); return LHS == rhs; }
00238 R123_STATIC_INLINE bool operator!=(R123_ULONG_LONG lhs, const r123m128i &rhs){
00239 return !(lhs==rhs);}
00240 R123_STATIC_INLINE std::ostream& operator<<(std::ostream& os, const r123m128i& m){
00241 union{
00242 uint64_t u64[2];
00243 __m128i m;
00244 }u;
00245 _mm_storeu_si128(&u.m, m.m);
00246 return os << u.u64[0] << " " << u.u64[1];
00247 }
00248
00249 R123_STATIC_INLINE std::istream& operator>>(std::istream& is, r123m128i& m){
00250 uint64_t u64[2];
00251 is >> u64[0] >> u64[1];
00252 m.m = _mm_set_epi64x(u64[1], u64[0]);
00253 return is;
00254 }
00255
00256 template<typename T> inline T assemble_from_u32(uint32_t *p32);
00257
00258 template <>
00259 inline r123m128i assemble_from_u32<r123m128i>(uint32_t *p32){
00260 r123m128i ret;
00261 ret.m = _mm_set_epi32(p32[3], p32[2], p32[1], p32[0]);
00262 return ret;
00263 }
00264
00265 #else
00266
00267 typedef struct {
00268 __m128i m;
00269 } r123m128i;
00270
00271 #endif
00272
00273 #else
00274 R123_STATIC_INLINE int haveAESNI(){
00275 return 0;
00276 }
00277 #endif
00278
00279 #endif