1 /*
2 Copyright 2010-2011, 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 #ifndef __ReinterpretCtr_dot_hpp__
33 #define __ReinterpretCtr_dot_hpp__
34 
35 #include "features/compilerfeatures.h"
36 #include <cstring>
37 
38 namespace r123{
39 /*!
40   ReinterpretCtr uses memcpy to map back and forth
41   between a CBRNG's ctr_type and the specified ToType.  For example,
42   after:
43 
44     typedef ReinterpretCtr<r123array4x32, Philox2x64> G;
45 
46   G is a bona fide CBRNG with ctr_type r123array4x32.
47 
48   WARNING:  ReinterpretCtr is endian dependent.  The
49   values returned by G, declared as above,
50   will depend on the endianness of the machine on which it runs.
51  */
52 
53 template <typename ToType, typename CBRNG>
54 struct ReinterpretCtr{
55     typedef ToType ctr_type;
56     typedef typename CBRNG::key_type key_type;
57     typedef typename CBRNG::ctr_type bctype;
58     typedef typename CBRNG::ukey_type ukey_type;
59     R123_STATIC_ASSERT(sizeof(ToType) == sizeof(bctype) && sizeof(typename bctype::value_type) != 16,
60                        "ReinterpretCtr:  sizeof(ToType) is not the same as sizeof(CBRNG::ctr_type) or CBRNG::ctr_type::value_type looks like it might be __m128i");
61     // It's amazingly difficult to safely do conversions with __m128i.
62     // If we use the operator() implementation below with a CBRNG
63     // whose ctr_type is r123array1xm128i, gcc4.6 optimizes away the
64     // memcpys, inlines the operator()(c,k), and produces assembly
65     // language that ends with an aesenclast instruction with a
66     // destination operand pointing to an unaligned memory address ...
67     // Segfault!  See:  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50444
68     // MSVC also produces code that crashes.  We suspect a
69     // similar mechanism but haven't done the debugging necessary to
70     // be sure.  We were able to 'fix' gcc4.6 by making bc a mutable
71     // data member rather than declaring it in the scope of
72     // operator().  That didn't fix the MSVC problems, though.
73     //
74     // Conclusion - don't touch __m128i, at least for now.  The
75     // easiest (but highly imprecise) way to do that is the static
76     // assertion above that rejects bctype::value_types of size 16. -
77     // Sep 2011.
operator ()r123::ReinterpretCtr78     ctr_type  operator()(ctr_type c, key_type k){
79         bctype bc;
80         std::memcpy(&bc, &c, sizeof(c));
81         CBRNG b;
82         bc = b(bc, k);
83         std::memcpy(&c, &bc, sizeof(bc));
84         return c;
85     }
86 };
87 } // namespace r123
88 #endif
89