//输入:待处理的数组,解调之后的数组大小,调制指数 //输出:已解调数组的首地址 double* DPSK_DeMod_SoftInfo(complex* Data_Mod_re,int LEN,int M) { int m1,i,j; int N; double* Data_DeMod=(double*)malloc(LEN*sizeof(double)); //complex Init_sym={0,1}; complex Init_sym={1, 0}; complex *D_data;//差分之后检测之前的数据初始化(数据长度的设定可以修改) m1=(int)ceil(log(M)/log(2)); N=LEN/m1; D_data=(complex*)malloc(N*sizeof(complex)); D_data[0]=fmul(fconj(Init_sym),Data_Mod_re[0]); for(i=1;i<N;i++) { D_data[i]=fmul(fconj(Data_Mod_re[i-1]),Data_Mod_re[i]); //差分之后的数据序列 } for(i=0,j=0;i<LEN;i+=m1) { if(1<=m1) Data_DeMod[i]=D_data[j].im; if(2<=m1) Data_DeMod[i+1]=D_data[j].re; if(3<=m1) Data_DeMod[i+2]=fabs(D_data[j].im); j++; } free(D_data); if(D_data!=NULL) D_data=NULL; return Data_DeMod; }
/** 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); } }
static int grproc_xadvance( INSTANCE * my, int * params ) { int angle = params[0] ; LOCINT32( mod_grproc, my, COORDX ) += fixtoi( fmul( fcos( angle ), itofix( params[1] ) ) ) ; LOCINT32( mod_grproc, my, COORDY ) -= fixtoi( fmul( fsin( angle ), itofix( params[1] ) ) ) ; return 1 ; }
//输入:待调制的数组,数组长度,调制阶数 //输出:已调制信号 complex* DPSK_Mod(uc* BitStream, int LEN, int M) { int i,j,m1; uc **sub_BitStream; double *pha; //complex Init_sym={0,1}; complex Init_sym={1, 0}; complex *Data_Mod; m1=(int)ceil(log(M)/log(2)); Data_Mod=(complex*)malloc(LEN/m1*sizeof(complex));//每一对符号的相对相位变化 sub_BitStream=(uc**)malloc((LEN/m1)*sizeof(uc*));//串并变换为二维数组 for(j=0; j<LEN/m1; j++) { sub_BitStream[j]=(uc*)malloc(sizeof(uc)*m1); } pha=(double(*))malloc(LEN/m1*sizeof(double)); //printf("%d\n", LEN/m1);//9920 for(i=0;i<(LEN/m1);i++) //串并变换 { for(j=0;j<m1;j++) { sub_BitStream[i][j] = BitStream[i*m1+j]; } } /*for(i=0;i<(LEN/m1);i++) //串并变换 { for(j=0;j<m1;j++) { printf("%d", sub_BitStream[i][j]); } printf("\n"); }*/ pha[0] = CalcPhase(sub_BitStream[0],M); //对第一对数据进行相位运算 Data_Mod[0] = fmul(Init_sym,fget(pha[0])); for(i=1;i<(LEN/m1);i++) { pha[i]=CalcPhase(sub_BitStream[i],M); Data_Mod[i]=fmul(Data_Mod[i-1],fget(pha[i])); } for(j=0;j<LEN/m1;j++) { free(sub_BitStream[j]); } free(sub_BitStream); free(pha); //for(i=0;i<10;i++) //printf("%f+%fI ",Data_Mod[i].re,Data_Mod[i].im); //printf("\n"); return Data_Mod; //Data_Mod=Data_Mod; //for(i=0;i<LEN/m1;i++) printf("%f",Data_Mod[i]); }
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; } } }
void curve25519_donna(u8 *mypublic, const u8 *secret, const u8 *basepoint) { felem bp[10], x[10], z[10], zmone[10]; fexpand(bp, basepoint); cmult(x, z, secret, bp); crecip(zmone, z); fmul(z, x, zmone); fcontract(mypublic, z); }
uint32_t kernel_sin(uint32_t f){ uint32_t x2 = fmul(f,f); uint32_t x3 = fmul(f,x2); uint32_t x4 = fmul(x2,x2); uint32_t x5 = fmul(x3,x2); uint32_t x7 = fmul(x3,x4); return fadd(fadd(f,fmul(0xb94d64b6,x7)), fadd(fmul(0xbe2aaaac,x3),fmul(0x3c088666,x5))); }
// Simultaneous modular inversion; See Section 2.25 of Guide to Elliptic Curve Cryptography (2004) void batch_inverse(felem *a, int n) { felem c[BATCH_SIZE]; fcopy(c[0], a[0]); for ( int i = 1; i < n; i ++ ) { fmul(c[i], c[i-1], a[i]); } felem u; crecip(u, c[n - 1]); for ( int i = n - 1; i > 0; i-- ) { felem t1, t2; fmul(t1, u, c[i-1]); fmul(t2, u, a[i]); fcopy(a[i], t1); fcopy(u, t2); } fcopy(a[0], u); }
bits256 cards777_initcrypt(bits256 data,bits256 privkey,bits256 pubkey,int32_t invert) { bits256 hash; bits320 hexp; hash = curve25519_shared(privkey,pubkey); hexp = fexpand(hash); if ( invert != 0 ) hexp = crecip(hexp); return(fcontract(fmul(fexpand(data),hexp))); }
uint32_t kernel_cos(uint32_t f){ uint32_t x2 = fmul(f,f); uint32_t x3 = fmul(f,x2); uint32_t x4 = fmul(x2,x2); uint32_t x6 = fmul(x3,x3); return fadd(fadd(0x3f800000,fmul(0xbf000000,x2)), fadd(fmul(0x3d2aa789,x4),fmul(0xbab38106,x6))); }
/* 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); }
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); } }
// Point doubling; See http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#doubling-mdbl-1987-m void xz_ge_double(felem xout, felem zout, const felem xin) { static const felem fone = {1}; felem xx1, t0, t1, t2; fsquare_times(xx1, xin, 1); fcopy(t0, fone); fdifference_backwards(t0, xx1); fsquare_times(xout, t0, 1); fscalar_product(t1, xin, 486662); fsum(t1, xx1); fsum(t1, fone); fmul(t2, xin, t1); fscalar_product(zout, t2, 4); }
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 }
fmonty(felem *x2, /* output 2Q */ felem *x3, /* output Q + Q' */ felem *x, /* input Q */ felem *xprime, /* input Q' */ const felem *qmqp /* input Q - Q' */) { felem *const z2 = &x2[5]; felem *const z3 = &x3[5]; felem *const z = &x[5]; felem *const zprime = &xprime[5]; felem origx[5], origxprime[5], zzz[5], xx[5], zz[5], xxprime[5], zzprime[5], zzzprime[5]; memcpy(origx, x, 5 * sizeof(felem)); fsum(x, z); fdifference_backwards(z, origx); // does x - z memcpy(origxprime, xprime, sizeof(felem) * 5); fsum(xprime, zprime); fdifference_backwards(zprime, origxprime); fmul(xxprime, xprime, z); fmul(zzprime, x, zprime); memcpy(origxprime, xxprime, sizeof(felem) * 5); fsum(xxprime, zzprime); fdifference_backwards(zzprime, origxprime); fsquare(x3, xxprime); fsquare(zzzprime, zzprime); fmul(z3, zzzprime, qmqp); fsquare(xx, x); fsquare(zz, z); fmul(x2, xx, zz); fdifference_backwards(zz, xx); // does zz = xx - zz fscalar(zzz, zz); // * 121665 freduce_coefficients(zzz); fsum(zzz, xx); fmul(z2, zz, zzz); }
int crypto_scalarmult(u8 *mypublic, const u8 *secret, const u8 *basepoint) { felem bp[5], x[5], z[5], zmone[5]; unsigned char e[32]; int i; for (i = 0;i < 32;++i) e[i] = secret[i]; e[0] &= 248; e[31] &= 127; e[31] |= 64; fexpand(bp, basepoint); cmult(x, z, e, bp); crecip(zmone, z); fmul(z, x, zmone); fcontract(mypublic, z); return 0; }
void fasinh(_MIPD_ flash x,flash y) { /* calculate y=asinh(x) */ #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif copy(x,y); if (mr_mip->ERNUM || size(y)==0) return; MR_IN(66) fmul(_MIPP_ y,y,mr_mip->w11); fincr(_MIPP_ mr_mip->w11,1,1,mr_mip->w11); froot(_MIPP_ mr_mip->w11,2,mr_mip->w11); fadd(_MIPP_ y,mr_mip->w11,y); flog(_MIPP_ y,y); MR_OUT }
void facosh(_MIPD_ flash x,flash y) { /* calculate y=acosh(x) */ #ifndef MR_GENERIC_MT miracl *mr_mip=get_mip(); #endif copy(x,y); if (mr_mip->ERNUM) return; MR_IN(62) fmul(_MIPP_ y,y,mr_mip->w11); fincr(_MIPP_ mr_mip->w11,(-1),1,mr_mip->w11); froot(_MIPP_ mr_mip->w11,2,mr_mip->w11); fadd(_MIPP_ y,mr_mip->w11,y); flog(_MIPP_ y,y); MR_OUT }
/** Multiplies m1 by m2. * Returns materr(2) if the two matrices can't * be multiplied, materr(1) if the initialization * didn't go right. */ matrix MatrixMul(matrix m1, matrix m2) { if (!isValidOp(m1,m2,'m')) { return materr(2); } matrix mx = initialize(m1.rows, m2.columns); if (mx.mx == NULL) { return materr(1); } for(int row = 0; row < mx.rows; row++) { for(int col = 0; col < mx.columns; col++) { for(int i = 0; i < m1.columns; i++) { mx.mx[row][col] = fadd(mx.mx[row][col], fmul(m1.mx[row][i], m2.mx[i][col])); } } } return mx; }
int main() { /* Brents example program */ flash x,pi; miracl *mip=mirsys(-35,0); x=mirvar(0); pi=mirvar(0); mip->RPOINT=ON; printf("Calculating pi..\n"); fpi(pi); cotnum(pi,stdout); /* output pi */ printf("Calculating exp(pi*(163/9)^0.5)\n"); fconv(163,9,x); froot(x,2,x); fmul(x,pi,x); fexp(x,x); cotnum(x,stdout); printf("Calculating exp(pi*(163)^0.5)\n"); fpower(x,3,x); cotnum(x,stdout); return 0; }
int main(void) { FILE *fp; union data_32bit a, b, result, correct; uint32_t a_uint32, b_uint32; int count_mistake = 0; //誤答数をカウント int count_1bit_diff = 0; //1bitずれをカウント if ((fp = fopen("testcase.txt", "r")) == NULL) { printf("file open error.\n"); exit(EXIT_FAILURE); } while(fscanf(fp, "%x %x", &a_uint32, &b_uint32) != EOF) { a.uint32 = normalize(a_uint32); b.uint32 = normalize(b_uint32); //printf("a: %10u ", a.uint32); //printf("b: %10u\n", b.uint32); result.uint32 = fmul(a.uint32, b.uint32); correct.fl32 = a.fl32 * b.fl32; //printf("result : %10u\n", result.uint32); //printf("correct : %10u\n", correct.uint32); if (equal(result.uint32, correct.uint32) == 1) { } else if (equal(result.uint32, correct.uint32) == 2) { count_1bit_diff++; } else { //printf("0\n"); //一致しなければ0 count_mistake++; show_testcase(a, b, result, correct); } } printf("total 1bit_diff : %d\n", count_1bit_diff); printf("total mistakes : %d\n", count_mistake); fclose(fp); return 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; }
char * all_tests (void) { fesetround (FE_TOWARDZERO); srand((unsigned)time(NULL)); union uint32_f a, b, c; for (int i = 0; i < 2000000 ; i++) { char aa[33],bb[33],cc[33]; a.i = (uint32_t)( (rand () << 2) + rand ()); b.i = (uint32_t)( (rand () << 2) + rand ()); if (fpclassify (a.f) != FP_NORMAL || fpclassify (b.f) != FP_NORMAL) continue; tests_run++; c.i = fmul (a.i, b.i); if (fpclassify (c.f) != FP_NORMAL || fpclassify (a.f*b.f) != FP_NORMAL) continue; for (int t = 0; t < 32;++t) { aa[31 - t] = a.i & (1 << t) ? '1' : '0'; bb[31 - t] = b.i & (1 << t) ? '1' : '0'; cc[31 - t] = c.i & (1 << t) ? '1' : '0'; } aa[32] = '\0'; bb[32] = '\0'; cc[32] = '\0'; // 非正規化数とかはやらない if (isnormal (a.f) && isnormal (b.f)) { if (!isnan (c.f)) printf ("%s\t%s\t%s\n",aa,bb,cc); } } return NULL; }
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; }
void PictureFlowAnimator::update() { if (!animateTimer.isActive()) return; if (step == 0) return; if (!state) return; int speed = 16384; #if 0 // deaccelerate when approaching the target const int max = 2 * 65536; int fi = frame; fi -= (target << 16); if (fi < 0) fi = -fi; fi = qMin(fi, max); int ia = IANGLE_MAX * (fi - max / 2) / (max * 2); speed = 512 + 16384 * (PFREAL_ONE + fsin(ia)) / PFREAL_ONE; #endif frame += speed * step; int index = frame >> 16; int pos = frame & 0xffff; int neg = 65536 - pos; int tick = (step < 0) ? neg : pos; PFreal ftick = (tick * PFREAL_ONE) >> 16; if (step < 0) index++; if (state->centerIndex != index) { state->centerIndex = index; frame = index << 16; state->centerSlide.slideIndex = state->centerIndex; for (int i = 0; i < (int)state->leftSlides.count(); i++) state->leftSlides[i].slideIndex = state->centerIndex - 1 - i; for (int i = 0; i < (int)state->rightSlides.count(); i++) state->rightSlides[i].slideIndex = state->centerIndex + 1 + i; } state->centerSlide.angle = (step * tick * state->angle) >> 16; state->centerSlide.cx = -step * fmul(state->offsetX, ftick); state->centerSlide.cy = fmul(state->offsetY, ftick); if (state->centerIndex == target) { stop(target); state->reset(); return; } for (int i = 0; i < (int)state->leftSlides.count(); i++) { SlideInfo& si = state->leftSlides[i]; si.angle = state->angle; si.cx = -(state->offsetX + state->spacing * i * PFREAL_ONE + step * state->spacing * ftick); si.cy = state->offsetY; } for (int i = 0; i < (int)state->rightSlides.count(); i++) { SlideInfo& si = state->rightSlides[i]; si.angle = -state->angle; si.cx = state->offsetX + state->spacing * i * PFREAL_ONE - step * state->spacing * ftick; si.cy = state->offsetY; } if (step > 0) { PFreal ftick = (neg * PFREAL_ONE) >> 16; state->rightSlides[0].angle = -(neg * state->angle) >> 16; state->rightSlides[0].cx = fmul(state->offsetX, ftick); state->rightSlides[0].cy = fmul(state->offsetY, ftick); } else {
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; }
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)); }
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; }
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); }
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; } }