util_m128.h (3327B)
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 UTIL_M128_H__ 33 #define UTIL_M128_H__ 34 #include <Random123/features/sse.h> 35 36 // The formatting in fips-197 seems to correspond to 37 // byte[15] [14] ... [0] 38 __m128i m128i_from_charbuf(const char *s){ 39 unsigned int bytes[16]; 40 sscanf(s, "%02x%02x%02x%02x" "%02x%02x%02x%02x" "%02x%02x%02x%02x" "%02x%02x%02x%02x", 41 &bytes[0], &bytes[1], &bytes[2], &bytes[3], 42 &bytes[4], &bytes[5], &bytes[6], &bytes[7], 43 &bytes[8], &bytes[9], &bytes[10], &bytes[11], 44 &bytes[12], &bytes[13], &bytes[14], &bytes[15]); 45 return _mm_set_epi8( 46 bytes[15], bytes[14], bytes[13], bytes[12], 47 bytes[11], bytes[10], bytes[9], bytes[8], 48 bytes[7], bytes[6], bytes[5], bytes[4], 49 bytes[3], bytes[2], bytes[1], bytes[0] 50 ); 51 } 52 53 #define M128_STR_SIZE 34 /* minimum size of the charbuf "hex" argument */ 54 55 char *m128i_to_charbuf(__m128i m, char *hex){ 56 union { 57 unsigned char bytes[16]; 58 __m128i m; 59 } u; 60 _mm_storeu_si128((__m128i*)&u.bytes[0], m); 61 sprintf(hex, "%02x%02x%02x%02x" "%02x%02x%02x%02x" 62 " " 63 "%02x%02x%02x%02x""%02x%02x%02x%02x", 64 u.bytes[0], u.bytes[1], u.bytes[2], u.bytes[3], 65 u.bytes[4], u.bytes[5], u.bytes[6], u.bytes[7], 66 u.bytes[8], u.bytes[9], u.bytes[10], u.bytes[11], 67 u.bytes[12], u.bytes[13], u.bytes[14], u.bytes[15]); 68 69 return hex; 70 } 71 72 #ifdef __cplusplus 73 #include <string> 74 75 __m128i m128i_from_string(const std::string& s) { 76 return m128i_from_charbuf(s.c_str()); 77 } 78 79 std::string m128i_to_string(__m128i m) { 80 char hex[M128_STR_SIZE]; 81 82 m128i_to_charbuf(m, hex); 83 return std::string(hex); 84 } 85 #endif /* __cplusplus */ 86 87 #endif /* UTIL_M128_H__ */