[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Xen-devel] libxc: Casting of xen virtual address type xen_vaddr_t to signed int64 type: (int64_t)vaddr



On Mon, May 20, 2019 at 12:22 PM Andrew Cooper
<andrew.cooper3@xxxxxxxxxx> wrote:
>
> On 20/05/2019 10:17, Viktor Mitin wrote:
> >>> Mean that result of "(int64_t)vaddr >> 63" can be 0 or 1.
> >>> So the next code may not work properly in case of another 
> >>> 'implementations'.
> >>> With another compiler (i.e. clang, etc) this code may introduce bugs
> >>> which are hard to find.
> >>>
> >>> ((int64_t)vaddr >> 47) == ((int64_t)vaddr >> 63)
> >>>
> >>> For this reason it is better to avoid implementation-defined code.
> >> Well, ideally we'd like to get away without using implementation
> >> defined behavior. But I'm afraid we're quite far from that, and
> >> we'd not always be willing to accept the worse source and/or
> >> binary code that would be needed to avoid it.
> > How about using the next one-liner to avoid implementation-defined code :
> >
> > //Returns true in case when the top left 17 bits are all zero or are all one
> > return ((!(vaddr >> 47)) || (((vaddr >> 47)&0x1FFFF) == 0x1FFFF))
>
> Have you tried compiling these two examples and seeing how they differ?
>

Yes, I've tried it with the next draft test code.
The result looks ok, however it takes time to check all 64bits values
one by one...
Do you see any issues with new one-liner?

//compile line: clang -fopenmp -g  b.c  && time ./a.out
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    unsigned long  vaddr_max = 0xffffffffffffffff;
    typedef signed long int64_t;

    unsigned long i;
#pragma omp parallel for private(i)
    for (i = 0 ; i != vaddr_max; i=i+0xffffffff)
    {
//      printf("i = 0x%lx \n", i );
        int res_orig = ((int64_t)i >> 47) == ((int64_t)i >> 63) ;

        unsigned long  v = i >> 47;
        int res_new = ((!v) || ((v&0x1FFFF) == 0x1FFFF));

        if ( res_orig != res_new )
        {
            printf("res_orig = %d \n", res_orig);
            printf("res_new  = %d \n", res_new);
            printf("i = %lx \n", i);
            printf("v = %lx \n", v);
            printf("(v&0x1FFFF) = %lx \n", (v&0x1FFFF) );
            printf("(v&0x1FFFF)  == v = %d \n", (v&0x1FFFF) == v );
            printf("i >> 47 = %lx \n", i >> 47);
            printf("(int64_t)i >> 47 = %lx \n", (int64_t)i >> 47);
            printf("wrong i = 0x%lx \n", i);
            exit(1);
        }
    }

    return 0;
}

Thanks

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.