Project Alice
Loading...
Searching...
No Matches
ReinterpretCtr.hpp
Go to the documentation of this file.
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
{
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 "
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.
79
ctr_type
operator()
(
ctr_type
c,
key_type
k) {
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
compilerfeatures.h
r123
Definition:
array.h:344
r123::ReinterpretCtr
Definition:
ReinterpretCtr.hpp:54
r123::ReinterpretCtr::ctr_type
ToType ctr_type
Definition:
ReinterpretCtr.hpp:55
r123::ReinterpretCtr::key_type
CBRNG::key_type key_type
Definition:
ReinterpretCtr.hpp:56
r123::ReinterpretCtr::ukey_type
CBRNG::ukey_type ukey_type
Definition:
ReinterpretCtr.hpp:58
r123::ReinterpretCtr::operator()
ctr_type operator()(ctr_type c, key_type k)
Definition:
ReinterpretCtr.hpp:79
r123::ReinterpretCtr::R123_STATIC_ASSERT
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")
r123::ReinterpretCtr::bctype
CBRNG::ctr_type bctype
Definition:
ReinterpretCtr.hpp:57
src
random123
ReinterpretCtr.hpp
Generated by
1.9.5