/* Sets the location in memory where a frame should be stored. This value is written into a software register * in the vision core and then incremented as bursting takes place. Because this value is used during a * frame capture, it SHOULD NOT be changed while the core is in operation. * @param frame_id int type of frame address to be set. Currently we only capture one frame, so there is only one choice, VISION_FRAME_RGB565, but more frame types can be added here so multiple images can be saved for each frame * @param frameAddr int memory address where this frame type should be saved. Make sure this memory address has been properly malloc'ed before passing it to the core. */ void SetFrameAddress(Xuint32 frame_id, Xuint32 frameAddr) { int offset = 0x0; switch(frame_id) { case VISION_FRAME_RGB565: offset = OFFSET_ORG_FRAME_MEM_ADDR; break; /*case VISION_FRAME_GRAYSCALE8: //this is just an example, this fifo is not built into hardware offset = OFFSET_SEG_FRAME_MEM_ADDR; break;*/ default: return; } *PTR(BASEADDR + offset) = frameAddr; }
/* Optimized naHash_get for looking up local variables (OP_LOCAL is by * far the most common opcode and deserves some special case * optimization). Assumes that the key is an interned symbol * (i.e. the hash code is precomputed, and we only need to test for * pointer identity). */ int naiHash_sym(struct naHash* hash, struct naStr* sym, naRef* out) { HashRec* hr = hash->rec; if(hr) { int* tab = TAB(hr); HashEnt* ents = ENTS(hr); unsigned int hc = sym->hashcode; int cell, mask = POW2(hr->lgsz+1) - 1, step = (2*hc+1) & mask; for(cell=HBITS(hr,hc); tab[cell] != ENT_EMPTY; cell=(cell+step)&mask) if(tab[cell]!=ENT_DELETED && sym==PTR(ents[tab[cell]].key).str) { *out = ents[tab[cell]].val; return 1; } } return 0; }
/* Create a fake mpz consisting of just a single 1 bit, with totbits being the total number of bits, inclusive of that 1 bit. */ void mpz_fake_bits (mpz_ptr z, unsigned long totbits) { static mp_limb_t n; unsigned long zero_bits, zero_limbs; zero_bits = totbits - 1; zero_limbs = zero_bits / GMP_NUMB_BITS; zero_bits %= GMP_NUMB_BITS; SIZ(z) = zero_limbs + 1; PTR(z) = (&n) - (SIZ(z) - 1); n = CNST_LIMB(1) << zero_bits; ASSERT_ALWAYS (mpz_sizeinbase (z, 2) == totbits); }
void mpz_array_init (mpz_ptr arr, mp_size_t arr_size, mp_size_t nbits) { mp_ptr p; mp_size_t i; mp_size_t nlimbs; nlimbs = nbits / GMP_NUMB_BITS + 1; p = __GMP_ALLOCATE_FUNC_LIMBS (arr_size * nlimbs); for (i = 0; i < arr_size; i++) { ALLOC (&arr[i]) = nlimbs + 1; /* Yes, lie a little... */ SIZ (&arr[i]) = 0; PTR (&arr[i]) = p + i * nlimbs; } }
/* this function is useful in debug mode to print residues */ static void mpres_print (mpres_t x, char* name, mpmod_t n) { mp_size_t m, xn; mpres_t t; mpres_init(t, n); mpz_set_ui(t, 1); mpres_mul (t, x, t, n); xn = SIZ(t); m = ABSIZ(t); MPN_NORMALIZE(PTR(t), m); SIZ(t) = xn >= 0 ? m : -m; gmp_printf ("%s=%Zd\n", name, t); SIZ(t) = xn; mpres_clear (t, n); }
/* generate nbits random bits into mp[], assuming mp was allocated to contain a sufficient number of limbs */ void mpfr_rand_raw (mpfr_limb_ptr mp, gmp_randstate_t rstate, mpfr_prec_t nbits) { mpz_t z; MPFR_ASSERTN (nbits >= 1); /* To be sure to avoid the potential allocation of mpz_urandomb */ ALLOC(z) = SIZ(z) = MPFR_PREC2LIMBS (nbits); PTR(z) = mp; #if __MPFR_GMP(5,0,0) /* Check for integer overflow (unless mp_bitcnt_t is signed, but according to the GMP manual, this shouldn't happen). Note: mp_bitcnt_t has been introduced in GMP 5.0.0. */ MPFR_ASSERTN ((mp_bitcnt_t) -1 < 0 || nbits <= (mp_bitcnt_t) -1); #endif mpz_urandomb (z, rstate, nbits); }
/* Whether the absolute value of z is a power of 2. */ int mpz_pow2abs_p (mpz_srcptr z) { mp_size_t size, i; mp_srcptr ptr; size = SIZ (z); if (size == 0) return 0; /* zero is not a power of 2 */ size = ABS (size); ptr = PTR (z); for (i = 0; i < size-1; i++) if (ptr[i] != 0) return 0; /* non-zero low limb means not a power of 2 */ return POW2_P (ptr[i]); /* high limb power of 2 */ }
int mpz_invert (mpz_ptr inverse, mpz_srcptr x, mpz_srcptr n) { mpz_t gcd, tmp; mp_size_t xsize, nsize, size; TMP_DECL; xsize = ABSIZ (x); nsize = ABSIZ (n); /* No inverse exists if the leftside operand is 0. Likewise, no inverse exists if the mod operand is 1. */ if (xsize == 0 || (nsize == 1 && (PTR (n))[0] == 1)) return 0; size = MAX (xsize, nsize) + 1; TMP_MARK; MPZ_TMP_INIT (gcd, size); MPZ_TMP_INIT (tmp, size); mpz_gcdext (gcd, tmp, (mpz_ptr) 0, x, n); /* If no inverse existed, return with an indication of that. */ if (!MPZ_EQUAL_1_P (gcd)) { TMP_FREE; return 0; } /* Make sure we return a positive inverse. */ if (SIZ (tmp) < 0) { if (SIZ (n) < 0) mpz_sub (inverse, tmp, n); else mpz_add (inverse, tmp, n); } else mpz_set (inverse, tmp); TMP_FREE; return 1; }
/* As above, a special naHash_set for setting local variables. * Assumes that the key is interned, and also that it isn't already * present in the hash. */ void naiHash_newsym(struct naHash* hash, naRef* sym, naRef* val) { HashRec* hr = hash->rec; int mask, step, cell, ent; struct naStr *s = PTR(*sym).str; if(!hr || hr->next >= POW2(hr->lgsz)) hr = resize(hash); mask = POW2(hr->lgsz+1) - 1; step = (2*s->hashcode+1) & mask; cell = HBITS(hr, s->hashcode); while(TAB(hr)[cell] != ENT_EMPTY) cell = (cell + step) & mask; ent = hr->next++; if(ent >= NCELLS(hr)) return; /* race protection, don't overrun */ TAB(hr)[cell] = ent; hr->size++; ENTS(hr)[TAB(hr)[cell]].key = *sym; ENTS(hr)[TAB(hr)[cell]].val = *val; }
void mpz_ui_sub (mpz_ptr w, unsigned long int uval, mpz_srcptr v) { mp_ptr vp, wp; mp_size_t vn, wn; mp_limb_t cy; #if GMP_NAIL_BITS != 0 if (uval > GMP_NUMB_MAX) { mpz_t u; mp_limb_t ul[2]; PTR(u) = ul; ul[0] = uval & GMP_NUMB_MASK; ul[1] = uval >> GMP_NUMB_BITS; SIZ(u) = 2; mpz_sub (w, u, v); return; }
unsigned long int mpz_tdiv_ui (mpz_srcptr dividend, unsigned long int divisor) { mp_size_t ns, nn; mp_ptr np; mp_limb_t rl; if (divisor == 0) DIVIDE_BY_ZERO; ns = SIZ(dividend); if (ns == 0) { return 0; } nn = ABS(ns); np = PTR(dividend); #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (divisor > GMP_NUMB_MAX) { mp_limb_t dp[2], rp[2]; mp_ptr qp; mp_size_t rn; TMP_DECL; if (nn == 1) /* tdiv_qr requirements; tested above for 0 */ { rl = np[0]; return rl; } TMP_MARK; dp[0] = divisor & GMP_NUMB_MASK; dp[1] = divisor >> GMP_NUMB_BITS; qp = TMP_ALLOC_LIMBS (nn - 2 + 1); mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2); TMP_FREE; rl = rp[0] + (rp[1] << GMP_NUMB_BITS); rn = 2 - (rp[1] == 0); rn -= (rp[rn - 1] == 0); }
int _mpz_cmp_si (mpz_srcptr u, signed long int v_digit) { mp_size_t usize = u->_mp_size; mp_size_t vsize; mp_limb_t u_digit; #if GMP_NAIL_BITS != 0 /* FIXME. This isn't very pretty. */ mpz_t tmp; mp_limb_t tt[2]; PTR(tmp) = tt; ALLOC(tmp) = 2; mpz_set_si (tmp, v_digit); return mpz_cmp (u, tmp); #endif vsize = 0; if (v_digit > 0) vsize = 1; else if (v_digit < 0) { vsize = -1; v_digit = -v_digit; } if (usize != vsize) return usize - vsize; if (usize == 0) return 0; u_digit = u->_mp_d[0]; if (u_digit == (mp_limb_t) (unsigned long) v_digit) return 0; if (u_digit > (mp_limb_t) (unsigned long) v_digit) return usize; else return -usize; }
// reading and writing through a pointer field int main(void) { struct st *p; p = __VERIFIER_nondet_st(); assume(p > 0); BASE_PTR(p); if (p->x == 42) { if (p->next != 0) { PTR(p->next, sizeof(struct st)); p->next->y = 474; if (p->next->x == 526) { if (p->next->x + p->next->y == 1000) { __VERIFIER_error(); } } } } return 0; }
unsigned long int mpz_gcd_ui (mpz_ptr w, mpz_srcptr u, unsigned long int v) { mp_size_t un; mp_limb_t res; #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (v > GMP_NUMB_MAX) { mpz_t vz; mp_limb_t vlimbs[2]; vlimbs[0] = v & GMP_NUMB_MASK; vlimbs[1] = v >> GMP_NUMB_BITS; PTR(vz) = vlimbs; SIZ(vz) = 2; mpz_gcd (w, u, vz); /* because v!=0 we will have w<=v hence fitting a ulong */ ASSERT (mpz_fits_ulong_p (w)); return mpz_get_ui (w); }
void mpz_lcm_ui (mpz_ptr r, mpz_srcptr u, mpir_ui v) { mp_size_t usize; mp_srcptr up; mp_ptr rp; mpir_ui g; mp_limb_t c; #if BITS_PER_UI > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (v > GMP_NUMB_MAX) { mpz_t vz; mp_limb_t vlimbs[2]; vlimbs[0] = v & GMP_NUMB_MASK; vlimbs[1] = v >> GMP_NUMB_BITS; PTR(vz) = vlimbs; SIZ(vz) = 2; mpz_lcm (r, u, vz); return; }
void mpf_set_d (mpf_ptr r, double d) { int negative; DOUBLE_NAN_INF_ACTION (d, __gmp_invalid_operation (), __gmp_invalid_operation ()); if (UNLIKELY (d == 0)) { SIZ(r) = 0; EXP(r) = 0; return; } negative = d < 0; d = ABS (d); SIZ(r) = negative ? -LIMBS_PER_DOUBLE : LIMBS_PER_DOUBLE; EXP(r) = __gmp_extract_double (PTR(r), d); }
static void refmpz_mul (mpz_t w, const mpz_t u, const mpz_t v) { mp_size_t usize = u->_mp_size; mp_size_t vsize = v->_mp_size; mp_size_t wsize; mp_size_t sign_product; mp_ptr up, vp; mp_ptr wp; mp_size_t talloc; sign_product = usize ^ vsize; usize = ABS (usize); vsize = ABS (vsize); if (usize == 0 || vsize == 0) { SIZ (w) = 0; return; } talloc = usize + vsize; up = u->_mp_d; vp = v->_mp_d; wp = __GMP_ALLOCATE_FUNC_LIMBS (talloc); if (usize > vsize) refmpn_mul (wp, up, usize, vp, vsize); else refmpn_mul (wp, vp, vsize, up, usize); wsize = usize + vsize; wsize -= wp[wsize - 1] == 0; MPZ_REALLOC (w, wsize); MPN_COPY (PTR(w), wp, wsize); SIZ(w) = sign_product < 0 ? -wsize : wsize; __GMP_FREE_FUNC_LIMBS (wp, talloc); }
// Recursive print function - updates buf_index as appropriate // during its traversal of c static int print(cell c) { switch (TYPE(c)) { case PAIR: if (TYPE(car(c)) == PAIR) { catf("("); print(car(c)); catf(")"); } else print(car(c)); if (!cdr(c)) return 0; catf(" "); if (TYPE(cdr(c)) != PAIR) catf(". "); return print(cdr(c)); case S64: case S32: return catf("%ld", INT_VAL(c)); case SYMBOL: return catf("%s", SYM_STR(c)); case NATIVE_FN: case NATIVE_FN_TCO: case NATIVE_MACRO: return catf("NATIVE_FUNCTION<%p>", PTR(c)); case FFI_SYM: return catf("FFI_SYM<%p>", PTR(c)); case FFI_FN: return catf("FFI_FN<%p>", PTR(c)); case FFI_LIBRARY: return catf("FFI_LIBRARY<%p>", PTR(c)); case MACRO: catf("(macro ("); goto print_args_body; case FN: catf("(lambda ("); print_args_body: print(((fn_t*)PTR(c))->args); catf(") "); print(((fn_t*)PTR(c))->body); return catf(")"); case CONS: return catf("CONS"); case NIL: return catf("()"); default: return catf("UNKNOWN<%p>", c); } }
lispobj alloc_string(const char *str) { int k; int len = strlen(str); lispobj result = alloc_vector(type_SimpleString, len + 1, 16); struct vector *vec = (struct vector *) PTR(result); unsigned short int *wide_char_data; vec->length = make_fixnum(len); wide_char_data = (unsigned short int*) vec->data; for (k = 0; k < len; ++k) { wide_char_data[k] = str[k] & 0xff; } #if 0 fprintf(stderr, "alloc-string: 0x%lx %d -> `%s'\n", result, len, str); #endif return result; }
void mpz_neg (mpz_ptr w, mpz_srcptr u) { mp_ptr wp; mp_srcptr up; mp_size_t usize, size; usize = SIZ (u); if (u != w) { size = ABS (usize); wp = MPZ_NEWALLOC (w, size); up = PTR (u); MPN_COPY (wp, up, size); } SIZ (w) = -usize; }
unsigned long int mpz_tdiv_q_ui (mpz_ptr quot, mpz_srcptr dividend, unsigned long int divisor) { mp_size_t ns, nn, qn; mp_ptr np, qp; mp_limb_t rl; if (UNLIKELY (divisor == 0)) DIVIDE_BY_ZERO; ns = SIZ(dividend); if (ns == 0) { SIZ(quot) = 0; return 0; } nn = ABS(ns); qp = MPZ_REALLOC (quot, nn); np = PTR(dividend); #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (divisor > GMP_NUMB_MAX) { mp_limb_t dp[2], rp[2]; if (nn == 1) /* tdiv_qr requirements; tested above for 0 */ { SIZ(quot) = 0; rl = np[0]; return rl; } dp[0] = divisor & GMP_NUMB_MASK; dp[1] = divisor >> GMP_NUMB_BITS; mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2); rl = rp[0] + (rp[1] << GMP_NUMB_BITS); qn = nn - 2 + 1; qn -= qp[qn - 1] == 0; qn -= qn != 0 && qp[qn - 1] == 0; }
double mpz_get_d_2exp (mpir_si *exp2, mpz_srcptr src) { mp_size_t size, abs_size; mp_srcptr ptr; int cnt; mpir_si exp; size = SIZ(src); if (UNLIKELY (size == 0)) { *exp2 = 0; return 0.0; } ptr = PTR(src); abs_size = ABS(size); count_leading_zeros (cnt, ptr[abs_size - 1]); exp = abs_size * GMP_NUMB_BITS - (cnt - GMP_NAIL_BITS); *exp2 = exp; return mpn_get_d (ptr, abs_size, size, -exp); }
void mpz_set_f (mpz_ptr w, mpf_srcptr u) { mp_ptr wp, up; mp_size_t size; mp_exp_t exp; /* abs(u)<1 truncates to zero */ exp = EXP (u); if (exp <= 0) { SIZ(w) = 0; return; } wp = MPZ_REALLOC (w, exp); up = PTR(u); size = SIZ (u); SIZ(w) = (size >= 0 ? exp : -exp); size = ABS (size); if (exp > size) { /* pad with low zeros to get a total "exp" many limbs */ mp_size_t zeros = exp - size; MPN_ZERO (wp, zeros); wp += zeros; } else { /* exp<=size, trucate to the high "exp" many limbs */ up += (size - exp); size = exp; } MPN_COPY (wp, up, size); }
unsigned long refmpz_popcount (mpz_t arg) { mp_size_t n, i; unsigned long cnt; mp_limb_t x; n = SIZ(arg); if (n < 0) return ~(unsigned long) 0; cnt = 0; for (i = 0; i < n; i++) { x = PTR(arg)[i]; while (x != 0) { cnt += (x & 1); x >>= 1; } } return cnt; }
/* Print "name=value\n" to stdout for an mpz_t value. */ void mpz_trace (const char *name, mpz_srcptr z) { mpq_t q; mp_limb_t one; if (z == NULL) { mpq_trace (name, NULL); return; } q->_mp_num._mp_alloc = ALLOC(z); q->_mp_num._mp_size = SIZ(z); q->_mp_num._mp_d = PTR(z); one = 1; q->_mp_den._mp_alloc = 1; q->_mp_den._mp_size = 1; q->_mp_den._mp_d = &one; mpq_trace(name, q); }
/** * Draw one plane of a DMD frame. * ID identifies the source of the frame data. * The output is always drawn to the low-mapped buffer. */ void frame_draw_plane (U16 id) { /* Lookup the image number in the global table. * For real ROMs, this is located at a fixed address. * In native mode, the images are kept in a separate file. */ U8 type; struct frame_pointer *p; U8 *data; page_push (IMAGEMAP_PAGE); p = (struct frame_pointer *)IMAGEMAP_BASE + id; data = PTR(p); /* Switch to the page containing the image data. * Pull the type byte out, then decode the remaining bytes * to the display buffer. */ pinio_set_bank (PINIO_BANK_ROM, p->page); type = data[0]; frame_decode (data + 1, type & ~0x1); page_pop (); }
/* convert mpzvi to CRT representation, naive version */ static void mpzspv_from_mpzv_slow (mpzspv_t x, const spv_size_t offset, mpz_t mpzvi, mpzspm_t mpzspm) { const unsigned int sp_num = mpzspm->sp_num; unsigned int j; mp_size_t n = mpz_size (mpzspm->modulus); /* GMP's comments on mpn_preinv_mod_1: * * "This function used to be documented, but is now considered obsolete. It * continues to exist for binary compatibility, even when not required * internally." * * It doesn't accept 0 as the dividend so we have to treat this case * separately */ /* Note: we can't use the mul_c field for mpn_preinv_mod_1, since on 64-bit it is floor(2^125/sp) where sp has 62 bits, and mpn_preinv_mod_1 needs floor(2^128/(4*sp))-2^64 = floor(2^126/sp)-2^64. On 32-bit it is floor(2^62/sp) where sp has 31 bits, and mpn_preinv_mod_1 needs floor(2^64/(2*sp))-2^32 = floor(2^63/sp)-2^32. */ /* Note: we could improve this as follows. Assume the number N to factor has n limbs. Instead of computing v mod p by reducing v by the high limbs, we first compute v/B^(n-1) mod p by reducing v by the low limbs, then deduce v mod p using a precomputed value of B^(n-1) mod p. The reduction v/B is done by using a precomputed k = 1/B mod p, thus v1*B+v0 = (v1+k*v0)*B and so on. */ for (j = 0; j < sp_num; j++) x[j][offset] = ecm_mod_1 (PTR(mpzvi), SIZ(mpzvi), (mp_limb_t) mpzspm->spm[j]->sp, n, mpzspm->spm[j]->invm, mpzspm->spm[j]->Bpow); /* The typecast to mp_limb_t assumes that mp_limb_t is at least as wide as sp_t */ }
static bool bch_extent_merge(struct btree_keys *bk, struct bkey *l, struct bkey *r) { struct btree *b = container_of(bk, struct btree, keys); unsigned i; if (key_merging_disabled(b->c)) return false; for (i = 0; i < KEY_PTRS(l); i++) if (l->ptr[i] + PTR(0, KEY_SIZE(l), 0) != r->ptr[i] || PTR_BUCKET_NR(b->c, l, i) != PTR_BUCKET_NR(b->c, r, i)) return false; /* Keys with no pointers aren't restricted to one bucket and could * overflow KEY_SIZE */ if (KEY_SIZE(l) + KEY_SIZE(r) > USHRT_MAX) { SET_KEY_OFFSET(l, KEY_OFFSET(l) + USHRT_MAX - KEY_SIZE(l)); SET_KEY_SIZE(l, USHRT_MAX); bch_cut_front(l, r); return false; } if (KEY_CSUM(l)) { if (KEY_CSUM(r)) l->ptr[KEY_PTRS(l)] = merge_chksums(l, r); else SET_KEY_CSUM(l, 0); } SET_KEY_OFFSET(l, KEY_OFFSET(l) + KEY_SIZE(r)); SET_KEY_SIZE(l, KEY_SIZE(l) + KEY_SIZE(r)); return true; }
int mpf_integer_p (mpf_srcptr f) { mp_srcptr ptr; mp_exp_t exp; mp_size_t size, frac, i; size = SIZ (f); if (size == 0) return 1; /* zero is an integer */ exp = EXP (f); if (exp <= 0) return 0; /* has only fraction limbs */ /* any fraction limbs must be zero */ frac = ABS (size) - exp; ptr = PTR (f); for (i = 0; i < frac; i++) if (ptr[i] != 0) return 0; return 1; }
/* convert mpzvi to CRT representation, fast version, assumes mpzspm->T has been precomputed (see mpzspm.c) */ static void mpzspv_from_mpzv_fast (mpzspv_t x, const spv_size_t offset, mpz_t mpzvi, mpzspm_t mpzspm) { const unsigned int sp_num = mpzspm->sp_num; unsigned int i, j, k, i0 = I0_THRESHOLD, I0; mpzv_t *T = mpzspm->T; unsigned int d = mpzspm->d, ni; ASSERT (d > i0); /* T[0] serves as vector of temporary mpz_t's, since it contains the small primes, which are also in mpzspm->spm[j]->sp */ /* initially we split mpzvi in two */ ni = 1 << (d - 1); mpz_mod (T[0][0], mpzvi, T[d-1][0]); mpz_mod (T[0][ni], mpzvi, T[d-1][1]); for (i = d-1; i-- > i0;) { /* goes down from depth i+1 to i */ ni = 1 << i; for (j = k = 0; j + ni < sp_num; j += 2*ni, k += 2) { mpz_mod (T[0][j+ni], T[0][j], T[i][k+1]); mpz_mod (T[0][j], T[0][j], T[i][k]); } /* for the last entry T[0][j] if j < sp_num, there is nothing to do */ } /* last steps */ I0 = 1 << i0; for (j = 0; j < sp_num; j += I0) for (k = j; k < j + I0 && k < sp_num; k++) x[k][offset] = mpn_mod_1 (PTR(T[0][j]), SIZ(T[0][j]), (mp_limb_t) mpzspm->spm[k]->sp); /* The typecast to mp_limb_t assumes that mp_limb_t is at least as wide as sp_t */ }