Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ill-formed reference to pointer

Reply
Thread Tools

ill-formed reference to pointer

 
 
m0shbear
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 4:44*am, Ian Collins <(E-Mail Removed)> wrote:
> On 01/30/11 09:26 PM, m0shbear wrote:
>
>
>
> > On Jan 30, 2:20 am, Ian Collins<(E-Mail Removed)> *wrote:
> >> On 01/30/11 06:58 PM, m0shbear wrote:
> >>> On Jan 30, 12:38 am, Ian Collins<(E-Mail Removed)> * *wrote:
> >>>> On 01/30/11 06:03 PM, m0shbear wrote:

>
> >>>>> #define XF64(dst,src) *PTR_CAST(u64be,dst) ^= *PTR_CAST(u64be
> >>>>> const,src); (src) += 8

>
> >>> The macro I'm trying to convert into an inline function.
> >>> What I'm doing is a wordsize-optimized endian-agnostic XOR of a set of
> >>> bytes, to minimize the amount of instructions generated. And the C++
> >>> version of the C-style cast maps to reinterpret_cast.

>
> >> Why don't you just write

>
> >> template<typename S, typename D>
> >> void xf64( S* dst, const D* src )
> >> {
> >> * * *reinterpret_cast<uint64_t*>(dst) ^= *reinterpret_cast<const
> >> uint64_t*>(src);
> >> * * src += 8;

>
> >> }

>
> >> Which is closer to your macro?

>
> > Nope: src will be unchanged since it's copied by value instead of by
> > reference.
> > In the macro, the ability to assume that the src token is a reference
> > is a precondition.
> > In any case, the function's arguments are (uint8_t *, const uint8_t
> > *), so it works. I was just playing around with constness and
> > references and got bit.

>
> OK,
>
> const uint8_t* xf64( uint8_t* dst, const uint8_t* src )
> {
> * **reinterpret_cast<uint64_t*>(dst) ^= *reinterpret_cast<const
> uint64_t*>(src);
> * *return src + 8;
>
> }
>
> But wouldn't you want both src and dst to advance?


Suppose dst points to a memory-mapped register, and src to an array.
Then dst points to the xor-reduction of src.

The goal is to evaulate the expression, where [x:y] is byte range:
*dst = *src[0:7] ^ *src[8:15] ^ ... ^ *src[n-8:n-1].

So it is useful to advance src, but not dst.

However, the double-or-nothing approach is also useful. This allows
for the elimination of the reference and simply use a pointer, and do
the post-increment of src in parallel with the increment of the loop
counter. It will also potentially stop enough off-by-one errors to
make it worth spending a minute re-coding.

> >>> Now why does the compiler auto-constify any non-const pointer used as
> >>> the second argument in memcpy() but refuses to do so here?

>
> >> Because the parameter is a reference.

>
> > References do a damn good job of killing off C tricks I've learned
> > over the years

>
> You'd hit the same problem with pointers to pointers.


Actually, no. I've managed to hack it into working with const_cast and
reinterpret_cast.

 
Reply With Quote
 
 
 
 
m0shbear
Guest
Posts: n/a
 
      01-30-2011
On Jan 30, 5:12*am, Vaclav Haisman <(E-Mail Removed)> wrote:
> m0shbear wrote, On 30.1.2011 6:03:
>
> > I'm trying to use this code to check the compiler's output to see if
> > the inlining is aggressive enough to use inline functions instead of
> > #defines and I'm getting the following error: "invalid initialization
> > of reference of type ‘const unsigned char*&’ from expression of type
> > ‘u8*’".
> > Does the inline lowercase match the macro uppercase?

>
> > code:
> > extern "C" {
> > #include <stdint.h>
> > }

>
> > typedef uint8_t u8;
> > typedef uint64_t u64be;

>
> > template <typename T>
> > struct itype {
> > * *typedef T value;
> > * *typedef T& reference;
> > * *typedef T const& const_reference;
> > * *typedef T* pointer;
> > * *typedef T const* const_pointer;
> > };

>
> > #define PTR_CAST(T, p) (reinterpret_cast<T*>(p))

>
> > #define XF64(dst,src) *PTR_CAST(u64be,dst) ^= *PTR_CAST(u64be
> > const,src); (src) += 8

>
> > template<typename T, typename P> inline T* ptr_cast(P* p) { return
> > reinterpret_cast<T*>(p); }

>
> > inline void xf64(itype<u8>:ointer dst,
> > itype<itype<u8>::const_pointer>::reference src) {
> > * **ptr_cast<u64be>(dst) ^= *ptr_cast<u64be const>(src); src += 8;
> > }

>
> > int main() {
> > * *u8 fop[64], foq[64], fos[64],fot[64];
> > * *u8* foP=fop;
> > * *u8* foQ=foq;
> > * *u8* foS=fos;
> > * *u8* foT=fot;
> > * *xf64(foP,foQ);
> > * *XF64(foS, foT);

>
> Do you realise that casting from this u8* to u64be* and then accessing the
> value as if it were u64be* is undefined behaviour?
>


A contiguous region of memory is a contiguous region of memory. If the
address is 8/16-aligned, going from byte access to 64-bit access has a
noticeable performance boost. It's like using 64-bit copy/cmp in mem/
strXXX() for aligned bytes and falling back to 8-bit after alignment.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments