- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 gmplib 获取大数并计算数值(数字总和:123
-> 6
, 74
-> 11
-> 2
)
这是我所做的:
unsigned short getnumericvalue(const char *in_str)
{
unsigned long number = 0;
const char *ptr = in_str;
do {
if (*ptr != '9') number += (*ptr - '0'); // Exclude '9'
ptr++;
} while (*ptr != 0);
unsigned short reduced = number % 9;
return reduced == 0 ? 9 : reduced;
}
它运行良好,但有没有更快的方法可以在 Xeon w-3235 上实现这一点?
最佳答案
您可以使用如下代码。算法的总体思路是:
请注意,下面的代码尚未经过测试。
// getnumericvalue(ptr)
.section .text
.type getnumericvalue, @function
.globl getnumericvalue
getnumericvalue:
xor %eax, %eax // digit counter
// process string until we reach cache-line alignment
test $64-1, %dil // is ptr aligned to 64 byte?
jz 0f
1: movzbl (%rdi), %edx // load a byte from the string
inc %rdi // advance pointer
test %edx, %edx // is this the NUL byte?
jz .Lend // if yes, finish this function
sub $'0', %edx // turn ASCII character into digit
add %edx, %eax // and add to counter
test $64-1, %dil // is ptr aligned to 64 byte?
jnz 1b // if not, process more data
// process data in cache line increments until the end
// of the string is found somewhere
0: vpbroadcastd zero(%rip), %zmm1 // mask of '0' characters
vpxor %xmm3, %xmm3, %xmm3 // vectorised digit counter
vmovdqa32 (%rdi), %zmm0 // load one cache line from the string
vptestmb %zmm0, %zmm0, %k0 // clear k0 bits if any byte is NUL
kortestq %k0, %k0 // clear CF if a NUL byte is found
jnc 0f // skip loop if a NUL byte is found
.balign 16
1: add $64, %rdi // advance pointer
vpsadbw %zmm1, %zmm0, %zmm0 // sum groups of 8 bytes into 8 words
// also subtracts '0' from each byte
vpaddq %zmm3, %zmm0, %zmm3 // add to counters
vmovdqa32 (%rdi), %zmm0 // load one cache line from the string
vptestmb %zmm0, %zmm0, %k0 // clear k0 bits if any byte is NUL
kortestq %k0, %k0 // clear CF if a NUL byte is found
jc 1b // go on unless a NUL byte was found
// reduce 8 vectorised counters into rdx
0: vextracti64x4 $1, %zmm3, %ymm2 // extract high 4 words
vpaddq %ymm2, %ymm3, %ymm3 // and add them to the low words
vextracti128 $1, %ymm3, %xmm2 // extract high 2 words
vpaddq %xmm2, %xmm3, %xmm3 // and add them to the low words
vpshufd $0x4e, %xmm3, %xmm2 // swap qwords into xmm2
vpaddq %xmm2, %xmm3, %xmm3 // and add to xmm0
vmovq %xmm3, %rdx // move digit counter back to rdx
add %rdx, %rax // and add to counts from scalar head
// process tail
1: movzbl (%rdi), %edx // load a byte from the string
inc %rdi // advance pointer
test %edx, %edx // is this the NUL byte?
jz .Lend // if yes, finish this function
sub $'0', %edx // turn ASCII character into digit
add %rdx, %rax // and add to counter
jnz 1b // if not, process more data
.Lend: xor %edx, %edx // zero-extend RAX into RDX:RAX
mov $9, %ecx // divide by 9
div %rcx // perform division
mov %edx, %eax // move remainder to result register
test %eax, %eax // is the remainder zero?
cmovz %ecx, %eax // if yes, set remainder to 9
vzeroupper // restore SSE performance
ret // and return
.size getnumericvalue, .-getnumericvalue
// constants
.section .rodata
.balign 4
zero: .byte '0', '0', '0', '0'
关于c - 计算大数数字和的最快方法(作为十进制字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63684581/
我正在尝试编写一个程序,该程序接受十六进制、八进制和小数的输入,将它们存储在整数变量中,并将它们连同它们转换为十进制形式一起输出。例如: 用户输入:0x43、0123、65 程序输出: 0x43 he
var re = /^([0-9]*)(\.[0-9]{2})$/ re.test(.22) true re.test(.20) false re.test(10.02) true re.test(1
我有一个类型为BigDecimal的属性“initialPrice”的“Trade”类。该属性可以 根据另一个类别“Symbol”中包含的属性“decimals”,小数位数会有所不同,因此需要使用不同
我是这里编码的新手。 我正在尝试使用编码来显示平均值、最大值和最小值的统计信息。 我遇到了错误,他们说我无法将 decimal[] 转换为 int[]。我在我的代码旁边评论了下面的错误。这是最后几行之
在不久的将来,我一直在研究小数类型以获得一些可能的编程乐趣,并希望将它用作比 Int64 更大的整数。一个关键点是我需要找出我可以安全地存储为小数(不丢失精度)的最大整数;我这样说,因为显然它在那里使
所以我这样做了: for(i=1;i0;i--) { if(MASKBINARY[i-1]==1 && MASKBINARY[i]==0) // check if the next eleme
我正在尝试将数字四舍五入到最接近的指定小数。 我希望它四舍五入到小数点 .11、.22、.33、.44、.55、.66、.77、.88 但不是 .99。相反,0.99 应该是 1。 例如: 1.14
获取 python (python 3) Decimal 的整数部分和小数部分的最有效方法是什么? 这是我现在拥有的: from decimal import * >>> divmod(Decimal
我有一个使用十进制列表样式的 ol: ol li { list-style-type: decimal; } First Second 编号工作正常,但如何删除句点?而不是显示: 1.
将字节数组转换为具有所选基数的字符串的最佳方法是什么? S.O. 上有大量示例。和其他地方转换为十六进制字符串。我在这里主要感兴趣的是将十六进制或十进制字符串转换为其他;也是一种更通用的方式。 这是我
不清楚十进制类型的 sizeof。以字节为单位的大小是否像在 sql server 中一样因精度而异? c# 类型的精度变量是“十进制”吗? 我不想打开不安全的代码来只调用小数类型的 sizeof。你
我必须向十进制值添加尾随零。不仅用于显示(因此 Format 不是一个选项),而且用于实际的底层数据,因为小数精度在我们的应用程序中很重要。 我试过: decimal value = 1M decim
我正在使用以下代码行将 decimal 转换为 string: decimal a = 0; a.ToString(); Resharper 给了我以下警告:“明确指定字符串区域性”。我想这是有道理的
我注意到 .NET 在涉及小数和尾随零时有一些古怪/不直观的行为。 0m == 0.000m //true 0.1m == 0.1000m //true 但是 (0m).ToString() == (
最近写单片机 RTC 日期、时间配置,需要实现十进制、BCD码互换,将示例Demo分享给各位朋友: BCD是指用二进制来表示十进制数的编码,即:用4位二进制来表示一位十进制数,因此4位二进制数
这似乎是重复的,但我找不到合适的答案(问题足够接近但是..)我有一个代表十进制数的字符串,它总是有很多小数位,至少 20,有时最多 2000 (代表特定的验证计算,即像'是数字 135 到 147 素
我想知道我是否会收到这样的问题: "Convert a decimal number to two's complement, then give your answer in Hex". 下面是路径
此存储过程不返回十进制格式 00.00 的薪水 ALTER PROCEDURE taxable_varsalary @emp_code bigint, @co_id bigint AS de
使用 leaflet R 包,有没有办法将 zoom 设置为非整数(即十进制)值? 也就是说,介于这两个缩放级别之间: 例子 随着 zoom 从第 3 级移动到第 4 级,它看起来好像正在四舍五入到最
我如何编写一个函数来将十进制数字符串转换为十进制数并将十进制数转换为字符串? 最佳答案 有非常方便的 clojure 函数可以将任何内容转换为字符串以及将类似数字的内容转换为 BigDecimal:
我是一名优秀的程序员,十分优秀!