Re: [问题] 九九乘法表不用循环是叫我直接从1列到81?

楼主: cole945 (跶跶..)   2018-07-22 19:15:53
用 template specialization 做 recursion 直接全展开,
连 runtime branch 都不会用到
乘法计算也是 compile time 用 template recursion, 不会有重复计算的问题
若要用 runtime cache, 可以搭 local static variable 来做 cache
因为是 compiler 计算, 所以不论计算方法, 没有 performance 的问题,
不过这里还是用 binary shift-add 来计算, 只需要做乘数的 ones-bit 次数的shift-add
再利用 pop-count 取 ones 较少的值当乘数可减少 shift-add 的次数, 例如
8 * 7 = (8) + (8 << 1) + (8 << 2)
7 * 8 = (7 << 3)
整个99乘法表只要做 26 次 ADD (吧?)
有开 optimization, 应该编完是直接展开成 printf call
leaq .LC1(%rip), %rsi
movl $24, %r8d
movl $6, %ecx
movl $4, %edx
movl $1, %edi
xorl %eax, %eax
call [email protected] // printf ("%d x %d = %2d; ", 4, 6, 24);
leaq .LC1(%rip), %rsi
movl $28, %r8d
movl $7, %ecx
movl $4, %edx
movl $1, %edi
xorl %eax, %eax
call [email protected] // printf ("%d x %d = %2d; ", 4, 7, 28);
作者: Killercat (杀人猫™)   2018-07-22 21:47:00
连int2type都出来了..... XDmodern c++ design也好几年没翻了(远目
作者: nowar100 (抛砖引玉)   2018-07-22 22:30:00
推 cole945下面给 LPH 接力
楼主: cole945 (跶跶..)   2018-07-23 00:16:00
原本想把结果cache在class member才写的这么恶搞..用class template就不会写这么绕了XD cache拿掉忘了改

Links booklink

Contact Us: admin [ a t ] ucptt.com