Project Alice
Loading...
Searching...
No Matches
ReinterpretCtr.hpp
Go to the documentation of this file.
1/*
2Copyright 2010-2011, D. E. Shaw Research.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
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
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32#ifndef __ReinterpretCtr_dot_hpp__
33#define __ReinterpretCtr_dot_hpp__
34
36#include <cstring>
37
38namespace r123 {
53template<typename ToType, typename CBRNG>
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 "
61 "like it might be __m128i");
62 // It's amazingly difficult to safely do conversions with __m128i.
63 // If we use the operator() implementation below with a CBRNG
64 // whose ctr_type is r123array1xm128i, gcc4.6 optimizes away the
65 // memcpys, inlines the operator()(c,k), and produces assembly
66 // language that ends with an aesenclast instruction with a
67 // destination operand pointing to an unaligned memory address ...
68 // Segfault! See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50444
69 // MSVC also produces code that crashes. We suspect a
70 // similar mechanism but haven't done the debugging necessary to
71 // be sure. We were able to 'fix' gcc4.6 by making bc a mutable
72 // data member rather than declaring it in the scope of
73 // operator(). That didn't fix the MSVC problems, though.
74 //
75 // Conclusion - don't touch __m128i, at least for now. The
76 // easiest (but highly imprecise) way to do that is the static
77 // assertion above that rejects bctype::value_types of size 16. -
78 // Sep 2011.
80 bctype bc;
81 std::memcpy(&bc, &c, sizeof(c));
82 CBRNG b;
83 bc = b(bc, k);
84 std::memcpy(&c, &bc, sizeof(bc));
85 return c;
86 }
87};
88} // namespace r123
89#endif
Definition: array.h:344
CBRNG::key_type key_type
CBRNG::ukey_type ukey_type
ctr_type operator()(ctr_type c, key_type k)
R123_STATIC_ASSERT(sizeof(ToType)==sizeof(bctype) &&sizeof(typename bctype::value_type) !=16, "ReinterpretCtr: sizeof(ToType) is not the same as sizeof(CBRNG::ctr_type) or CBRNG::ctr_type::value_type looks " "like it might be __m128i")
CBRNG::ctr_type bctype