如何在C/C 或CUDA中有效地通过对角有效地翻转char阵列
How to bitwise flip an char array by diagonal effectively in C/C++ or Cuda?
我有一个char阵列char input[8] = "abcdabcd"
,我想用对角线将其倒数,这是指
input
:
input[0] == 'a': 0 1 1 0 0 0 0 1
input[1] == 'b': 0 1 1 0 0 0 1 0
input[2] == 'c': 0 1 1 0 0 0 1 1
input[3] == 'd': 0 1 1 0 0 1 0 0
input[4] == 'a': 0 1 1 0 0 0 0 1
input[5] == 'b': 0 1 1 0 0 0 1 0
input[6] == 'c': 0 1 1 0 0 0 1 1
input[7] == 'd': 0 1 1 0 0 1 0 0
output
:
a b c d a b c d
output[0] == 0 : 0 0 0 0 0 0 0 0
output[1] == 255 : 1 1 1 1 1 1 1 1
output[2] == 255 : 1 1 1 1 1 1 1 1
output[3] == 0 : 0 0 0 0 0 0 0 0
output[4] == 0 : 0 0 0 0 0 0 0 0
output[5] == 17 : 0 0 0 1 0 0 0 1
output[6] == 102 : 0 1 1 0 0 1 1 0
output[7] == 170 : 1 0 1 0 1 0 1 0
很明显,我们可以使用两个循环与位置或操作组合一个一个一个一个一个逐一设置目标位,但是,这意味着我们至少需要64 * n
操作,我认为这不是有效的。
由于输入和输出仅是在不同方向上读取内存(通过行或列),是否有任何有效的方式?
此外,我认为根据特殊内存布局进行此操作是非常有意义的,或更改数组中的数字或字符。
谢谢!
这是我的代码,基于黑客喜悦的技巧。尽管它是CPU代码,但可以轻松地转换为并行CUDA代码。
此代码本身是为了转移任意大小的位图。您真正需要的是将uint64_t
X转换为另一个uint64_t
y的代码。
using BitBlock = uint8_t;
using BitBlocks = std::vector<BitBlock>;
void FPTransMap::transpose_bitmap( BitBlocks& bitmap, size_type blocks_per_row )
{
assert( bitmap.size() % blocks_per_row == 0 );
assert( ( bitmap.size() / blocks_per_row ) % 8 == 0 );
BitBlocks transposed( bitmap.size() );
size_type nrow = bitmap.size() / blocks_per_row, row_blocks = nrow / 8;
for ( index_type i = 0; i < row_blocks; ++i ) {
for ( index_type j = 0; j < blocks_per_row; ++j ) {
uint64_t x = ( uint64_t( bitmap[ i * 8 * blocks_per_row + j ] ) << 56 ) |
( uint64_t( bitmap[ ( i * 8 + 1 ) * blocks_per_row + j ] ) << 48 ) |
( uint64_t( bitmap[ ( i * 8 + 2 ) * blocks_per_row + j ] ) << 40 ) |
( uint64_t( bitmap[ ( i * 8 + 3 ) * blocks_per_row + j ] ) << 32 ) |
( uint64_t( bitmap[ ( i * 8 + 4 ) * blocks_per_row + j ] ) << 24 ) |
( uint64_t( bitmap[ ( i * 8 + 5 ) * blocks_per_row + j ] ) << 16 ) |
( uint64_t( bitmap[ ( i * 8 + 6 ) * blocks_per_row + j ] ) << 8 ) |
( uint64_t( bitmap[ ( i * 8 + 7 ) * blocks_per_row + j ] ) );
uint64_t y = (x & 0x8040201008040201LL) |
((x & 0x0080402010080402LL) << 7) |
((x & 0x0000804020100804LL) << 14) |
((x & 0x0000008040201008LL) << 21) |
((x & 0x0000000080402010LL) << 28) |
((x & 0x0000000000804020LL) << 35) |
((x & 0x0000000000008040LL) << 42) |
((x & 0x0000000000000080LL) << 49) |
((x >> 7) & 0x0080402010080402LL) |
((x >> 14) & 0x0000804020100804LL) |
((x >> 21) & 0x0000008040201008LL) |
((x >> 28) & 0x0000000080402010LL) |
((x >> 35) & 0x0000000000804020LL) |
((x >> 42) & 0x0000000000008040LL) |
((x >> 49) & 0x0000000000000080LL);
transposed[ ( j * 8 ) * row_blocks + i ] = uint8_t( ( y >> 56 ) & 0xFF );
transposed[ ( j * 8 + 1 ) * row_blocks + i ] = uint8_t( ( y >> 48 ) & 0xFF );
transposed[ ( j * 8 + 2 ) * row_blocks + i ] = uint8_t( ( y >> 40 ) & 0xFF );
transposed[ ( j * 8 + 3 ) * row_blocks + i ] = uint8_t( ( y >> 32 ) & 0xFF );
transposed[ ( j * 8 + 4 ) * row_blocks + i ] = uint8_t( ( y >> 24 ) & 0xFF );
transposed[ ( j * 8 + 5 ) * row_blocks + i ] = uint8_t( ( y >> 16 ) & 0xFF );
transposed[ ( j * 8 + 6 ) * row_blocks + i ] = uint8_t( ( y >> 8 ) & 0xFF );
transposed[ ( j * 8 + 7 ) * row_blocks + i ] = uint8_t( y & 0xFF );
}
}
std::swap( bitmap, transposed );
}
相关文章:
- 有效地使用std::unordered_map来插入或增加键的值
- 如何有效地在 std::vector 中插入一对?
- 有效地计算多维数组的累积和?
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 有效地将大数存储为 2 的幂用于路径问题
- 如何在C++中写入 1000 个文件时有效地缓冲
- 如何有效地找到数组中三元组和的最小差异?
- 如何在C++中有效地将数字值重新分配给字符数组
- C++有效地找到向量中第一个最接近的匹配值?
- 如何有效地操作满足给定谓词的向量中的所有项目?
- 有效地将数据加载到 std::vector 中<char>
- 如何在使用 cin 请求 int 时有效地使用户输入万无一失?
- C++:有效地将Sha256摘要放入OpenSSL Bignum?
- 如何有效地收集给定数组中的重复元素?
- 如何有效地修剪和合并四叉树中的节点?
- 可以有效地转换 std::any 与 std::any_cast
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 如何使用包含内部类的类实例有效地从内部类访问成员?
- 当表示为对象的一维向量时,有效地旋转 NxM 矩阵 (C++)
- 如何在C/C 或CUDA中有效地通过对角有效地翻转char阵列