int rb_freopen(VALUE fname, const char *mode, FILE *file) { WCHAR *wname, wmode[4]; long len; int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0); if (n > numberof(wmode)) return EINVAL; MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode)); wname = rb_w32_mbstr_to_wstr(CP_UTF8, RSTRING_PTR(fname), rb_long2int(RSTRING_LEN(fname)) + 1, &len); wname[len - 1] = L'\0'; RB_GC_GUARD(fname); #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S) e = _wfreopen(wname, wmode, file) ? 0 : errno; #else { FILE *newfp = 0; e = _wfreopen_s(&newfp, wname, wmode, file); } #endif free(wname); return e; }
void check_data (void) { static const struct { long x; mp_size_t want_size; mp_limb_t want_limb; } data[] = { { 0L, 0 }, { 1L, 1, 1 }, { -1L, -1, 1 }, { LONG_MAX, 1, LONG_MAX }, { -LONG_MAX, -1, LONG_MAX }, { LONG_HIGHBIT, -1, ULONG_HIGHBIT }, }; mpf_t x; int i; for (i = 0; i < numberof (data); i++) { mpf_init (x); mpf_set_si (x, data[i].x); MPF_CHECK_FORMAT (x); if (x->_mp_size != data[i].want_size || (x->_mp_size != 0 && (x->_mp_d[0] != data[i].want_limb || x->_mp_exp != 1))) { printf ("mpf_set_si wrong on data[%d]\n", i); abort(); } mpf_clear (x); mpf_init_set_si (x, data[i].x); MPF_CHECK_FORMAT (x); if (x->_mp_size != data[i].want_size || (x->_mp_size != 0 && (x->_mp_d[0] != data[i].want_limb || x->_mp_exp != 1))) { printf ("mpf_init_set_si wrong on data[%d]\n", i); abort(); } mpf_clear (x); } }
int HTTPConnectionWriteRequest(HTTPConnectionRef const conn, HTTPMethod const method, strarg_t const requestURI, strarg_t const host) { if(!conn) return 0; strarg_t methodstr = http_method_str(method); uv_buf_t parts[] = { uv_buf_init((char *)methodstr, strlen(methodstr)), uv_buf_init((char *)STR_LEN(" ")), uv_buf_init((char *)requestURI, strlen(requestURI)), uv_buf_init((char *)STR_LEN(" HTTP/1.1\r\n")), uv_buf_init((char *)STR_LEN("Host: ")), uv_buf_init((char *)host, strlen(host)), uv_buf_init((char *)STR_LEN("\r\n")), }; return HTTPConnectionWritev(conn, parts, numberof(parts)); }
int EInterfaces::update_time_since_last_send(ifnet_t ifref) { int n; // Iterate over all our interfaces looking for ifref for(n=0; n<numberof(m_aInterfaces); n++) if ( ifref==m_aInterfaces[n].m_ifnet ) { clock_get_uptime(&m_aInterfaces[n].m_TimeSinceLastSend); return 0; } return -1; }
int EInterfaces::set_ssthresh(ifnet_t ifref, int nSSThresh) { int n; // Iterate over all our interfaces looking for ifref for(n=0; n<numberof(m_aInterfaces); n++) if ( ifref==m_aInterfaces[n].m_ifnet ) { m_aInterfaces[n].m_nSSThresh = nSSThresh; return 0; } return -1; }
int main (int argc, char *argv[]) { mpfr_t x, z; int y; int i; tests_start_mpfr (); mpfr_inits2 (53, x, z, (mpfr_ptr) 0); for(i = 0 ; i < numberof (tab) ; i++) { mpfr_set_str (x, tab[i].op1, 16, MPFR_RNDN); y = tab[i].op2; mpfr_add_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_add, 16, MPFR_RNDN)) ERROR1("add_si", i, z, tab[i].res_add); mpfr_sub_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) ERROR1("sub_si", i, z, tab[i].res_sub); mpfr_si_sub (z, y, x, MPFR_RNDZ); mpfr_neg (z, z, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_sub, 16, MPFR_RNDN)) ERROR1("si_sub", i, z, tab[i].res_sub); mpfr_mul_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_mul, 16, MPFR_RNDN)) ERROR1("mul_si", i, z, tab[i].res_mul); mpfr_div_si (z, x, y, MPFR_RNDZ); if (mpfr_cmp_str (z, tab[i].res_div, 16, MPFR_RNDN)) ERROR1("div_si", i, z, tab[i].res_div); } mpfr_set_str1 (x, "1"); mpfr_si_div (z, 1024, x, MPFR_RNDN); if (mpfr_cmp_str1 (z, "1024")) ERROR1("si_div", i, z, "1024"); mpfr_si_div (z, -1024, x, MPFR_RNDN); if (mpfr_cmp_str1 (z, "-1024")) ERROR1("si_div", i, z, "-1024"); mpfr_clears (x, z, (mpfr_ptr) 0); check_invert (); test_generic_add_si (2, 200, 17); test_generic_sub_si (2, 200, 17); test_generic_mul_si (2, 200, 17); test_generic_div_si (2, 200, 17); tests_end_mpfr (); return 0; }
void check_data (void) { static const struct { const char *a; const char *d; int want; } data[] = { { "0", "0", 1 }, { "17", "0", 0 }, { "0", "1", 1 }, { "123", "1", 1 }, { "-123", "1", 1 }, { "0", "2", 1 }, { "1", "2", 0 }, { "2", "2", 1 }, { "-2", "2", 1 }, { "0x100000000000000000000000000000000", "2", 1 }, { "0x100000000000000000000000000000001", "2", 0 }, { "0x3333333333333333", "3", 1 }, { "0x3333333333333332", "3", 0 }, { "0x33333333333333333333333333333333", "3", 1 }, { "0x33333333333333333333333333333332", "3", 0 }, /* divisor changes from 2 to 1 limb after stripping 2s */ { "0x3333333300000000", "0x180000000", 1 }, { "0x33333333333333330000000000000000", "0x18000000000000000", 1 }, { "0x133333333333333330000000000000000", "0x18000000000000000", 0 }, }; mpz_t a, d; int i; mpz_init (a); mpz_init (d); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (a, data[i].a, 0); mpz_set_str_or_abort (d, data[i].d, 0); check_one (a, d, data[i].want); } mpz_clear (a); mpz_clear (d); }
static void bug20081028 (void) { int i; int inexact, res; mpfr_rnd_t rnd; mpfr_t x, y; char *s; mpfr_init2 (x, 32); mpfr_init2 (y, 32); for (i = 0 ; i < numberof (Bug20081028Table) ; i++) { rnd = Bug20081028Table[i].rnd; inexact = Bug20081028Table[i].inexact; mpfr_set_str_binary (x, Bug20081028Table[i].binstr); res = mpfr_strtofr (y, Bug20081028Table[i].str, &s, 10, rnd); if (s == NULL || *s != 0) { printf ("Error in Bug20081028: strtofr didn't parse entire input\n" "for (i=%d) Str=\"%s\"", i, Bug20081028Table[i].str); exit (1); } if (! SAME_SIGN (res, inexact)) { printf ("Error in Bug20081028: expected %s ternary value, " "got %d\nfor (i=%d) Rnd=%s Str=\"%s\"\n Set binary gives: ", inexact > 0 ? "positive" : "negative", res, i, mpfr_print_rnd_mode(rnd), Bug20081028Table[i].str); mpfr_dump (x); printf (" strtofr gives: "); mpfr_dump (y); exit (1); } if (mpfr_cmp (x, y)) { printf ("Error in Bug20081028: Results differ between strtofr and " "set_binary\nfor (i=%d) Rnd=%s Str=\"%s\"\n" " Set binary gives: ", i, mpfr_print_rnd_mode(rnd), Bug20081028Table[i].str); mpfr_dump (x); printf (" strtofr gives: "); mpfr_dump (y); exit (1); } } mpfr_clear (y); mpfr_clear (x); }
static void check_onebit (void) { static const unsigned long data[] = { 1, 32, 52, 53, 54, 63, 64, 65, 128, 256, 511, 512, 513 }; mpz_t z; double got, want; long got_exp, want_exp; int i; mpz_init (z); for (i = 0; i < numberof (data); i++) { mpz_set_ui (z, 1L); mpz_mul_2exp (z, z, data[i]); want = 0.5; want_exp = data[i] + 1; got = mpz_get_d_2exp (&got_exp, z); if (got != want || got_exp != want_exp) { printf ("mpz_get_d_2exp wrong on 2**%ld\n", data[i]); mpz_trace (" z ", z); d_trace (" want ", want); d_trace (" got ", got); printf (" want exp %ld\n", want_exp); printf (" got exp %ld\n", got_exp); abort(); } mpz_set_si (z, -1L); mpz_mul_2exp (z, z, data[i]); want = -0.5; want_exp = data[i] + 1; got = mpz_get_d_2exp (&got_exp, z); if (got != want || got_exp != want_exp) { printf ("mpz_get_d_2exp wrong on -2**%ld\n", data[i]); mpz_trace (" z ", z); d_trace (" want ", want); d_trace (" got ", got); printf (" want exp %ld\n", want_exp); printf (" got exp %ld\n", got_exp); abort(); } } mpz_clear (z); }
void check_com_negs (void) { static const struct { unsigned long bit; mp_size_t inp_size; mp_limb_t inp_n[5]; mp_size_t want_size; mp_limb_t want_n[5]; } data[] = { { GMP_NUMB_BITS, 2, { 1, 1 }, 1, { 1 } }, { GMP_NUMB_BITS+1, 2, { 1, 1 }, 2, { 1, 3 } }, { GMP_NUMB_BITS, 2, { 0, 1 }, 2, { 0, 2 } }, { GMP_NUMB_BITS+1, 2, { 0, 1 }, 2, { 0, 3 } }, }; mpz_t inp, got, want; int i; mpz_init (got); mpz_init (want); mpz_init (inp); for (i = 0; i < numberof (data); i++) { mpz_set_n (inp, data[i].inp_n, data[i].inp_size); mpz_neg (inp, inp); mpz_set_n (want, data[i].want_n, data[i].want_size); mpz_neg (want, want); mpz_set (got, inp); mpz_combit (got, data[i].bit); if (mpz_cmp (got, want) != 0) { printf ("mpz_combit: wrong on neg data[%d]\n", i); mpz_trace ("inp ", inp); printf ("bit %lu\n", data[i].bit); mpz_trace ("got ", got); mpz_trace ("want", want); abort (); } } mpz_clear (inp); mpz_clear (got); mpz_clear (want); }
/*--------------------------------------------------------------------------- * Force a cwnd value ---------------------------------------------------------------------------*/ int EInterfaces::set_cwnd(ifnet_t ifref, int nCwd) { int n; // Iterate over all our interfaces looking for ifref for(n=0; n<numberof(m_aInterfaces); n++) if ( ifref==m_aInterfaces[n].m_ifnet ) { m_aInterfaces[n].m_nCwd = nCwd; m_aInterfaces[n].m_nCwdFractional = 0; return 0; } return -1; }
/* * Returns system temporary directory; typically "/tmp". */ static VALUE etc_systmpdir(void) { VALUE tmpdir; #ifdef _WIN32 WCHAR path[_MAX_PATH]; UINT len = rb_w32_system_tmpdir(path, numberof(path)); if (!len) return Qnil; tmpdir = rb_w32_conv_from_wchar(path, rb_filesystem_encoding()); #else tmpdir = rb_filesystem_str_new_cstr("/tmp"); #endif FL_UNSET(tmpdir, FL_TAINT); return tmpdir; }
int SLNSubmissionWrite(SLNSubmissionRef const sub, byte_t const *const buf, size_t const len) { if(!sub) return 0; assert(sub->tmpfile >= 0); uv_buf_t parts[] = { uv_buf_init((char *)buf, len) }; int rc = async_fs_writeall(sub->tmpfile, parts, numberof(parts), -1); if(rc < 0) { alogf("SLNSubmission write error: %s\n", sln_strerror(rc)); return rc; } sub->size += len; SLNHasherWrite(sub->hasher, buf, len); return 0; }
static VALUE math_gamma(VALUE obj, VALUE x) { static const double fact_table[] = { /* fact(0) */ 1.0, /* fact(1) */ 1.0, /* fact(2) */ 2.0, /* fact(3) */ 6.0, /* fact(4) */ 24.0, /* fact(5) */ 120.0, /* fact(6) */ 720.0, /* fact(7) */ 5040.0, /* fact(8) */ 40320.0, /* fact(9) */ 362880.0, /* fact(10) */ 3628800.0, /* fact(11) */ 39916800.0, /* fact(12) */ 479001600.0, /* fact(13) */ 6227020800.0, /* fact(14) */ 87178291200.0, /* fact(15) */ 1307674368000.0, /* fact(16) */ 20922789888000.0, /* fact(17) */ 355687428096000.0, /* fact(18) */ 6402373705728000.0, /* fact(19) */ 121645100408832000.0, /* fact(20) */ 2432902008176640000.0, /* fact(21) */ 51090942171709440000.0, /* fact(22) */ 1124000727777607680000.0, /* fact(23)=25852016738884976640000 needs 56bit mantissa which is * impossible to represent exactly in IEEE 754 double which have * 53bit mantissa. */ }; double d0, d; double intpart, fracpart; Need_Float(x); d0 = RFLOAT_VALUE(x); /* check for domain error */ if (isinf(d0) && signbit(d0)) domain_error("gamma"); fracpart = modf(d0, &intpart); if (fracpart == 0.0) { if (intpart < 0) domain_error("gamma"); if (0 < intpart && intpart - 1 < (double)numberof(fact_table)) { return DBL2NUM(fact_table[(int)intpart - 1]); } } d = tgamma(d0); return DBL2NUM(d); }
void dopanic(const char *fmt, ...) { /* wrap the cursor */ cprintf("\033v\r\n"); /* TODO use sane screen settings (color, address) */ if (proc_lives != 0x12345678) { kcprintf("No saved info in dopanic; halted.\n"); halt(); } if (proc_enum == 0) { /* Call to panic(const char *fmt, ...) */ struct { LONG pc; } *s = (void *)proc_stk; va_list ap; va_start(ap, fmt); vkcprintf(fmt, ap); va_end(ap); kcprintf("pc = %08lx\n", s->pc); #ifdef __mcoldfire__ } else { /* On ColdFire, the exception frame is the same for all exceptions. */ struct { WORD format_word; WORD sr; LONG pc; } *s = (void *)proc_stk; if (proc_enum >= 2 && proc_enum < numberof(exc_messages)) { kcprintf("Panic: %s.\n", exc_messages[proc_enum]); } else { kcprintf("Panic: Exception number %d.\n", (int) proc_enum); } kcprintf("fw = %04x (format = %d, vector = %d, fault = %d), sr = %04x, pc = %08lx\n", s->format_word, (s->format_word & 0xf000) >> 12, (s->format_word & 0x03fc) >> 2, (s->format_word & 0x0c00) >> 8 | (s->format_word & 0x0002), s->sr, s->pc); } #else } else if (mcpu == 0 && (proc_enum == 2 || proc_enum == 3)) {
node UniqueStringN(char *s, unsigned int len){ int h = hashn(s,len) % numberof(hash_buckets); node p; for (p = hash_buckets[h]; p != NULL; p = CDR(p)) { node q = CAR(p); assertpos(q->tag == unique_string_tag,q); if (strequaln(q->body.unique_string.characters,s,len)) { return q; } } node q = newnode(UNIQUE_STRING,unique_string_tag); q->body.unique_string.characters = strnperm(s,len); q->body.unique_string.hash = h; push(hash_buckets[h],q); return q; }
void printstringlist(){ node p; unsigned int h; pput("String Table\n"); for (h=0; h<numberof(hash_buckets); h++) { for (p = hash_buckets[h]; p != NULL; p = CDR(p)) { node str = CAR(p); assertpos(isstr(str),str); pprint(str); pput(" : "); pprint(str->body.unique_string.symbol_list); pput("\n"); } } pput("\n"); }
void check_data (void) { static const struct { const char *a; const char *b; const char *want; } data[] = { /* This tickled a bug in gmp 4.1.2 mpn/x86/k6/gcd_finda.asm. */ { "0x3FFC000007FFFFFFFFFF00000000003F83FFFFFFFFFFFFFFF80000000000000001", "0x1FFE0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC000000000000000000000001", "5" } }; mpz_t a, b, got, want; int i; mpz_init (a); mpz_init (b); mpz_init (got); mpz_init (want); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (a, data[i].a, 0); mpz_set_str_or_abort (b, data[i].b, 0); mpz_set_str_or_abort (want, data[i].want, 0); mpz_gcd (got, a, b); MPZ_CHECK_FORMAT (got); if (mpz_cmp (got, want) != 0) { printf ("mpz_gcd wrong on data[%d]\n", i); printf (" a %s\n", data[i].a); printf (" b %s\n", data[i].b); mpz_trace (" a", a); mpz_trace (" b", b); mpz_trace (" want", want); mpz_trace (" got ", got); abort (); } } mpz_clear (a); mpz_clear (b); mpz_clear (got); mpz_clear (want); }
void check_data (void) { static const struct { const char *x; double y; int cmp, cmpabs; } data[] = { { "0", 0.0, 0, 0 }, { "1", 0.0, 1, 1 }, { "-1", 0.0, -1, 1 }, { "1", 0.5, 1, 1 }, { "-1", -0.5, -1, 1 }, { "0", 1.0, -1, -1 }, { "0", -1.0, 1, -1 }, { "0x1000000000000000000000000000000000000000000000000", 1.0, 1, 1 }, { "-0x1000000000000000000000000000000000000000000000000", 1.0, -1, 1 }, { "0", 1e100, -1, -1 }, { "0", -1e100, 1, -1 }, { "2", 1.5, 1, 1 }, { "2", -1.5, 1, 1 }, { "-2", 1.5, -1, 1 }, { "-2", -1.5, -1, 1 }, }; mpz_t x; unsigned i; mpz_init (x); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (x, data[i].x, 0); check_one ("check_data", x, data[i].y, data[i].cmp, data[i].cmpabs); } mpz_clear (x); }
/* Check that hardware rounding doesn't make mpfr_get_d_2exp return a value outside its defined range. */ static void check_round (void) { static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 }; mpfr_t f; double got; long got_exp; int i, rnd_mode, neg; mpfr_init2 (f, 1024L); for (rnd_mode = 0; rnd_mode < MPFR_RND_MAX ; rnd_mode++) { for (i = 0; i < (int) numberof (data); i++) { mpfr_set_ui (f, 1L, MPFR_RNDZ); mpfr_mul_2exp (f, f, data[i], MPFR_RNDZ); mpfr_sub_ui (f, f, 1L, MPFR_RNDZ); for (neg = 0; neg <= 1; neg++) { got = mpfr_get_d_2exp (&got_exp, f, (mpfr_rnd_t) rnd_mode); if (neg == 0 ? (got < 0.5 || got >= 1.0) : (got <= -1.0 || got > -0.5)) { printf ("mpfr_get_d_2exp wrong on 2**%lu-1\n", data[i]); printf ("result out of range, expect 0.5 <= got < 1.0\n"); printf (" rnd_mode = %d\n", rnd_mode); printf (" data[i] = %lu\n", data[i]); printf (" f "); mpfr_out_str (stdout, 2, 0, f, MPFR_RNDN); printf ("\n"); d_trace (" got ", got); printf (" got exp %ld\n", got_exp); exit(1); } mpfr_neg (f, f, MPFR_RNDZ); } } } mpfr_clear (f); }
int HTTPConnectionWriteResponse(HTTPConnectionRef const conn, uint16_t const status, strarg_t const message) { assertf(status >= 100 && status < 600, "Invalid HTTP status %d", (int)status); if(!conn) return 0; str_t status_str[4+1]; int status_len = snprintf(status_str, sizeof(status_str), "%d", status); assert(3 == status_len); uv_buf_t parts[] = { uv_buf_init((char *)STR_LEN("HTTP/1.1 ")), uv_buf_init(status_str, status_len), uv_buf_init((char *)STR_LEN(" ")), uv_buf_init((char *)message, strlen(message)), uv_buf_init((char *)STR_LEN("\r\n")), }; return HTTPConnectionWritev(conn, parts, numberof(parts)); }
int HTTPConnectionWriteSetCookie(HTTPConnectionRef const conn, strarg_t const cookie, strarg_t const path, uint64_t const maxage) { assert(cookie); assert(path); if(!conn) return 0; str_t maxage_str[16]; int const maxage_len = snprintf(maxage_str, sizeof(maxage_str), "%llu", (unsigned long long)maxage); uv_buf_t parts[] = { uv_buf_init((char *)STR_LEN("Set-Cookie: ")), uv_buf_init((char *)cookie, strlen(cookie)), uv_buf_init((char *)STR_LEN("; Path=")), uv_buf_init((char *)path, strlen(path)), uv_buf_init((char *)STR_LEN("; Max-Age=")), uv_buf_init(maxage_str, maxage_len), uv_buf_init((char *)STR_LEN("; HttpOnly\r\n")), }; return HTTPConnectionWritev(conn, parts, numberof(parts)); }
/*--------------------------------------------------------------------------- * Check if the outstanding count on all enabled interfaces has been reached ---------------------------------------------------------------------------*/ int EInterfaces::all_full(int nMax) { int n; int nRet = 0; for(n=0; n<numberof(m_aInterfaces); n++) if ( m_aInterfaces[n].m_fEnabled ) { if ( m_aInterfaces[n].m_nOutstandingCount < nMax ) { nRet = -1; break; } } return nRet; }
int SLNFilterWriteURIs(SLNFilterRef const filter, SLNSessionRef const session, SLNFilterPosition *const pos, bool const meta, uint64_t const max, bool const wait, SLNFilterWriteCB const writecb, SLNFilterFlushCB const flushcb, void *ctx) { uint64_t remaining = max; for(;;) { ssize_t const count = SLNFilterWriteURIBatch(filter, session, pos, meta, remaining, writecb, ctx); if(count < 0) return count; remaining -= count; if(!remaining) return 0; if(!count) break; } if(!wait || pos->dir < 0) return 0; SLNRepoRef const repo = SLNSessionGetRepo(session); for(;;) { int rc = flushcb ? flushcb(ctx) : 0; if(rc < 0) return rc; uint64_t latest = pos->sortID; uint64_t const timeout = uv_now(async_loop)+(1000 * 30); rc = SLNRepoSubmissionWait(repo, &latest, timeout); if(UV_ETIMEDOUT == rc) { uv_buf_t const parts[] = { uv_buf_init((char *)STR_LEN("\r\n")) }; rc = writecb(ctx, parts, numberof(parts)); if(rc < 0) break; continue; } assert(rc >= 0); // TODO: Handle cancellation? for(;;) { ssize_t const count = SLNFilterWriteURIBatch(filter, session, pos, meta, remaining, writecb, ctx); if(count < 0) return count; remaining -= count; if(!remaining) return 0; if(count < BATCH_SIZE) break; } // This is how far we scanned, even if we didn't find anything. if(pos->sortID < latest) { pos->sortID = latest; pos->fileID = 0; } } return 0; }
/* Test operands from a table of seed data. This variant creates the operands using plain ol' mpz_rrandomb. This is a hack for better coverage of the gcd code, which depends on that the random number generators give the exact numbers we expect. */ void check_kolmo1 (void) { static const struct { unsigned int seed; int nb; const char *want; } data[] = { { 59618, 38208, "5"}, { 76521, 49024, "3"}, { 85869, 54976, "1"}, { 99449, 63680, "1"}, {112453, 72000, "1"} }; gmp_randstate_t rs; mpz_t bs, a, b, want; int i, unb, vnb, nb; gmp_randinit_default (rs); mpz_inits (bs, a, b, want, NULL); for (i = 0; i < numberof (data); i++) { nb = data[i].nb; gmp_randseed_ui (rs, data[i].seed); mpz_urandomb (bs, rs, 32); unb = mpz_get_ui (bs) % nb; mpz_urandomb (bs, rs, 32); vnb = mpz_get_ui (bs) % nb; mpz_rrandomb (a, rs, unb); mpz_rrandomb (b, rs, vnb); mpz_set_str_or_abort (want, data[i].want, 0); one_test (a, b, want, -1); } mpz_clears (bs, a, b, want, NULL); gmp_randclear (rs); }
MPFR_HOT_FUNCTION_ATTR void mpfr_mpz_init (mpz_t z) { if (MPFR_LIKELY (n_alloc > 0)) { /* Get a mpz_t from the MPFR stack of previously used mpz_t. It reduces memory pressure, and it allows to reuse a mpz_t that should be sufficiently big. */ MPFR_ASSERTD (n_alloc <= numberof (mpz_tab)); memcpy (z, &mpz_tab[--n_alloc], sizeof (mpz_t)); SIZ(z) = 0; } else { /* Call the real GMP function */ mpz_init (z); } }
/* * call-seq: * SSLContext.new => ctx * SSLContext.new(:TLSv1) => ctx * SSLContext.new("SSLv23_client") => ctx * * You can get a list of valid methods with OpenSSL::SSL::SSLContext::METHODS */ static VALUE ossl_sslctx_initialize(int argc, VALUE *argv, VALUE self) { VALUE ssl_method; int i; for(i = 0; i < numberof(ossl_sslctx_attrs); i++){ char buf[32]; snprintf(buf, sizeof(buf), "@%s", ossl_sslctx_attrs[i]); rb_iv_set(self, buf, Qnil); } if (rb_scan_args(argc, argv, "01", &ssl_method) == 0){ return self; } ossl_sslctx_set_ssl_version(self, ssl_method); return self; }
static void check_reftable (void) { int i, base; mpfr_t x, y; mpfr_prec_t p; char *s; mpfr_init2 (x, 200); mpfr_init2 (y, 200); for (i = 0 ; i < numberof (RefTable) ; i++) { base = RefTable[i].base; p = RefTable[i].prec; mpfr_set_prec (x, p); mpfr_set_prec (y, p); mpfr_set_str_binary (x, RefTable[i].binstr); mpfr_strtofr (y, RefTable[i].str, &s, base, MPFR_RNDN); if (s == NULL || *s != 0) { printf ("strtofr didn't parse entire input for i=%d:\n" " Str=%s", i, RefTable[i].str); exit (1); } if (mpfr_cmp (x, y)) { printf ("Results differ between strtofr and set_binary for i=%d:\n" " Set binary gives: ", i); mpfr_dump (x); printf (" strtofr gives: "); mpfr_dump (y); printf (" setstr gives: "); mpfr_set_str (x, RefTable[i].str, base, MPFR_RNDN); mpfr_dump (x); mpfr_set_prec (x, 2*p); mpfr_set_str (x, RefTable[i].str, base, MPFR_RNDN); printf (" setstr ++ gives: "); mpfr_dump (x); exit (1); } } mpfr_clear (y); mpfr_clear (x); }
int mpz_kronecker_si (mpz_srcptr a, long b) { mp_srcptr a_ptr; mp_size_t a_size; mp_limb_t a_rem, b_limb; int result_bit1; a_size = SIZ(a); if (a_size == 0) return JACOBI_0S (b); #if GMP_NUMB_BITS < BITS_PER_ULONG if (b > GMP_NUMB_MAX || b < -GMP_NUMB_MAX) { mp_limb_t blimbs[2]; mpz_t bz; ALLOC(bz) = numberof (blimbs); PTR(bz) = blimbs; mpz_set_si (bz, b); return mpz_kronecker (a, bz); } #endif result_bit1 = JACOBI_BSGN_SS_BIT1 (a_size, b); b_limb = (unsigned long) ABS (b); a_ptr = PTR(a); if ((b_limb & 1) == 0) { mp_limb_t a_low = a_ptr[0]; int twos; if (b_limb == 0) return JACOBI_LS0 (a_low, a_size); /* (a/0) */ if (! (a_low & 1)) return 0; /* (even/even)=0 */ /* (a/2)=(2/a) for a odd */ count_trailing_zeros (twos, b_limb); b_limb >>= twos; result_bit1 ^= JACOBI_TWOS_U_BIT1 (twos, a_low); }
static VALUE math_gamma(VALUE obj, VALUE x) { static const double fact_table[] = { /* fact(0) */ 1.0, /* fact(1) */ 1.0, /* fact(2) */ 2.0, /* fact(3) */ 6.0, /* fact(4) */ 24.0, /* fact(5) */ 120.0, /* fact(6) */ 720.0, /* fact(7) */ 5040.0, /* fact(8) */ 40320.0, /* fact(9) */ 362880.0, /* fact(10) */ 3628800.0, /* fact(11) */ 39916800.0, /* fact(12) */ 479001600.0, /* fact(13) */ 6227020800.0, /* fact(14) */ 87178291200.0, /* fact(15) */ 1307674368000.0, /* fact(16) */ 20922789888000.0, /* fact(17) */ 355687428096000.0, /* fact(18) */ 6402373705728000.0, /* fact(19) */ 121645100408832000.0, /* fact(20) */ 2432902008176640000.0, /* fact(21) */ 51090942171709440000.0, /* fact(22) */ 1124000727777607680000.0, /* fact(23)=25852016738884976640000 needs 56bit mantissa which is * impossible to represent exactly in IEEE 754 double which have * 53bit mantissa. */ }; enum {NFACT_TABLE = numberof(fact_table)}; double d; d = Get_Double(x); /* check for domain error */ if (isinf(d) && signbit(d)) domain_error("gamma"); if (d == floor(d)) { if (d < 0.0) domain_error("gamma"); if (1.0 <= d && d <= (double)NFACT_TABLE) { return DBL2NUM(fact_table[(int)d - 1]); } } return DBL2NUM(tgamma(d)); }