Scott Zhong wrote:
Right now it is comparing pointer addresses?

Correct.

__lo and __hi are pointers
and the operation <  for(; __lo < __hi; ++__lo; ++__hi)  > would step
through until the memory address __lo is greater than or equal to __hi?

Correct. It's the same thing as rewriting the loop like this:

    for (ptrdiff_t = __i = 0; i < __hi - __lo; ++__i)
        __dest [__i] = narrow (__lo [__i], __dfault);

The only difference between the two forms might be efficiency.
In general, I think the pointer version lends itself to being
optimized to more efficient code because, unlike the indexed
form, it doesn't require that the address of each element be
computed for each access.

The assembly code generated by Sun C++ 5.8 for SPARC at -O3
for these two functions is below. Notice how foo() is more
compact and that it contains only two add instructions in
the body of the loop while bar contains three.

int f (int, int);

void foo (const int *lo, const int *hi, int *dst, int def) {
    for ( ; lo < hi; ++lo, ++dst)
        *dst = f (*lo, def);
}

void bar (const int *lo, const int *hi, int *dst, int def) {
    for (long i = 0; i < hi - lo; ++i)
        dst [i] = f (lo [i], def);
}

                        __1cDfoo6Fpki1pii_v_:
/* 000000          4 */         save    %sp,-96,%sp
/* 0x0004          5 */         cmp     %i0,%i1
/* 0x0008            */         bcc,pn  %icc,.L77000026
/* 0x000c            */         nop
/* 0x0010          6 */         ld      [%i0],%o0
                        .L900000109:
/* 0x0014 6 */ call __1cBf6Fii_i_ ! params = %o0 %o1
/* 0x0018            */         or      %g0,%i3,%o1
/* 0x001c            */         st      %o0,[%i2]
/* 0x0020            */         add     %i0,4,%i0
/* 0x0024            */         add     %i2,4,%i2
/* 0x0028            */         cmp     %i0,%i1
/* 0x002c            */         bcs,a,pt        %icc,.L900000109
/* 0x0030            */         ld      [%i0],%o0
                        .L77000026:
/* 0x0034          6 */         ret     ! Result =
/* 0x0038            */         restore %g0,%g0,%g0


                        __1cDbar6Fpki1pii_v_:
/* 000000         10 */         save    %sp,-96,%sp
/* 0x0004         11 */         sub     %i1,%i0,%i4
/* 0x0008         10 */         or      %g0,%i0,%i5
/* 0x000c         11 */         sra     %i4,31,%i0
/* 0x0010            */         and     %i0,3,%l7
/* 0x0014            */         add     %i4,%l7,%l6
/* 0x0018            */         sra     %l6,2,%l5
/* 0x001c            */         cmp     %l5,0
/* 0x0020            */         ble,pn  %icc,.L77000037
/* 0x0024            */         add     %l5,-1,%l4
/* 0x0028         12 */         ld      [%i5],%o0
/* 0x002c         11 */         or      %g0,0,%l3
                        .L900000204:
/* 0x0030 12 */ call __1cBf6Fii_i_ ! params = %o0 %o1
/* 0x0034            */         or      %g0,%i3,%o1
/* 0x0038            */         st      %o0,[%i2]
/* 0x003c            */         add     %l3,1,%l3
/* 0x0040            */         add     %i5,4,%i5
/* 0x0044            */         add     %i2,4,%i2
/* 0x0048            */         cmp     %l3,%l4
/* 0x004c            */         ble,a,pt        %icc,.L900000204
/* 0x0050            */         ld      [%i5],%o0
                        .L77000037:
/* 0x0054         12 */         ret     ! Result =
/* 0x0058            */         restore %g0,%g0,%g0

Martin


Yu (Scott) Zhong
Consulting Engineer
Rogue Wave Software, a Quovadxtm division
[EMAIL PROTECTED]
ph: 303 545 3182
-----Original Message-----
From: Martin Sebor [mailto:[EMAIL PROTECTED] Sent: Wednesday, April 04, 2007 5:29 PM
To: [email protected]
Subject: Re: ctype index?

Scott Zhong wrote:
in the for loop, why __lo char_type length isn't being tested?

I'm not sure what you mean. What length isn't being tested
and why should it be?

Martin

in _ctype.cc line 141:

template <class _CharT>
/* virtual */ const _TYPENAME ctype<_CharT>::char_type*
ctype<_CharT>::
do_narrow (const char_type *__lo, const char_type *__hi,
           char __dfault, char *__dest) const
{
    _RWSTD_ASSERT (__lo <= __hi);
    _RWSTD_ASSERT (!__lo || __dest);

    for (; __lo < __hi; ++__lo, ++__dest)
        *__dest = narrow (*__lo, __dfault);

    return __hi;
}


Yu (Scott) Zhong




Reply via email to