Exemple #1
0
/* draw_object just draws a single object at a fixed position, although
this can easily be modified to allow for more objects.
bmp = bitmap to draw to. obj = sprite for the object.
angle, cx, cy define the camera position.
*/
void draw_object (BITMAP *bmp, BITMAP *obj, fixed angle, fixed cx, fixed cy, MODE_7_PARAMS params)
{
    int width, height;
    int screen_y, screen_x;

    // The object in this case is at a fixed position of (160, 100).
    // Calculate the position relative to the camera.
    fixed obj_x = itofix(160) - cx;
    fixed obj_y = itofix(100) - cy;

    // use a rotation transformation to rotate the object by the camera
    // angle
    fixed space_x = fmul (obj_x, fcos (angle)) + fmul (obj_y, fsin (angle));
    fixed space_y = -fmul (obj_x, fsin (angle)) + fmul (obj_y, fcos (angle));

    // calculate the screen coordinates that go with these space coordinates
    // by dividing everything by space_x (the distance)
    screen_x = bmp->w/2 + fixtoi (fmul (fdiv (params.scale_x, space_x), space_y));
    screen_y = fixtoi (fdiv (fmul (params.space_z, params.scale_y), space_x)) - params.horizon;

    // the size of the object has to be scaled according to the distance
    height = fixtoi (obj->h * fdiv(params.obj_scale_y, space_x));
    width = fixtoi (obj->w * fdiv(params.obj_scale_x, space_x));

    // draw the object
    stretch_sprite (bmp, obj, screen_x - width / 2, screen_y - height, width, height);
}
Exemple #2
0
void mode_7 (BITMAP *bmp, BITMAP *tile, fixed angle, fixed cx, fixed cy, MODE_7_PARAMS params)
{
    // current screen position
    int screen_x, screen_y;

    // the distance and horizontal scale of the line we are drawing
    fixed distance, horizontal_scale;

    // masks to make sure we don't read pixels outside the tile
    int mask_x = (tile->w - 1);
    int mask_y = (tile->h -1);

    // step for points in space between two pixels on a horizontal line
    fixed line_dx, line_dy;

    // current space position
    fixed space_x, space_y;

    for (screen_y = 0; screen_y < bmp->h; screen_y++)
    {
        // first calculate the distance of the line we are drawing
        distance = fdiv (fmul (params.space_z, params.scale_y),
            itofix (screen_y + params.horizon));
        // then calculate the horizontal scale, or the distance between
        // space points on this horizontal line
        horizontal_scale = fdiv (distance, params.scale_x);

        // calculate the dx and dy of points in space when we step
        // through all points on this line
        line_dx = fmul (-fsin(angle), horizontal_scale);
        line_dy = fmul (fcos(angle), horizontal_scale);

        // calculate the starting position
        space_x = cx + fmul (distance, fcos(angle)) - bmp->w/2 * line_dx;
        space_y = cy + fmul (distance, fsin(angle)) - bmp->w/2 * line_dy;

        // go through all points in this screen line
        for (screen_x = 0; screen_x < bmp->w; screen_x++)
        {
            // get a pixel from the tile and put it on the screen
            putpixel (bmp, screen_x, screen_y,
                getpixel (tile,
                    fixtoi (space_x) & mask_x,
                    fixtoi (space_y) & mask_y ));
            // advance to the next position in space
            space_x += line_dx;
            space_y += line_dy;
        }
    }
}
Exemple #3
0
/** Inverses mx.
 *  Returns materr(2) if mx isn't a square, 2x2 matrix,
 *  or materr(1) if the initialization didn't go right.
 */
matrix MatrixInv(matrix mx)
{
    if (!isValidOp(mx,mx,'i')) {
        return materr(2);
    }

    if(mx.rows == 2) {

        matrix inv = initialize(mx.rows, mx.columns);
        if(inv.mx == NULL) {
            return materr(1);
        }

        frac tmp;
        frac det = fdiv(i2f(1),
                        fsub(fmul(mx.mx[0][0], mx.mx[1][1]),
                             fmul(mx.mx[0][1], mx.mx[1][0])));

        tmp = mx.mx[0][0];
        inv.mx[0][0] = mx.mx[1][1];
        inv.mx[1][1] = tmp;

        inv.mx[0][1] = fmul(mx.mx[0][1], i2f(-1));
        inv.mx[1][0] = fmul(mx.mx[1][0], i2f(-1));
        return MatrixSMul(mx, det);
    } else {
        return materr(2);
    }
}
Exemple #4
0
/* fatan2:
 *  Like the libc atan2, but for fixed point numbers.
 */
fixed fatan2(fixed y, fixed x)
{
   fixed r;

   if (x==0) {
      if (y==0) {
	 errno = EDOM;
	 return 0L;
      }
      else
	 return ((y < 0) ? -0x00400000L : 0x00400000L);
   } 

   errno = 0;
   r = fdiv(y, x);

   if (errno) {
      errno = 0;
      return ((y < 0) ? -0x00400000L : 0x00400000L);
   }

   r = fatan(r);

   if (x >= 0)
      return r;

   if (y >= 0)
      return 0x00800000L + r;

   return r - 0x00800000L;
}
Exemple #5
0
int plane::intersect(Vec origin, Vec dir, double& t){
  double denominator = dot(norm, dir);
  if(denominator == 0) return 0;
  t = fdiv(dot((v - origin), norm),denominator);
  //t = ((dot(normal, v) + c)/denominator);
  if(t<=0) return 0;
  else return 2;

  /*
	float d = dot( normal, dir );
	if (d != 0)
	{
		double dist = -(dot( normal, origin ) + value) / d;
		if (dist > 0)
		{
		  //set an arbitrary distance limit
			if (dist < 100) 
			{
				t = dist;
				return 2;
			}
		}
	}
	return 0;*/
}
Exemple #6
0
int main()
{
    /* Lokalne varijable */
    int a, b;
    scanf("%d%d", &a, &b);
    printf("zbroj: %d\nrazlika: %d\numnozak: %d\nkolicnik: %d\nostatak: %d\n", sum(a, b), sub(a, b), mul(a, b), div(a, b), mod(a, b));
    printf("%f\n", fdiv(a, b));
    return 0;
}
Exemple #7
0
/* calc_spline:
 *  Calculates a set of pixels for the bezier spline defined by the four
 *  points specified in the points array. The required resolution
 *  is specified by the npts parameter, which controls how many output
 *  pixels will be stored in the x and y arrays.
 */
void calc_spline(int points[8], int npts, int *x, int *y)
{
   int i;
   fixed denom;

   for (i=0; i<npts; i++) {
      denom = fdiv(itofix(i), itofix(npts-1));
      x[i] = fixtoi(bezval(denom, points));
      y[i] = fixtoi(bezval(denom, points+1));
   }
}
Exemple #8
0
void shade(
	MicropolygonMesh<VertexType> const & mesh,
	TileCache & cache,
	ShaderType const & shader
)
{
	mesh.for_each_sample(
		cache.get_sample_rate(),
		[&cache, &shader] (Fragment<VertexType> const & frag) {
			int tile_length = cache.get_tile_length(),
				stride = cache.get_tile_stride();

			Vec<2, int> pixel = fdiv(frag.sample_indices, frag.sample_rate),
				tile = fdiv(pixel, tile_length),
				offset = frag.sample_indices - stride * tile;

			SampleBuffer * buffer = cache.get_tile(tile);
			shader((*buffer)(offset.x(), offset.y()), frag);
		}
	);
}
Exemple #9
0
float
fdivAdapter (float x, float y)
{
  union
  {
    float as_float;
    int32_t as_int;
  } a, b, c;
  a.as_float = x;
  b.as_float = y;
  c.as_int = fdiv(a.as_int, b.as_int);
  return c.as_float;
}
Exemple #10
0
int
main(int argc, const char *argv[])
{
    int i;
    int *mem = malloc(1100000);

    capture_stdio();
    for (i = 0;; ++i) {
	int32_t t0[2], t1[2];
	char *msg;
	int n;

	gp_get_usertime(t0);
	switch (i) {
	    case 0:
		iadd(0, n = 10000000, &msg);
		break;
	    case 1:
		imul(1, n = 1000000, &msg);
		break;
	    case 2:
		idiv(1, n = 1000000, &msg);
		break;
	    case 3:
		fadd(3.14, n = 10000000, &msg);
		break;
	    case 4:
		fmul(1.0000001, n = 10000000, &msg);
		break;
	    case 5:
		fdiv(1.0000001, n = 1000000, &msg);
		break;
	    case 6:
		fconv(12345, n = 10000000, &msg);
		break;
	    case 7:
		mfast(mem, n = 10000000, &msg);
		break;
	    case 8:
		mslow(mem, n = 1000000, &msg);
		break;
	    default:
		free(mem);
		exit(0);
	}
	gp_get_usertime(t1);
	fprintf(stdout, "Time for %9d %s = %g ms\n", n, msg,
		(t1[0] - t0[0]) * 1000.0 + (t1[1] - t0[1]) / 1000000.0);
	fflush(stdout);
    }
}
Exemple #11
0
void fmodulo(_MIPD_ flash x,flash y,flash z)
{ /* sets z=x mod y */
#ifdef MR_OS_THREADS
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return;

    MR_IN(89)
    fdiv(_MIPP_ x,y,mr_mip->w8);
    ftrunc(_MIPP_ mr_mip->w8,mr_mip->w8,mr_mip->w8);
    fmul(_MIPP_ mr_mip->w8,y,mr_mip->w8);
    fsub(_MIPP_ x,mr_mip->w8,z);
    MR_OUT
}
Exemple #12
0
int main(int argc, char* argv[])
{
  if(argc < 3)
  {
     fprintf(stderr, "%s <float> <float >\n", argv[0]);
     return(1);
  }
  	uint64_t h=1,i=1,j=1,k=1,l=1,d=1,e=1,f=1,g=1;
  float  c = fdiv(atof(argv[1]), atof(argv[2]));
  /*	uint64_t	d = read_uint64("in_data1");
	uint64_t	e = read_uint64("in_data2");
	uint64_t	f = read_uint64("in_data3");
	uint64_t	g = read_uint64("in_data4");*/
  

  fprintf(stdout,"Result = %f.\n Mantissa_a :: %lu. \nMantissa_b :: %lu. \n Man_before_while :: %lu. \n Man_after_while :: %lu. \n Man_after_shifting :: %d. \n exp_after_shifting :: %d. \n exp_after_shifting :: %d. \n exp_a :: %d. \nexp_b :: %d. \n", c,d,e,f,g,h,i,j,k,l);
  return(0);
}
Exemple #13
0
static int norm(_MIPD_ int type,flash y)
{ /* convert y to first quadrant angle *
   * and return sign of result         */
    int s;
#ifndef MR_GENERIC_MT
    miracl *mr_mip=get_mip();
#endif
    if (mr_mip->ERNUM) return 0;
    s=PLUS;
    if (size(y)<0)
    {
        negify(y,y);
        if (type!=COS) s=(-s);
    }
    fpi(_MIPP_ mr_mip->pi);
    fpmul(_MIPP_ mr_mip->pi,1,2,mr_mip->w8);
    if (fcomp(_MIPP_ y,mr_mip->w8)<=0) return s;
    fpmul(_MIPP_ mr_mip->pi,2,1,mr_mip->w8);
    if (fcomp(_MIPP_ y,mr_mip->w8)>0)
    { /* reduce mod 2.pi */
        fdiv(_MIPP_ y,mr_mip->w8,mr_mip->w9);
        ftrunc(_MIPP_ mr_mip->w9,mr_mip->w9,mr_mip->w9);
        fmul(_MIPP_ mr_mip->w9,mr_mip->w8,mr_mip->w9);
        fsub(_MIPP_ y,mr_mip->w9,y);
    }
    if (fcomp(_MIPP_ y,mr_mip->pi)>0)
    { /* if greater than pi */
        fsub(_MIPP_ y,mr_mip->pi,y);
        if (type!=TAN) s=(-s);
    }
    fpmul(_MIPP_ mr_mip->pi,1,2,mr_mip->w8);
    if (fcomp(_MIPP_ y,mr_mip->w8)>0)
    { /* if greater than pi/2 */
        fsub(_MIPP_ mr_mip->pi,y,y);
        if (type!=SIN) s=(-s);
    }
    return s;
}
static void do_emu(struct info * info)
{
	unsigned short code;
	temp_real tmp;
	char * address;

	if (I387.cwd & I387.swd & 0x3f)
		I387.swd |= 0x8000;
	else
		I387.swd &= 0x7fff;
	ORIG_EIP = EIP;
/* 0x0007 means user code space */
	if (CS != 0x000F) {
		printk("math_emulate: %04x:%08x\n\r",CS,EIP);
		panic("Math emulation needed in kernel");
	}
	code = get_fs_word((unsigned short *) EIP);
	bswapw(code);
	code &= 0x7ff;
	I387.fip = EIP;
	*(unsigned short *) &I387.fcs = CS;
	*(1+(unsigned short *) &I387.fcs) = code;
	EIP += 2;
	switch (code) {
		case 0x1d0: /* fnop */
			return;
		case 0x1d1: case 0x1d2: case 0x1d3:
		case 0x1d4: case 0x1d5: case 0x1d6: case 0x1d7:
			math_abort(info,1<<(SIGILL-1));
		case 0x1e0:
			ST(0).exponent ^= 0x8000;
			return;
		case 0x1e1:
			ST(0).exponent &= 0x7fff;
			return;
		case 0x1e2: case 0x1e3:
			math_abort(info,1<<(SIGILL-1));
		case 0x1e4:
			ftst(PST(0));
			return;
		case 0x1e5:
			printk("fxam not implemented\n\r");
			math_abort(info,1<<(SIGILL-1));
		case 0x1e6: case 0x1e7:
			math_abort(info,1<<(SIGILL-1));
		case 0x1e8:
			fpush();
			ST(0) = CONST1;
			return;
		case 0x1e9:
			fpush();
			ST(0) = CONSTL2T;
			return;
		case 0x1ea:
			fpush();
			ST(0) = CONSTL2E;
			return;
		case 0x1eb:
			fpush();
			ST(0) = CONSTPI;
			return;
		case 0x1ec:
			fpush();
			ST(0) = CONSTLG2;
			return;
		case 0x1ed:
			fpush();
			ST(0) = CONSTLN2;
			return;
		case 0x1ee:
			fpush();
			ST(0) = CONSTZ;
			return;
		case 0x1ef:
			math_abort(info,1<<(SIGILL-1));
		case 0x1f0: case 0x1f1: case 0x1f2: case 0x1f3:
		case 0x1f4: case 0x1f5: case 0x1f6: case 0x1f7:
		case 0x1f8: case 0x1f9: case 0x1fa: case 0x1fb:
		case 0x1fc: case 0x1fd: case 0x1fe: case 0x1ff:
			printk("%04x fxxx not implemented\n\r",code + 0xc800);
			math_abort(info,1<<(SIGILL-1));
		case 0x2e9:
			fucom(PST(1),PST(0));
			fpop(); fpop();
			return;
		case 0x3d0: case 0x3d1:
			return;
		case 0x3e2:
			I387.swd &= 0x7f00;
			return;
		case 0x3e3:
			I387.cwd = 0x037f;
			I387.swd = 0x0000;
			I387.twd = 0x0000;
			return;
		case 0x3e4:
			return;
		case 0x6d9:
			fcom(PST(1),PST(0));
			fpop(); fpop();
			return;
		case 0x7e0:
			*(short *) &EAX = I387.swd;
			return;
	}
	switch (code >> 3) {
		case 0x18:
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x19:
			fmul(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x1a:
			fcom(PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x1b:
			fcom(PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			fpop();
			return;
		case 0x1c:
			real_to_real(&ST(code & 7),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(0),&tmp,&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x1d:
			ST(0).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x1e:
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x1f:
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x38:
			fpush();
			ST(0) = ST((code & 7)+1);
			return;
		case 0x39:
			fxchg(&ST(0),&ST(code & 7));
			return;
		case 0x3b:
			ST(code & 7) = ST(0);
			fpop();
			return;
		case 0x98:
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0x99:
			fmul(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0x9a:
			fcom(PST(code & 7),PST(0));
			return;
		case 0x9b:
			fcom(PST(code & 7),PST(0));
			fpop();
			return;			
		case 0x9c:
			ST(code & 7).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0x9d:
			real_to_real(&ST(0),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(code & 7),&tmp,&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0x9e:
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0x9f:
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return;
		case 0xb8:
			printk("ffree not implemented\n\r");
			math_abort(info,1<<(SIGILL-1));
		case 0xb9:
			fxchg(&ST(0),&ST(code & 7));
			return;
		case 0xba:
			ST(code & 7) = ST(0);
			return;
		case 0xbb:
			ST(code & 7) = ST(0);
			fpop();
			return;
		case 0xbc:
			fucom(PST(code & 7),PST(0));
			return;
		case 0xbd:
			fucom(PST(code & 7),PST(0));
			fpop();
			return;
		case 0xd8:
			fadd(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xd9:
			fmul(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xda:
			fcom(PST(code & 7),PST(0));
			fpop();
			return;
		case 0xdc:
			ST(code & 7).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xdd:
			real_to_real(&ST(0),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(code & 7),&tmp,&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xde:
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xdf:
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return;
		case 0xf8:
			printk("ffree not implemented\n\r");
			math_abort(info,1<<(SIGILL-1));
			fpop();
			return;
		case 0xf9:
			fxchg(&ST(0),&ST(code & 7));
			return;
		case 0xfa:
		case 0xfb:
			ST(code & 7) = ST(0);
			fpop();
			return;
	}
	switch ((code>>3) & 0xe7) {
		case 0x22:
			put_short_real(PST(0),info,code);
			return;
		case 0x23:
			put_short_real(PST(0),info,code);
			fpop();
			return;
		case 0x24:
			address = ea(info,code);
			for (code = 0 ; code < 7 ; code++) {
				((long *) & I387)[code] =
				   get_fs_long((unsigned long *) address);
				address += 4;
			}
			return;
		case 0x25:
			address = ea(info,code);
			*(unsigned short *) &I387.cwd =
				get_fs_word((unsigned short *) address);
			return;
		case 0x26:
			address = ea(info,code);
			verify_area(address,28);
			for (code = 0 ; code < 7 ; code++) {
				put_fs_long( ((long *) & I387)[code],
					(unsigned long *) address);
				address += 4;
			}
			return;
		case 0x27:
			address = ea(info,code);
			verify_area(address,2);
			put_fs_word(I387.cwd,(short *) address);
			return;
		case 0x62:
			put_long_int(PST(0),info,code);
			return;
		case 0x63:
			put_long_int(PST(0),info,code);
			fpop();
			return;
		case 0x65:
			fpush();
			get_temp_real(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return;
		case 0x67:
			put_temp_real(PST(0),info,code);
			fpop();
			return;
		case 0xa2:
			put_long_real(PST(0),info,code);
			return;
		case 0xa3:
			put_long_real(PST(0),info,code);
			fpop();
			return;
		case 0xa4:
			address = ea(info,code);
			for (code = 0 ; code < 27 ; code++) {
				((long *) & I387)[code] =
				   get_fs_long((unsigned long *) address);
				address += 4;
			}
			return;
		case 0xa6:
			address = ea(info,code);
			verify_area(address,108);
			for (code = 0 ; code < 27 ; code++) {
				put_fs_long( ((long *) & I387)[code],
					(unsigned long *) address);
				address += 4;
			}
			I387.cwd = 0x037f;
			I387.swd = 0x0000;
			I387.twd = 0x0000;
			return;
		case 0xa7:
			address = ea(info,code);
			verify_area(address,2);
			put_fs_word(I387.swd,(short *) address);
			return;
		case 0xe2:
			put_short_int(PST(0),info,code);
			return;
		case 0xe3:
			put_short_int(PST(0),info,code);
			fpop();
			return;
		case 0xe4:
			fpush();
			get_BCD(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return;
		case 0xe5:
			fpush();
			get_longlong_int(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return;
		case 0xe6:
			put_BCD(PST(0),info,code);
			fpop();
			return;
		case 0xe7:
			put_longlong_int(PST(0),info,code);
			fpop();
			return;
	}
	switch (code >> 9) {
		case 0:
			get_short_real(&tmp,info,code);
			break;
		case 1:
			get_long_int(&tmp,info,code);
			break;
		case 2:
			get_long_real(&tmp,info,code);
			break;
		case 4:
			get_short_int(&tmp,info,code);
	}
	switch ((code>>3) & 0x27) {
		case 0:
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 1:
			fmul(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 2:
			fcom(&tmp,PST(0));
			return;
		case 3:
			fcom(&tmp,PST(0));
			fpop();
			return;
		case 4:
			tmp.exponent ^= 0x8000;
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 5:
			ST(0).exponent ^= 0x8000;
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 6:
			fdiv(PST(0),&tmp,&tmp);
			real_to_real(&tmp,&ST(0));
			return;
		case 7:
			fdiv(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return;
	}
	if ((code & 0x138) == 0x100) {
			fpush();
			real_to_real(&tmp,&ST(0));
			return;
	}
	printk("Unknown math-insns: %04x:%08x %04x\n\r",CS,EIP,code);
	math_abort(info,1<<(SIGFPE-1));
}
Exemple #15
0
void mode_7 (M7_TiledMap tmap, M7_Parameters *params)
{

    // current screen position
	int screen_x, screen_y;

    // the distance and horizontal scale of the line we are drawing
	fix distance, horizontal_scale;

    // masks to make sure we don't read pixels outside the tile
	const int mask = 0x7;

    // step for points in space between two pixels on a horizontal line
	fix line_dx, line_dy;

    // current space position
	fix space_x, space_y;

	int sx, sy;

	const int map_width = tmap.width <<3;
	const int map_height = tmap.height<<3;

	char value;
	char* tile;

	// Trig optimisation
	const fix ca = fcos(params->camera_angle), sa = fsin(params->camera_angle);

	// horizon calculation
	int horiz;
	const fix cpa = fcos(params->camera_pitch), spa = fsin(params->camera_pitch);
	if (cpa != 0) {
		fix h = fmul(spa, FIX(64)) - params->camera_z;
		horiz = UNFIX(fdiv(h, cpa));
	}
	else    // looking straight down (w.y > 0) means horizon at -inf scanline
    horiz= spa > 0 ? INT_MIN : INT_MAX;

	params->horizon = horiz;

	for (screen_y = 0; screen_y < 64; screen_y++)
	{

		if(!(screen_y + horiz)) continue;
    // first calculate the distance of the line we are drawing
		distance = (fmul (params->camera_z, params->scale_y) /
		(screen_y + horiz));

		if(distance < 0)
			continue;

    // then calculate the horizontal scale, or the distance between
    // space points on this horizontal line
		horizontal_scale = fdiv (distance, params->scale_x);

    // calculate the dx and dy of points in space when we step
    // through all points on this line
		line_dx = fmul (-sa, horizontal_scale);
		line_dy = fmul (ca, horizontal_scale);

    // calculate the starting position
		space_x = params->camera_x + fmul (distance, ca) - 64 * line_dx;
		space_y = params->camera_y + fmul (distance, sa) - 64 * line_dy;

    // go through all points in this screen line
		for (screen_x = 0; screen_x < 128; screen_x++)
		{
			sx = UNFIX(space_x);
			sy = UNFIX(space_y);
			if(sx >= 0 && sy >= 0 && sx < map_width && sy < map_height) {
        // get a pixel from the tile and put it on the screen
				tile = tmap.tiles + ((tmap.map[((sy>>3)&255)*tmap.width + tmap.width - 1 - ((sx>>3)&255)])<<3);
				value = (tile[sy&mask] & (1<<(sx&mask))) != 0;
			}
			else
				value = ML_CHECKER;
			ML_pixel(screen_x, screen_y, value);

            // advance to the next position in space
			space_x += line_dx;
			space_y += line_dy;
		}
Exemple #16
0
void test_mode_7 ()
{
    MODE_7_PARAMS params;
    BITMAP *tile, *sprite, *buffer;
    PALETTE pal;
    int quit = FALSE;
    fixed angle = itofix (0);
    fixed x = 0, y = 0;
    fixed dx = 0, dy = 0;
    fixed speed = 0;
    int i, j, r2;

    params.space_z = itofix (50);
    params.scale_x = ftofix (200.0);
    params.scale_y = ftofix (200.0);
    params.obj_scale_x = ftofix (50.0);
    params.obj_scale_y = ftofix (50.0);
    params.horizon = 20;

    // to avoid flicker the program makes use of a double-buffering system
    buffer = create_bitmap (screen->w, screen->h);
    // create a 64x64 tile bitmap and draw something on it.
    tile = create_bitmap (64, 64);
    for (i = 0; i < 32; i++)
    {
        for (j = i; j < 32; j++)
        {
            putpixel (tile, i, j, i);
            putpixel (tile, i, 63-j, i);
            putpixel (tile, 63-i, j, i);
            putpixel (tile, 63-i, 63-j, i);
            putpixel (tile, j, i, i);
            putpixel (tile, j, 63-i, i);
            putpixel (tile, 63-j, i, i);
            putpixel (tile, 63-j, 63-i, i);
        }
    }
    text_mode (-1);

    // Create another bitmap and draw something to it.
    // This bitmap contains the object.
    sprite = create_bitmap (64, 64);
    clear (sprite);
    for (i = 0; i < 64; i++)
    {
        for (j = 0; j < 64; j++)
        {
            r2 = (32 - i) * (32 - i) + (32 - j) * (32 - j);
            if (r2 < 30 * 30)
            {
                r2 = (24 - i) * (24 - i) + (24 - j) * (24 - j);
                putpixel (sprite, i, j, 127 - fixtoi(fsqrt(itofix(r2))));
            }
        }
    }

    // create a palette
    // colors for the tiles
    for (i = 0; i < 64; i++)
    {
        pal[i].r = i;
        pal[i].g = i;
        pal[i].b = 0;
    }
    // colors for the object
    for (i = 0; i < 32; i++)
    {
        pal[i+64].r = 0;
        pal[i+64].g = 0;
        pal[i+64].b = 2 * i;
        pal[i+96].r = 2 * i;
        pal[i+96].g = 2 * i;
        pal[i+96].b = 63;
    }
    set_palette (pal);

    while (!quit)
    {
        // act on keyboard input
        if (key[KEY_ESC]) quit = TRUE;
        if (key[KEY_UP] && speed < itofix (5))
            speed += ftofix (0.1);
        if (key[KEY_DOWN] && speed > itofix (-5))
            speed -= ftofix (0.1);
        if (key[KEY_LEFT])
            angle = (angle - itofix (3)) & 0xFFFFFF;
        if (key[KEY_RIGHT])
            angle = (angle + itofix (3)) & 0xFFFFFF;
        if (key[KEY_Z])
            params.space_z += itofix(5);
        if (key[KEY_X])
            params.space_z -= itofix(5);
        if (key[KEY_Q])
            params.scale_x = fmul (params.scale_x, ftofix (1.5));
        if (key[KEY_W])
            params.scale_x = fdiv (params.scale_x, ftofix (1.5));
        if (key[KEY_E])
            params.scale_y = fmul (params.scale_y, ftofix (1.5));
        if (key[KEY_R])
            params.scale_y = fdiv (params.scale_y, ftofix (1.5));
        if (key[KEY_H])
            params.horizon++;
        if (key[KEY_J])
            params.horizon--;

        dx = fmul (speed, fcos (angle));
        dy = fmul (speed, fsin (angle));

        x += dx;
        y += dy;

        mode_7 (buffer, tile, angle, x, y, params);
        draw_object (buffer, sprite, angle, x, y, params);
        vsync();
        blit (buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

    }
    destroy_bitmap (tile);
    destroy_bitmap (sprite);
    destroy_bitmap (buffer);
}
Exemple #17
0
BOOL gauss(flash A[][50],flash b[],int n)
{ /* solve Ax=b using Gaussian elimination *
   * solution x returned in b              */
    int i,j,k,m;
    BOOL ok;
    flash w,s;
    w=mirvar(0);
    s=mirvar(0);
    ok=TRUE;
    for (i=0;i<n;i++)
        copy(b[i],A[i][n]);
    for (i=0;i<n;i++)
    { /* Gaussian elimination */
        m=i;
        for (j=i+1;j<n;j++)
        {
            absol(A[j][i],w);
            absol(A[m][i],s);
            if (fcomp(w,s)>0) m=j;
        }
        if (m!=i) for (k=i;k<=n;k++)
        {
            copy(A[i][k],w);
            copy(A[m][k],A[i][k]);
            copy(w,A[m][k]);
        }
        if (size(A[i][i])==0)
        {
            ok=FALSE;
            break;
        }
        for (j=i+1;j<n;j++)
        {
            fdiv(A[j][i],A[i][i],s);

            for (k=n;k>=i;k--)
            {
                fmul(s,A[i][k],w);
                fsub(A[j][k],w,A[j][k]);
            }
        }
    }
    if (ok) for (j=n-1;j>=0;j--)
    { /* Backward substitution */
        zero(s);
        for (k=j+1;k<n;k++)
        {
            fmul(A[j][k],b[k],w);
            fadd(s,w,s);
        }
        fsub(A[j][n],s,w);
        if (size(A[j][j])==0)
        {
            ok=FALSE;
            break;
        } 
        fdiv(w,A[j][j],b[j]);
    }
    mirkill(s);
    mirkill(w);
    return ok;
}
Exemple #18
0
float
attribute_hidden
fdivl (double x, double y)
{
  return fdiv (x, y);
}
Exemple #19
0
static int
math_emulate(struct trapframe * info)
{
	unsigned short code;
	temp_real tmp;
	char * address;
	u_long oldeip;

	/* ever used fp? */
	if ((((struct pcb *)curproc->p_addr)->pcb_flags & FP_SOFTFP) == 0) {
		((struct pcb *)curproc->p_addr)->pcb_flags |= FP_SOFTFP;
		I387.cwd = 0x037f;
		I387.swd = 0x0000;
		I387.twd = 0x0000;
	}

	if (I387.cwd & I387.swd & 0x3f)
		I387.swd |= 0x8000;
	else
		I387.swd &= 0x7fff;
	oldeip = info->tf_eip;
/* 0x001f means user code space */
	if ((u_short)info->tf_cs != 0x001F) {
		printf("math_emulate: %04x:%08lx\n", (u_short)info->tf_cs,
			oldeip);
		panic("?Math emulation needed in kernel?");
	}
	code = get_fs_word((unsigned short *) oldeip);
	bswapw(code);
	code &= 0x7ff;
	I387.fip = oldeip;
	*(unsigned short *) &I387.fcs = (u_short) info->tf_cs;
	*(1+(unsigned short *) &I387.fcs) = code;
	info->tf_eip += 2;
	switch (code) {
		case 0x1d0: /* fnop */
			return(0);
		case 0x1d1: case 0x1d2: case 0x1d3:  /* fst to 32-bit mem */
		case 0x1d4: case 0x1d5: case 0x1d6: case 0x1d7:
			math_abort(info,SIGILL);
		case 0x1e0: /* fchs */
			ST(0).exponent ^= 0x8000;
			return(0);
		case 0x1e1: /* fabs */
			ST(0).exponent &= 0x7fff;
			return(0);
		case 0x1e2: case 0x1e3:
			math_abort(info,SIGILL);
		case 0x1e4: /* ftst */
			ftst(PST(0));
			return(0);
		case 0x1e5: /* fxam */
			printf("fxam not implemented\n");
			math_abort(info,SIGILL);
		case 0x1e6: case 0x1e7: /* fldenv */
			math_abort(info,SIGILL);
		case 0x1e8: /* fld1 */
			fpush();
			ST(0) = CONST1;
			return(0);
		case 0x1e9: /* fld2t */
			fpush();
			ST(0) = CONSTL2T;
			return(0);
		case 0x1ea: /* fld2e */
			fpush();
			ST(0) = CONSTL2E;
			return(0);
		case 0x1eb: /* fldpi */
			fpush();
			ST(0) = CONSTPI;
			return(0);
		case 0x1ec: /* fldlg2 */
			fpush();
			ST(0) = CONSTLG2;
			return(0);
		case 0x1ed: /* fldln2 */
			fpush();
			ST(0) = CONSTLN2;
			return(0);
		case 0x1ee: /* fldz */
			fpush();
			ST(0) = CONSTZ;
			return(0);
		case 0x1ef:
			math_abort(info,SIGILL);
		case 0x1f0: /* f2xm1 */
		case 0x1f1: /* fyl2x */
		case 0x1f2: /* fptan */
		case 0x1f3: /* fpatan */
		case 0x1f4: /* fxtract */
		case 0x1f5: /* fprem1 */
		case 0x1f6: /* fdecstp */
		case 0x1f7: /* fincstp */
		case 0x1f8: /* fprem */
		case 0x1f9: /* fyl2xp1 */
		case 0x1fa: /* fsqrt */
		case 0x1fb: /* fsincos */
		case 0x1fe: /* fsin */
		case 0x1ff: /* fcos */
			uprintf(
			 "math_emulate: instruction %04x not implemented\n",
			  code + 0xd800);
			math_abort(info,SIGILL);
		case 0x1fc: /* frndint */
			frndint(PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x1fd: /* fscale */
			/* incomplete and totally inadequate -wfj */
			Fscale(PST(0), PST(1), &tmp);
			real_to_real(&tmp,&ST(0));
			return(0);			/* 19 Sep 92*/
		case 0x2e9: /* ????? */
/* if this should be a fucomp ST(0),ST(1) , it must be a 0x3e9  ATS */
			fucom(PST(1),PST(0));
			fpop(); fpop();
			return(0);
		case 0x3d0: case 0x3d1: /* fist ?? */
			return(0);
		case 0x3e2: /* fclex */
			I387.swd &= 0x7f00;
			return(0);
		case 0x3e3: /* fninit */
			I387.cwd = 0x037f;
			I387.swd = 0x0000;
			I387.twd = 0x0000;
			return(0);
		case 0x3e4:
			return(0);
		case 0x6d9: /* fcompp */
			fcom(PST(1),PST(0));
			fpop(); fpop();
			return(0);
		case 0x7e0: /* fstsw ax */
			*(short *) &info->tf_eax = I387.swd;
			return(0);
	}
	switch (code >> 3) {
		case 0x18: /* fadd */
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x19: /* fmul */
			fmul(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x1a: /* fcom */
			fcom(PST(code & 7),PST(0));
			return(0);
		case 0x1b: /* fcomp */
			fcom(PST(code & 7),PST(0));
			fpop();
			return(0);
		case 0x1c: /* fsubr */
			real_to_real(&ST(code & 7),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(0),&tmp,&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x1d: /* fsub */
			ST(0).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x1e: /* fdivr */
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x1f: /* fdiv */
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x38: /* fld */
			fpush();
			ST(0) = ST((code & 7)+1);  /* why plus 1 ????? ATS */
			return(0);
		case 0x39: /* fxch */
			fxchg(&ST(0),&ST(code & 7));
			return(0);
		case 0x3b: /*  ??? ??? wrong ???? ATS */
			ST(code & 7) = ST(0);
			fpop();
			return(0);
		case 0x98: /* fadd */
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0x99: /* fmul */
			fmul(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0x9a: /* ???? , my manual don't list a direction bit
for fcom , ??? ATS */
			fcom(PST(code & 7),PST(0));
			return(0);
		case 0x9b: /* same as above , ATS */
			fcom(PST(code & 7),PST(0));
			fpop();
			return(0);
		case 0x9c: /* fsubr */
			ST(code & 7).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0x9d: /* fsub */
			real_to_real(&ST(0),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(code & 7),&tmp,&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0x9e: /* fdivr */
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0x9f: /* fdiv */
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			return(0);
		case 0xb8: /* ffree */
			printf("ffree not implemented\n");
			math_abort(info,SIGILL);
		case 0xb9: /* fstp ???? where is the pop ? ATS */
			fxchg(&ST(0),&ST(code & 7));
			return(0);
		case 0xba: /* fst */
			ST(code & 7) = ST(0);
			return(0);
		case 0xbb: /* ????? encoding of fstp to mem ? ATS */
			ST(code & 7) = ST(0);
			fpop();
			return(0);
		case 0xbc: /* fucom */
			fucom(PST(code & 7),PST(0));
			return(0);
		case 0xbd: /* fucomp */
			fucom(PST(code & 7),PST(0));
			fpop();
			return(0);
		case 0xd8: /* faddp */
			fadd(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xd9: /* fmulp */
			fmul(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xda: /* ??? encoding of ficom with 16 bit mem ? ATS */
			fcom(PST(code & 7),PST(0));
			fpop();
			return(0);
		case 0xdc: /* fsubrp */
			ST(code & 7).exponent ^= 0x8000;
			fadd(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xdd: /* fsubp */
			real_to_real(&ST(0),&tmp);
			tmp.exponent ^= 0x8000;
			fadd(PST(code & 7),&tmp,&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xde: /* fdivrp */
			fdiv(PST(0),PST(code & 7),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xdf: /* fdivp */
			fdiv(PST(code & 7),PST(0),&tmp);
			real_to_real(&tmp,&ST(code & 7));
			fpop();
			return(0);
		case 0xf8: /* fild 16-bit mem ???? ATS */
			printf("ffree not implemented\n");
			math_abort(info,SIGILL);
			fpop();
			return(0);
		case 0xf9: /*  ????? ATS */
			fxchg(&ST(0),&ST(code & 7));
			return(0);
		case 0xfa: /* fist 16-bit mem ? ATS */
		case 0xfb: /* fistp 16-bit mem ? ATS */
			ST(code & 7) = ST(0);
			fpop();
			return(0);
	}
	switch ((code>>3) & 0xe7) {
		case 0x22:
			put_short_real(PST(0),info,code);
			return(0);
		case 0x23:
			put_short_real(PST(0),info,code);
			fpop();
			return(0);
		case 0x24:
			address = ea(info,code);
			for (code = 0 ; code < 7 ; code++) {
				((long *) & I387)[code] =
				   get_fs_long((unsigned long *) address);
				address += 4;
			}
			return(0);
		case 0x25:
			address = ea(info,code);
			*(unsigned short *) &I387.cwd =
				get_fs_word((unsigned short *) address);
			return(0);
		case 0x26:
			address = ea(info,code);
			/*verify_area(address,28);*/
			for (code = 0 ; code < 7 ; code++) {
				put_fs_long( ((long *) & I387)[code],
					(unsigned long *) address);
				address += 4;
			}
			return(0);
		case 0x27:
			address = ea(info,code);
			/*verify_area(address,2);*/
			put_fs_word(I387.cwd,(short *) address);
			return(0);
		case 0x62:
			put_long_int(PST(0),info,code);
			return(0);
		case 0x63:
			put_long_int(PST(0),info,code);
			fpop();
			return(0);
		case 0x65:
			fpush();
			get_temp_real(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0x67:
			put_temp_real(PST(0),info,code);
			fpop();
			return(0);
		case 0xa2:
			put_long_real(PST(0),info,code);
			return(0);
		case 0xa3:
			put_long_real(PST(0),info,code);
			fpop();
			return(0);
		case 0xa4:
			address = ea(info,code);
			for (code = 0 ; code < 27 ; code++) {
				((long *) & I387)[code] =
				   get_fs_long((unsigned long *) address);
				address += 4;
			}
			return(0);
		case 0xa6:
			address = ea(info,code);
			/*verify_area(address,108);*/
			for (code = 0 ; code < 27 ; code++) {
				put_fs_long( ((long *) & I387)[code],
					(unsigned long *) address);
				address += 4;
			}
			I387.cwd = 0x037f;
			I387.swd = 0x0000;
			I387.twd = 0x0000;
			return(0);
		case 0xa7:
			address = ea(info,code);
			/*verify_area(address,2);*/
			put_fs_word(I387.swd,(short *) address);
			return(0);
		case 0xe2:
			put_short_int(PST(0),info,code);
			return(0);
		case 0xe3:
			put_short_int(PST(0),info,code);
			fpop();
			return(0);
		case 0xe4:
			fpush();
			get_BCD(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0xe5:
			fpush();
			get_longlong_int(&tmp,info,code);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 0xe6:
			put_BCD(PST(0),info,code);
			fpop();
			return(0);
		case 0xe7:
			put_longlong_int(PST(0),info,code);
			fpop();
			return(0);
	}
	switch (code >> 9) {
		case 0:
			get_short_real(&tmp,info,code);
			break;
		case 1:
			get_long_int(&tmp,info,code);
			break;
		case 2:
			get_long_real(&tmp,info,code);
			break;
		case 4:
			get_short_int(&tmp,info,code);
	}
	switch ((code>>3) & 0x27) {
		case 0:
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 1:
			fmul(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 2:
			fcom(&tmp,PST(0));
			return(0);
		case 3:
			fcom(&tmp,PST(0));
			fpop();
			return(0);
		case 4:
			tmp.exponent ^= 0x8000;
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 5:
			ST(0).exponent ^= 0x8000;
			fadd(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 6:
			fdiv(PST(0),&tmp,&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
		case 7:
			fdiv(&tmp,PST(0),&tmp);
			real_to_real(&tmp,&ST(0));
			return(0);
	}
	if ((code & 0x138) == 0x100) {
			fpush();
			real_to_real(&tmp,&ST(0));
			return(0);
	}
	printf("Unknown math-insns: %04x:%08x %04x\n",(u_short)info->tf_cs,
		info->tf_eip,code);
	math_abort(info,SIGFPE);
}
Exemple #20
0
int encode_op(char *opcode, char *op_data)
{
	int rd,rs,rt,imm,funct,shaft,target;
	char tmp[256];
	const char *fi = "%s %d";
	const char *fg = "%s %%g%d";
	const char *ff = "%s %%f%d";
	const char *fl = "%s %s";
	const char *fgi = "%s %%g%d, %d";
	const char *fgl = "%s %%g%d, %s";
	const char *fgg = "%s %%g%d, %%g%d";
	const char *fggl = "%s %%g%d, %%g%d, %s";
	const char *fggi = "%s %%g%d, %%g%d, %d";
	const char *fggg = "%s %%g%d, %%g%d, %%g%d";
	const char *fff = "%s %%f%d, %%f%d";
	const char *fgf = "%s %%g%d, %%f%d";
	const char *ffg = "%s %%f%d, %%g%d";
	const char *fffl = "%s %%f%d, %%f%d, %s";
	const char *ffff = "%s %%f%d, %%f%d, %%f%d";
	const char *ffgi = "%s %%f%d, %%g%d, %d";
	const char *ffgg = "%s %%f%d, %%g%d, %%g%d";
	char lname[256];

	shaft = funct = target = 0;

	if(strcmp(opcode, "mvhi") == 0){
		if(sscanf(op_data, fgi, tmp, &rs, &imm) == 3)
		    return mvhi(rs,0,imm);
	}
	if(strcmp(opcode, "mvlo") == 0){
		if(sscanf(op_data, fgi, tmp, &rs, &imm) == 3)
		    return mvlo(rs,0,imm);
	}
	if(strcmp(opcode, "add") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return add(rs,rt,rd,0);
	}
	if(strcmp(opcode, "nor") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return nor(rs,rt,rd,0);
	}
	if(strcmp(opcode, "sub") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return sub(rs,rt,rd,0);
	}
	if(strcmp(opcode, "mul") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return mul(rs,rt,rd,0);
	}
	if(strcmp(opcode, "addi") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return addi(rs,rt,imm);
	}
	if(strcmp(opcode, "subi") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return subi(rs,rt,imm);
	}
	if(strcmp(opcode, "muli") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return muli(rs,rt,imm);
	}
	if(strcmp(opcode, "input") == 0){
		if(sscanf(op_data, fg, tmp, &rd) == 2)
		    return input(0,0,rd,0);
	}
	if(strcmp(opcode, "inputw") == 0){
		if(sscanf(op_data, fg, tmp, &rd) == 2)
		    return inputw(0,0,rd,0);
	}
	if(strcmp(opcode, "inputf") == 0){
		if(sscanf(op_data, ff, tmp, &rd) == 2)
		    return inputf(0,0,rd,0);
	}
	if(strcmp(opcode, "output") == 0){
		if(sscanf(op_data, fg, tmp, &rs) == 2)
		    return output(rs,0,0,0);
	}
	if(strcmp(opcode, "outputw") == 0){
		if(sscanf(op_data, fg, tmp, &rs) == 2)
		    return outputw(rs,0,0,0);
	}
	if(strcmp(opcode, "outputf") == 0){
		if(sscanf(op_data, ff, tmp, &rs) == 2)
		    return outputf(rs,0,0,0);
	}
	if(strcmp(opcode, "and") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return _and(rs,rt,rd,0);
	}
	if(strcmp(opcode, "or") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return _or(rs,rt,rd,0);
	}
	if(strcmp(opcode, "sll") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return sll(rs,rt,rd,0);
	}
	if(strcmp(opcode, "srl") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return srl(rs,rt,rd,0);
	}
	if(strcmp(opcode, "slli") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return slli(rs,rt,imm);
	}
	if(strcmp(opcode, "srli") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return srli(rs,rt,imm);
	}
	if(strcmp(opcode, "b") == 0){
		if(sscanf(op_data, fg, tmp, &rs) == 2)
		    return b(rs,0,0,0);
	}
	if(strcmp(opcode, "jmp") == 0){
		if(sscanf(op_data, fl, tmp, lname) == 2) {
			strcpy(label_name[label_cnt],lname);
		    return jmp(label_cnt++);
		}
	}
	if(strcmp(opcode, "jeq") == 0){
		if(sscanf(op_data, fggl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return jeq(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "jne") == 0){
		if(sscanf(op_data, fggl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return jne(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "jlt") == 0){
		if(sscanf(op_data, fggl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return jlt(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "jle") == 0){
		if(sscanf(op_data, fggl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return jle(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "call") == 0){
		if(sscanf(op_data, fl, tmp, lname) == 2)  {
			strcpy(label_name[label_cnt],lname);
		    return call(label_cnt++);
		}
	}
	if(strcmp(opcode, "callR") == 0){
		if(sscanf(op_data, fg, tmp, &rs) == 2)
		    return callr(rs,0,0,0);
	}
	if(strcmp(opcode, "return") == 0){
		    return _return(0);
	}
	if(strcmp(opcode, "ld") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return ld(rs,rt,rd,0);
	}
	if(strcmp(opcode, "ldi") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return ldi(rs,rt,imm);
	}
	if(strcmp(opcode, "ldlr") == 0){
		if(sscanf(op_data, fgi, tmp, &rs, &imm) == 3)
		    return ldlr(rs,0,imm);
	}
	if(strcmp(opcode, "fld") == 0){
		if(sscanf(op_data, ffgg, tmp, &rd, &rs,&rt) == 4)
		    return fld(rs,rt,rd,0);
	}
	if(strcmp(opcode, "st") == 0){
		if(sscanf(op_data, fggg, tmp, &rd, &rs,&rt) == 4)
		    return st(rs,rt,rd,0);
	}
	if(strcmp(opcode, "sti") == 0){
		if(sscanf(op_data, fggi, tmp, &rt, &rs, &imm) == 4)
		    return sti(rs,rt,imm);
	}
	if(strcmp(opcode, "stlr") == 0){
		if(sscanf(op_data, fgi, tmp, &rs, &imm) == 3)
		    return stlr(rs,0,imm);
	}
	if(strcmp(opcode, "fst") == 0){
		if(sscanf(op_data, ffgg, tmp, &rd, &rs,&rt) == 4)
		    return fst(rs,rt,rd,0);
	}
	if(strcmp(opcode, "fadd") == 0){
		if(sscanf(op_data, ffff, tmp, &rd, &rs, &rt) == 4)
		    return fadd(rs,rt,rd,0);
	}
	if(strcmp(opcode, "fsub") == 0){
		if(sscanf(op_data, ffff, tmp, &rd, &rs, &rt) == 4)
		    return fsub(rs,rt,rd,0);
	}
	if(strcmp(opcode, "fmul") == 0){
		if(sscanf(op_data, ffff, tmp, &rd, &rs, &rt) == 4)
		    return fmul(rs,rt,rd,0);
	}
	if(strcmp(opcode, "fdiv") == 0){
		if(sscanf(op_data, ffff, tmp, &rd, &rs, &rt) == 4)
		    return fdiv(rs,rt,rd,0);
	}
	if(strcmp(opcode, "fsqrt") == 0){
		if(sscanf(op_data, fff, tmp, &rd, &rs) == 3)
		    return fsqrt(rs,0,rd,0);
	}
	if(strcmp(opcode, "fabs") == 0){
		if(sscanf(op_data, fff, tmp, &rd, &rs) == 3)
		    return _fabs(rs,0,rd,0);
	}
	if(strcmp(opcode, "fmov") == 0){
		if(sscanf(op_data, fff, tmp, &rd, &rs) == 3)
		    return fmov(rs,0,rd,0);
	}
	if(strcmp(opcode, "fneg") == 0){
		if(sscanf(op_data, fff, tmp, &rd, &rs) == 3)
		    return fneg(rs,0,rd,0);
	}
	if(strcmp(opcode, "fldi") == 0){
		if(sscanf(op_data, ffgi, tmp, &rt, &rs, &imm) == 4)
		    return fldi(rs,rt,imm);
	}
	if(strcmp(opcode, "fsti") == 0){
		if(sscanf(op_data, ffgi, tmp, &rt, &rs, &imm) == 4)
		    return fsti(rs,rt,imm);
	}
	if(strcmp(opcode, "fjeq") == 0){
		if(sscanf(op_data, fffl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return fjeq(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "fjlt") == 0){
		if(sscanf(op_data, fffl, tmp, &rs, &rt, lname) == 4) {
			strcpy(label_name[label_cnt],lname);
		    return fjlt(rs,rt,label_cnt++);
		}
	}
	if(strcmp(opcode, "halt") == 0){
		    return halt(0,0,0,0);
	}
	if(strcmp(opcode, "setL") == 0){
		if(sscanf(op_data, fgl, tmp, &rd, lname) == 3) {
			strcpy(label_name[label_cnt],lname);
		    return setl(0,rd,label_cnt++);
		}
	}
	if(strcmp(opcode, "padd") == 0){
		if(sscanf(op_data, fgi, tmp, &rt, &imm) == 3) {
		    return padd(0,rt,imm);
		}
	}
	if(strcmp(opcode, "link") == 0){
		if(sscanf(op_data, fi, tmp, &imm) == 2) {
		    return link(0,0,imm);
		}
	}
	if(strcmp(opcode, "movlr") == 0){
		return movlr(0,0,0,0);
	}
	if(strcmp(opcode, "btmplr") == 0){
		return btmplr(0,0,0,0);
	}
	/*
	if(strcmp(opcode, "padd") == 0){
		if(sscanf(op_data, fgg, tmp, &rd, &rt) == 3) {
		    return padd(0,rt,d,0);
		}
	}
	*/

	return -1;
}
Exemple #21
0
int do_assemble2(program *program, program2 * program2)
{
  int pc = 0;
  int nextpc = 0;
  instruction2 ist;
  int iname, arg1, arg2, arg3;
  int marume;
  int count = 0;
  int firstflag = 1;
  int printflag  = 0;

  char buff1[40];
  char buff2[40];
  char buff3[40];

  FILE *fadd_fp,*fmul_fp,*fsub_fp,*fdiv_fp,*flr_fp,*foi_fp;

  char fadd_char[30];
  int fadd_file_count = 0;
  int fadd_count = 0;

  char fsub_char[30];
  int fsub_file_count = 0;
  int fsub_count = 0;

  char fmul_char[30];
  int fmul_file_count = 0;
  int fmul_count = 0;

  char fdiv_char[30];
  int fdiv_file_count = 0;
  int fdiv_count = 0;

  char flr_char[30];
  int flr_file_count = 0;
  int flr_count = 0;

  char foi_char[30];
  int foi_file_count = 0;
  int foi_count = 0;

  
  while (1) {
    pc = nextpc;

    if(cnt % 10000000 == 0)
      {
        printf("%lld\n", cnt);
      }
    cnt++;
    
    ist = program2->insts[pc];
    iname = ist.name[0];
    arg1 = ist.name[1];
    arg2 = ist.name[2];
    arg3 = ist.name[3];

    /*
      ++counter;

      if(counter > 575600 && counter < 575719)
      {
      print_label_from_index(program, pc);
      print_instruction(program->insts[pc]);
      print_register();
      }
    */
	
    if (iname == 0) {
      // printf("this is nop\n");
    }
    DO_INST_1(1, +)
      DO_INST_1(2, -)
      DO_INST_1(3, *)
      DO_INST_1(4, &)
      DO_INST_1(5, |)
      /*ALU命令 */
    else if (iname == 6) {
      regist[ist.name[1]] = ~(regist[ist.name[2]] | regist[ist.name[3]]);
    }
    else if (iname == 7) {
      regist[ist.name[1]] = regist[ist.name[2]] ^ regist[ist.name[3]];
    }
    else if (iname == 8) {
      regist[ist.name[1]] = regist[ist.name[2]] + ist.name[3];
    }
    else if (iname == 9) {
      regist[ist.name[1]] = regist[ist.name[2]] - ist.name[3];
    }
    else if (iname == 10) {
      regist[ist.name[1]] = regist[ist.name[2]] * ist.name[3];
    }
    else if (iname == 11) {
      regist[ist.name[1]] = regist[ist.name[2]] & ist.name[3];
    }
    else if (iname == 12) {
      regist[ist.name[1]] = regist[ist.name[2]] | ist.name[3];
    }
    else if (iname == 13) {
      regist[ist.name[1]] = ~(regist[ist.name[2]] | ist.name[3]);
    }
    else if (iname == 14) {
      regist[ist.name[1]] = regist[ist.name[2]] ^ ist.name[3];
    }
    /*FPU命令 */
    else if (iname == 15) {
      if(fadd_count == 0){
        fadd_file_count++;
        sprintf(fadd_char,"fadd-result-%d.txt",fadd_file_count);
        fadd_fp = fopen(fadd_char,"w");
      }

      u_buff[2].d = freg[ist.name[2]];
      u_buff[3].d = freg[ist.name[3]];

      freg[ist.name[1]] = freg[ist.name[2]] + freg[ist.name[3]];

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      trans_unsigned_to_char(u_buff[2].i,buff2);
      trans_unsigned_to_char(u_buff[3].i,buff3);
      fprintf(fadd_fp,"%s\n%s\n%s\n",buff2,buff3,buff1);

      fadd_count++;
      if(fadd_count >= 1000000){
        fadd_count = 0;
        fclose(fadd_fp);
      }
    }
    else if (iname == 16) {
      if(fsub_count == 0){
        fsub_file_count++;
        sprintf(fsub_char,"fsub-result-%d.txt",fsub_file_count);
        fsub_fp = fopen(fsub_char,"w");
      }
      u_buff[2].d = freg[ist.name[2]];
      u_buff[3].d = freg[ist.name[3]];

      freg[ist.name[1]] = freg[ist.name[2]] - freg[ist.name[3]];

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      trans_unsigned_to_char(u_buff[2].i,buff2);
      trans_unsigned_to_char(u_buff[3].i,buff3);
      fprintf(fsub_fp,"%s\n%s\n%s\n",buff2,buff3,buff1);

      fsub_count++;
      if(fsub_count >= 1000000){
        fsub_count = 0;
        fclose(fsub_fp);
      }

    }
    else if (iname == 17) {
      if(fmul_count == 0){
        fmul_file_count++;
        sprintf(fmul_char,"fmul-result-%d.txt",fmul_file_count);
        fmul_fp = fopen(fmul_char,"w");
      }

      u_buff[2].d = freg[ist.name[2]];
      u_buff[3].d = freg[ist.name[3]];

      freg[ist.name[1]] = freg[ist.name[2]] * freg[ist.name[3]];

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      trans_unsigned_to_char(u_buff[2].i,buff2);
      trans_unsigned_to_char(u_buff[3].i,buff3);
      fprintf(fmul_fp,"%s\n%s\n%s\n",buff2,buff3,buff1);

      fmul_count++;
      if(fmul_count >= 1000000){
        fmul_count = 0;
        fclose(fmul_fp);
      }

    }
    else if (iname == 18) {
      if(fdiv_count == 0){
        fdiv_file_count++;
        sprintf(fdiv_char,"fdiv-result-%d.txt",fdiv_file_count);
        fdiv_fp = fopen(fdiv_char,"w");
      }

      u_buff[2].d = freg[ist.name[2]];
      u_buff[3].d = freg[ist.name[3]];

      freg[ist.name[1]] = fdiv(freg[ist.name[2]],freg[ist.name[3]]);

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      trans_unsigned_to_char(u_buff[2].i,buff2);
      trans_unsigned_to_char(u_buff[3].i,buff3);
      fprintf(fdiv_fp,"%s\n%s\n%s\n",buff2,buff3,buff1);

      fdiv_count++;
      if(fdiv_count >= 1000000){
        fdiv_count = 0;
        fclose(fdiv_fp);
      }

    }
    else if (iname == 19) {
      freg[ist.name[1]] = 1 / freg[ist.name[2]];
    }
    else if (iname == 20) {
      freg[ist.name[1]] = sqrt(freg[ist.name[2]]);
    }
    else if (iname == 21) {
      if(flr_count == 0){
        flr_file_count++;
        sprintf(flr_char,"flr-result-%d.txt",flr_file_count);
        flr_fp = fopen(flr_char,"w");
      }

      u_buff[2].d = freg[ist.name[2]];

      freg[ist.name[1]] = floor(freg[ist.name[2]]);

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      trans_unsigned_to_char(u_buff[2].i,buff2);
      fprintf(flr_fp,"%s\n%s\n",buff2,buff1);

      flr_count++;
      if(flr_count >= 1000000){
        flr_count = 0;
        fclose(flr_fp);
      }

    }
    else if (iname == 22) {
      if(foi_count == 0){
        foi_file_count++;
        sprintf(foi_char,"foi-result-%d.txt",foi_file_count);
        foi_fp = fopen(foi_char,"w");
      }

      trans_unsigned_to_char(regist[ist.name[2]],buff2);

      freg[ist.name[1]] = (float) (regist[ist.name[2]]);

      u_buff[1].d = freg[ist.name[1]];
      trans_unsigned_to_char(u_buff[1].i,buff1);
      fprintf(foi_fp,"%s\n%s\n",buff2,buff1);

      foi_count++;
      if(foi_count >= 1000000){
        foi_count = 0;
        fclose(foi_fp);
      }

    }


    /*MEM ACSESS命令 */
    else if (iname == 23) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3]) == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//	 printf("%d\n", iname);
	//printf("%lld\n", counter);
	exit(1);
      }
      regist[ist.name[1]] = memory[regist[ist.name[2]] + ist.name[3]].i;
    }

    else if (iname == 24) {
      //memory_check
      /*
	if (check_memory(regist[ist.name[2]] + ist.name[3]) == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//printf("%d\n", iname);
	exit(1);
	}
      */
      memory[regist[ist.name[2]] + ist.name[3]].i =  regist[ist.name[1]];
    }

    else if (iname == 25) {
      //memory_check
      /*
	if (check_memory(regist[ist.name[2]] + ist.name[3])
	== ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	//printf("%d\n", iname);
	exit(1);
	}
      */
      freg[ist.name[1]] =
	memory[regist[ist.name[2]] + ist.name[3]].d;
    }

    else if (iname == 26) {
      //memory_check
      if (check_memory(regist[ist.name[2]] + ist.name[3])
	  == ACSESS_BAD) {
	printf("Error:ACSESS_BAD :\n");
	// printf("%d\n", iname);
	exit(1);
      }
      memory[regist[ist.name[2]] + ist.name[3]].d =
	freg[ist.name[1]];
    }


    /*BRANCH命令 */
    else if (iname == 27) {
      if (regist[ist.name[1]] == regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 28) {
      if (regist[ist.name[1]] != regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }
    else if (iname == 29) {
      if (regist[ist.name[1]] > regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 30) {
      if (regist[ist.name[1]] < regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 31) {
      if (regist[ist.name[1]] >= regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 32) {
      if (regist[ist.name[1]] <= regist[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }

    } else if (iname == 33) {
      if (freg[ist.name[1]] == freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 34) {
      if (freg[ist.name[1]] != freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 35) {
      if (freg[ist.name[1]] > freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 36) {
      if (freg[ist.name[1]] < freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 37) {
      if (freg[ist.name[1]] >= freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }

    else if (iname == 38) {
      if (freg[ist.name[1]] <= freg[ist.name[2]]) {
	nextpc = pc + ist.name[3] - 1;
      }
    }


    /*JUMP命令 */
    else if (iname == 39) {
	  nextpc = ist.name[1] - 1;
    }
    else if (iname == 40) {
      nextpc = ist.name[1] - 1;
      push(&call_stack, (pc + 1));
    }
    else if (iname == RDI) {
      regist[ist.name[1]] = read_int();
      //fscanf(input_fp,"%X",&regist[ist.name[1]]);
    }
    else if (iname == RDF) {
      freg[ist.name[1]] = read_float();
      //fscanf(input_fp,"%X",&buffer.i);
      //freg[ist.name[1]] = buffer.d;
    }
    else if (iname == PTC) {
      /*最初のOXAAは無視する*/
      if(firstflag == 1){
        if(regist[ist.name[1]] != 170){
          printf("first char is not 0xAA!!: %c\n",regist[ist.name[1]]);
          exit(1);
        }
        firstflag = 0;
      }else{
        fprintf(out_fp,"%c",regist[ist.name[1]]);
      }
    }
    else if (iname == PTF) {
      marume = (int) (freg[ist.name[1]] + 0.5);
      if(marume > 255){
        fprintf(out_fp,"%c",255);
      }else if (marume < 0){
        fprintf(out_fp,"%c",0);
      }else{
        fprintf(out_fp,"%c",(char)marume);
      }
    }
    else {//if (iname == 41) {
      nextpc = pop(&call_stack) - 1;
    }
    /*
      else if (iname = 42) {
      print_register();
      print_memory();
      }
    */
    //命令が存在しなかった場合error parseでやっているのでいらない。
    //printf("ist = %d\n",iname);

    /*    
    if(pc == 1535){
      printflag = 1;
    }
    
    if(printflag == 1){ 
      count++;
      if(count < 10000){
        printf("%d  [%d] ",count,pc);
        print_instruction(program->insts[pc]);
        //if(count % 20 == 0){
        print_register();
        //}
      }
    }
    */
    nextpc++;


    /*命令がラストの行まで行けば処理を終了する */
    if (nextpc >= program2->inst_count) {
      break;
    }
  }


  fclose(fadd_fp);
  fclose(fsub_fp);
  fclose(fmul_fp);
  fclose(fdiv_fp);
  fclose(flr_fp);
  fclose(foi_fp);

  return 0;
}
Exemple #22
0
int main() {
  float_b f1,f2,f3,f4;
  int i = 0;
  int c1 = 0;
  int c2 = 0;
  int c3 = 0;
  int c4 = 0;

  srand(time(NULL));

  while(i < 1000000) { //正規化数ランダム
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = rand()%254+1;
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = rand()%254+1;
    f2.ieee.fraction = rand()%8388608;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f3.f = f1.f * f2.f;
    f4.f = fmul(f1.f,f2.f);
    if (f3.f != f4.f && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f*%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    f3.f = f1.f / f2.f;
    f4.f = fdiv(f1.f,f2.f);
    if (f3.f != f4.f && ((f3.ieee.exp != 0 && f3.ieee.exp != 255) || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f/%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c3++;
    }

    f1.ieee.sign = 0;
    f3.f = sqrtf(f1.f);
    f4.f = sqrt_m(f1.f);
    if (f3.f != f4.f) {
      printf("sqrt(%f)=%f\n",f1.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c4++;
    }

    i++;
  }

  printf("fadd:%d fmul:%d fdiv:%d sqrt:%d\n",c1,c2,c3,c4);

  i = 0;
  c1 = 0;
  c2 = 0;
  c3 = 0;
  c4 = 0;

  while(i < 1000000) { //0.0に対する計算
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = (rand()%254+1)*(i%2);
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = (rand()%254+1)*((i+1)%2);
    f2.ieee.fraction = rand()%8388608;

    if (i%2 == 1) f3.f = f1.f;
    else f3.f = f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f3.ieee.sign = (f1.ieee.sign + f2.ieee.sign)%2;
    f3.ieee.exp = 0;
    f3.ieee.fraction = f1.ieee.fraction;
    f4.f = fmul(f1.f,f2.f);
    if (f3.f != f4.f) {
      printf("%f*%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    if (i%2 == 1) {
      f3.ieee.sign = f1.ieee.sign + f2.ieee.sign;
      f3.ieee.exp = 255;
      f3.ieee.fraction = f1.ieee.fraction;
    } else {
      f3.f = 0.0 / f2.f;
    }
    f4.f = fdiv(f1.f,f2.f);
    if ((f3.ieee.sign != f4.ieee.sign || f3.ieee.exp != f4.ieee.exp || f3.ieee.fraction != f4.ieee.fraction) && (i%2 == 1 || (f3.ieee.exp != 0 && f3.ieee.exp != 255) || (f4.ieee.exp != 0 && f4.ieee.exp != 255))) {
      printf("%f/%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c3++;
    }

    f1.ieee.sign = 0;
    if (i%2 == 1) f3.f = sqrtf(f1.f);
    else f3.f = f1.f;
    f4.f = sqrt_m(f1.f);
    if (f3.f != f4.f) {
      printf("sqrt(%f)=%f\n",f1.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c4++;
    }

    i++;
  }

  printf("コーナーケース\n");
  printf("0.0(とみなされる数)の演算 fadd:%d fmul:%d fdiv:%d sqrt:%d\n",c1,c2,c3,c4);

  i = 0;
  c1 = 0;
  c2 = 0;
  c3 = 0;
  c4 = 0;

  while(i < 1000000) { 
    f1.ieee.sign = rand()%2;
    f1.ieee.exp = (rand()%254+1);
    f1.ieee.fraction = rand()%8388608;
    f2.ieee.sign = rand()%2;
    f2.ieee.exp = f1.ieee.exp + (rand()%3-1);
    f2.ieee.fraction = rand()%8388608;
    if (f2.ieee.exp == 255) f2.ieee.exp = 254;
    if (f2.ieee.exp == 0) f2.ieee.exp = 1;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f  && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      print_bit(f1);
      print_bit(f2);
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c1++;
    }

    f2.f = f1.f;
    f2.ieee.sign = f1.ieee.sign + 1;

    f3.f = f1.f + f2.f;
    f4.f = fadd(f1.f,f2.f);
    if (f3.f != f4.f  && (f3.ieee.exp != 0 || f4.ieee.exp != 0) && (f3.ieee.exp != 255 || f4.ieee.exp != 255)) {
      printf("%f+%f=%f\n",f1.f,f2.f,f3.f);
      print_bit(f3);
      print_bit(f4);
    } else {
      c2++;
    }

    i++;
  }

  printf("指数の差が高々1 fadd:%d\n",c1);
  printf("絶対値が等しく符号が逆 fadd:%d\n",c2);

  return 0;
}
Exemple #23
0
static BOOL act(int p,int q)
{ /* act on selected key */
    int k,n,c;
    aprint(PRESSED,4+5*p,6+3*q,keys[q][p]);
    switch(p+7*q)
    {
    case 0:  if (degrees) fmul(x,radeg,x);
             if (hyp) fsinh(x,x);
             else     fsin(x,x);
             newx=TRUE;
             break;
    case 1:  if (degrees) fmul(x,radeg,x);
             if (hyp) fcosh(x,x);
             else     fcos(x,x);
             newx=TRUE;
             break;
    case 2:  if (degrees) fmul(x,radeg,x);
             if (hyp) ftanh(x,x);
             else     ftan(x,x);
             newx=TRUE;
             break;
    case 3:  if (lgbase>0)
             {
                 n=size(x);
                 if (abs(n)<MR_TOOBIG)
                 {
                     convert(lgbase,x);
                     if (n<0) frecip(x,x);
                     fpower(x,abs(n),x);
                     newx=TRUE;
                     break;
                 }
                 if (lgbase==2)  fmul(x,loge2,x);
                 if (lgbase==10) fmul(x,loge10,x);
             }
             fexp(x,x);
             newx=TRUE;
             break;
    case 4:  mip->RPOINT=!mip->RPOINT;
             newx=TRUE;
             break;
    case 5:  clrall();
             newx=TRUE;
             break;
    case 6:  return TRUE;
    case 7:  if (hyp) fasinh(x,x);
             else     fasin(x,x);
             if (degrees) fdiv(x,radeg,x);
             newx=TRUE;
             break;
    case 8:  if (hyp) facosh(x,x);
             else     facos(x,x);
             if (degrees) fdiv(x,radeg,x);
             newx=TRUE;
             break;
    case 9:  if (hyp) fatanh(x,x);
             else     fatan(x,x);
             if (degrees) fdiv(x,radeg,x);
             newx=TRUE;
             break;
    case 10: flog(x,x);
             if (lgbase==2)  fdiv(x,loge2,x);
             if (lgbase==10) fdiv(x,loge10,x);
             newx=TRUE;
             break;
    case 11: newx=TRUE;
             k=3;
             forever
             {
                 aprint(INVER,2+stptr[k],2,settings[k][option[k]]);
                 curser(2+stptr[k],2);
                 c=arrow(gethit());
                 if (c==1)
                 {
                     if (option[k]==nops[k]) option[k]=0;
                     else option[k]+=1;
                     continue;
                 }
                 aprint(STATCOL,2+stptr[k],2,settings[k][option[k]]);
                 if (c==0 || c==2) break;
                 if (c==4 && k>0) k--;
                 if (c==3 && k<3) k++;
             }
             setopts();
             break;
    case 12: chekit(7);
             break;
    case 13: result=FALSE;
             if (ipt==0) break;
             ipt--;
             mybuff[ipt]='\0';
             if (ipt==0) clr();
             just(mybuff);
             cinstr(x,mybuff);
             newx=TRUE;
             break;
    case 14: if (!next('7')) putchar(BELL);
             break;
    case 15: if (!next('8')) putchar(BELL);
             break;
    case 16: if (!next('9')) putchar(BELL);
             break;
    case 17: chekit(6);
             break;
    case 18: chekit(5);
             break;
    case 19: chekit(4);
             break;
    case 20: copy(m,x);
             newx=TRUE;
             break;
    case 21: if (!next('4')) putchar(BELL);
             break;
    case 22: if (!next('5')) putchar(BELL);
             break;
    case 23: if (!next('6')) putchar(BELL);
             break;
    case 24: fmul(x,x,x);
             newx=TRUE;
             break;
    case 25: froot(x,2,x);
             newx=TRUE;
             break;
    case 26: chekit(3);
             break;
    case 27: brkt=0;
             chekit(0);
             flag=OFF;
             fadd(m,x,m);
             newx=TRUE;
             break;
    case 28: if (!next('1')) putchar(BELL);
             break;
    case 29: if (!next('2')) putchar(BELL);
             break;
    case 30: if (!next('3')) putchar(BELL);
             break;
    case 31: frecip(x,x);
             newx=TRUE;
             break;
    case 32: fpi(x);
             newx=TRUE;
             break;
    case 33: chekit(2);
             break;
    case 34: negify(x,x);
             newx=TRUE;
             break;
    case 35: if (!next('0')) putchar(BELL);
             break;
    case 36: if (!next('/')) putchar(BELL);
             break;
    case 37: if (!next('.')) putchar(BELL);
             break;
    case 38: if (ipt>0)
             {
                 putchar(BELL);
                 result=FALSE;
             }
             else
             {
                 zero(x);
                 brkt+=1;
                 newx=TRUE;
             }
             break;
    case 39: if (brkt>0)
             {
                 chekit(0);
                 brkt-=1;
             }
             else
             {
                 putchar(BELL);
                 result=FALSE;
             }
             break;
    case 40: chekit(1);
             break;
    case 41: brkt=0;
             equals(0);
             flag=OFF;
             break;
    }
    return FALSE;
}
Exemple #24
0
// Renders a slide to offscreen buffer. Returns a rect of the rendered area.
// alpha=256 means normal, alpha=0 is fully black, alpha=128 half transparent
// col1 and col2 limit the column for rendering.
QRect PictureFlowPrivate::renderSlide(const SlideInfo &slide, int alpha, 
int col1, int col2)
{
  QImage* src = surface(slide.slideIndex);
  if(!src)
    return QRect();

  QRect rect(0, 0, 0, 0);  
  
#ifdef PICTUREFLOW_BILINEAR_FILTER
  int sw = src->height() / BILINEAR_STRETCH_HOR;
  int sh = src->width() / BILINEAR_STRETCH_VER;
#else
  int sw = src->height();
  int sh = src->width();
#endif
  int h = buffer.height();
  int w = buffer.width();

  if(col1 > col2)
  {
    int c = col2;
    col2 = col1;
    col1 = c;
  }

  col1 = (col1 >= 0) ? col1 : 0;
  col2 = (col2 >= 0) ? col2 : w-1;
  col1 = qMin(col1, w-1);
  col2 = qMin(col2, w-1);

  int distance = h * 100 / zoom;
  PFreal sdx = fcos(slide.angle);
  PFreal sdy = fsin(slide.angle);
  PFreal xs = slide.cx - slideWidth * sdx/2;
  PFreal ys = slide.cy - slideWidth * sdy/2;
  PFreal dist = distance * PFREAL_ONE;

  int xi = qMax((PFreal)0, ((w*PFREAL_ONE/2) + fdiv(xs*h, dist+ys)) >> PFREAL_SHIFT);
  if(xi >= w)
    return rect;

  bool flag = false;
  rect.setLeft(xi);

  for(int x = qMax(xi, col1); x <= col2; x++)
  {
    PFreal hity = 0;
    PFreal fk = rays[x];
    if(sdy)
    {
      fk = fk - fdiv(sdx,sdy);
      hity = -fdiv((rays[x]*distance - slide.cx + slide.cy*sdx/sdy), fk);
    }

    dist = distance*PFREAL_ONE + hity;
    if(dist < 0)
      continue;

    PFreal hitx = fmul(dist, rays[x]);
    PFreal hitdist = fdiv(hitx - slide.cx, sdx);

#ifdef PICTUREFLOW_BILINEAR_FILTER
    int column = sw*BILINEAR_STRETCH_HOR/2 + (hitdist*BILINEAR_STRETCH_HOR >> PFREAL_SHIFT);
    if(column >= sw*BILINEAR_STRETCH_HOR)
      break;
#else
    int column = sw/2 + (hitdist >> PFREAL_SHIFT);
    if(column >= sw)
      break;
#endif
    if(column < 0)
      continue;

    rect.setRight(x);  
    if(!flag)
      rect.setLeft(x);
    flag = true;  

    int y1 = h/2;
    int y2 = y1+ 1;
    QRgb* pixel1 = (QRgb*)(buffer.scanLine(y1)) + x;
    QRgb* pixel2 = (QRgb*)(buffer.scanLine(y2)) + x;
    QRgb pixelstep = pixel2 - pixel1;

#ifdef PICTUREFLOW_BILINEAR_FILTER
    int center = (sh*BILINEAR_STRETCH_VER/2);
    int dy = dist*BILINEAR_STRETCH_VER / h;
#else
    int center = (sh/2);
    int dy = dist / h;
#endif
    int p1 = center*PFREAL_ONE - dy/2;
    int p2 = center*PFREAL_ONE + dy/2;

    const QRgb *ptr = (const QRgb*)(src->scanLine(column));
    if(alpha == 256)
      while((y1 >= 0) && (y2 < h) && (p1 >= 0))
      {
        *pixel1 = ptr[p1 >> PFREAL_SHIFT];
        *pixel2 = ptr[p2 >> PFREAL_SHIFT];
        p1 -= dy;
        p2 += dy;
        y1--;
        y2++;
        pixel1 -= pixelstep;
        pixel2 += pixelstep;
      }  
    else
      while((y1 >= 0) && (y2 < h) && (p1 >= 0))
      {
        QRgb c1 = ptr[p1 >> PFREAL_SHIFT];
        QRgb c2 = ptr[p2 >> PFREAL_SHIFT];

        int r1 = qRed(c1) * alpha/256;
        int g1 = qGreen(c1) * alpha/256;
        int b1 = qBlue(c1) * alpha/256;
        int r2 = qRed(c2) * alpha/256;
        int g2 = qGreen(c2) * alpha/256;
        int b2 = qBlue(c2) * alpha/256;

        *pixel1 = qRgb(r1, g1, b1);
        *pixel2 = qRgb(r2, g2, b2);
        p1 -= dy;
        p2 += dy;
        y1--;
        y2++;
        pixel1 -= pixelstep;
        pixel2 += pixelstep;
     }  
   }  
Exemple #25
-1
static void equals(int no)
{ /* perform binary operation */
    BOOL pop;
    newx=FALSE;
    pop=TRUE;
    while (pop)
    {
        if (prec(no)>prop[sp])
        { /* if higher precedence, keep this one pending */
            if (sp==top)
            {
                mip->ERNUM=(-1);
                result=FALSE;
                newx=TRUE;
            }
            else sp++;
            return;
        }
        newx=TRUE;
        if (flag && op[sp]!=3 && op[sp]!=0) swap();
        switch (op[sp])
        {
        case 7: fdiv(x,y[sp],t);
                ftrunc(t,t,t);
                fmul(t,y[sp],t);
                fsub(x,t,x);
                break;
        case 6: fpowf(x,y[sp],x);
                break;
        case 5: frecip(y[sp],t);
                fpowf(x,t,x);
                break;
        case 4: fdiv(x,y[sp],x);
                break;
        case 3: fmul(x,y[sp],x);
                break;
        case 2: fsub(x,y[sp],x);
                break;
        case 1: fadd(x,y[sp],x);
                break;
        case 0: break;
        }
        if (sp>0 && (prec(no)<=prop[sp-1])) sp--;
        else pop=FALSE;
    }
}