Warning, file /libraries/qca/src/botantools/botan/bit_ops.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved. 0003 0004 Redistribution and use in source and binary forms, for any use, with or without 0005 modification, is permitted provided that the following conditions are met: 0006 0007 1. Redistributions of source code must retain the above copyright notice, this 0008 list of conditions, and the following disclaimer. 0009 0010 2. Redistributions in binary form must reproduce the above copyright notice, 0011 this list of conditions, and the following disclaimer in the documentation 0012 and/or other materials provided with the distribution. 0013 0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED 0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. 0017 0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, 0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0025 */ 0026 // LICENSEHEADER_END 0027 namespace QCA { // WRAPNS_LINE 0028 /************************************************* 0029 * Bit/Word Operations Source File * 0030 * (C) 1999-2007 The Botan Project * 0031 *************************************************/ 0032 0033 } // WRAPNS_LINE 0034 #include <botan/bit_ops.h> 0035 namespace QCA { // WRAPNS_LINE 0036 0037 namespace Botan { 0038 0039 /************************************************* 0040 * XOR arrays together * 0041 *************************************************/ 0042 void xor_buf(byte data[], const byte mask[], u32bit length) 0043 { 0044 while (length >= 8) { 0045 data[0] ^= mask[0]; 0046 data[1] ^= mask[1]; 0047 data[2] ^= mask[2]; 0048 data[3] ^= mask[3]; 0049 data[4] ^= mask[4]; 0050 data[5] ^= mask[5]; 0051 data[6] ^= mask[6]; 0052 data[7] ^= mask[7]; 0053 data += 8; 0054 mask += 8; 0055 length -= 8; 0056 } 0057 for (u32bit j = 0; j != length; ++j) 0058 data[j] ^= mask[j]; 0059 } 0060 0061 void xor_buf(byte out[], const byte in[], const byte mask[], u32bit length) 0062 { 0063 while (length >= 8) { 0064 out[0] = in[0] ^ mask[0]; 0065 out[1] = in[1] ^ mask[1]; 0066 out[2] = in[2] ^ mask[2]; 0067 out[3] = in[3] ^ mask[3]; 0068 out[4] = in[4] ^ mask[4]; 0069 out[5] = in[5] ^ mask[5]; 0070 out[6] = in[6] ^ mask[6]; 0071 out[7] = in[7] ^ mask[7]; 0072 in += 8; 0073 out += 8; 0074 mask += 8; 0075 length -= 8; 0076 } 0077 for (u32bit j = 0; j != length; ++j) 0078 out[j] = in[j] ^ mask[j]; 0079 } 0080 0081 /************************************************* 0082 * Return true iff arg is 2**n for some n > 0 * 0083 *************************************************/ 0084 bool power_of_2(u64bit arg) 0085 { 0086 if (arg == 0 || arg == 1) 0087 return false; 0088 if ((arg & (arg - 1)) == 0) 0089 return true; 0090 return false; 0091 } 0092 0093 /************************************************* 0094 * Return the index of the highest set bit * 0095 *************************************************/ 0096 u32bit high_bit(u64bit n) 0097 { 0098 for (u32bit count = 64; count > 0; --count) 0099 if ((n >> (count - 1)) & 0x01) 0100 return count; 0101 return 0; 0102 } 0103 0104 /************************************************* 0105 * Return the index of the lowest set bit * 0106 *************************************************/ 0107 u32bit low_bit(u64bit n) 0108 { 0109 for (u32bit count = 0; count != 64; ++count) 0110 if ((n >> count) & 0x01) 0111 return (count + 1); 0112 return 0; 0113 } 0114 0115 /************************************************* 0116 * Return the number of significant bytes in n * 0117 *************************************************/ 0118 u32bit significant_bytes(u64bit n) 0119 { 0120 for (u32bit j = 0; j != 8; ++j) 0121 if (get_byte(j, n)) 0122 return 8 - j; 0123 return 0; 0124 } 0125 0126 /************************************************* 0127 * Return the Hamming weight of n * 0128 *************************************************/ 0129 u32bit hamming_weight(u64bit n) 0130 { 0131 u32bit weight = 0; 0132 for (u32bit j = 0; j != 64; ++j) 0133 if ((n >> j) & 0x01) 0134 ++weight; 0135 return weight; 0136 } 0137 0138 } 0139 } // WRAPNS_LINE