[SciPy-dev] fft code for testing

Chuck Harris Chuck.Harris at sdl.usu.edu
Sat May 31 12:23:01 CDT 2003


Hi Pearu,

> Btw, does anybody know an efficient way how to test if
> an integer is a power-of-two in C? Currently djbfft wrapper
> uses
>   switch (n) {
>     case 2:;case 4:;... case 8192: ..
>   }
> but there must be a better way.

Fooling around a bit, I think the construction

int foo(int n)
{
    int ok = 1;

    if ( n == 1 ||
	 n == 2 ||
	 n == 4 ||
	 n == 8 );
    else
        ok = 0;
    return ok;
}

produces the best looking assembly here. gcc optimizes the
case statement without knowing that:

1) there will usually be a match,
2) it should be faster for small numbers than large numbers.

That said, it probably doesn't matter much. For the curious the
assembler looks like :

foo:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        movl    $1, -4(%ebp)
        cmpl    $1, 8(%ebp)
        je      .L4
        cmpl    $2, 8(%ebp)
        je      .L4
        cmpl    $4, 8(%ebp)
        je      .L4
        cmpl    $8, 8(%ebp)
        je      .L4
        movl    $0, -4(%ebp)
.L4:
        movl    -4(%ebp), %eax
        leave
        ret

Just how I would do it. Extremely unusual, really.

Chuck
_______________________________________________
Scipy-dev mailing list
Scipy-dev at scipy.net
http://www.scipy.net/mailman/listinfo/scipy-dev



-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 2869 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/scipy-dev/attachments/20030531/fed329af/attachment.bin 


More information about the Scipy-dev mailing list