/*
 * This file was generated automatically by xsubpp version 1.937 from the 
 * contents of Io.xs. Do not edit this file, edit Io.xs instead.
 *
 *	ANY CHANGES MADE HERE WILL BE LOST! 
 *
 */


/* 
   Io.xs   Under construction

*/

#include "EXTERN.h"   /* std perl include */
#include "perl.h"     /* std perl include */
#include "XSUB.h"     /* XSUB include */

#include "pdl.h"      /* Data structure declarations */
#include "pdlcore.h"  /* Core declarations */
 
#include "pdlio.h"     /* Local decs */

/* Return a integer or numeric scalar as approroate */

#define SET_RETVAL_NV x.datatype<PDL_F ? (RETVAL=newSViv( (IV)result )) : (RETVAL=newSVnv( result ))

static Core* PDL; /* Structure hold core C functions */
SV* CoreSV;       /* Get's pointer to perl var holding core structure */

XS(XS_PDL__Io_isbigendian)
{
    dXSARGS;
    if (items != 0)
	croak("Usage: PDL::Io::isbigendian()");
    {
	int	RETVAL;
     unsigned short i;
     unsigned char *b;

     i = 42; b = (unsigned char*) (void*) &i;

     if (*b == 42) 
        RETVAL = 0;
     else if (*(b+1) == 42) 
        RETVAL = 1;
     else
         croak("Impossible - machine is neither big nor middle endian!!\n");
	ST(0) = sv_newmortal();
	sv_setiv(ST(0), (IV)RETVAL);
    }
    XSRETURN(1);
}

XS(XS_PDL__Io_bswap2)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: PDL::Io::bswap2(x)");
    {
	pdl*	x = SvPOK(ST(0)) ? pdl_frombuff(PDL, ST(0)) : PDL->SvPDLV(ST(0));
      int i;
      short *aa = (short*) x->data; short bb;
      unsigned char *a,*b;
      int n = x->nvals * PDL->howbig(x->datatype) / sizeof(short);
      for(i=0;i<n; i++) {
         bb = aa[i]; a = (unsigned char*) (void*) (aa+i);  
         b = (unsigned char*) &bb;
         *a = *(b+1);  *(a+1) = *b; 
     }
    }
    XSRETURN(1);
}

XS(XS_PDL__Io_bswap4)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: PDL::Io::bswap4(x)");
    {
	pdl*	x = SvPOK(ST(0)) ? pdl_frombuff(PDL, ST(0)) : PDL->SvPDLV(ST(0));
      int i;
      long *aa = (long*) x->data; long bb;
      unsigned char *a,*b;
      int n = x->nvals * PDL->howbig(x->datatype) / sizeof(long);
      for(i=0;i<n; i++) {
         bb = aa[i]; a = (unsigned char*) (void*) (aa+i);  
         b = (unsigned char*) &bb;
         *a = *(b+3);  *(a+1) = *(b+2);  *(a+2) = *(b+1); *(a+3) = *b; 
     }
    }
    XSRETURN(1);
}

XS(XS_PDL__Io_bswap8)
{
    dXSARGS;
    if (items != 1)
	croak("Usage: PDL::Io::bswap8(x)");
    {
	pdl*	x = SvPOK(ST(0)) ? pdl_frombuff(PDL, ST(0)) : PDL->SvPDLV(ST(0));
      int i;
      double *aa = (double*) x->data; double bb;
      unsigned char *a,*b;
      int n = x->nvals * PDL->howbig(x->datatype) / sizeof(double);
      for(i=0;i<n; i++) {
         bb = aa[i]; a = (unsigned char*) (void*) (aa+i);  
         b = (unsigned char*) &bb;
         *a     = *(b+7);  *(a+1) = *(b+6);  *(a+2) = *(b+5); *(a+3) = *(b+4); 
         *(a+4) = *(b+3);  *(a+5) = *(b+2);  *(a+6) = *(b+1); *(a+7) = *b; 
     }
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS(boot_PDL__Io)
{
    dXSARGS;
    char* file = __FILE__;

    XS_VERSION_BOOTCHECK ;

        newXS("PDL::Io::isbigendian", XS_PDL__Io_isbigendian, file);
        newXS("PDL::Io::bswap2", XS_PDL__Io_bswap2, file);
        newXS("PDL::Io::bswap4", XS_PDL__Io_bswap4, file);
        newXS("PDL::Io::bswap8", XS_PDL__Io_bswap8, file);

    /* Initialisation Section */


   /* Get pointer to structure of core shared C routines */

   CoreSV = perl_get_sv("PDL::SHARE",FALSE);  /* SV* value */
   if (CoreSV==NULL)
      croak("This module requires use of PDL::Core first");

   PDL = (Core*) (void*) SvIV( CoreSV );  /* Core* value */


    /* End of Initialisation Section */

    ST(0) = &sv_yes;
    XSRETURN(1);
}