Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At my machine <code>kroundup32</code> gives 6.000m rounds/sec<br> And next function gives 7.693m rounds/sec</p> <pre><code>inline int scan_msb(int x) { #if defined(__i386__) || defined(__x86_64__) int y; __asm__("bsr %1, %0" : "=r" (y) : "r" (x) : "flags"); /* ZF */ return y; #else #error "Implement me for your platform" #endif } inline int roundup32(int x) { if (x == 0) return x; else { const int bit = scan_msb(x); const int mask = ~((~0) &lt;&lt; bit); if (x &amp; mask) return (1 &lt;&lt; (bit+1)); else return (1 &lt;&lt; bit); } } </code></pre> <p>So @thomasrutter I woudn't say that it is "highly optimized".</p> <p>And appropriate (only meaningful part) assembly (for GCC 4.4.4):</p> <pre><code>kroundup32: subl $1, %edi movl %edi, %eax sarl %eax orl %edi, %eax movl %eax, %edx sarl $2, %edx orl %eax, %edx movl %edx, %eax sarl $4, %eax orl %edx, %eax movl %eax, %edx sarl $8, %edx orl %eax, %edx movl %edx, %eax sarl $16, %eax orl %edx, %eax addl $1, %eax ret roundup32: testl %edi, %edi movl %edi, %eax je .L6 movl $-1, %edx bsr %edi, %ecx sall %cl, %edx notl %edx testl %edi, %edx jne .L10 movl $1, %eax sall %cl, %eax .L6: rep ret .L10: addl $1, %ecx movl $1, %eax sall %cl, %eax ret </code></pre> <p>By some reason I haven't found appropriate implementation of <code>scan_msb</code> (like <code>#define scan_msb(x) if (__builtin_constant_p (x)) ...</code>) within standart headers of GCC (only <a href="http://lists.freebsd.org/pipermail/freebsd-hackers/2007-September/021846.html" rel="nofollow noreferrer"><code>__TBB_machine_lg</code>/<code>__TBB_Log2</code></a>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload