AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79595693
Accepted
David Wohlferd
David Wohlferd
Asked: 2025-04-28 09:49:55 +0800 CST2025-04-28 09:49:55 +0800 CST 2025-04-28 09:49:55 +0800 CST

Vector64.ExtractMostSignificantBits 不使用 pext 指令有什么原因吗?

  • 772

令我惊讶的是,与 Vector256 不同,Vector64 中的 ExtractMostSignificantBits 函数并没有使用内部函数来完成工作。使用内部函数后,以下 36 行代码如下:

mov      qword ptr [rsp+0x80], r12
movzx    rdx, byte  ptr [rsp+0x80]
shr      edx, 7
movzx    r8, byte  ptr [rsp+0x81]
shr      r8d, 7
add      r8d, r8d
or       edx, r8d
movzx    r8, byte  ptr [rsp+0x82]
shr      r8d, 7
shl      r8d, 2
or       r8d, edx
mov      edx, r8d
movzx    r8, byte  ptr [rsp+0x83]
shr      r8d, 7
shl      r8d, 3
or       r8d, edx
mov      edx, r8d
movzx    r8, byte  ptr [rsp+0x84]
shr      r8d, 7
shl      r8d, 4
or       r8d, edx
mov      edx, r8d
movzx    r8, byte  ptr [rsp+0x85]
shr      r8d, 7
shl      r8d, 5
or       r8d, edx
mov      edx, r8d
movzx    r8, byte  ptr [rsp+0x86]
shr      r8d, 7
shl      r8d, 6
or       r8d, edx
mov      edx, r8d
movzx    r8, byte  ptr [rsp+0x87]
shr      r8d, 7
shl      r8d, 7
or       r8d, edx

分为这4个:

mov      qword ptr [rsp+0x70], rcx
mov      r10, qword ptr [rsp+0x70]
mov      r15, 0x8080808080808080
pext     r10, r10, r15

似乎你可以用这样的方法将它卷入Vector64.cs :

public static uint ExtractMostSignificantBits<T>(this Vector64<T> vector)
{
    if (Bmi2.X64.IsSupported)
    {
        ulong mask = 0;

        if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte))
            mask = 0x8080808080808080;
        else if (typeof(T) == typeof(short) || typeof(T) == typeof(ushort))
            mask = 0x8000800080008000;
        else if (typeof(T) == typeof(int) || typeof(T) == typeof(uint) || typeof(T) == typeof(float))
            mask = 0x8000000080000000;
        else if (typeof(T) == typeof(long) || typeof(T) == typeof(ulong) || typeof(T) == typeof(double))
            mask = 0x8000000000000000;
        else if (typeof(T) == typeof(nint) || typeof(T) == typeof(nuint))
            if (IntPtr.Size == 4)
                mask = 0x8000000080000000;
            else
                mask = 0x8000000000000000;

        ulong u = vector._00;

        ulong retval = Bmi2.X64.ParallelBitExtract(u, mask);

        return (uint)retval;
    }

    // Fall back to the old code
    uint result = 0;
    for (int index = 0; index < Vector64<T>.Count; index++)
    {
        uint value = Scalar<T>.ExtractMostSignificantBit(vector.GetElementUnsafe(index));
        result |= (value << index);
    }

    return result;
}

BMI2 已经不算“新”了,早在 2013 年左右就已推出。而且,考虑到 JIT 编译,这样做成本也不高。

我已经编写了一些测试代码(可根据要求提供),它产生的结果与现有代码相同,只是速度快了两倍多(如果对 Vector64.GetElementUnsafe 进行一些改进,速度可能会更快)。

我是不是忽略了什么细微差别或特殊情况,导致使用内在函数不可接受?还是有人只是忽略了这一点?

c#
  • 1 1 个回答
  • 85 Views

1 个回答

  • Voted
  1. Best Answer
    David Wohlferd
    2025-04-30T11:59:35+08:002025-04-30T11:59:35+08:00

    所以,总结一下。我提出的解决方案不是.Net团队在撰写时选择的解决方案,原因(至少)有3个Vector64.ExtractMostSignificantBits:

    • BMI2 系列在 Excavator 和 Zen1/Zen2 系列处理器上的性能表现极其糟糕。虽然添加我建议的代码可以提升我机器的性能,但应尽可能避免让其他人的体验更差。

    • BMI2 系列比 SSE 系列更新,这意味着使用 SSE 指令不仅可以避免 Zen1/Zen2 问题,还能支持更老的机器。BMI2 于 2013 年推出,而 SSE2 则可追溯到 2000 年。
      英特尔的低端(赛扬/奔腾)CPU 和低功耗(Silvermont 系列)直到后来才分别在 Ice Lake Pentium/Celeron 和 Gracemont(Alder Lake E 核)上获得 BMI2 支持。奔腾/赛扬品牌的 Skylake 衍生 CPU 及更早的 CPU 禁用了 AVX1/2,BMI1/2 也禁用了,可能是通过禁用 VEX 前缀解码来实现的。

    • 即使在处理更多位时,SSE 指令也能比 BMI2 指令生成更快的代码。

    看看这些数字:

    Execution times of my test suite (in seconds):
    
    Original code: 20.276
    BMI2: 8.759
    V128: 6.937
    

    因此,正如大家预测的那样,使用 128 位矢量指令显然是赢家。

    现在我将利用我所学到的知识并将其推销给 .Net 人员,看看他们是否感兴趣。

    • 1

相关问题

  • Polly DecorlatedJitterBackoffV2 - 如何计算完成所有重试所需的最长时间?

  • Wpf。在 ScrollViewer 中滚动 DataGrid

  • 我在使用 .NET MAUI MVVM 的游戏页面上获得的分数在其他页面上不可见。如何在本地设备中保存分数数据

  • 从 DataTemplate 内部将 TreeView 层次结构与 HierarchicalDataTemplate 结合使用

  • 如何改进 .NET 中的验证接口?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve