int main(void) { bit_array *ba; ba = bit_array_create(8); printf("%d\n", bit_array_set(ba, 6)); printf("%d\n", bit_array_set(ba, 6)); // print_bits(ba->bits[0]); return 0; }
/* mark nodal domain containing grid[i][j] as counted non-recursive version inputs: grid - 2d array of function values counted - 2d array indicating whether a point has been counted i - row of initial point in grid j - column of initial point in grid nd - number of current nodal domain ny - rows in grid nx - columns in grid precondition: grid and counted are ny x nx outputs: return value: area of domain (in pixels) updates stats */ int findDomain(bit_array_t *signs, bit_array_t *counted, int i, int j, int nd, int ny, int nx) { stack *s = newStack(); push(s, j, i); bit_array_set(counted, j, i); int x, y; int currentSign; int size = 0; while (pop(s, &x, &y)) { size++; currentSign = bit_array_get(signs, j, i); #ifdef DEBUG domain_numbers[y][x] = nd; #endif // orthongal directions // left if (x >= 1 && !bit_array_get(counted, x-1, y)) { if (bit_array_get(signs, x-1, y) == currentSign) { bit_array_set(counted, x-1, y); push(s, x - 1, y); } } // above if (y >= 1 && !bit_array_get(counted, x, y-1)) { if (bit_array_get(signs, x, y-1) == currentSign) { bit_array_set(counted, x, y-1); push(s, x, y-1); } } // right if (x < nx-1 && !bit_array_get(counted, x+1, y)) { if (bit_array_get(signs, x+1, y) == currentSign) { bit_array_set(counted, x+1, y); push(s, x+1, y); } } // below if (y < ny-1 && !bit_array_get(counted, x, y+1)) { if (bit_array_get(signs, x, y+1) == currentSign) { bit_array_set(counted, x, y+1); push(s, x, y+1); } } } destroyStack(s); return size; }
/* mark nodal domain containing grid[i][j] as counted non-recursive version inputs: grid - 2d array of function values counted - 2d array indicating whether a point has been counted i - row of initial point in grid j - column of initial point in grid nd - number of current nodal domain ny - rows in grid nx - columns in grid precondition: grid and counted are ny x nx outputs: return value: area of domain (in pixels) updates stats */ int findDomainNoInterp(double **grid, bit_array_t *counted, int i, int j, int nd, int ny, int nx) { stack *s = newStack(); push(s, j, i); bit_array_set(counted, j, i); int x, y; int currentSign; int size = 0; while (pop(s, &x, &y)) { size++; currentSign = SIGN(grid[i][j]); // orthongal directions // left if (x >= 1 && !bit_array_get(counted, x-1, y)) { if (SIGN(grid[y][x-1]) == currentSign) { bit_array_set(counted, x-1, y); push(s, x - 1, y); } } // above if (y >= 1 && !bit_array_get(counted, x, y-1)) { if (SIGN(grid[y-1][x]) == currentSign) { bit_array_set(counted, x, y-1); push(s, x, y-1); } } // right if (x < nx-1 && !bit_array_get(counted, x+1, y)) { if (SIGN(grid[y][x+1]) == currentSign) { bit_array_set(counted, x+1, y); push(s, x+1, y); } } // below if (y < ny-1 && !bit_array_get(counted, x, y+1)) { if (SIGN(grid[y+1][x]) == currentSign) { bit_array_set(counted, x, y+1); push(s, x, y+1); } } } destroyStack(s); return size; }
static void dma_si_write(struct si_controller* si) { int i; if (si->regs[SI_PIF_ADDR_WR64B_REG] != 0x1FC007C0) { DebugMessage(si->r4300->state, M64MSG_ERROR, "dma_si_write(): unknown SI use"); si->r4300->state->stop=1; } for (i = 0; i < PIF_RAM_SIZE; i += 4) { *((uint32_t*)(&si->pif.ram[i])) = sl(si->ri->rdram.dram[(si->regs[SI_DRAM_ADDR_REG]+i)/4]); } if (si->r4300->state->enable_trimming_mode) { for (i = 0; i < PIF_RAM_SIZE; i += 4) { unsigned int ram_address = si->regs[SI_DRAM_ADDR_REG] + i; if (!bit_array_test(si->r4300->state->barray_ram_written_first, ram_address / 4)) bit_array_set(si->r4300->state->barray_ram_read, ram_address / 4); } } update_pif_write(si); update_count(si->r4300->state); if (si->r4300->state->g_delay_si) { add_interupt_event(si->r4300->state, SI_INT, /*0x100*/0x900); } else { si->regs[SI_STATUS_REG] |= 0x1000; // INTERRUPT signal_rcp_interrupt(si->r4300, MI_INTR_SI); } }
int create_block_reservation(int blocks_needed) { // Finds an area of free blocks in our database file. int i,j; bool found = false; int retval = -1; sem_wait(BLOCK_BITMAP_LOCK); for (j = 0; j < MAX_BLOCKS; j++) { for (i = 0; i < blocks_needed; i++) { if (bit_array_test(SHM_BLOCK_BITMAP, i + j) != 0) {// didn't find a contiguous block j += i; break; } } if (i == blocks_needed) { found = true; break; } } if (found) { for (i = 0; i < blocks_needed; i++) // Found a good set of blocks. Mark them as used. bit_array_set(SHM_BLOCK_BITMAP, i + j); retval = j; } msync(SHM_BLOCK_BITMAP, BLOCK_BITMAP_BYTES, MS_SYNC); // commit the whole block bitmap to disk sem_post(BLOCK_BITMAP_LOCK); return(retval); }
void pmt_cache_set_dirty(UINT32 const pmt_idx, BOOL8 const is_dirty) { UINT32 page_idx = get_page(pmt_idx); ASSERT(page_idx != NULL_PAGE_IDX); if (is_dirty) bit_array_set(cached_pmt_is_dirty, page_idx); else bit_array_clear(cached_pmt_is_dirty, page_idx); }
void hash_write_lock(int hash_number) { while (1) { sem_wait(HASHBUCKET_LOCK); if ((bit_array_test(SHM_HASHBUCKET_BITMAP, hash_number)) == 0) { bit_array_set(SHM_HASHBUCKET_BITMAP, hash_number); sem_post(HASHBUCKET_LOCK); break; } sem_post(HASHBUCKET_LOCK); } }
bit_array_t *createScaledMaskFromBilliard(Billiard *b, double xl, double xh, double yl, double yh, double dx, double upsample_ratio, double scale, int ny, int nx) { int nx_should_be, ny_should_be; if (xh - xl == 0.0) { ny_should_be = ceil((b->yh - b->yl + 0.9) / dx) * upsample_ratio + 1; nx_should_be = ceil((b->xh - b->xl + 0.9) / dx) * upsample_ratio + 1; } else { ny_should_be = (int)((yh - yl) / dx + 0.9) * upsample_ratio + 1; nx_should_be = (int)((xh - xl) / dx + 0.9) * upsample_ratio + 1; } if ((float)fabs(nx-nx_should_be)/nx_should_be > DIMENSION_ERROR_MARGIN || (float)fabs(ny-ny_should_be)/ny_should_be > DIMENSION_ERROR_MARGIN) { ERROR("Mask dimensions do not match expected dimesnions. Given %d x %d, expected %d x %d", ny, nx, ny_should_be, nx_should_be); exit(DIMENSION_ERR); } bit_array_t *counted = new_bit_array(ny, nx); MALLOC_CHECK(counted); int i, j; for (i = 0 ; i < ny ; i++) { for (j = 0 ; j < nx ; j++) { if (i == 0 || j == 0) { bit_array_set(counted, j, i); // mask out first row and column } if (!inside_billiard(j * (dx / upsample_ratio) / scale, i * (dx / upsample_ratio) / scale, b)) { bit_array_set(counted, j, i); // bit array is zeroed when allocated } } } // special case for qugrs billiard: mask out the two points at the tip if (b->type == QU_GEN_RECT_SINAI) { bit_array_set(counted, nx-1, ny-1); bit_array_set(counted, nx-2, ny-2); } return counted; }
void interpolate(double **grid, bit_array_t *upsampled, int i, int j, int ny, int nx, int upsample_ratio, gsl_matrix *interp, interp_workspace *w) { int k; int x, y, l; int n = upsample_ratio + 1; int rc; int currentSign; int refl_i[STENCIL_WIDTH], refl_j[STENCIL_WIDTH], refl_i_sign[STENCIL_WIDTH], refl_j_sign[STENCIL_WIDTH]; // validate size of interp matrix if (interp->size1 != n*n || interp->size2 != NUM_STENCIL_POINTS) { ERROR("invalid size for interpolation matrix"); exit(INTERP_ERR); } w->stats->interp_count++; fill_interp_input(grid, i, j, w, ny, nx, refl_i, refl_j, refl_i_sign, refl_j_sign); // interp_output = interp * w->input rc = gsl_blas_dgemv(CblasNoTrans, 1, interp, w->input, 0, w->output); if (rc) { ERROR("interpolation failed. gsl_blas_dgemv returned %d", rc); exit(INTERP_ERR); } // write signs into upsampled for (y = 0 ; y < n ; y++) { for (x = 0 ; x < n ; x++) { if (gsl_vector_get(w->output, IDX(x,y)) > 0) { bit_array_set(upsampled, j*upsample_ratio + x, i*upsample_ratio + y); } else { bit_array_reset(upsampled, j*upsample_ratio + x, i*upsample_ratio + y); } } } // debug output /* char interp_input_file[100]; char interp_output_file[100]; sprintf(interp_input_file, "interp_input_%d_%d.dat", j, i); sprintf(interp_output_file, "interp_output_%d_%d.dat", j, i); dump_vector(w->input, interp_input_file); dump_vector(w->output, interp_output_file); */ }
void keydb_lock(int64_t pos) { char key[1024] = ""; int hash_number; sprintf(key, "%llu", pos); // turn pos into a string. hash_number = get_hash_val(10, key); // 2^10 = 1024. See SHM_KEYDB_BITMAP. while (1) { sem_wait(KEYDB_LOCK); if ((bit_array_test(SHM_KEYDB_BITMAP, hash_number)) == 0) { bit_array_set(SHM_KEYDB_BITMAP, hash_number); sem_post(KEYDB_LOCK); break; } sem_post(KEYDB_LOCK); } }
// TODO: update to return bit_array_t * bit_array_t *createMaskFromFile(char *file, int *ny_p, int *nx_p) { FILE *fp = fopen(file, "r"); if (fp == NULL) { ERROR("failed to open %s", file); return NULL; } char c; int n_e, nx, ny; double temp_double; float temp_float; fscanf(fp,"%c",&c); if (c == 'b') { fscanf(fp,"%c",&c); fscanf(fp,"%c",&c); fscanf(fp,"%c",&c); } else { fclose(fp); ERROR("incorrect file format in %s", file); return NULL; } if (fread(&n_e, (size_t)4,1,fp) != 1) { ERROR("failed to read n_e in %s", file); return NULL; } if (fread(nx_p, (size_t)4,1,fp) != 1) { ERROR("failed to read nx in %s", file); return NULL; } if (fread(ny_p, (size_t)4,1,fp) != 1) { ERROR("failed to read ny in %s", file); return NULL; } if (n_e != 1) { ERROR("more than one eigenfunction in %s", file); return NULL; } nx = *nx_p; ny = *ny_p; bit_array_t *counted = new_bit_array(ny, nx); MALLOC_CHECK(counted) if (fread(&temp_double,sizeof(double),1,fp) != 1) { // this is the energy - we don't care about it here ERROR("failed to read E_1 in %s", file); return NULL; } int i; for (i = 0 ; i < nx * ny ; i++) { if (fread(&temp_float,sizeof(float),n_e,fp) != (unsigned int)n_e) { ERROR("failed to read data in %s", file); return NULL; } if (!temp_float) { bit_array_set(counted, i%nx, i/nx); } } fclose(fp); return counted; }