- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上我有一个 __m256i
变量,其中每个字节代表一个需要在 uint64_t
中设置的位置.请注意,所有字节值都将 < 64。
我对如何远程有效地做到这一点感到有些茫然。
我正在考虑的一种选择是在某些情况下字节之间有很多重复项,因此类似于:
__m256i indexes = foo();
uint64_t result = 0;
uint32_t aggregate_mask = ~0;
do {
uint32_t idx = _mm256_extract_epi8(indexes, __tzcnt_u32(aggregate_mask));
uint32_t idx_mask =
_mm256_movemask_epi8(_mm256_cmpeq_epi(indexes, _mm256_set1_epi8(idx)));
aggregate_mask ^= idx_mask;
result |= ((1UL) << idx);
} while (aggregate_mask);
有了足够多的重复项,我相信这可能会有些效率,但我不能保证总是有足够的重复项来使这比仅遍历字节并按顺序设置更快。
__m256i indexes = foo();
uint8_t index_arr[32];
_mm256_store_si256((__m256i *)index_arr, indexes);
uint64_t result = 0;
for (uint32_t i = 0; i < 32; ++i) {
result |= ((1UL) << index_arr[i];
}
如果可能的话,我正在寻找一种可以在 Skylake 上运行的解决方案(没有 AVX512)。如果 AVX512 是必要的(我认为分组可能有一些半有效的东西,然后使用
_mm256_shldv_epi16
)总比没有好:)
// 32 bit
__m256i lo_shifts = _mm256_sllv_epi32(_mm256_set1_epi32(1), indexes);
__m256i t0 = _mm256_sub_epi32(indexes, _mm256_set1_epi32(1));
__m256i hi_shifts = _mm256_sllv_epi32(_mm256_set1_epi32(1), t0);
__m256i lo_shifts_lo = _mm256_shuffle_epi32(lo_shifts, 0x5555);
__m256i hi_shifts_lo = _mm256_shuffle_epi32(hi_shifts, 0x5555);
__m256i hi_shifts_hi0 = _mm256_slli_epi64(hi_shifts, 32);
__m256i hi_shifts_hi1 = _mm256_slli_epi64(hi_shifts_lo, 32);
__m256i all_hi_shifts = _mm256_or_epi64(hi_shifts_hi0, hi_shifts_hi1);
__m256i all_lo_shifts_garbage = _mm256_or_epi64(lo_shifts_lo, lo_shifts);
__m256i all_lo_shifts = _mm256_and_epi64(all_lo_shifts_garbage, _mm256_set1_epi64(0xffffffff));
__m256i all_shifts = _mm256_or_epi64(all_lo_shifts, all_hi_shifts);
或者从epi64位开始:
// 64 bit
__m256i indexes0 = _m256_and_epi64(indexes, _mm256_set1_epi64(0xffffffff));
__m256i indexes1 = _m256_shuffle_epi32(indexes, 0x5555);
__m256i shifts0 = _m256_sllv_epi64(_mm256_set1_epi64x(1), indexes0);
__m256i shifts1 = _m256_sllv_epi64(_mm256_set1_epi64x(1), indexes1);
__m256i all_shifts = _m256_or_epi64(shifts0, shifts1);
我的猜测是来自epi64 更快。
最佳答案
关键要素是 _mm256_sllv_epi64
使用运行时可变的移位距离在 64 位 channel 内移位位。
代码需要 C++/17,仅在 VC++ 2019 中测试。
虽然不确定它是否会比标量代码快得多,但大多数指令都是 1 周期延迟,但对我来说太多了,VC++ 在关键路径上产生了大约 35 个。
// Move a single bit within 64-bit lanes
template<int index>
inline __m256i moveBit( __m256i position )
{
static_assert( index >= 0 && index < 8 );
// Extract index-th byte from the operand
if constexpr( 7 == index )
{
// Most significant byte only needs 1 instruction to shift into position
position = _mm256_srli_epi64( position, 64 - 8 );
}
else
{
if constexpr( index > 0 )
{
// Shift the operand by `index` bytes to the right.
// On many CPUs, _mm256_srli_si256 is slightly faster than _mm256_srli_epi64
position = _mm256_srli_si256( position, index );
}
const __m256i lowByte = _mm256_set1_epi64x( 0xFF );
position = _mm256_and_si256( position, lowByte );
}
const __m256i one = _mm256_set1_epi64x( 1 );
return _mm256_sllv_epi64( one, position );
}
inline uint64_t setBitsAvx2( __m256i positions )
{
// Process each of the 8 bytes within 64-bit lanes
const __m256i r0 = moveBit<0>( positions );
const __m256i r1 = moveBit<1>( positions );
const __m256i r2 = moveBit<2>( positions );
const __m256i r3 = moveBit<3>( positions );
const __m256i r4 = moveBit<4>( positions );
const __m256i r5 = moveBit<5>( positions );
const __m256i r6 = moveBit<6>( positions );
const __m256i r7 = moveBit<7>( positions );
// vpor instruction is very fast with 1 cycle latency,
// however modern CPUs can issue and dispatch multiple instructions per cycle,
// it still makes sense to try reducing dependencies.
const __m256i r01 = _mm256_or_si256( r0, r1 );
const __m256i r23 = _mm256_or_si256( r2, r3 );
const __m256i r45 = _mm256_or_si256( r4, r5 );
const __m256i r67 = _mm256_or_si256( r6, r7 );
const __m256i r0123 = _mm256_or_si256( r01, r23 );
const __m256i r4567 = _mm256_or_si256( r45, r67 );
const __m256i result = _mm256_or_si256( r0123, r4567 );
// Reduce 4 8-byte values to scalar
const __m128i res16 = _mm_or_si128( _mm256_castsi256_si128( result ), _mm256_extracti128_si256( result, 1 ) );
const __m128i res8 = _mm_or_si128( res16, _mm_unpackhi_epi64( res16, res16 ) );
return (uint64_t)_mm_cvtsi128_si64( res8 );
};
inline uint64_t setBitsScalar( __m256i positions )
{
alignas( 32 ) std::array<uint8_t, 32> index_arr;
_mm256_store_si256( ( __m256i * )index_arr.data(), positions );
uint64_t result = 0;
for( uint32_t i = 0; i < 32; i++ )
result |= ( ( 1ull ) << index_arr[ i ] );
return result;
}
static void testShuffleBits()
{
const __m128i src16 = _mm_setr_epi8( 0, 0, 0, 0, 1, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 31 );
const __m256i src32 = _mm256_setr_m128i( src16, _mm_setzero_si128() );
printf( "AVX2: %" PRIx64 "\n", setBitsAvx2( src32 ) );
printf( "Scalar: %" PRIx64 "\n", setBitsScalar( src32 ) );
}
关于c++ - 将 __m256i 寄存器转换为 uint64_t 位掩码,使得每个字节的值是输出中的一个设置位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63757108/
我无法理解如何使用一些旧的 VGA 代码在这个示例中设置序列 Controller 寄存器: mov dx,SC_INDEX mov ax,0604h out dx,ax
我希望对 zmm 0-31 寄存器集的四字元素执行整数算术运算并保留这些运算产生的进位位。看来这只有在通用寄存器集中处理数据时才有可能。 因此,我想将信息从 zmm 0-31 寄存器之一复制到通用寄存
ARM 64中包含多种寄存器,下面介绍一些常见的寄存器。 1 通用寄存器 ARM 64包含31个64bit寄存器,记为X0~X30。 每一个通用寄存器,它的低32bit都可以被访问,记为W0~W
1.寄存器 组合逻辑存在一个最大的缺点就是存在竞争与冒险,系统会产生不定态;使用时序逻辑电路就会极大的改善这种情况 寄存器具有存储功能,一般是由D触发器构成,由时钟脉冲控制,每个D触发器能够
使用 $gp 是否存在危险?注册以存储值?我想我的问题是 $gp 的真正功能是什么?它是否以某种方式在幕后调用,以便如果我使用它,事情可能会变得非常非常错误? 最佳答案 那么,$gp register
我遇到了这段代码的问题,我无法弄清楚问题出在哪里。所以当我运行这段代码时:if $row["count"] > 0 else块运行和 $_SESSION["error"]设置。 当$row["coun
所以我正在做二进制炸弹的变体。这就是阶段 0x0000000000401205 : sub $0x8,%rsp 0x0000000000401209 : cmp $0x3,
我在一个名为 (EmployeeDetailKey - varchar(10)) 的字段中获得了一个值,其中包含顺序值,例如 00001, 00002, 00003.... 它位于 Employeed
我有一个要求,应该为每个调用的线程分配一个计数器变量。但我没有得到预期的结果,实际上计数器在线程中重复。我创建了一个虚拟表和一个过程来将计数器值插入到表中。无论如何,是否可以更改代码以便线程获得递增的
预期输出:需要打印第4季度的wage_amt +--------------+--------------+--------------+--------------+ | wages_amt_q1
如何匹配模式 abc_[someArbitaryStringHere]_xyz? 为了澄清,我希望正则表达式能够匹配以下性质的字符串: abc_xyz、abc_asdfsdf_xyz、abc_32rw
从下拉列表(自定义)中选择一个值而不是常规下拉列表,它有很多下拉值 我可以用代码选择第一个值 find('.selected', :text=>arg1,exact: false).click 但无法
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有 .csv 文件中的数据,它包含 2 列 x 轴和 y 轴。从 .csv 文件读取轴,然后使用拉伸(stretch)指数函数拟合数据,但显示错误。 这里我给出示例数据以方便理解。 我的函数是f(x
我正在尝试使用以下汇编代码将磁盘扇区加载到内存中,但正如我在终端中使用一些 int 0x10 时发现的那样,它不起作用的原因是它陷入了无限循环。我以为循环会自动为我递减 cx 寄存器。下面是代码,其中
我正在尝试编写一个脚本,该脚本将在 vim 中打开一个文件并将其中的特定行复制到 vim 的寄存器之一中。当脚本再次运行时,它会决定再次打开文件,然后将 vim 寄存器中的值粘贴回。实际上,脚本应该在
我目前正在尝试弄清楚如何将指针寄存器 SI 指向的内存中的第一个字节添加到 AX 寄存器的当前内容中。 因此,如果 SI 包含某个地址,并且该地址在内存中的值是:00 和 01,我希望将 00 添加到
我试图将两个 16 位数字与以下 NASM 代码相乘: mov ax, [input1] mov bx, [input2] mul bx 前面代码的结果存储在 DX:AX 我试图使用来自单独库“pri
我正在尝试修改 rip 寄存器(只是为了好玩)。 buffer 应该是内存地址,所以不知道为什么会得到Error: operand type mismatch for 'movq' #include
我可以告诉gcc-style inline assembly把我的__m512i变量到特定 zmm注册,如 zmm31 ? 最佳答案 就像在根本没有特定寄存器约束的目标(如 ARM)上一样,使用 lo
我是一名优秀的程序员,十分优秀!