/** * \brief initializes a dma descriptor ring and allocates memory for it * * \param ring the ring structure to initialize * \param size number of elements in the ring * * \returns SYS_ERR_OK on success * errval on error */ errval_t xeon_phi_dma_desc_ring_alloc(struct xdma_ring *ring, uint16_t size) { errval_t err; memset(ring, 0, sizeof(*ring)); assert(size < (XEON_PHI_DMA_DESC_RING_MAX)); assert(IS_POW2(size)); #ifndef __k1om__ /* * we set the ram affinity to the maximum range mapped by the system memory * page tables when being on the host. Otherwise the card cannot access it. */ uint64_t minbase, maxlimit; ram_get_affinity(&minbase, &maxlimit); ram_set_affinity(0, XEON_PHI_SYSMEM_SIZE-8*XEON_PHI_SYSMEM_PAGE_SIZE); #endif size_t frame_size = ((size_t) size) * XEON_PHI_DMA_DESC_SIZE; err = frame_alloc(&ring->cap, frame_size, NULL); #ifndef __k1om__ ram_set_affinity(minbase, maxlimit); #endif if (err_is_fail(err)) { return err; } err = vspace_map_one_frame_attr(&ring->vbase, frame_size, ring->cap, VREGION_FLAGS_READ_WRITE, NULL, NULL); if (err_is_fail(err)) { cap_destroy(ring->cap); return err; } struct frame_identity id; err = invoke_frame_identify(ring->cap, &id); assert(err_is_ok(err)); ring->pbase = id.base; ring->size = size; memset(ring->vbase, 0, frame_size); return SYS_ERR_OK; }
void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str) { MemHeadAligned *memh; /* It's possible that MemHead's size is not properly aligned, * do extra padding to deal with this. * * We only support small alignments which fits into short in * order to save some bits in MemHead structure. */ size_t extra_padding = MEMHEAD_ALIGN_PADDING(alignment); /* Huge alignment values doesn't make sense and they * wouldn't fit into 'short' used in the MemHead. */ assert(alignment < 1024); /* We only support alignment to a power of two. */ assert(IS_POW2(alignment)); len = SIZET_ALIGN_4(len); memh = (MemHeadAligned *)aligned_malloc( len + extra_padding + sizeof(MemHeadAligned), alignment); if (LIKELY(memh)) { /* We keep padding in the beginning of MemHead, * this way it's always possible to get MemHead * from the data pointer. */ memh = (MemHeadAligned *)((char *)memh + extra_padding); if (UNLIKELY(malloc_debug_memset && len)) { memset(memh + 1, 255, len); } memh->len = len | (size_t) MEMHEAD_ALIGN_FLAG; memh->alignment = (short) alignment; atomic_add_and_fetch_u(&totblock, 1); atomic_add_and_fetch_z(&mem_in_use, len); update_maximum(&peak_mem, mem_in_use); return PTR_FROM_MEMHEAD(memh); } print_error("Malloc returns null: len=" SIZET_FORMAT " in %s, total %u\n", SIZET_ARG(len), str, (unsigned int) mem_in_use); return NULL; }
// if chunksz <= 0, pick chunksz for you vector<hash_t> Hash::hashChunks(const char *buf, size_t len, const alg_t alg, const string& key, size_t &chunksz_out) { vector<hash_t> out; assert(len); if (chunksz_out <= 0) { // pick chunk size #if 0 if ( !IS_POW2(len) ) { // len not a power of two chunksz_out = (len >= 4) ? (len/4+1) : len; // just make 4 chunks (XXX) } else { // len is power of two if (len >= (1 << 6)) { // len >= 64 B chunksz_out = (len >> 6); // make 64 chunks } else { // len < 64 bytes chunksz_out = len; // make 1 chunk } }
int mem_pool_alloc(struct mem_pool * pool, size_t size) { size_t sz = size >= ND_MIN_SZ ? size : ND_MIN_SZ; sz = IS_POW2(sz) ? sz : NEXT_POW2(sz); printf("aligning requested size %d to %d...\n", size, sz); unsigned order = pool->order + 4 - LOG2(sz); printf("requested order is %d\n", order); unsigned i = 0, rval = 0; if (!potential(pool, i, order)) { return -1; } for (;;) { printf("checking node %d ...\n", i); struct node * n = &pool->nl[i]; if (n->status == FREE && HEIGHT(i) == order) { n->status = FULL; mark_parent(pool, i); while (i < pool->valid_nr) { rval = i; i = LEFT(i); } return rval - LEFT_LEAF(pool->order); //good } else if (potential(pool, LEFT(i), order)) { i = LEFT(i); } else if (potential(pool, RIGHT(i), order)) { i = RIGHT(i); } else { break; } } return -1; }
int mpfr_mul_ui (mpfr_ptr y, mpfr_srcptr x, unsigned long int u, mpfr_rnd_t rnd_mode) { mp_limb_t *yp; mp_size_t xn; int cnt, inexact; MPFR_TMP_DECL (marker); if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x))) { if (MPFR_IS_NAN (x)) { MPFR_SET_NAN (y); MPFR_RET_NAN; } else if (MPFR_IS_INF (x)) { if (u != 0) { MPFR_SET_INF (y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET (0); /* infinity is exact */ } else /* 0 * infinity */ { MPFR_SET_NAN (y); MPFR_RET_NAN; } } else /* x is zero */ { MPFR_ASSERTD (MPFR_IS_ZERO (x)); MPFR_SET_ZERO (y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET (0); /* zero is exact */ } } else if (MPFR_UNLIKELY (u <= 1)) { if (u < 1) { MPFR_SET_ZERO (y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET (0); /* zero is exact */ } else return mpfr_set (y, x, rnd_mode); } else if (MPFR_UNLIKELY (IS_POW2 (u))) return mpfr_mul_2si (y, x, MPFR_INT_CEIL_LOG2 (u), rnd_mode); yp = MPFR_MANT (y); xn = MPFR_LIMB_SIZE (x); MPFR_ASSERTD (xn < MP_SIZE_T_MAX); MPFR_TMP_MARK(marker); yp = MPFR_TMP_LIMBS_ALLOC (xn + 1); MPFR_ASSERTN (u == (mp_limb_t) u); yp[xn] = mpn_mul_1 (yp, MPFR_MANT (x), xn, u); /* x * u is stored in yp[xn], ..., yp[0] */ /* since the case u=1 was treated above, we have u >= 2, thus yp[xn] >= 1 since x was msb-normalized */ MPFR_ASSERTD (yp[xn] != 0); if (MPFR_LIKELY (MPFR_LIMB_MSB (yp[xn]) == 0)) { count_leading_zeros (cnt, yp[xn]); mpn_lshift (yp, yp, xn + 1, cnt); } else { cnt = 0; } /* now yp[xn], ..., yp[0] is msb-normalized too, and has at most PREC(x) + (GMP_NUMB_BITS - cnt) non-zero bits */ MPFR_RNDRAW (inexact, y, yp, (mpfr_prec_t) (xn + 1) * GMP_NUMB_BITS, rnd_mode, MPFR_SIGN (x), cnt -- ); MPFR_TMP_FREE (marker); cnt = GMP_NUMB_BITS - cnt; if (MPFR_UNLIKELY (__gmpfr_emax < MPFR_EMAX_MIN + cnt || MPFR_GET_EXP (x) > __gmpfr_emax - cnt)) return mpfr_overflow (y, rnd_mode, MPFR_SIGN(x)); MPFR_SET_EXP (y, MPFR_GET_EXP (x) + cnt); MPFR_SET_SAME_SIGN (y, x); return inexact; }
/* returns 0 if result exact, non-zero otherwise */ int mpfr_div_ui (mpfr_ptr y, mpfr_srcptr x, unsigned long int u, mpfr_rnd_t rnd_mode) { long i; int sh; mp_size_t xn, yn, dif; mp_limb_t *xp, *yp, *tmp, c, d; mpfr_exp_t exp; int inexact, middle = 1, nexttoinf; MPFR_TMP_DECL(marker); if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x))) { if (MPFR_IS_NAN (x)) { MPFR_SET_NAN (y); MPFR_RET_NAN; } else if (MPFR_IS_INF (x)) { MPFR_SET_INF (y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET (0); } else { MPFR_ASSERTD (MPFR_IS_ZERO(x)); if (u == 0) /* 0/0 is NaN */ { MPFR_SET_NAN(y); MPFR_RET_NAN; } else { MPFR_SET_ZERO(y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET(0); } } } else if (MPFR_UNLIKELY (u <= 1)) { if (u < 1) { /* x/0 is Inf since x != 0*/ MPFR_SET_INF (y); MPFR_SET_SAME_SIGN (y, x); MPFR_RET (0); } else /* y = x/1 = x */ return mpfr_set (y, x, rnd_mode); } else if (MPFR_UNLIKELY (IS_POW2 (u))) return mpfr_div_2si (y, x, MPFR_INT_CEIL_LOG2 (u), rnd_mode); MPFR_SET_SAME_SIGN (y, x); MPFR_TMP_MARK (marker); xn = MPFR_LIMB_SIZE (x); yn = MPFR_LIMB_SIZE (y); xp = MPFR_MANT (x); yp = MPFR_MANT (y); exp = MPFR_GET_EXP (x); dif = yn + 1 - xn; /* we need to store yn+1 = xn + dif limbs of the quotient */ /* don't use tmp=yp since the mpn_lshift call below requires yp >= tmp+1 */ tmp = (mp_limb_t*) MPFR_TMP_ALLOC ((yn + 1) * BYTES_PER_MP_LIMB); c = (mp_limb_t) u; MPFR_ASSERTN (u == c); if (dif >= 0) c = mpn_divrem_1 (tmp, dif, xp, xn, c); /* used all the dividend */ else /* dif < 0 i.e. xn > yn, don't use the (-dif) low limbs from x */ c = mpn_divrem_1 (tmp, 0, xp - dif, yn + 1, c); inexact = (c != 0); /* First pass in estimating next bit of the quotient, in case of RNDN * * In case we just have the right number of bits (postpone this ?), * * we need to check whether the remainder is more or less than half * * the divisor. The test must be performed with a subtraction, so as * * to prevent carries. */ if (MPFR_LIKELY (rnd_mode == MPFR_RNDN)) { if (c < (mp_limb_t) u - c) /* We have u > c */ middle = -1; else if (c > (mp_limb_t) u - c) middle = 1; else middle = 0; /* exactly in the middle */ } /* If we believe that we are right in the middle or exact, we should check that we did not neglect any word of x (division large / 1 -> small). */ for (i=0; ((inexact == 0) || (middle == 0)) && (i < -dif); i++) if (xp[i]) inexact = middle = 1; /* larger than middle */ /* If the high limb of the result is 0 (xp[xn-1] < u), remove it. Otherwise, compute the left shift to be performed to normalize. In the latter case, we discard some low bits computed. They contain information useful for the rounding, hence the updating of middle and inexact. */ if (tmp[yn] == 0) { MPN_COPY(yp, tmp, yn); exp -= GMP_NUMB_BITS; } else { int shlz; count_leading_zeros (shlz, tmp[yn]); /* shift left to normalize */ if (MPFR_LIKELY (shlz != 0)) { mp_limb_t w = tmp[0] << shlz; mpn_lshift (yp, tmp + 1, yn, shlz); yp[0] += tmp[0] >> (GMP_NUMB_BITS - shlz); if (w > (MPFR_LIMB_ONE << (GMP_NUMB_BITS - 1))) { middle = 1; } else if (w < (MPFR_LIMB_ONE << (GMP_NUMB_BITS - 1))) { middle = -1; } else { middle = (c != 0); } inexact = inexact || (w != 0); exp -= shlz; } else { /* this happens only if u == 1 and xp[xn-1] >=
/* Should NOT be called with cs_error held */ KHMEXP kherr_event * KHMAPI kherr_report(enum kherr_severity severity, const wchar_t * short_desc, const wchar_t * facility, const wchar_t * location, const wchar_t * long_desc, const wchar_t * suggestion, khm_int32 facility_id, enum kherr_suggestion suggestion_id, kherr_param p1, kherr_param p2, kherr_param p3, kherr_param p4, khm_int32 flags #ifdef _WIN32 ,HMODULE h_module #endif ) { kherr_context * c; kherr_event * e; e = get_empty_event(); e->thread_id = GetCurrentThreadId(); e->time_ticks = GetTickCount(); GetSystemTimeAsFileTime(&e->time_ft); e->severity = severity; e->short_desc = short_desc; e->facility = facility; e->location = location; e->long_desc = long_desc; e->suggestion = suggestion; e->facility_id = facility_id; e->suggestion_id = suggestion_id; e->p1 = p1; e->p2 = p2; e->p3 = p3; e->p4 = p4; e->flags = flags; #ifdef _WIN32 e->h_module = h_module; #endif /* sanity check */ if (!IS_POW2(flags & KHERR_RFMASK_SHORT_DESC) || !IS_POW2(flags & KHERR_RFMASK_LONG_DESC) || !IS_POW2(flags & KHERR_RFMASK_SUGGEST)) { /* the reason why we are doing it this way is because p1..p4, the descriptions and the suggestion may contain allocations that has to be freed. */ #ifdef DEBUG assert(FALSE); #else if (IsDebuggerPresent()) DebugBreak(); #endif free_event(e); e = NULL; } else { EnterCriticalSection(&cs_error); c = peek_context(); if(c) { add_event(c,e); } else { free_event(e); e = NULL; } LeaveCriticalSection(&cs_error); } return e; }
/* Given a 32 bit word 'n', calculates ceil(log_2('n')). It is an error to * call this function with 'n' == 0. */ int log_2_ceil(uint32_t n) { return log_2_floor(n) + !IS_POW2(n); }