00001 /* 00002 Copyright 2010-2011, D. E. Shaw Research. 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions, and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions, and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 00016 * Neither the name of D. E. Shaw Research nor the names of its 00017 contributors may be used to endorse or promote products derived from 00018 this software without specific prior written permission. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00021 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00022 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00023 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00024 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00026 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00027 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00028 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00029 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00030 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 #ifndef __ReinterpretCtr_dot_hpp__ 00033 #define __ReinterpretCtr_dot_hpp__ 00034 00035 #include "features/compilerfeatures.h" 00036 #include <cstring> 00037 00038 namespace r123{ 00053 template <typename ToType, typename CBRNG> 00054 struct ReinterpretCtr{ 00055 typedef ToType ctr_type; 00056 typedef typename CBRNG::key_type key_type; 00057 typedef typename CBRNG::ctr_type bctype; 00058 typedef typename CBRNG::ukey_type ukey_type; 00059 R123_STATIC_ASSERT(sizeof(ToType) == sizeof(bctype) && sizeof(typename bctype::value_type) != 16, 00060 "ReinterpretCtr: sizeof(ToType) is not the same as sizeof(CBRNG::ctr_type) or CBRNG::ctr_type::value_type looks like it might be __m128i"); 00061 // It's amazingly difficult to safely do conversions with __m128i. 00062 // If we use the operator() implementation below with a CBRNG 00063 // whose ctr_type is r123array1xm128i, gcc4.6 optimizes away the 00064 // memcpys, inlines the operator()(c,k), and produces assembly 00065 // language that ends with an aesenclast instruction with a 00066 // destination operand pointing to an unaligned memory address ... 00067 // Segfault! MSVC also produces code that crashes. We suspect a 00068 // similar mechanism but haven't done the debugging necessary to 00069 // be sure. We were able to 'fix' gcc4.6 by making bc a mutable 00070 // data member rather than declaring it in the scope of 00071 // operator(). That didn't fix the MSVC problems, though. 00072 // 00073 // Conclusion - don't touch __m128i, at least for now. The 00074 // easiest (but highly imprecise) way to do that is the static 00075 // assertion above that rejects bctype::value_types of size 16. - 00076 // Sep 2011. 00077 ctr_type operator()(ctr_type c, key_type k){ 00078 bctype bc; 00079 memcpy(&bc, &c, sizeof(c)); 00080 CBRNG b; 00081 bc = b(bc, k); 00082 memcpy(&c, &bc, sizeof(bc)); 00083 return c; 00084 } 00085 }; 00086 } // namespace r123 00087 #endif