/*=================================================================
 *
 * SETROUND.C   Change Rounding Mode	
 *
 * The calling syntax is:
 *
 *		setround(n)
 *
 *              if n=0, round to the nearest
 *              if n=1, round to the +INF
 *              if n=-1, round to the -INF
 *            
 * This is a MEX-file for MATLAB.  
 * Copyright 2003 Shin'ichi OISHI 
 *
 *=================================================================*/
/* $Revision: 1.10 $ */
#include <math.h>
#include "mex.h"
#include <stdio.h>
#ifdef __ppc__
/*  set rounding to round toward - infinity*/
#define rounddn  __asm__ ("mtfsfi 7,3") ;

/*  set rounding to round toward +infinity*/
#define roundup   __asm__ ("mtfsfi 7,2") ;

/* set rounding to round toward nearest  */
#define roundnr  __asm__ ("mtfsfi 7,0") ;

#endif  /* def ppc */

static void setround(double x)
{
   if(x == 0.) {
      roundnr;
   } else {
      if(x == 1.) {
         roundup;
      } else {
         rounddn;
      }
   }
}

void mexFunction( int nlhs, mxArray *plhs[], 
		  int nrhs, const mxArray*prhs[] )
     
{ 
    int m, n; 
    double *x, k;
    
    /* Check for proper number of arguments */
    
    if (nrhs != 1) { 
	mexErrMsgTxt("One input arguments required."); 
    } else if (nlhs > 0) {
	mexErrMsgTxt("Too many output arguments."); 
    } 
    
    /* Check the dimensions of Y.  Y can be 4 X 1 or 1 X 4. */ 
    
    m = mxGetM(prhs[0]); 
    n = mxGetN(prhs[0]);
    
    x = mxGetPr(prhs[0]); 
    k = *x;
    if (k > 1 || k < -1) {
	mexErrMsgTxt("SETROUND requires INT 0 or 1 or -1."); 
    }
        
    setround(k); 
    return;
    
}