- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑一个 64 位无符号整数,它在可被 4 整除的位置恰好包含一个值为 0000b 的半字节。
是否有一个次线性的,即比 O(16) 算法更好的算法来提取这个空半字节的位置? SIMD 解决方案也是可以接受的。
最佳答案
一种方法是使用 Alan Mycroft 的空字节检测算法的变体。包含零的字节变成 0x80
,其他字节变成 0x00。
这可以通过调整掩码简单地修改为处理半字节而不是字节。使用 Posix 函数 ffsll()
,我们可以找到第一个设置位并对位索引进行必要的调整,因为 ffsll()
使用基于 1 的位位置Mycroft 的算法标记零半字节中的最高有效位,而不是最低有效位。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/* Adapted from Alan Mycroft's null-byte detection algorithm
newsgroup comp.lang.c, 1987/04/08,
https://groups.google.com/forum/#!original/comp.lang.c/2HtQXvg7iKc/xOJeipH6KLMJ
*/
int zero_nibble_position (uint64_t a)
{
const uint64_t nibble_lsb = 0x1111111111111111ULL;
const uint64_t nibble_msb = 0x8888888888888888ULL;
uint64_t t = (a - nibble_lsb) & (~a & nibble_msb);
return (t) ? (ffsll (t) - 4) : -1;
}
int zero_nibble_position_ref (uint64_t a)
{
if (!(a & (0xfULL << 0))) return 0;
if (!(a & (0xfULL << 4))) return 4;
if (!(a & (0xfULL << 8))) return 8;
if (!(a & (0xfULL << 12))) return 12;
if (!(a & (0xfULL << 16))) return 16;
if (!(a & (0xfULL << 20))) return 20;
if (!(a & (0xfULL << 24))) return 24;
if (!(a & (0xfULL << 28))) return 28;
if (!(a & (0xfULL << 32))) return 32;
if (!(a & (0xfULL << 36))) return 36;
if (!(a & (0xfULL << 40))) return 40;
if (!(a & (0xfULL << 44))) return 44;
if (!(a & (0xfULL << 48))) return 48;
if (!(a & (0xfULL << 52))) return 52;
if (!(a & (0xfULL << 56))) return 56;
if (!(a & (0xfULL << 60))) return 60;
return -1;
}
/*
https://groups.google.com/forum/#!original/comp.lang.c/qFv18ql_WlU/IK8KGZZFJx4J
From: geo <gmars...@gmail.com>
Newsgroups: sci.math,comp.lang.c,comp.lang.fortran
Subject: 64-bit KISS RNGs
Date: Sat, 28 Feb 2009 04:30:48 -0800 (PST)
This 64-bit KISS RNG has three components, each nearly
good enough to serve alone. The components are:
Multiply-With-Carry (MWC), period (2^121+2^63-1)
Xorshift (XSH), period 2^64-1
Congruential (CNG), period 2^64
*/
static uint64_t kiss64_x = 1234567890987654321ULL;
static uint64_t kiss64_c = 123456123456123456ULL;
static uint64_t kiss64_y = 362436362436362436ULL;
static uint64_t kiss64_z = 1066149217761810ULL;
static uint64_t kiss64_t;
#define MWC64 (kiss64_t = (kiss64_x << 58) + kiss64_c, \
kiss64_c = (kiss64_x >> 6), kiss64_x += kiss64_t, \
kiss64_c += (kiss64_x < kiss64_t), kiss64_x)
#define XSH64 (kiss64_y ^= (kiss64_y << 13), kiss64_y ^= (kiss64_y >> 17), \
kiss64_y ^= (kiss64_y << 43))
#define CNG64 (kiss64_z = 6906969069ULL * kiss64_z + 1234567ULL)
#define KISS64 (MWC64 + XSH64 + CNG64)
int main (void)
{
for (int i = 0; i < 1000000000; i++) {
uint64_t a = KISS64;
int res = zero_nibble_position (a);
int ref = zero_nibble_position_ref (a);
if (res != ref) {
printf ("a=%016llx res=%d ref=%d\n", a, res, ref);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
如果您的平台不支持 POSIX 函数 ffsll()
,您可以使用特定于编译器的内置函数,例如 gcc 的 __builtin_ctz()
、MSVC 的 _BitScanForward64()
或 Intel 编译器的 _tzcnt_u64()
,使用内联汇编来访问提供计数的机器指令尾随零,或滚动你自己的,例如像这样:
int clzll (uint64_t a)
{
uint64_t r = 64;
if (a >= 0x100000000ULL) { a >>= 32; r -= 32; }
if (a >= 0x000010000ULL) { a >>= 16; r -= 16; }
if (a >= 0x000000100ULL) { a >>= 8; r -= 8; }
if (a >= 0x000000010ULL) { a >>= 4; r -= 4; }
if (a >= 0x000000004ULL) { a >>= 2; r -= 2; }
r -= a - (a & (a >> 1));
return r;
}
int ffsll (uint64_t a)
{
return 64 - clzll(a & -a);
}
由于我们在这里不需要完全通用的 ffsll()
实现,因此还可以基于 previous answer of mine 构建一个更快的变体,它使用寄存器内查找表:
/* return the position of a single set bit at (one-based) position n*4 */
int bit_pos (uint64_t a)
{
const uint64_t magic_multiplier =
(( 0ULL << 60) | ( 1ULL << 56) | ( 2ULL << 52) | ( 3ULL << 48) |
( 4ULL << 44) | ( 5ULL << 40) | ( 6ULL << 36) | ( 7ULL << 32) |
( 8ULL << 28) | ( 9ULL << 24) | (10ULL << 20) | (11ULL << 16) |
(12ULL << 12) | (13ULL << 8) | (14ULL << 4) | (15ULL << 0));
return (int)((((a >> 3) * magic_multiplier) >> 60) * 4 + 4);
}
/* special version for MSBs of nibbles only! */
int ffsll (uint64_t a)
{
#if NEVER_MORE_THAN_ONE_ZERO_NIBBLE
/* find the position of the only bit set */
return bit_pos (a);
#else // NEVER_MORE_THAN_ONE_ZERO_NIBBLE
/* isolate least significant set bit and find its position */
return bit_pos (a & -a);
#endif // NEVER_MORE_THAN_ONE_ZERO_NIBBLE
}
关于bit-manipulation - 快速查找 64 位无符号中的空半字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480527/
如何更改循环中变量的名称?比如 number1 、 number2 、 number3 、 number4 ? var array = [2,4,6,8] func ap ( number1: Int
我想设置 View 的背景颜色并在一定延迟后将其更改为另一种颜色。这是我的尝试方式: print("setting color 1") self.view.backgroundColor = UICo
我在使用 express-session 时遇到问题。 session 数据不会在请求之间持续存在。 正如您在下面的代码中看到的那样,/join 路由设置了一些 session 属性,但是当 /sur
我试图从叶渲染器获得一个非常简单的结果,用于快速 Steam 的 for 循环。 我正在上传叶文件 HTML,因为它不接受此处格式正确的代码 - 下面的pizza.swift代码- import
你们中有人有什么好的链接可以与我分享吗?我正在寻找一个 FAST 程序员编辑器,它可以非常快速地打开包含超过 100, 000 行代码的文件?我目前正在使用记事本自动取款机,打开一个 29000 行长
我现在正在处理眼动追踪数据,因此拥有一个巨大的数据集(想想数百万行),因此希望有一种快速的方法来完成此任务。这是它的简化版本。 数据告诉您眼睛在每个时间点正在查看的位置以及我们正在查看的每个文件。 X
我是新手,想为计时器或其他设备选择提示音。 如何打开此列表,以选择其中一种声音? Alert sound list 最佳答案 您将无法在应用中使用系统声音。 但是,您可以包括自己的声音文件,并将其显示
我编写了以下代码来构建具有顺序字符串的数组。 它的工作方式与我预期的一样,但我希望它能更快地运行。有没有更有效的方法在PowerShell中产生我想要的结果? 我是PowerShell的新手,非常感谢
我有一个包含一些非唯一行的矩阵,例如: x 尝试 y <- rle(apply(x, 1, paste, collapse = " ")) # y$lengths is the vector con
我的函数“keyboardWillShown”有问题。所以我想要的是菜单打开时,菜单正好出现在键盘上方。它可以在Iphone 8 plus,8、7、6上完美运行。但是,当我在模拟器上运行Iphone
我正在尝试通过Swift 5中的HTTP get方法从API提取数据。它在启动时成功加载了数据,但是当我刷新页面时,它说“索引超出范围”,这是因为数据是不再会在我的日志中读取,因此索引中没有任何内容。
我想做什么: 从我的数据库中获取时间戳并将其转换为用户的时区。 我的代码: let tryItNow = "\(model.timestampName)" let format = D
给定字体名称和字体大小,如何查找字符串的宽度(CGFloat)? (目标是将UIView的宽度设置为足以容纳字符串的宽度。) 我有两个字符串:一个重复“1”,重复36次,另一个重复“M”,重复36次。
我正在尝试解析此JSON ["Items": ( { AccountBalance = 0; AlphabetType = 3; Description = "\U0631\U
我在UINavigationBar内放置了一个UILabel。 我想根据navigationBar的高度增加该标签的字体大小。当navigationBar很大时,我希望字体大小更大;当滚动并缩小nav
我想将用户输入限制为仅有效数字并使用以下内容: func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, rep
目前我有一个包含超过 100.000 张图像的数据库,它们大小不一或类似,但我想为我的公司制作以下内容: 我插入/上传一张图片,系统返回最有可能相同的图片。我不知道使用什么算法,但它需要快速。我可以预
在我的 swift 项目中,我有一个按钮,我想在标签上打印按下该按钮的时间。 如何解决这个问题? 最佳答案 添加到DHEERAJ的答案中,您只需在func press(sender: UIButton
我必须发表评论,尝试在解析中导入数组。然而,有一个问题。 当我尝试从 Parse 加载数组时,我的输出是 ("Blah","Blah","Blah")这是一个元组...而不是一个数组 TT... 如何
我的应用程序有一个名为 MyDevice 的类,我用它来与硬件通信。该硬件是可选的,实例变量也是可选的: var theDevice:MyDevice = nil 然后,在应用程序中,我必须初始化设备
我是一名优秀的程序员,十分优秀!