static inline int get_ipaddr_bitcount(ipaddr_t ip) { return bitcount(&ip[0]) + bitcount(&ip[1]) + bitcount(&ip[2]) + bitcount(&ip[3]); }
int main() { printf("%d's bit count is: %d\n", 1023, bitcount(1023)); printf("%d's bit count is: %d\n", 1024, bitcount(1024)); }
int main(int argc, char *argv[]) { const struct flashchip *chip = NULL; /* Probe for up to eight flash chips. */ struct flashctx flashes[8] = {{0}}; struct flashctx *fill_flash; const char *name; int namelen, opt, i, j; int startchip = -1, chipcount = 0, option_index = 0, force = 0, ifd = 0, fmap = 0; #if CONFIG_PRINT_WIKI == 1 int list_supported_wiki = 0; #endif int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; int dont_verify_it = 0, dont_verify_all = 0, list_supported = 0, operation_specified = 0; struct flashrom_layout *layout = NULL; enum programmer prog = PROGRAMMER_INVALID; enum { OPTION_IFD = 0x0100, OPTION_FMAP, OPTION_FMAP_FILE, OPTION_FLASH_CONTENTS, }; int ret = 0; static const char optstring[] = "r:Rw:v:nNVEfc:l:i:p:Lzho:"; static const struct option long_options[] = { {"read", 1, NULL, 'r'}, {"write", 1, NULL, 'w'}, {"erase", 0, NULL, 'E'}, {"verify", 1, NULL, 'v'}, {"noverify", 0, NULL, 'n'}, {"noverify-all", 0, NULL, 'N'}, {"chip", 1, NULL, 'c'}, {"verbose", 0, NULL, 'V'}, {"force", 0, NULL, 'f'}, {"layout", 1, NULL, 'l'}, {"ifd", 0, NULL, OPTION_IFD}, {"fmap", 0, NULL, OPTION_FMAP}, {"fmap-file", 1, NULL, OPTION_FMAP_FILE}, {"image", 1, NULL, 'i'}, {"flash-contents", 1, NULL, OPTION_FLASH_CONTENTS}, {"list-supported", 0, NULL, 'L'}, {"list-supported-wiki", 0, NULL, 'z'}, {"programmer", 1, NULL, 'p'}, {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'R'}, {"output", 1, NULL, 'o'}, {NULL, 0, NULL, 0}, }; char *filename = NULL; char *referencefile = NULL; char *layoutfile = NULL; char *fmapfile = NULL; #ifndef STANDALONE char *logfile = NULL; #endif /* !STANDALONE */ char *tempstr = NULL; char *pparam = NULL; flashrom_set_log_callback((flashrom_log_callback *)&flashrom_print_cb); print_version(); print_banner(); if (selfcheck()) exit(1); setbuf(stdout, NULL); /* FIXME: Delay all operation_specified checks until after command * line parsing to allow --help overriding everything else. */ while ((opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != EOF) { switch (opt) { case 'r': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } filename = strdup(optarg); read_it = 1; break; case 'w': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } filename = strdup(optarg); write_it = 1; break; case 'v': //FIXME: gracefully handle superfluous -v if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } if (dont_verify_it) { fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n"); cli_classic_abort_usage(); } filename = strdup(optarg); verify_it = 1; break; case 'n': if (verify_it) { fprintf(stderr, "--verify and --noverify are mutually exclusive. Aborting.\n"); cli_classic_abort_usage(); } dont_verify_it = 1; break; case 'N': dont_verify_all = 1; break; case 'c': chip_to_probe = strdup(optarg); break; case 'V': verbose_screen++; if (verbose_screen > FLASHROM_MSG_DEBUG2) verbose_logfile = verbose_screen; break; case 'E': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } erase_it = 1; break; case 'f': force = 1; break; case 'l': if (layoutfile) { fprintf(stderr, "Error: --layout specified " "more than once. Aborting.\n"); cli_classic_abort_usage(); } if (ifd) { fprintf(stderr, "Error: --layout and --ifd both specified. Aborting.\n"); cli_classic_abort_usage(); } if (fmap) { fprintf(stderr, "Error: --layout and --fmap-file both specified. Aborting.\n"); cli_classic_abort_usage(); } layoutfile = strdup(optarg); break; case OPTION_IFD: if (layoutfile) { fprintf(stderr, "Error: --layout and --ifd both specified. Aborting.\n"); cli_classic_abort_usage(); } if (fmap) { fprintf(stderr, "Error: --fmap-file and --ifd both specified. Aborting.\n"); cli_classic_abort_usage(); } ifd = 1; break; case OPTION_FMAP_FILE: if (fmap) { fprintf(stderr, "Error: --fmap or --fmap-file specified " "more than once. Aborting.\n"); cli_classic_abort_usage(); } if (ifd) { fprintf(stderr, "Error: --fmap-file and --ifd both specified. Aborting.\n"); cli_classic_abort_usage(); } if (layoutfile) { fprintf(stderr, "Error: --fmap-file and --layout both specified. Aborting.\n"); cli_classic_abort_usage(); } fmapfile = strdup(optarg); fmap = 1; break; case OPTION_FMAP: if (fmap) { fprintf(stderr, "Error: --fmap or --fmap-file specified " "more than once. Aborting.\n"); cli_classic_abort_usage(); } if (ifd) { fprintf(stderr, "Error: --fmap and --ifd both specified. Aborting.\n"); cli_classic_abort_usage(); } if (layoutfile) { fprintf(stderr, "Error: --layout and --fmap both specified. Aborting.\n"); cli_classic_abort_usage(); } fmap = 1; break; case 'i': tempstr = strdup(optarg); if (register_include_arg(tempstr)) { free(tempstr); cli_classic_abort_usage(); } break; case OPTION_FLASH_CONTENTS: referencefile = strdup(optarg); break; case 'L': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } list_supported = 1; break; case 'z': #if CONFIG_PRINT_WIKI == 1 if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } list_supported_wiki = 1; #else fprintf(stderr, "Error: Wiki output was not compiled " "in. Aborting.\n"); cli_classic_abort_usage(); #endif break; case 'p': if (prog != PROGRAMMER_INVALID) { fprintf(stderr, "Error: --programmer specified " "more than once. You can separate " "multiple\nparameters for a programmer " "with \",\". Please see the man page " "for details.\n"); cli_classic_abort_usage(); } for (prog = 0; prog < PROGRAMMER_INVALID; prog++) { name = programmer_table[prog].name; namelen = strlen(name); if (strncmp(optarg, name, namelen) == 0) { switch (optarg[namelen]) { case ':': pparam = strdup(optarg + namelen + 1); if (!strlen(pparam)) { free(pparam); pparam = NULL; } break; case '\0': break; default: /* The continue refers to the * for loop. It is here to be * able to differentiate between * foo and foobar. */ continue; } break; } } if (prog == PROGRAMMER_INVALID) { fprintf(stderr, "Error: Unknown programmer \"%s\". Valid choices are:\n", optarg); list_programmers_linebreak(0, 80, 0); msg_ginfo(".\n"); cli_classic_abort_usage(); } break; case 'R': /* print_version() is always called during startup. */ if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } exit(0); break; case 'h': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); cli_classic_abort_usage(); } cli_classic_usage(argv[0]); exit(0); break; case 'o': #ifdef STANDALONE fprintf(stderr, "Log file not supported in standalone mode. Aborting.\n"); cli_classic_abort_usage(); #else /* STANDALONE */ logfile = strdup(optarg); if (logfile[0] == '\0') { fprintf(stderr, "No log filename specified.\n"); cli_classic_abort_usage(); } #endif /* STANDALONE */ break; default: cli_classic_abort_usage(); break; } } if (optind < argc) { fprintf(stderr, "Error: Extra parameter found.\n"); cli_classic_abort_usage(); } if ((read_it | write_it | verify_it) && check_filename(filename, "image")) { cli_classic_abort_usage(); } if (layoutfile && check_filename(layoutfile, "layout")) { cli_classic_abort_usage(); } if (fmapfile && check_filename(fmapfile, "fmap")) { cli_classic_abort_usage(); } if (referencefile && check_filename(referencefile, "reference")) { cli_classic_abort_usage(); } #ifndef STANDALONE if (logfile && check_filename(logfile, "log")) cli_classic_abort_usage(); if (logfile && open_logfile(logfile)) cli_classic_abort_usage(); #endif /* !STANDALONE */ #if CONFIG_PRINT_WIKI == 1 if (list_supported_wiki) { print_supported_wiki(); goto out; } #endif if (list_supported) { if (print_supported()) ret = 1; goto out; } #ifndef STANDALONE start_logging(); #endif /* !STANDALONE */ print_buildinfo(); msg_gdbg("Command line (%i args):", argc - 1); for (i = 0; i < argc; i++) { msg_gdbg(" %s", argv[i]); } msg_gdbg("\n"); if (layoutfile && read_romlayout(layoutfile)) { ret = 1; goto out; } if (!ifd && !fmap && process_include_args(get_global_layout())) { ret = 1; goto out; } /* Does a chip with the requested name exist in the flashchips array? */ if (chip_to_probe) { for (chip = flashchips; chip && chip->name; chip++) if (!strcmp(chip->name, chip_to_probe)) break; if (!chip || !chip->name) { msg_cerr("Error: Unknown chip '%s' specified.\n", chip_to_probe); msg_gerr("Run flashrom -L to view the hardware supported in this flashrom version.\n"); ret = 1; goto out; } /* Keep chip around for later usage in case a forced read is requested. */ } if (prog == PROGRAMMER_INVALID) { if (CONFIG_DEFAULT_PROGRAMMER != PROGRAMMER_INVALID) { prog = CONFIG_DEFAULT_PROGRAMMER; /* We need to strdup here because we free(pparam) unconditionally later. */ pparam = strdup(CONFIG_DEFAULT_PROGRAMMER_ARGS); msg_pinfo("Using default programmer \"%s\" with arguments \"%s\".\n", programmer_table[CONFIG_DEFAULT_PROGRAMMER].name, pparam); } else { msg_perr("Please select a programmer with the --programmer parameter.\n" "Previously this was not necessary because there was a default set.\n" #if CONFIG_INTERNAL == 1 "To choose the mainboard of this computer use 'internal'. " #endif "Valid choices are:\n"); list_programmers_linebreak(0, 80, 0); msg_ginfo(".\n"); ret = 1; goto out; } } /* FIXME: Delay calibration should happen in programmer code. */ myusec_calibrate_delay(); if (programmer_init(prog, pparam)) { msg_perr("Error: Programmer initialization failed.\n"); ret = 1; goto out_shutdown; } tempstr = flashbuses_to_text(get_buses_supported()); msg_pdbg("The following protocols are supported: %s.\n", tempstr); free(tempstr); for (j = 0; j < registered_master_count; j++) { startchip = 0; while (chipcount < ARRAY_SIZE(flashes)) { startchip = probe_flash(®istered_masters[j], startchip, &flashes[chipcount], 0); if (startchip == -1) break; chipcount++; startchip++; } } if (chipcount > 1) { msg_cinfo("Multiple flash chip definitions match the detected chip(s): \"%s\"", flashes[0].chip->name); for (i = 1; i < chipcount; i++) msg_cinfo(", \"%s\"", flashes[i].chip->name); msg_cinfo("\nPlease specify which chip definition to use with the -c <chipname> option.\n"); ret = 1; goto out_shutdown; } else if (!chipcount) { msg_cinfo("No EEPROM/flash device found.\n"); if (!force || !chip_to_probe) { msg_cinfo("Note: flashrom can never write if the flash chip isn't found " "automatically.\n"); } if (force && read_it && chip_to_probe) { struct registered_master *mst; int compatible_masters = 0; msg_cinfo("Force read (-f -r -c) requested, pretending the chip is there:\n"); /* This loop just counts compatible controllers. */ for (j = 0; j < registered_master_count; j++) { mst = ®istered_masters[j]; /* chip is still set from the chip_to_probe earlier in this function. */ if (mst->buses_supported & chip->bustype) compatible_masters++; } if (!compatible_masters) { msg_cinfo("No compatible controller found for the requested flash chip.\n"); ret = 1; goto out_shutdown; } if (compatible_masters > 1) msg_cinfo("More than one compatible controller found for the requested flash " "chip, using the first one.\n"); for (j = 0; j < registered_master_count; j++) { mst = ®istered_masters[j]; startchip = probe_flash(mst, 0, &flashes[0], 1); if (startchip != -1) break; } if (startchip == -1) { // FIXME: This should never happen! Ask for a bug report? msg_cinfo("Probing for flash chip '%s' failed.\n", chip_to_probe); ret = 1; goto out_shutdown; } if (map_flash(&flashes[0]) != 0) { free(flashes[0].chip); ret = 1; goto out_shutdown; } msg_cinfo("Please note that forced reads most likely contain garbage.\n"); ret = read_flash_to_file(&flashes[0], filename); unmap_flash(&flashes[0]); free(flashes[0].chip); goto out_shutdown; } ret = 1; goto out_shutdown; } else if (!chip_to_probe) { /* repeat for convenience when looking at foreign logs */ tempstr = flashbuses_to_text(flashes[0].chip->bustype); msg_gdbg("Found %s flash chip \"%s\" (%d kB, %s).\n", flashes[0].chip->vendor, flashes[0].chip->name, flashes[0].chip->total_size, tempstr); free(tempstr); } fill_flash = &flashes[0]; print_chip_support_status(fill_flash->chip); unsigned int limitexceeded = count_max_decode_exceedings(fill_flash); if (limitexceeded > 0 && !force) { enum chipbustype commonbuses = fill_flash->mst->buses_supported & fill_flash->chip->bustype; /* Sometimes chip and programmer have more than one bus in common, * and the limit is not exceeded on all buses. Tell the user. */ if ((bitcount(commonbuses) > limitexceeded)) { msg_pdbg("There is at least one interface available which could support the size of\n" "the selected flash chip.\n"); } msg_cerr("This flash chip is too big for this programmer (--verbose/-V gives details).\n" "Use --force/-f to override at your own risk.\n"); ret = 1; goto out_shutdown; } if (!(read_it | write_it | verify_it | erase_it)) { msg_ginfo("No operations were specified.\n"); goto out_shutdown; } if (layoutfile) { layout = get_global_layout(); } else if (ifd && (flashrom_layout_read_from_ifd(&layout, fill_flash, NULL, 0) || process_include_args(layout))) { ret = 1; goto out_shutdown; } else if (fmap && fmapfile) { struct stat s; if (stat(fmapfile, &s) != 0) { msg_gerr("Failed to stat fmapfile \"%s\"\n", fmapfile); ret = 1; goto out_shutdown; } size_t fmapfile_size = s.st_size; uint8_t *fmapfile_buffer = malloc(fmapfile_size); if (!fmapfile_buffer) { ret = 1; goto out_shutdown; } if (read_buf_from_file(fmapfile_buffer, fmapfile_size, fmapfile)) { ret = 1; free(fmapfile_buffer); goto out_shutdown; } if (flashrom_layout_read_fmap_from_buffer(&layout, fill_flash, fmapfile_buffer, fmapfile_size) || process_include_args(layout)) { ret = 1; free(fmapfile_buffer); goto out_shutdown; } free(fmapfile_buffer); } else if (fmap && (flashrom_layout_read_fmap_from_rom(&layout, fill_flash, 0, fill_flash->chip->total_size * 1024) || process_include_args(layout))) { ret = 1; goto out_shutdown; } flashrom_layout_set(fill_flash, layout); flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE, !!force); #if CONFIG_INTERNAL == 1 flashrom_flag_set(fill_flash, FLASHROM_FLAG_FORCE_BOARDMISMATCH, !!force_boardmismatch); #endif flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_AFTER_WRITE, !dont_verify_it); flashrom_flag_set(fill_flash, FLASHROM_FLAG_VERIFY_WHOLE_CHIP, !dont_verify_all); /* FIXME: We should issue an unconditional chip reset here. This can be * done once we have a .reset function in struct flashchip. * Give the chip time to settle. */ programmer_delay(100000); if (read_it) ret = do_read(fill_flash, filename); else if (erase_it) ret = do_erase(fill_flash); else if (write_it) ret = do_write(fill_flash, filename, referencefile); else if (verify_it) ret = do_verify(fill_flash, filename); flashrom_layout_release(layout); out_shutdown: programmer_shutdown(); out: for (i = 0; i < chipcount; i++) free(flashes[i].chip); layout_cleanup(); free(filename); free(fmapfile); free(referencefile); free(layoutfile); free(pparam); /* clean up global variables */ free((char *)chip_to_probe); /* Silence! Freeing is not modifying contents. */ chip_to_probe = NULL; #ifndef STANDALONE free(logfile); ret |= close_logfile(); #endif /* !STANDALONE */ return ret; }
void mcxHashStats ( FILE* fp , mcxHash* h ) { dim buckets = h->n_buckets ; dim buckets_used = 0 ; float ctr = 0.0 ; float cb = 0.0 ; dim max = 0 ; dim entries = 0 ; const char* me = "mcxHashStats" ; int j, k, distr[32] ; mcx_bucket *buck ; for (j=0;j<32;j++) distr[j] = 0 ; for (buck=h->buckets; buck<h->buckets + h->n_buckets; buck++) { dim d = hash_link_size(buck->base) ; hash_link* this= buck->base ; if (d) { buckets_used++ ; entries += d ; ctr += (float) d * d ; cb += (float) d * d * d ; max = MCX_MAX(max, d) ; } while(this) { u32 u = (h->hash)(this->kv.key) ; int ct = bitcount(u) ; this = this->next ; distr[ct]++ ;if (0) fprintf(stderr, "bucket [%d] key [%s]\n", (int)d, ((mcxTing*) this->kv.key)->str) ; } } ctr = ctr / MCX_MAX(1, entries) ; cb = sqrt(cb / MCX_MAX(1, entries)) ; if (buckets && buckets_used) mcxTellf ( fp , me , "%4.2f bucket usage (%ld available, %ld used, %ld entries)" , (double) ((double) buckets_used) / buckets , (long) buckets , (long) buckets_used , (long) entries ) , mcxTellf ( fp , me , "bucket average: %.2f, center: %.2f, cube: %.2f, max: %ld" , (double) entries / ((double) buckets_used) , (double) ctr , (double) cb , (long) max ) ; mcxTellf(fp, me, "bit distribution (promilles):") ; fprintf ( fp , " %-37s %s\n" , "Current bit distribution" , "Ideally random distribution" ) ; for (k=0;k<4;k++) { for (j=k*8;j<(k+1)*8;j++) fprintf(fp, "%3.0f ", entries ? (1000 * (float)distr[j]) / entries : 0.0) ; fprintf(fp, " "); ; for (j=k*8;j<(k+1)*8;j++) fprintf(fp, "%3d ", promilles[j]) ; fprintf(fp, "\n") ; } mcxTellf(fp, me, "link count: %ld", (long) (mcxGrimCount(h->src_link))) ; mcxTellf(fp, me, "link mem count: %ld", (long) (mcxGrimMemSize(h->src_link))) ; mcxTellf(fp, me, "done") ; }
static void grad_dims(unsigned int D, long dims2[D + 1], unsigned int flags, const long dims[D]) { md_copy_dims(D, dims2, dims); dims2[D] = bitcount(flags); }
void refine_cluster_mass2(dyn *b, // root node int verbose) // default = 0 { // Self-consistently determine the total mass within the outermost // closed zero-velocity surface under the specified external field(s). // Use a point-mass approximation for the cluster potential and iterate // until the actual mass within the surface agrees with the mass used // to generate the surface. // // This code is most likely to be called from refine_cluster_mass(). // Quit if internal forces are to be neglected. if (b->get_ignore_internal()) return; // Do nothing if all we want is to set the dyn story and the current // values are up to date. if (verbose == 0 && twiddles(getrq(b->get_dyn_story(), "bound_center_time"), b->get_system_time(), TTOL)) return; unsigned int ext = b->get_external_field(); if (!ext || b->get_tidal_field()) return; // Method assumes that the external field can be characterized as // depending on distance from a single point -- i.e. that it has a // single center -- and that it is independent of velocity. OK to // have multiple external fields (e.g. central mass + power law), // so long as they have a common center. int bits_set = bitcount(ext); vec external_center = 0; if (bits_set != 1) { if (bits_set < 1) return; else { // We have multiple external fields. Check for common center. cerr << " refine_cluster_mass2: "; PRC(ext); PRL(bits_set); int bit = 0, cbit = -1; for (unsigned int i = ext; i != 0; i >>= 1, bit++) { if (i & 01) { if (cbit < 0) { cbit = bit; external_center = b->get_external_center(cbit); } else { if (!twiddles(square(b->get_external_center(bit) - external_center), 0)) { cerr << " refine_cluster_mass2: center " << bit << " inconsistent with center " << cbit << endl; return; } } } } cerr << " common center = " << external_center << endl; } } else
int main(void) { printf("%d %d\n", bitcount(10), bitcount(100)); return EXIT_SUCCESS; }
int main() { int test = 0x1010102; // 4 bits printf("%d\n", bitcount(test)); }
int main(void) { printf("%d\n",bitcount((unsigned)12)); printf("%d\n",bitcount((unsigned)15)); printf("%d\n",bitcount((unsigned)32)); }
int main() { int x; x ^= 7; printf("%d\n", bitcount(x)); return 0; }
bool wxGLCanvas::Create( wxWindow *parent, const wxGLContext *shared, const wxGLCanvas *shared_context_of, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name, int *attribList, const wxPalette& palette) { XVisualInfo *vi, vi_templ; XWindowAttributes xwa; int val, n; m_sharedContext = (wxGLContext*)shared; // const_cast m_sharedContextOf = (wxGLCanvas*)shared_context_of; // const_cast m_glContext = (wxGLContext*) NULL; Display* display = (Display*) wxGetDisplay(); // Check for the presence of the GLX extension if(!glXQueryExtension(display, NULL, NULL)) { wxLogDebug(wxT("wxGLCanvas: GLX extension is missing\n")); return false; } if(attribList) { int data[512], arg=0, p=0; while( (attribList[arg]!=0) && (p<512) ) { switch( attribList[arg++] ) { case WX_GL_RGBA: data[p++] = GLX_RGBA; break; case WX_GL_BUFFER_SIZE: data[p++]=GLX_BUFFER_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_LEVEL: data[p++]=GLX_LEVEL; data[p++]=attribList[arg++]; break; case WX_GL_DOUBLEBUFFER: data[p++] = GLX_DOUBLEBUFFER; data[p++] = 1; break; case WX_GL_STEREO: data[p++] = GLX_STEREO; data[p++] = 1; break; case WX_GL_AUX_BUFFERS: data[p++]=GLX_AUX_BUFFERS; data[p++]=attribList[arg++]; break; case WX_GL_MIN_RED: data[p++]=GLX_RED_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_GREEN: data[p++]=GLX_GREEN_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_BLUE: data[p++]=GLX_BLUE_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_ALPHA: data[p++]=GLX_ALPHA_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_DEPTH_SIZE: data[p++]=GLX_DEPTH_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_STENCIL_SIZE: data[p++]=GLX_STENCIL_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_ACCUM_RED: data[p++]=GLX_ACCUM_RED_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_ACCUM_GREEN: data[p++]=GLX_ACCUM_GREEN_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_ACCUM_BLUE: data[p++]=GLX_ACCUM_BLUE_SIZE; data[p++]=attribList[arg++]; break; case WX_GL_MIN_ACCUM_ALPHA: data[p++]=GLX_ACCUM_ALPHA_SIZE; data[p++]=attribList[arg++]; break; default: break; } } data[p] = 0; attribList = (int*) data; // Get an appropriate visual vi = glXChooseVisual(display, DefaultScreen(display), attribList); if(!vi) return false; // Here we should make sure that vi is the same visual as the // one used by the xwindow drawable in wxCanvas. However, // there is currently no mechanism for this in wx_canvs.cc. } else { // By default, we use the visual of xwindow // NI: is this really senseful ? opengl in e.g. color index mode ? XGetWindowAttributes(display, (Window)wxGetClientAreaWindow(this), &xwa); vi_templ.visualid = XVisualIDFromVisual(xwa.visual); vi = XGetVisualInfo(display, VisualIDMask, &vi_templ, &n); if(!vi) return false; glXGetConfig(display, vi, GLX_USE_GL, &val); if(!val) return false; // Basically, this is it. It should be possible to use vi // in glXCreateContext() below. But this fails with Mesa. // I notified the Mesa author about it; there may be a fix. #ifdef OLD_MESA // Construct an attribute list matching the visual int a_list[32]; n = 0; if(vi->c_class==TrueColor || vi->c_class==DirectColor) { // RGBA visual a_list[n++] = GLX_RGBA; a_list[n++] = GLX_RED_SIZE; a_list[n++] = bitcount(vi->red_mask); a_list[n++] = GLX_GREEN_SIZE; a_list[n++] = bitcount(vi->green_mask); a_list[n++] = GLX_BLUE_SIZE; a_list[n++] = bitcount(vi->blue_mask); glXGetConfig(display, vi, GLX_ALPHA_SIZE, &val); a_list[n++] = GLX_ALPHA_SIZE; a_list[n++] = val; } else { // Color index visual glXGetConfig(display, vi, GLX_BUFFER_SIZE, &val); a_list[n++] = GLX_BUFFER_SIZE; a_list[n++] = val; } a_list[n] = None; // XFree(vi); vi = glXChooseVisual(display, DefaultScreen(display), a_list); if(!vi) return false; #endif /* OLD_MESA */ } m_vi = vi; // safe for later use wxCHECK_MSG( m_vi, false, wxT("required visual couldn't be found") ); // Create the GLX context and make it current wxGLContext *share= m_sharedContext; if (share==NULL && m_sharedContextOf) share = m_sharedContextOf->GetContext(); m_glContext = new wxGLContext( TRUE, this, wxNullPalette, share ); #ifndef OLD_MESA // XFree(vi); #endif SetCurrent(); return true; }
FeriteAMTNode *ferite_amt_compressed_get( FeriteScript *script, FeriteAMTTree *tree, int index ) { int local_index = bitcount( LOCAL_INDEX(tree, index) ); return tree->base[local_index]; }
int main_reshape(int argc, char* argv[]) { int c; while (-1 != (c = getopt(argc, argv, "h"))) { switch (c) { case 'h': usage(argv[0], stdout); help(); exit(0); default: usage(argv[0], stderr); exit(1); } } if (argc - optind < 3) { usage(argv[0], stderr); exit(1); } unsigned int flags = atoi(argv[optind + 0]); unsigned int n = bitcount(flags); assert((int)n + 3 == argc - optind); long in_dims[DIMS]; long in_strs[DIMS]; long out_dims[DIMS]; long out_strs[DIMS]; complex float* in_data = load_cfl(argv[optind + n + 1], DIMS, in_dims); md_calc_strides(DIMS, in_strs, in_dims, CFL_SIZE); md_copy_dims(DIMS, out_dims, in_dims); unsigned int j = 0; for (unsigned int i = 0; i < DIMS; i++) if (MD_IS_SET(flags, i)) out_dims[i] = atoi(argv[optind + j++ + 1]); assert(j == n); assert(md_calc_size(DIMS, in_dims) == md_calc_size(DIMS, out_dims)); md_calc_strides(DIMS, out_strs, out_dims, CFL_SIZE); for (unsigned int i = 0; i < DIMS; i++) if (!(MD_IS_SET(flags, i) || (in_strs[i] == out_strs[i]))) error("Dimensions are not consistent at index %d.\n"); complex float* out_data = create_cfl(argv[optind + n + 2], DIMS, out_dims); md_copy(DIMS, in_dims, out_data, in_data, CFL_SIZE); unmap_cfl(DIMS, in_dims, in_data); unmap_cfl(DIMS, out_dims, out_data); exit(0); }
int main(void) { for (unsigned i = 0; i < 16; ++i) { printf("%d\n", bitcount(i)); } return EXIT_SUCCESS; }
static F parseHexFloat(char *number, char **end) { bool negp = false; unsigned long long mantissa = 0; int mantissaBits = 0; int usedMantissaBits = 0; size_t exponentI = 0; int point = 0; int pointp = false; for (size_t i = 0; number[i] != '\0'; ++i) { char c = number[i]; if (c == '-') { assert(i == 0); // "junk before negative sign in hex float literal" negp = true; } else if (c == '+') { assert(i == 0); // "junk before positive sign in hex float literal" negp = false; } else if (c == '.') { assert(!pointp); // "multiple decimal points in hex float literal" pointp = true; } else if (c == 'p' || c == 'P') { assert(exponentI == 0); // "multiple P markers in hex float literal" exponentI = i; break; } else if (c == 'x') { assert(mantissa == 0); // "junk before hex designator in hex float literal" continue; } else { if (pointp) point += 4; if (c == '0' && mantissa == 0) continue; mantissaBits += 4; if (c != '0') usedMantissaBits = mantissaBits; if (mantissaBits <= 64) mantissa = (mantissa << 4) | unsigned(hexdigit(c)); } } assert(number[exponentI] == 'p' || number[exponentI] == 'P'); // "no exponent in hex float literal" long exponent = strtol(number + exponentI + 1, end, 10); assert(**end == '\0'); // "junk after exponent in hex float literal" double value = 0.0; if (mantissa == 0) { value = floatFromParts(negp, 0, 0); } else { int mantissaLog = bitcount(mantissa); int mantissaExponent = mantissaLog + (mantissaBits > 64 ? mantissaBits - 64 : 0) - point + int(exponent) + 1023; mantissa = mantissa << (60 - mantissaLog); int mantissaShift = 8 + std::max(0, 1-mantissaExponent); if (mantissaShift > 61) { mantissaExponent = 0; mantissa = 0; } else if (mantissaExponent >= 2047) { mantissaExponent = 2047; mantissa = 0; } else { unsigned long long roundBit = mantissa & (1ULL << (mantissaShift-1)); unsigned long long roundMask = mantissa & ((1ULL << (mantissaShift-1)) - 1); unsigned long long evenBit = mantissa & (1ULL << mantissaShift); mantissa = mantissa >> mantissaShift; if (roundBit != 0 && (roundMask != 0 || usedMantissaBits > 60 || evenBit != 0)) mantissa += 1; if (mantissaExponent < 0) mantissaExponent = 0; } value = floatFromParts(negp, mantissaExponent, mantissa); } return value; }
/** * In a two's complemenbt number system, `x &= (x - 1)` deletes the rightmost 1-bit in `x`. * This happens because `x` and `x-1` can never have their rightmost bits equal * to `1` at the same time. */ int main() { unsigned int x = read_bits_i(); printf("%d\n", bitcount(x)); }
int apple_chan_chunk(pcm_io_context_t *io, uint32_t chunk_size, pcm_sample_description_t *fmt, uint8_t *mapping) { /* * Although FDK encoder supports upto 5.1ch, we handle upto * 8 channels here. */ uint32_t i, mChannelLayoutTag, mChannelBitmap, mNumberChannelDescriptions; uint32_t mask = 0; const uint32_t nchannels = fmt->channels_per_frame; uint8_t channels[9] = { 0 }; uint8_t *index[8] = { 0 }; const char *layout = 0; ENSURE(chunk_size >= 12); TRY_IO(pcm_scanb(io, "LLL", &mChannelLayoutTag, &mChannelBitmap, &mNumberChannelDescriptions) != 3); switch (mChannelLayoutTag) { case kAudioChannelLayoutTag_UseChannelBitmap: ENSURE(bitcount(mChannelBitmap) == nchannels); TRY_IO(pcm_skip(io, chunk_size - 12)); fmt->channel_mask = mChannelBitmap; for (i = 0; i < nchannels; ++i) mapping[i] = i; return 0; case kAudioChannelLayoutTag_UseChannelDescriptions: ENSURE(mNumberChannelDescriptions == nchannels); ENSURE(chunk_size >= 12 + nchannels * 20); for (i = 0; i < mNumberChannelDescriptions; ++i) { uint32_t mChannelLabel; TRY_IO(pcm_read32be(io, &mChannelLabel)); ENSURE(mChannelLabel && mChannelLabel <= 0xff); channels[i] = mChannelLabel; TRY_IO(pcm_skip(io, 16)); } TRY_IO(pcm_skip(io, chunk_size - 12 - nchannels * 20)); apple_translate_channel_labels(channels, nchannels); for (i = 0; i < nchannels; ++i) if (channels[i] > kAudioChannelLabel_TopBackLeft) goto FAIL; break; default: ENSURE((mChannelLayoutTag & 0xffff) == nchannels); TRY_IO(pcm_skip(io, chunk_size - 12)); switch (mChannelLayoutTag) { /* 1ch */ case kAudioChannelLayoutTag_Mono: layout = "\x03"; break; /* 1.1ch */ case kAudioChannelLayoutTag_AC3_1_0_1: layout = "\x03\x04"; break; /* 2ch */ case kAudioChannelLayoutTag_Stereo: case kAudioChannelLayoutTag_MatrixStereo: case kAudioChannelLayoutTag_Binaural: layout = "\x01\x02"; break; /* 2.1ch */ case kAudioChannelLayoutTag_DVD_4: layout = "\x01\x02\x04"; break; /* 3ch */ case kAudioChannelLayoutTag_MPEG_3_0_A: layout = "\x01\x02\x03"; break; case kAudioChannelLayoutTag_AC3_3_0: layout = "\x01\x03\x02"; break; case kAudioChannelLayoutTag_MPEG_3_0_B: layout = "\x03\x01\x02"; break; case kAudioChannelLayoutTag_ITU_2_1: layout = "\x01\x02\x09"; break; /* 3.1ch */ case kAudioChannelLayoutTag_DVD_10: layout = "\x01\x02\x03\x04"; break; case kAudioChannelLayoutTag_AC3_3_0_1: layout = "\x01\x03\x02\x04"; break; case kAudioChannelLayoutTag_DVD_5: layout = "\x01\x02\x04\x09"; break; case kAudioChannelLayoutTag_AC3_2_1_1: layout = "\x01\x02\x09\x04"; break; /* 4ch */ case kAudioChannelLayoutTag_Quadraphonic: case kAudioChannelLayoutTag_ITU_2_2: layout = "\x01\x02\x0A\x0B"; break; case kAudioChannelLayoutTag_MPEG_4_0_A: layout = "\x01\x02\x03\x09"; break; case kAudioChannelLayoutTag_MPEG_4_0_B: layout = "\x03\x01\x02\x09"; break; case kAudioChannelLayoutTag_AC3_3_1: layout = "\x01\x03\x02\x09"; break; /* 4.1ch */ case kAudioChannelLayoutTag_DVD_6: layout = "\x01\x02\x04\x0A\x0B"; break; case kAudioChannelLayoutTag_DVD_18: layout = "\x01\x02\x0A\x0B\x04"; break; case kAudioChannelLayoutTag_DVD_11: layout = "\x01\x02\x03\x04\x09"; break; case kAudioChannelLayoutTag_AC3_3_1_1: layout = "\x01\x03\x02\x09\x04"; break; /* 5ch */ case kAudioChannelLayoutTag_MPEG_5_0_A: layout = "\x01\x02\x03\x0A\x0B"; break; case kAudioChannelLayoutTag_Pentagonal: case kAudioChannelLayoutTag_MPEG_5_0_B: layout = "\x01\x02\x0A\x0B\x03"; break; case kAudioChannelLayoutTag_MPEG_5_0_C: layout = "\x01\x03\x02\x0A\x0B"; break; case kAudioChannelLayoutTag_MPEG_5_0_D: layout = "\x03\x01\x02\x0A\x0B"; break; /* 5.1ch */ case kAudioChannelLayoutTag_MPEG_5_1_A: layout = "\x01\x02\x03\x04\x0A\x0B"; break; case kAudioChannelLayoutTag_MPEG_5_1_B: layout = "\x01\x02\x0A\x0B\x03\x04"; break; case kAudioChannelLayoutTag_MPEG_5_1_C: layout = "\x01\x03\x02\x0A\x0B\x04"; break; case kAudioChannelLayoutTag_MPEG_5_1_D: layout = "\x03\x01\x02\x0A\x0B\x04"; break; /* 6ch */ case kAudioChannelLayoutTag_Hexagonal: case kAudioChannelLayoutTag_AudioUnit_6_0: layout = "\x01\x02\x0A\x0B\x03\x09"; break; case kAudioChannelLayoutTag_AAC_6_0: layout = "\x03\x01\x02\x0A\x0B\x09"; break; /* 6.1ch */ case kAudioChannelLayoutTag_MPEG_6_1_A: layout = "\x01\x02\x03\x04\x0A\x0B\x09"; break; case kAudioChannelLayoutTag_AAC_6_1: layout = "\x03\x01\x02\x0A\x0B\x09\x04"; break; /* 7ch */ case kAudioChannelLayoutTag_AudioUnit_7_0: layout = "\x01\x02\x0A\x0B\x03\x05\x06"; break; case kAudioChannelLayoutTag_AudioUnit_7_0_Front: layout = "\x01\x02\x0A\x0B\x03\x07\x08"; break; case kAudioChannelLayoutTag_AAC_7_0: layout = "\x03\x01\x02\x0A\x0B\x05\x06"; break; /* 7.1ch */ case kAudioChannelLayoutTag_MPEG_7_1_A: layout = "\x01\x02\x03\x04\x0A\x0B\x07\x08"; break; case kAudioChannelLayoutTag_MPEG_7_1_B: layout = "\x03\x07\x08\x01\x02\x05\x06\x04"; break; case kAudioChannelLayoutTag_MPEG_7_1_C: layout = "\x01\x02\x03\x04\x0A\x0B\x05\x06"; break; case kAudioChannelLayoutTag_Emagic_Default_7_1: layout = "\x01\x02\x0A\x0B\x03\x04\x07\x08"; break; /* 8ch */ case kAudioChannelLayoutTag_Octagonal: layout = "\x01\x02\x05\x06\x03\x09\x0A\x0B"; break; case kAudioChannelLayoutTag_AAC_Octagonal: layout = "\x03\x01\x02\x0A\x0B\x05\x06\x09"; break; default: goto FAIL; } strcpy((char*)channels, layout); } for (i = 0; i < nchannels; ++i) mask |= 1 << (channels[i] - 1); fmt->channel_mask = mask; ENSURE(bitcount(mask) == nchannels); for (i = 0; i < nchannels; ++i) index[i] = channels + i; qsort(index, nchannels, sizeof(char*), channel_compare); for (i = 0; i < nchannels; ++i) mapping[i] = index[i] - channels; return 0; FAIL: return -1; }
static int cheriabi_fetch_syscall_args(struct thread *td, struct syscall_args *sa) { struct trapframe *locr0 = td->td_frame; /* aka td->td_pcb->pcv_regs */ struct cheri_frame *capreg = &td->td_pcb->pcb_cheriframe; register_t intargs[8]; uintptr_t ptrargs[8]; struct sysentvec *se; int error, i, isaved, psaved, curint, curptr, nintargs, nptrargs; error = 0; bzero(sa->args, sizeof(sa->args)); /* compute next PC after syscall instruction */ td->td_pcb->pcb_tpc = sa->trapframe->pc; /* Remember if restart */ if (DELAYBRANCH(sa->trapframe->cause)) /* Check BD bit */ locr0->pc = MipsEmulateBranch(locr0, sa->trapframe->pc, 0, 0); else locr0->pc += sizeof(int); sa->code = locr0->v0; switch (sa->code) { case CHERIABI_SYS___syscall: case CHERIABI_SYS_syscall: /* * This is an indirect syscall, in which the code is the first * argument. */ sa->code = locr0->a0; intargs[0] = locr0->a1; intargs[1] = locr0->a2; intargs[2] = locr0->a3; intargs[3] = locr0->a4; intargs[4] = locr0->a5; intargs[5] = locr0->a6; intargs[6] = locr0->a7; isaved = 7; break; default: /* * A direct syscall, arguments are just parameters to the syscall. */ intargs[0] = locr0->a0; intargs[1] = locr0->a1; intargs[2] = locr0->a2; intargs[3] = locr0->a3; intargs[4] = locr0->a4; intargs[5] = locr0->a5; intargs[6] = locr0->a6; intargs[7] = locr0->a7; isaved = 8; break; } #if defined(CPU_CHERI_CHERI0) || defined (CPU_CHERI_CHERI8) || defined(CPU_CHERI_CHERI16) #error CHERIABI does not support fewer than 8 argument registers #endif /* * XXXBD: we should idealy use a user capability rather than KDC * to generate the pointers, but then we have to answer: which one? */ CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c3, 0); CHERI_CTOPTR(ptrargs[0], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c4, 0); CHERI_CTOPTR(ptrargs[1], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c5, 0); CHERI_CTOPTR(ptrargs[2], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c6, 0); CHERI_CTOPTR(ptrargs[3], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c7, 0); CHERI_CTOPTR(ptrargs[4], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c8, 0); CHERI_CTOPTR(ptrargs[5], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c9, 0); CHERI_CTOPTR(ptrargs[6], CHERI_CR_CTEMP0, CHERI_CR_KDC); CHERI_CLC(CHERI_CR_CTEMP0, CHERI_CR_KDC, &capreg->cf_c10, 0); CHERI_CTOPTR(ptrargs[7], CHERI_CR_CTEMP0, CHERI_CR_KDC); psaved = 8; #ifdef TRAP_DEBUG if (trap_debug) printf("SYSCALL #%d pid:%u\n", sa->code, td->td_proc->p_pid); #endif se = td->td_proc->p_sysent; /* * XXX * Shouldn't this go before switching on the code? */ if (se->sv_mask) sa->code &= se->sv_mask; if (sa->code >= se->sv_size) sa->callp = &se->sv_table[0]; else sa->callp = &se->sv_table[sa->code]; sa->narg = sa->callp->sy_narg; nptrargs = bitcount(CHERIABI_SYS_argmap[sa->code].sam_ptrmask); nintargs = sa->narg - nintargs; KASSERT(nintargs <= isaved, ("SYSCALL #%u pid:%u, nintargs (%u) > isaved (%u).\n", sa->code, td->td_proc->p_pid, nintargs, isaved)); KASSERT(nptrargs <= psaved, ("SYSCALL #%u pid:%u, nptrargs (%u) > psaved (%u).\n", sa->code, td->td_proc->p_pid, nptrargs, psaved)); /* * Check each argument to see if it is a pointer and pop an argument * off the appropriate list. */ curint = curptr = 0; for (i = 0; i < sa->narg; i++) sa->args[i] = (CHERIABI_SYS_argmap[sa->code].sam_ptrmask & 1 << i) ? ptrargs[curptr++] : intargs[curint++]; td->td_retval[0] = 0; td->td_retval[1] = locr0->v1; return (error); }