// Computes z = Delta(q) (see Cohen). static void compute_Delta(mpc_t z, mpc_t q) { int d; int n; int power; mpc_t z0, z1, z2; mpc_init(z0); mpc_init(z1); mpc_init(z2); mpc_set_ui(z0, 1); d = -1; for(n=1; n<100; n++) { power = n *(3 * n - 1) / 2; mpc_pow_ui(z1, q, power); mpc_pow_ui(z2, q, n); mpc_mul(z2, z2, z1); mpc_add(z1, z1, z2); if (d) { mpc_sub(z0, z0, z1); d = 0; } else { mpc_add(z0, z0, z1); d = 1; } } mpc_pow_ui(z0, z0, 24); mpc_mul(z, z0, q); mpc_clear(z0); mpc_clear(z1); mpc_clear(z2); }
// Computes j = j(tau). static void compute_j(mpc_t j, mpc_t tau) { mpc_t h; mpc_t z0; mpc_init(h); mpc_init(z0); compute_h(h, tau); //mpc_mul_ui(z0, h, 256); mpc_mul_2exp(z0, h, 8); mpc_add_ui(z0, z0, 1); mpc_pow_ui(z0, z0, 3); mpc_div(j, z0, h); mpc_clear(z0); mpc_clear(h); }
// Computes z = h(tau) // (called h() by Blake et al, f() by Cohen.) static void compute_h(mpc_t z, mpc_t tau) { mpc_t z0, z1, q; mpc_init(q); mpc_init(z0); mpc_init(z1); compute_q(q, tau); mpc_mul(z0, q, q); compute_Delta(z0, z0); compute_Delta(z1, q); mpc_div(z, z0, z1); mpc_clear(q); mpc_clear(z0); mpc_clear(z1); }
static void timemul (void) { /* measures the time needed with different precisions for naive and */ /* Karatsuba multiplication */ mpc_t x, y, z; unsigned long int i, j; const unsigned long int tests = 10000; struct tms time_old, time_new; double passed1, passed2; mpc_init (x); mpc_init (y); mpc_init_set_ui_ui (z, 1, 0, MPC_RNDNN); for (i = 1; i < 50; i++) { mpc_set_prec (x, i * BITS_PER_MP_LIMB); mpc_set_prec (y, i * BITS_PER_MP_LIMB); mpc_set_prec (z, i * BITS_PER_MP_LIMB); test_default_random (x, -1, 1, 128, 25); test_default_random (y, -1, 1, 128, 25); times (&time_old); for (j = 0; j < tests; j++) mpc_mul_naive (z, x, y, MPC_RNDNN); times (&time_new); passed1 = ((double) (time_new.tms_utime - time_old.tms_utime)) / 100; times (&time_old); for (j = 0; j < tests; j++) mpc_mul_karatsuba (z, x, y, MPC_RNDNN); times (&time_new); passed2 = ((double) (time_new.tms_utime - time_old.tms_utime)) / 100; printf ("Time for %3li limbs naive/Karatsuba: %5.2f %5.2f\n", i, passed1, passed2); } mpc_clear (x); mpc_clear (y); mpc_clear (z); }
// Computes q = exp(2 pi i tau). static void compute_q(mpc_t q, mpc_t tau) { mpc_t z0; mpf_t f0, f1; mpf_ptr fp0; pbc_mpui pwr; mpc_init(z0); mpf_init(f0); mpf_init(f1); //compute z0 = 2 pi i tau mpc_set(z0, tau); //first remove integral part of Re(tau) //since exp(2 pi i) = 1 //it seems |Re(tau)| < 1 anyway? fp0 = mpc_re(z0); mpf_trunc(f1, fp0); mpf_sub(fp0, fp0, f1); mpc_mul_mpf(z0, z0, pi); mpc_mul_ui(z0, z0, 2); mpc_muli(z0, z0); //compute q = exp(z0); //first write z0 = A + a + b i //where A is a (negative) integer //and a, b are in [-1, 1] //compute e^A separately fp0 = mpc_re(z0); pwr = mpf_get_ui(fp0); mpf_pow_ui(f0, recipeulere, pwr); mpf_add_ui(fp0, fp0, pwr); mpf_exp(f1, mpc_re(z0)); mpf_mul(f0, f1, f0); mpc_cis(q, mpc_im(z0)); /* old_mpc_exp(q, z0); */ mpc_mul_mpf(q, q, f0); mpc_clear(z0); mpf_clear(f0); mpf_clear(f1); }
int main(int ac, char **av, char **ae) { appclassname_t cn; appsubclassname_t sc; char ch[256]; channel_t chan[2]; u_short cluster; pnode_t pnode; int nclusters; int res; mpc_init(); mpc_get_local_infos(&cluster, &pnode, &nclusters); printf("cluster=%d/pnode=%d/nclusters=%d\n", cluster, pnode, nclusters); printf("node_count[1st cluster]=%d\n", mpc_get_node_count(0)); cn = make_appclass(); printf("cn : 0x%lx\n", cn); mpc_spawn_task("sleep 10000; ls", atoi(av[1]), atoi(av[2]), cn); sc = make_subclass_prefnode(/*cluster*/ atoi(av[1]), /* pnode */ atoi(av[2]), /* value */ atoi(av[3])); res = mpc_get_channel(APPCLASS_INTERNET, sc, &chan[0], &chan[1], HSL_PROTO_SLRP_V); if (res) printf("mpc_get_channel() failed\n"); else printf("chan0 = 0x%x - chan1 = 0x%x\n", (int) chan[0], (int) chan[1]); fgets(ch, sizeof(ch), stdin); delete_appclass(cn); mpc_close(); return 0; }
int main(int ac, char **av, char **ae) { appsubclassname_t sc; char ch[256]; channel_t chan[2]; u_short cluster; pnode_t pnode, distnode; int nclusters; int i, res; fd_set readfds; mpc_chan_set readchs; struct timeval tv; int raw; if (ac > 1) raw = 1; else raw = 0; mpc_init(); mpc_get_local_infos(&cluster, &pnode, &nclusters); distnode = (!pnode) ? 1 : 0; sc = make_subclass_prefnode(cluster, distnode, 0xAFAF); res = mpc_get_channel(APPCLASS_INTERNET, sc, &chan[0], &chan[1], HSL_PROTO_MDCP); if (res) printf("mpc_get_channel() failed\n"); else if (!raw) printf("chan0 = 0x%x - chan1 = 0x%x\n", (int) chan[0], (int) chan[1]); for (;;) { FD_ZERO(&readfds); FD_SET(0, &readfds); MPC_CHAN_ZERO(&readchs); MPC_CHAN_SET(distnode, chan[0], &readchs); tv.tv_sec = 5; tv.tv_usec = 0; if (!raw) printf("mpc_select()\n"); res = mpc_select(1, &readfds, NULL, NULL, &readchs, NULL, NULL, &tv); if (res < 0) { perror("mpc_select"); exit(1); } if (!raw) printf("mpc_select() returned %d\n", res); #if 0 printf("readchs.max_index = %d\n", readchs.max_index); for (i = 0; i <= readchs.max_index; i++) printf("readchs: idx=%d is_set=%d\n", i, readchs.chan_set[i].is_set); #endif if (FD_ISSET(0, &readfds) == TRUE) { if (!raw) printf("WE CAN READ ON STDIN\n"); res = read(0, ch, sizeof ch); if (res < 0) { perror("read"); exit(1); } if (!res) { printf("EOF!\n"); exit(0); } if (res > 0) { res = mpc_write(distnode, chan[0], ch, res); if (res < 0) { perror("write"); exit(1); } } } if (MPC_CHAN_ISSET(distnode, chan[0], &readchs) == TRUE) { if (!raw) printf("WE CAN READ ON THE CHANNEL\n"); res = mpc_read(distnode, chan[0], ch, sizeof ch); if (res < 0) perror("mpc_read"); if (!raw) printf("mpc_read() -> %d\n", res); if (!res) { printf("EOF!\n"); exit(0); } if (res > 0) { if (raw) write(1, ch, res); else for (i = 0; i < res; i++) printf("ch[%d] = %#x (%c)\n", i, (int) ch[i], ch[i]); } } } #if 0 res = mpc_close_channel(APPCLASS_INTERNET, sc); printf("status = %d\n", res); #endif mpc_close(); return 0; }
int music_init(void) { pspTime tm; cache_init(); xrRtcGetCurrentClockLocalTime(&tm); srand(tm.microseconds); xr_lock_init(&music_l); #ifdef ENABLE_MPC mpc_init(); #endif #ifdef ENABLE_WAV wav_init(); #endif #ifdef ENABLE_TTA tta_init(); #endif #ifdef ENABLE_APE ape_init(); #endif #ifdef ENABLE_MP3 mp3_init(); #endif #ifdef ENABLE_FLAC flac_init(); #endif #ifdef ENABLE_OGG ogg_init(); #endif #ifdef ENABLE_WMA wmadrv_init(); #endif #ifdef ENABLE_WAVPACK wv_init(); #endif #ifdef ENABLE_AT3 at3_init(); #endif #ifdef ENABLE_M4A m4a_init(); #endif #ifdef ENABLE_AAC aac_init(); #endif #ifdef ENABLE_AA3 aa3_init(); #endif memset(&g_list, 0, sizeof(g_list)); g_list.first_time = true; g_shuffle.first_time = true; stack_init(&played); g_music_thread = xrKernelCreateThread("Music Thread", music_thread, 0x12, 0x10000, 0, NULL); if (g_music_thread < 0) { return -EBUSY; } xrKernelStartThread(g_music_thread, 0, 0); return 0; }
int music_init(void) { u32 seed; cache_init(); seed = sctrlKernelRand(); if (seed == 0x8002013A) { pspTime tm; sceRtcGetCurrentClockLocalTime(&tm); seed = tm.microseconds; } srand(seed); xr_lock_init(&music_l); #ifdef ENABLE_MPC mpc_init(); #endif #ifdef ENABLE_WAV wav_init(); #endif #ifdef ENABLE_TTA tta_init(); #endif #ifdef ENABLE_APE ape_init(); #endif #ifdef ENABLE_MP3 mp3_init(); #endif #ifdef ENABLE_FLAC flac_init(); #endif #ifdef ENABLE_OGG ogg_init(); #endif #ifdef ENABLE_WMA wmadrv_init(); #endif #ifdef ENABLE_WAVPACK wv_init(); #endif #ifdef ENABLE_AT3 at3_init(); #endif #ifdef ENABLE_M4A m4a_init(); #endif #ifdef ENABLE_AAC aac_init(); #endif #ifdef ENABLE_AA3 aa3_init(); #endif musiclist_init(&g_music_list); memset(&g_list, 0, sizeof(g_list)); g_list.first_time = true; stack_init(&g_played); g_music_thread = sceKernelCreateThread("Music Thread", music_thread, 0x12, 0x10000, 0, NULL); if (g_music_thread < 0) { return -EBUSY; } sceKernelStartThread(g_music_thread, 0, 0); return 0; }
// See Cohen; my D is -D in his notation. size_t pbc_hilbert(mpz_t **arr, int D) { int a, b; int t; int B = (int)floor(sqrt((double) D / 3.0)); mpc_t alpha; mpc_t j; mpf_t sqrtD; mpf_t f0; darray_t Pz; mpc_t z0, z1, z2; double d = 1.0; int h = 1; int jcount = 1; // Compute required precision. b = D % 2; for (;;) { t = (b*b + D) / 4; a = b; if (a <= 1) { a = 1; goto step535_4; } step535_3: if (!(t % a)) { jcount++; if ((a == b) || (a*a == t) || !b) { d += 1.0 / ((double) a); h++; } else { d += 2.0 / ((double) a); h+=2; } } step535_4: a++; if (a * a <= t) { goto step535_3; } else { b += 2; if (b > B) break; } } //printf("modulus: %f\n", exp(3.14159265358979 * sqrt(D)) * d * 0.5); d *= sqrt(D) * 3.14159265358979 / log(2); precision_init((int)(d + 34)); pbc_info("class number %d, %d bit precision", h, (int) d + 34); darray_init(Pz); mpc_init(alpha); mpc_init(j); mpc_init(z0); mpc_init(z1); mpc_init(z2); mpf_init(sqrtD); mpf_init(f0); mpf_sqrt_ui(sqrtD, D); b = D % 2; h = 0; for (;;) { t = (b*b + D) / 4; if (b > 1) { a = b; } else { a = 1; } step3: if (t % a) { step4: a++; if (a * a <= t) goto step3; } else { // a, b, t/a are coeffs of an appropriate primitive reduced positive // definite form. // Compute j((-b + sqrt{-D})/(2a)). h++; pbc_info("[%d/%d] a b c = %d %d %d", h, jcount, a, b, t/a); mpf_set_ui(f0, 1); mpf_div_ui(f0, f0, 2 * a); mpf_mul(mpc_im(alpha), sqrtD, f0); mpf_mul_ui(f0, f0, b); mpf_neg(mpc_re(alpha), f0); compute_j(j, alpha); if (0) { int i; for (i=Pz->count - 1; i>=0; i--) { printf("P %d = ", i); mpc_out_str(stdout, 10, 4, Pz->item[i]); printf("\n"); } } if (a == b || a * a == t || !b) { // P *= X - j int i, n; mpc_ptr p0; p0 = (mpc_ptr) pbc_malloc(sizeof(mpc_t)); mpc_init(p0); mpc_neg(p0, j); n = Pz->count; if (n) { mpc_set(z1, Pz->item[0]); mpc_add(Pz->item[0], z1, p0); for (i=1; i<n; i++) { mpc_mul(z0, z1, p0); mpc_set(z1, Pz->item[i]); mpc_add(Pz->item[i], z1, z0); } mpc_mul(p0, p0, z1); } darray_append(Pz, p0); } else { // P *= X^2 - 2 Re(j) X + |j|^2 int i, n; mpc_ptr p0, p1; p0 = (mpc_ptr) pbc_malloc(sizeof(mpc_t)); p1 = (mpc_ptr) pbc_malloc(sizeof(mpc_t)); mpc_init(p0); mpc_init(p1); // p1 = - 2 Re(j) mpf_mul_ui(f0, mpc_re(j), 2); mpf_neg(f0, f0); mpf_set(mpc_re(p1), f0); // p0 = |j|^2 mpf_mul(f0, mpc_re(j), mpc_re(j)); mpf_mul(mpc_re(p0), mpc_im(j), mpc_im(j)); mpf_add(mpc_re(p0), mpc_re(p0), f0); n = Pz->count; if (!n) { } else if (n == 1) { mpc_set(z1, Pz->item[0]); mpc_add(Pz->item[0], z1, p1); mpc_mul(p1, z1, p1); mpc_add(p1, p1, p0); mpc_mul(p0, p0, z1); } else { mpc_set(z2, Pz->item[0]); mpc_set(z1, Pz->item[1]); mpc_add(Pz->item[0], z2, p1); mpc_mul(z0, z2, p1); mpc_add(Pz->item[1], z1, z0); mpc_add(Pz->item[1], Pz->item[1], p0); for (i=2; i<n; i++) { mpc_mul(z0, z1, p1); mpc_mul(alpha, z2, p0); mpc_set(z2, z1); mpc_set(z1, Pz->item[i]); mpc_add(alpha, alpha, z0); mpc_add(Pz->item[i], z1, alpha); } mpc_mul(z0, z2, p0); mpc_mul(p1, p1, z1); mpc_add(p1, p1, z0); mpc_mul(p0, p0, z1); } darray_append(Pz, p1); darray_append(Pz, p0); } goto step4; } b+=2; if (b > B) break; } // Round polynomial and assign. int k = 0; { *arr = pbc_malloc(sizeof(mpz_t) * (Pz->count + 1)); int i; for (i=Pz->count - 1; i>=0; i--) { if (mpf_sgn(mpc_re(Pz->item[i])) < 0) { mpf_set_d(f0, -0.5); } else { mpf_set_d(f0, 0.5); } mpf_add(f0, f0, mpc_re(Pz->item[i])); mpz_init((*arr)[k]); mpz_set_f((*arr)[k], f0); k++; mpc_clear(Pz->item[i]); pbc_free(Pz->item[i]); } mpz_init((*arr)[k]); mpz_set_ui((*arr)[k], 1); k++; } darray_clear(Pz); mpc_clear(z0); mpc_clear(z1); mpc_clear(z2); mpf_clear(f0); mpf_clear(sqrtD); mpc_clear(alpha); mpc_clear(j); precision_clear(); return k; }