This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: std::pow implementation


On 29 Jul 2003, Steven Bosscher wrote:

> Op di 29-07-2003, om 14:55 schreef Richard Guenther:
> > double foo2(double x)
> > {
> >   pow(x, 2);
> > }
>
> Not that it didn't even unroll the loop at all.
>
> I have no idea if it makes any difference, I'm just curious: What
> happens if you replace
>
>       while (__n >>= 1)
>
> with
>       while (__n = __n / 2)
> ?
>
> Maybe the loop stuff can't handle shifts...

I get it to unroll with

  template<typename _Tp>
    inline _Tp
    __cmath_power(_Tp __x, unsigned int __n)
    {
      _Tp __y = __n & 1 ? __x : 1;
      const int ni = __n == 0 ? 0 : sizeof(unsigned int)*8 - __builtin_clz(__n);
      for (int i=0; i<ni; ++i)
        {
          __n >>= 1;
          __x = __x * __x;
          if (__n & 1)
            __y = __y * __x;
        }

      return __y;
    }

But neither the __n >>= 1; (or __n /= 2), not the following bit test
is constant folded and we end up with

_Z4foo2d:
.LFB12:
        pushl   %ebp    #
.LCFI4:
        movl    %esp, %ebp      #,
.LCFI5:
        fldl    8(%ebp) # x
        movl    $2, %eax        #, __n
        fld1
        fxch    %st(1)  #
        shrl    %eax    # tmp91
        movb    %al, %dl        #, tmp93
        fmul    %st(0), %st     #,
        andb    $1, %dl #, tmp93
        testb   %dl, %dl        # tmp93
        je      .L92    #,
        fmul    %st, %st(1)     #,
.L92:
        shrl    %eax    # __n
        movb    %al, %dl        #, tmp93
        fmul    %st(0), %st     #,
        andb    $1, %dl #, tmp93
        testb   %dl, %dl        # tmp93
        je      .L97    #,
        fmulp   %st, %st(1)     #,
        jmp     .L80    #
        .p2align 4,,7
.L97:
        fstp    %st(0)  #
.L80:
        popl    %ebp    #
        ret

It seems this fact is worth another PR.

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]