[Numpy-discussion] RE: Numpy

Lee Taylor taylor at rhino.llnl.gov
Wed Dec 6 13:22:17 EST 2000


I think a more portable solution is to just make it all an if statement.
I'm not comfortable with presuming that floats are always 4 bytes.


        if (src_nd == 0 && dest_nd == 0) {
            if (elsize == sizeof(char)) {
                memset(dest, *src, copies);
            } else if (elsize == sizeof(short)) {
                for(j=copies; j; --j, dest += sizeof(short))
                    *(short*)dest = *(short*)src;
            } else if (elsize == sizeof(int)) {
                for(j=copies; j; --j, dest += sizeof(int))
                    *(int*)dest = *(int*)src;
            } else if (elsize == sizeof(long)) {
                for(j=copies; j; --j, dest += sizeof(long))
                    *(long*)dest = *(long*)src;
            } else if (elsize == sizeof(float)) {
                for(j=copies; j; --j, dest += sizeof(float))
                    *(float*)dest = *(float*)src;
            } else if (elsize == sizeof(double)) {
                for(j=copies; j; --j, dest += sizeof(double))
                    *(double*)dest = *(double*)src;
            } else {
                for(j=copies; j; --j, dest += elsize)
                    memcpy(dest, src, elsize);
            }


I've thrown in the int and float cases to make sure we cover every
machine that may be out there.

Lee Taylor

On Wed, 6 Dec 2000 rlw at stsci.edu wrote:

> A suggestion for an easy way to fix this problem: use float instead of
> long.  Presumably floats are always 4 bytes.
> 				Rick
> 
> Paul Dubois writes:
> 
> >This was a patch that I put in before making this release. If you get 17.1.1
> >it won't be in there.
> >
> > [...]
> >
> >cc: Error: Src/arrayobject.c, line 79: The switch statement containing this
> >case label already has a case label for "8". (dupcase)
> >            case sizeof(double):
> >------------^
> >error: command 'cc' failed with exit status 1
> >tc05:/usr/tmp/chase/Numeric-17.1.2[408]
> >
> >
> >int do_sliced_copy(char *dest, int *dest_strides, int *dest_dimensions,
> >                   int dest_nd, char *src, int *src_strides,
> >                   int *src_dimensions, int src_nd, int elsize,
> >                   int copies) {
> >        int i, j;
> >
> >        if (src_nd == 0 && dest_nd == 0) {
> >            switch(elsize) {
> >            case sizeof(char):
> >                memset(dest, *src, copies);
> >                break;
> >            case sizeof(short):
> >                for(j=copies; j; --j, dest += sizeof(short))
> >                    *(short*)dest = *(short*)src;
> >                break;
> >            case sizeof(long):
> >                for(j=copies; j; --j, dest += sizeof(int))
> >                    *(int*)dest = *(int*)src;
> >                break;
> >            case sizeof(double):
> >                for(j=copies; j; --j, dest += sizeof(double))
> >                    *(double*)dest = *(double*)src;
> >                break;
> >            default:
> >                for(j=copies; j; --j, dest += elsize)
> >                    memcpy(dest, src, elsize);
> >            }
> >            return 0;
> >        }
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> http://lists.sourceforge.net/mailman/listinfo/numpy-discussion
> 




More information about the NumPy-Discussion mailing list