random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

ut_M128.cpp (4543B)


      1 /*
      2 Copyright 2010-2016, D. E. Shaw Research.
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are
      7 met:
      8 
      9 * Redistributions of source code must retain the above copyright
     10   notice, this list of conditions, and the following disclaimer.
     11 
     12 * Redistributions in binary form must reproduce the above copyright
     13   notice, this list of conditions, and the following disclaimer in the
     14   documentation and/or other materials provided with the distribution.
     15 
     16 * Neither the name of D. E. Shaw Research nor the names of its
     17   contributors may be used to endorse or promote products derived from
     18   this software without specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 #include <Random123/features/compilerfeatures.h>
     33 #if !R123_USE_SSE
     34 #include <stdio.h>
     35 int main(){ printf("No SSE.  Nothing to check\n"); return 0; }
     36 #else
     37 
     38 #include <Random123/features/sse.h>
     39 #include <sstream>
     40 
     41 int main(int, char **){
     42     r123m128i uninitialized;
     43     __m128i zm = _mm_setzero_si128();
     44 #if R123_USE_CXX1X_UNRESTRICTED_UNIONS
     45     r123m128i zM(zm);
     46 #else
     47     r123m128i zM; zM.m = zm;
     48 #endif
     49     uninitialized.m = _mm_setzero_si128();
     50 
     51     // operator bool (or maybe void*)
     52     assert(!uninitialized);
     53     assert(!zM);
     54 
     55     // operator=(__m128i)
     56     // conversion to __m128i
     57     __m128i one = _mm_set_epi32(0, 0, 0, 1);
     58     __m128i two = _mm_set_epi32(0, 0, 0, 2);
     59     r123m128i One, Two;
     60     One = one;
     61     Two = two;
     62     assert(!!One);
     63     assert(!!Two);
     64     r123m128i AnotherOne;
     65     AnotherOne = one;
     66 
     67     assert( AnotherOne == One );
     68     assert( Two != One );
     69     __m128i m = One;
     70     AnotherOne = m;
     71     assert( AnotherOne ==  One );
     72 
     73     // operator++ (prefix)
     74     ++One;
     75     assert( One == Two );
     76     assert( One != AnotherOne );
     77 
     78     // operator+=(R123_ULONG_LONG)
     79     // operator==(R123_ULONG_LONG, r123m128i)
     80     R123_ULONG_LONG ull = 2;
     81     AnotherOne += 1;
     82     for(int i=0; i<1000; ++i){
     83         AnotherOne += i;
     84         ull += i;
     85         for(int j=0; j<i; ++j){
     86             assert(One != AnotherOne);
     87             ++One;
     88         }
     89         assert(One == AnotherOne);
     90         assert(ull == AnotherOne);
     91     }
     92 
     93     // Do some additions that require carrying.
     94     // Check the identity behavior of the streams
     95     // as well
     96 #if R123_USE_64BIT
     97     for(uint64_t i=0; i<1000; ++i){
     98         uint64_t fff = (~((uint64_t)0)) - i;
     99         AnotherOne += fff;
    100         ull += fff; // will overflow
    101         One += fff/2;
    102         One += fff - fff/2;
    103         assert(AnotherOne == One);
    104         assert( !(ull == One) );
    105         std::stringstream ss;
    106         r123m128i YetAnother;
    107         ss << AnotherOne;
    108         ss >> YetAnother;
    109 
    110         assert( YetAnother == AnotherOne );
    111     }
    112 #endif
    113 
    114     // Sep 2011 - clang in the fink build of llvm-2.9.1 on MacOS 10.5.8
    115     // fails to catch anything, and hence fails this test.  I suspect
    116     // a problem with the packaging/installation rather than a bug
    117     // in llvm.  However, if it shows up in other contexts, some
    118     // kind of #ifndef might be appropriate.  N.B.  There's a similar
    119     // exception test in ut_carray.cpp
    120     bool caught;
    121     caught = false;
    122     try{
    123         (void)(One < AnotherOne);
    124     }catch(std::runtime_error& ){ caught = true; }
    125     assert(caught);
    126 
    127     caught = false;
    128     try{
    129         (void)(One <= AnotherOne);
    130     }catch(std::runtime_error& ){ caught = true; }
    131     assert(caught);
    132 
    133     caught = false;
    134     try{
    135         (void)(One > AnotherOne);
    136     }catch(std::runtime_error& ){ caught = true; }
    137     assert(caught);
    138 
    139     caught = false;
    140     try{
    141         (void)(One >= AnotherOne);
    142     }catch(std::runtime_error& ){ caught = true; }
    143     assert(caught);
    144 
    145     // assemble_from_u32<r123m128i>
    146 
    147     std::cout << "ut_M128: OK\n";
    148     return 0;
    149 }
    150 
    151 #endif