TaMaDiDecoder::TaMaDiDecoder(Target* target, int n): Operator(target) { srcFileName="TaMaDiDecoder"; ostringstream name; int outWires = intlog2(n-1); name <<"TaMaDiDecoder_n"<<n<<"_output"<<outWires<<"_uid"<<getNewUId(); setName(name.str()); setCopyrightString("Bogdan Pasca (2011)"); /* Set up the I/O signals of of the entity */ int inWires = intlog2(n-1); addInput ("X",inWires,true); addOutput ("R",n,1,true); vhdl << tab << "with X select" << endl; vhdl << tab << "R <= "<<endl; for (int i=0;i<n;i++){ vhdl << tab << tab <<"\""; for (int k=0;k<i;k++) vhdl << "0"; vhdl << "1"; for (int k=i+1;k<n;k++) vhdl << "0"; vhdl <<"\""; vhdl << " when \"" << unsignedBinary( n-1-i, inWires) <<"\","<<endl; } vhdl << tab << tab << zg(n) <<" when others;" << endl; }
// EINT3 (INT_GPIO) interrupt handler static void int_handler_eint3() { elua_int_id id = ELUA_INT_INVALID_INTERRUPT; pio_code resnum = 0; int pidx, pin; EXTINT |= 1 << EINT3_BIT; // clear interrupt // Look for interrupt source // In can only be GPIO0/GPIO2, as the EXT interrupts are not (yet) used pidx = ( IO_INT_STAT & 1 ) ? 0 : 1; if( *posedge_status[ pidx ] ) { id = INT_GPIO_POSEDGE; pin = intlog2( *posedge_status[ pidx ] ); } else { id = INT_GPIO_NEGEDGE; pin = intlog2( *negedge_status[ pidx ] ); } resnum = PLATFORM_IO_ENCODE( pidx * 2, pin, PLATFORM_IO_ENC_PIN ); *intclr_regs[ pidx ] = 1 << pin; // Run the interrupt through eLua cmn_int_handler( id, resnum ); VICVectAddr = 0; // ACK interrupt }
ShiftAddOp::ShiftAddOp(ShiftAddDag* impl, ShiftAddOpType op, ShiftAddOp* i, int s, ShiftAddOp* j) : impl(impl), op(op), i(i), j(j), s(s) { n=impl->computeConstant(op, i, s, j); // now compute the size according to this constant, as the log2 of the max value the result can take if (n >= 0) size = intlog2(n * ((mpz_class(1)<<impl->icm->xsize)-1)); else size = 1 + intlog2(-n* ((mpz_class(1)<<impl->icm->xsize)-1)); // we need a sign bit if(n==1) size=impl->icm->xsize; // compute the cost in terms of full adders of this node switch(op) { case X: cost_in_full_adders = 0; break; case Add: if (s >= j->size) // no overlap of bits of Vi<<s and Vj cost_in_full_adders = 0; else cost_in_full_adders = size - s - 1; // -1 because the cout bit is for free break; case Sub: cost_in_full_adders = size - 1; // -1 because the cout bit is for free break; case RSub: if (s >= j->size) // no overlap of bits of Vi<<s and Vj cost_in_full_adders = i->size - 1; // still need to negate i else cost_in_full_adders = size - s - 1; // -1 because the cout bit is for free break; case Shift: cost_in_full_adders = 0; break; case Neg: cost_in_full_adders = size -1; // -1 because the cout bit is for free break; } // build the variable name ostringstream o; if(n==1) o <<"X"; else if(n>=0) o<<"P"<<mpz2string(n)<<"X"; else o<<"M"<<mpz2string(-n)<<"X"; name = o.str(); // and add this object to the dictionary of the implementation if (op!=X) impl->saolist.push_back(this); }
u32 platform_spi_setup( unsigned id, int mode, u32 clock, unsigned cpol, unsigned cpha, unsigned databits ) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; u8 prescaler_idx = intlog2( ( unsigned ) ( SPI_GET_BASE_CLK( id ) / clock ) ); if ( prescaler_idx < 0 ) prescaler_idx = 0; if ( prescaler_idx > 7 ) prescaler_idx = 7; /* Configure SPI pins */ GPIO_InitStructure.GPIO_Pin = spi_gpio_pins[ id ]; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(spi_gpio_port[ id ], &GPIO_InitStructure); /* Take down, then reconfigure SPI peripheral */ SPI_Cmd( spi[ id ], DISABLE ); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = mode ? SPI_Mode_Master : SPI_Mode_Slave; SPI_InitStructure.SPI_DataSize = ( databits == 16 ) ? SPI_DataSize_16b : SPI_DataSize_8b; // not ideal, but defaults to sane 8-bits SPI_InitStructure.SPI_CPOL = cpol ? SPI_CPOL_High : SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = cpha ? SPI_CPHA_2Edge : SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = spi_prescaler[ prescaler_idx ]; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init( spi[ id ], &SPI_InitStructure ); SPI_Cmd( spi[ id ], ENABLE ); return ( SPI_GET_BASE_CLK( id ) / ( ( ( u16 )2 << ( prescaler_idx ) ) ) ); }
void ntt_forward(uint64_t *data, size_t len) { // Determined using nttgen const uint64_t p = 4179340454199820289; if(len == 1) { return; } uint64_t *twiddle = twiddles[intlog2(len)]; for(size_t i=0;i<len/2;i++) { uint64_t a = data[i]; uint64_t b = data[i+len/2]; data[i] = (a+b)%p; // FIXME: Prevent overflow, but bad for branch predition if(a < b) { a+=p; } data[i+len/2] = (a-b)%p; data[i+len/2] = modmul(data[i+len/2], twiddle[i], p); } ntt_forward(data, len/2); ntt_forward(data+len/2, len/2); }
/** * KMetis Algorithm */ void partition(MetisGraph* metisGraph, int nparts) { int coarsenTo = (int) max(metisGraph->getNumNodes() / (40 * intlog2(nparts)), 20 * (nparts)); int maxVertexWeight = (int) (1.5 * ((metisGraph->getNumNodes()) / (double) coarsenTo)); Coarsener coarsener(false, coarsenTo, maxVertexWeight); Galois::StatTimer T; T.start(); Galois::Timer t; t.start(); MetisGraph* mcg = coarsener.coarsen(metisGraph); t.stop(); cout<<"coarsening time: " << t.get() << " ms"<<endl; float* totalPartitionWeights = new float[nparts]; std::fill_n(totalPartitionWeights, nparts, 1 / (float) nparts); maxVertexWeight = (int) (1.5 * ((mcg->getNumNodes()) / COARSEN_FRACTION)); PMetis pmetis(20, maxVertexWeight); Galois::Timer init_part_t; init_part_t.start(); pmetis.mlevelRecursiveBisection(mcg, nparts, totalPartitionWeights, 0, 0); init_part_t.stop(); cout << "initial partition time: "<< init_part_t.get() << " ms"<<endl; Galois::Timer refine_t; std::fill_n(totalPartitionWeights, nparts, 1 / (float) nparts); refine_t.start(); refineKWay(mcg, metisGraph, totalPartitionWeights, (float) 1.03, nparts); refine_t.stop(); cout << "refine time: " << refine_t.get() << " ms"<<endl; delete[] totalPartitionWeights; T.stop(); }
// Lua: sample( id, count ) static int adc_sample( lua_State* L ) { unsigned id, count, nchans = 1; int res, i; count = luaL_checkinteger( L, 2 ); if ( ( count == 0 ) || count & ( count - 1 ) ) return luaL_error( L, "count must be power of 2 and > 0" ); // If first parameter is a table, extract channel list if ( lua_istable( L, 1 ) == 1 ) { nchans = lua_objlen(L, 1); // Get/check list of channels and setup for( i = 0; i < nchans; i++ ) { lua_rawgeti( L, 1, i+1 ); id = luaL_checkinteger( L, -1 ); MOD_CHECK_ID( adc, id ); res = adc_setup_channel( id, intlog2( count ) ); if ( res != PLATFORM_OK ) return luaL_error( L, "sampling setup failed" ); } // Initiate sampling platform_adc_start_sequence(); } else if ( lua_isnumber( L, 1 ) == 1 ) { id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( adc, id ); res = adc_setup_channel( id, intlog2( count ) ); if ( res != PLATFORM_OK ) return luaL_error( L, "sampling setup failed" ); platform_adc_start_sequence(); } else { return luaL_error( L, "invalid channel selection" ); } return 0; }
// (adc-sample 'num 'num) -> Nil any plisp_adc_sample(any ex) { unsigned id, count = 0, nchans = 1; int res, i; any x, y, s; // get count value, the second parameter // in the picoLisp function call. s = cdr(ex), y = EVAL(car(s)); s = cdr(s); NeedNum(ex, y = EVAL(car(s))); count = unBox(y); // validate count. if ((count == 0) || count & (count - 1)) err(ex, y, "count must be power of 2 and > 0"); // get first parameter in the function // call. x = cdr(ex), y = EVAL(car(x)); // If first parameter is a table, // extract channel list. if (isCell(y)) { nchans = length(y); for (i = 0; i < nchans; i++) { NeedNum(y, car(y)); id = unBox(car(y)); MOD_CHECK_ID(ex, adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) err(ex, y, "sampling setup failed"); } // initiate sampling. platform_adc_start_sequence(); } else if (isNum(y)) { NeedNum(ex, y); id = unBox(y); MOD_CHECK_ID(ex, adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) err(ex, y, "sampling setup failed"); platform_adc_start_sequence(); } else { err(ex, y, "invalid channel selection"); } return Nil; }
KDTree::KDTree(const Mesh& mesh, int maxDepth) : _mesh(mesh) { if (maxDepth < 0){ // if no maxdepth specified, let's make it so that ~ 50 items are in the leaves maxDepth = 1+intlog2(mesh.getTriangles().size()/50); } buildRootNode(maxDepth); }
// Lua: uart.set_buffer( id, size ) static int uart_set_buffer( lua_State *L ) { int id = luaL_checkinteger( L, 1 ); u32 size = ( u32 )luaL_checkinteger( L, 2 ); MOD_CHECK_ID( uart, id ); if( size && ( size & ( size - 1 ) ) ) return luaL_error( L, "the buffer size must be a power of 2 or 0" ); if( size == 0 && id >= SERMUX_SERVICE_ID_FIRST ) return luaL_error( L, "disabling buffers on virtual UARTs is not allowed" ); if( platform_uart_set_buffer( id, intlog2( size ) ) == PLATFORM_ERR ) return luaL_error( L, "unable to set UART buffer" ); return 0; }
// FIXME - i386 specific (EM_386) L4_Word_t elf_load32(AddrSpace_t *space, L4_Word_t image, L4_Word_t size) { Elf32_Ehdr *eh = (Elf32_Ehdr *) image; Elf32_Phdr *ph; int i; assert(space != NULL); debug("elf_load32: loading image at %lx (size %lx) for as: %p\n", image, size, space); if ((eh->e_ident[EI_MAG0] != ELFMAG0) || (eh->e_ident[EI_MAG1] != ELFMAG1) || (eh->e_ident[EI_MAG2] != ELFMAG2) || (eh->e_ident[EI_MAG3] != ELFMAG3) || (eh->e_type != ET_EXEC) || (eh->e_machine != EM_386) || (eh->e_phoff == 0)) { debug("elf_load32: illegal ELF image at %lx\n", (L4_Word_t) image); return 0; } for (i = 0; i < eh->e_phnum; i++) { L4_Fpage_t vp, fp; L4_Word_t size; uint8_t *src, *dest; ph = (Elf32_Phdr *) (image + eh->e_phoff + i * eh->e_phentsize); if (ph->p_type != PT_LOAD) continue; assert((ph->p_offset + ph->p_filesz) < size); assert(ph->p_filesz <= ph->p_memsz); size = intlog2(ph->p_memsz); vp = L4_Fpage(ph->p_vaddr, size); fp = address_space_request_page(space, vp); if (L4_IsNilFpage(fp)) { debug("elf_load32: can't allocate memory\n"); return 0; } dest = (uint8_t *) L4_Address(fp); src = (uint8_t *) (image + ph->p_offset); memcpy(dest, src, ph->p_filesz); memset(dest + ph->p_filesz, 0, size - ph->p_filesz); } return eh->e_entry; }
static L4_Word_t thread_prepare_stack(AddrSpace_t *as, L4_Word_t sp, char *cmdline, int n, char **paths, HpfCapability *caps) { L4_Word_t size; int i; assert(as != NULL); /* nothing to do in this case */ if (cmdline == NULL) return 0; /* we need room for cmdline and for path/capability pairs */ size = strlen(cmdline) + 1 + 4 * sizeof(uint8_t *); size += n * sizeof(HpfCapability); for (i = 0; i < n; i++) { size += strlen(paths[i]) + 1; } if (size < PAGE_SIZE) size = PAGE_SIZE; size = intlog2(size); L4_Fpage_t vp = L4_Fpage(sp - size, size); L4_Fpage_t fp = address_space_request_page(as, vp); if (L4_IsNilFpage(fp)) { debug("thread_prepare_stack: can't setup stack!\n"); return 0; } uint8_t *start = (uint8_t *) L4_Address(fp); uint32_t *tmp = (uint32_t *) start; tmp[0] = L4_Address(vp) + 4 * sizeof(uint8_t *); /* pointer to cmdline */ tmp[1] = n; /* number of path/capability pairs */ tmp[2] = L4_Address(vp) + 4 * sizeof(uint8_t *) + strlen(cmdline) + 1; /* pointer to array of paths */ tmp[3] = L4_Address(vp) + size - n * sizeof(HpfCapability); /* pointer to array of caps */ strcpy(start + 4 * sizeof(uint8_t *), cmdline); memcpy(start + size - n * sizeof(HpfCapability), caps, n * sizeof(HpfCapability)); start += 4 * sizeof(uint8_t *) + strlen(cmdline) + 1; for (i = 0; i < n; i++) { char *x = paths[i]; while (*x) *start++ = *x++; *start++ = 0; } return size; }
/************************************************************************* * This function is the entry point for KWMETIS **************************************************************************/ void METIS_WPartGraphVKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *vsize, int *wgtflag, int *numflag, int *nparts, float *tpwgts, int *options, int *volume, idxtype *part) { /*int i, j;*/ GraphType graph; CtrlType ctrl; if (*numflag == 1) Change2CNumbering(*nvtxs, xadj, adjncy); VolSetUpGraph(&graph, OP_KVMETIS, *nvtxs, 1, xadj, adjncy, vwgt, vsize, *wgtflag); if (options[0] == 0) { /* Use the default parameters */ ctrl.CType = KVMETIS_CTYPE; ctrl.IType = KVMETIS_ITYPE; ctrl.RType = KVMETIS_RTYPE; ctrl.dbglvl = KVMETIS_DBGLVL; } else { ctrl.CType = options[OPTION_CTYPE]; ctrl.IType = options[OPTION_ITYPE]; ctrl.RType = options[OPTION_RTYPE]; ctrl.dbglvl = options[OPTION_DBGLVL]; } ctrl.optype = OP_KVMETIS; ctrl.CoarsenTo = amax((*nvtxs)/(40*intlog2(*nparts)), 20*(*nparts)); ctrl.maxvwgt = 1.5*((graph.vwgt ? idxsum(*nvtxs, graph.vwgt) : (*nvtxs))/ctrl.CoarsenTo); InitRandom(-1); AllocateWorkSpace(&ctrl, &graph, *nparts); IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl)); IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr)); *volume = MlevelVolKWayPartitioning(&ctrl, &graph, *nparts, part, tpwgts, 1.03); IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr)); IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl)); FreeWorkSpace(&ctrl, &graph); if (*numflag == 1) Change2FNumbering(*nvtxs, xadj, adjncy, part); }
// PicoC: adc_sample(id, count); // In PicoC, you could write an array and // use an iteration to init and setup a // list of channels. static void adc_sample(pstate *p, val *r, val **param, int n) { unsigned id, count; int res; count = param[1]->Val->UnsignedInteger; if ((count == 0) || count & (count - 1)) return pmod_error("count must be power of 2 and > 0"); id = param[0]->Val->UnsignedInteger; MOD_CHECK_ID(adc, id); res = adc_setup_channel(id, intlog2(count)); if (res != PLATFORM_OK) return pmod_error("sampling setup failed"); platform_adc_start_sequence(); r->Val->Integer = res; }
TaMaDiPriorityEncoder::TaMaDiPriorityEncoder(Target* target, int n): Operator(target) { srcFileName="TaMaDiPriorityEncoder"; ostringstream name; int outWires = intlog2(n-1); name <<"TaMaDiPriorityEncoder_n"<<n<<"_output"<<outWires<<"_uid"<<getNewUId(); setName(name.str()); setCopyrightString("Bogdan Pasca (2011)"); /* Set up the I/O signals of of the entity */ addInput ("X",n,true); addOutput ("R",outWires,1,true); // vhdl << tab << "with X select" << endl; // vhdl << tab << "R <= "<<endl; // for (int i=0;i<n;i++){ // vhdl << tab << tab << "\""<< unsignedBinary(n-1-i,outWires) <<"\" when \""; // for (int k=0;k<i;k++) // vhdl << "0"; // vhdl << "1"; // for (int k=i+1;k<n;k++) // vhdl << "X"; // vhdl <<"\","<<endl; // } // vhdl << tab << tab << tab << zg(outWires) <<" when others;" << endl; vhdl << tab << "R <= "<<endl; for (int i=0;i<n;i++){ vhdl << tab << tab << "\""<< unsignedBinary(n-1-i,outWires) <<"\" when "; vhdl << "X("<<n-i-1<<")='1' else" << endl; } ostringstream dcare; dcare << "\""; for (int i=0; i<outWires; i++) dcare << "X"; dcare << "\""; vhdl << tab << tab << tab << dcare.str() <<";" << endl; }
u32 platform_adc_op( unsigned id, int op, u32 data ) { elua_adc_ch_state *s = adc_get_ch_state( id ); elua_adc_dev_state *d = adc_get_dev_state( 0 ); u32 res = 0; switch( op ) { case PLATFORM_ADC_GET_MAXVAL: res = pow( 2, ADC_BIT_RESOLUTION ) - 1; break; case PLATFORM_ADC_SET_SMOOTHING: res = adc_update_smoothing( id, ( u8 )intlog2( ( unsigned ) data ) ); break; case PLATFORM_ADC_SET_BLOCKING: s->blocking = data; break; case PLATFORM_ADC_IS_DONE: res = ( s->op_pending == 0 ); break; case PLATFORM_ADC_OP_SET_TIMER: if ( d->timer_id != data ) d->running = 0; platform_adc_stop( id ); d->timer_id = data; break; case PLATFORM_ADC_OP_SET_CLOCK: res = platform_adc_setclock( id, data ); break; case PLATFORM_ADC_SET_FREERUNNING: s->freerunning = data; break; } return res; }
void ntt_inverse(uint64_t *data, size_t len) { // Determined using nttgen const uint64_t p = 4179340454199820289; const uint64_t scale = 2089670227099910145; // Fast shortcut if(len == 1) { return; } ntt_inverse(data, len/2); ntt_inverse(data+len/2, len/2); uint64_t *twiddle = twiddles[intlog2(len)]; for(size_t i=0;i<len/2;i++) { uint64_t a = data[i]; uint64_t b = modmul(data[i+len/2], twiddle[(len - i)%len], p); data[i] = modmul(a+b, scale, p); data[i+len/2] = modmul(a+p-b, scale, p); } }
static int m88ds3103_read_status(struct dvb_frontend *fe, enum fe_status *status) { struct m88ds3103_dev *dev = fe->demodulator_priv; struct i2c_client *client = dev->client; struct dtv_frontend_properties *c = &fe->dtv_property_cache; int ret, i, itmp; unsigned int utmp; u8 buf[3]; *status = 0; if (!dev->warm) { ret = -EAGAIN; goto err; } switch (c->delivery_system) { case SYS_DVBS: ret = regmap_read(dev->regmap, 0xd1, &utmp); if (ret) goto err; if ((utmp & 0x07) == 0x07) *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; break; case SYS_DVBS2: ret = regmap_read(dev->regmap, 0x0d, &utmp); if (ret) goto err; if ((utmp & 0x8f) == 0x8f) *status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK; break; default: dev_dbg(&client->dev, "invalid delivery_system\n"); ret = -EINVAL; goto err; } dev->fe_status = *status; dev_dbg(&client->dev, "lock=%02x status=%02x\n", utmp, *status); /* CNR */ if (dev->fe_status & FE_HAS_VITERBI) { unsigned int cnr, noise, signal, noise_tot, signal_tot; cnr = 0; /* more iterations for more accurate estimation */ #define M88DS3103_SNR_ITERATIONS 3 switch (c->delivery_system) { case SYS_DVBS: itmp = 0; for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { ret = regmap_read(dev->regmap, 0xff, &utmp); if (ret) goto err; itmp += utmp; } /* use of single register limits max value to 15 dB */ /* SNR(X) dB = 10 * ln(X) / ln(10) dB */ itmp = DIV_ROUND_CLOSEST(itmp, 8 * M88DS3103_SNR_ITERATIONS); if (itmp) cnr = div_u64((u64) 10000 * intlog2(itmp), intlog2(10)); break; case SYS_DVBS2: noise_tot = 0; signal_tot = 0; for (i = 0; i < M88DS3103_SNR_ITERATIONS; i++) { ret = regmap_bulk_read(dev->regmap, 0x8c, buf, 3); if (ret) goto err; noise = buf[1] << 6; /* [13:6] */ noise |= buf[0] & 0x3f; /* [5:0] */ noise >>= 2; signal = buf[2] * buf[2]; signal >>= 1; noise_tot += noise; signal_tot += signal; } noise = noise_tot / M88DS3103_SNR_ITERATIONS; signal = signal_tot / M88DS3103_SNR_ITERATIONS; /* SNR(X) dB = 10 * log10(X) dB */ if (signal > noise) { itmp = signal / noise; cnr = div_u64((u64) 10000 * intlog10(itmp), (1 << 24)); } break; default: dev_dbg(&client->dev, "invalid delivery_system\n"); ret = -EINVAL; goto err; } if (cnr) { c->cnr.stat[0].scale = FE_SCALE_DECIBEL; c->cnr.stat[0].svalue = cnr; } else { c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE; } } else {
u32 platform_adc_set_smoothing( unsigned id, u32 length ) { return adc_update_smoothing( id, ( u8 )intlog2( ( unsigned ) length ) ); }
void rdcircsum(float *outpsf, osm_ds *head, int bin, float distance, float size, int tandem) { int Nxy,Nz,Nr; int ovrsmp; int symmetric; FILE *logfp,*infofp; float *pupil; /* Pupil function and its Fourier transform */ fcomplex *Cpupil; float *psfobj; /* One (xy) plane of the objective's PSF */ float *psfbin1; /* One plane (xy) of the intermediate PSF */ float *psfbin; /* One plane (xy) of the final PSF */ float *psfcond; /* One plane of the condenser PSF */ fcomplex *otfcond; float peak; /* Max. value of arrays (normalization) */ float illum; /* One sample of the illumination patern */ float tmp; float alpha; float *radpsf; /* PSF intensity image XY crossection */ float *psfxz; float Vxx, Vyx, /* Elements of the sampling matrix */ Vxy, Vyy; int iz, ir, ix, /* indices for depth, radial distance, */ iy, jx, jy; /* and the two lateral coordinates */ int n1, n2; /* indices for periodic sampling in the PSF */ int Maxn1, Maxn2, Minn1, Minn2; int Maxir; /* __Stuff to calculte min and max index values */ float Sxmin, Sxmax, xmax, Maxxprime; float Symin, Symax, ymax, Maxyprime; float Maxrprime; int sMinn1, lMaxn1; float mytemp; /* Number of samples in (x, y), in z and radially (= Nxy*sqrt(2))*/ int HalfNxy, Zup; /* Number of samples radially on the convolution of psfcond and pupil */ int NetNr; /* Number of samples in (x,y) before binning */ int Lxy; /* Number of samples in (x,y) before binning and after oversampling for the summation*/ int Mxy, Halfway; /* Pupil function region of support */ int Pxy; /* normalized radial or lateral sampling rate */ float nrm_deltar, nrm_deltaxy=1.0; /* new lateral sampling rate based on oversampling */ float deltaxy1; /* oversampling rate for the xy plane */ int osamp; /* Floating point version of the above */ float ratio; /* Weigth factors for apodization */ float weight, appod; /* Distances in the detector plane (mm) */ float rd, x, y, ysq; /* Shifted coordinates */ float xprime, yprime, rprime; /* Shifting of coodrinates */ float dx, dy; /* Shifted radial coordinate normalized by deltar */ float normRprime; /* ..To prompt for things */ char temp_string[255]; char answer[255]; /* --------------------- BEGIN EXECUTABLE CODE -------------------- */ /* __Initialize whatever paremeters here */ /* ..Periodicity matrix */ Vxx = 1.0; Vyx = 0.0; Vxy = -0.5; Vyy = (float)sqrt(3.0)*0.5; /* ..Prompt for all data */ Nr = head->nx; Nz = head->ny; psfxz = (float*)head->data; ovrsmp = head->xstart; Nxy = head->ystart; symmetric = head->zstart; /* leave at 1.0 nrm_deltar = head->xlength; nrm_deltaxy = head->ylength; */ HalfNxy = Nxy/2; Lxy = Nxy*bin; /* Number of pixels before binning */ Halfway = (HalfNxy+1)*bin; NetNr = Nr; /* Number of pixels in convolution of psfcond and pupil */ nrm_deltar = nrm_deltaxy/((float)ovrsmp); /* oversampling rate */ /* make it beat nyquist */ osamp = (int)(deltaxy/deltaxy_nyq) + 1; if(osamp < 5) osamp = 5; /* make odd */ if(!(osamp & 1)) osamp++; /* New deltaxy for oversampling */ deltaxy1 = deltaxy/((float)osamp); ratio = (float) deltaxy1 / deltar; printf("DXY: %.4f DXY_NYQ: %.4f SAMP = %d NEW_DXY: %.4f\n", deltaxy,deltaxy_nyq,osamp,deltaxy1); Mxy = Lxy * osamp; /* number of pixels after oversampling */ Halfway = (HalfNxy+1)*bin*osamp; printf("ratio = %f Mxy = %d \n", ratio,Mxy); /* check for single aperture */ if(distance <= 0.0) distance = 1E20; distance = distance*nrm_deltaxy; /* Scale by pre-binning pixel size */ Zup = Nz; /* Limit for z index */ if (symmetric) Zup = (Nz/2) + 1; /* ..Maximum values for row sampling index (NOTE: Assumes Vyy != 0 <--) */ Minn2 = ((float)-Mxy)/(Vyy*distance); Maxn2 = ((float)(2*Mxy))/(Vyy*distance); /* __Calculate a conservative extimate of the maximum value if the 'ir' index that is expected to be used. The estimate is calculated with the following assumptions: Vxx > 0, Vyy > 0 (strictly greater) Vxy < 0 (Striclty less than) Vyx = 0 (exactly) ..Most negative value of n1 expected */ sMinn1 = ((float)-Mxy)/distance - (float)Minn2*Vxy/Vxx; /* ..Most positive value of n1 expected */ lMaxn1 = (((float)(2*Mxy))/distance - (float)Maxn2*Vxy)/Vxx; /* ..most negative value of term subtracted from xprime */ Sxmin = Vxx*(float)sMinn1 + Vyx*(float)Maxn2; /* ..Most positive value of term subtracted from xprime */ Sxmax = Vxx*(float)lMaxn1 + Vyx*(float)Minn2; /* ..Largest value of x */ xmax = (float)(Halfway-1) * nrm_deltaxy; /* ..Largest absolute value of xprime */ Maxxprime = amax1(Sxmax*distance, xmax - Sxmin*distance); /* ..Most negative value of the term subtracted from yprime (with Vyx=0,Vyy>0) */ Symin = Vyy*(float)Minn2; /* ..Most positive value of the term subtracted from yprime (with Vyx=0,Vyy>0) */ /* In the original fortran code, Maxn1 is used in the next line without being initialized first. I suspect it's a bug, and should really be Maxn2. */ Symax = Vyy*(float)Maxn2; /* ..largest value of y */ ymax = (float)(Halfway-1) * nrm_deltaxy; /* ..Largest absolute value of yprime */ Maxyprime=amax1(Vyy*(float)Maxn2*distance,ymax-Vyy*(float)Minn2*distance); /* ..Largest value of rprime we can conservativelly expect */ Maxrprime = (float) sqrt (Maxxprime*Maxxprime + Maxyprime*Maxyprime); /* ..Largetst index ir expected */ Maxir = (int) ((Maxrprime + 1.0)/nrm_deltar); /* ..Factor to be used in linear interpolation used as apodization */ appod = 1.0; if (Maxir > Nr) appod = 1.0/(float)(Maxir - Nr) ; /* ..Calculate size of pupil function array at the higher resolution*/ Pxy = Nxy*ovrsmp*bin; /* ..Calculate the least power of 2 that will fit */ Pxy = intlog2(Pxy-1); Pxy = (int) pow(2.0 , Pxy+1); /* ..Make sure that the log file exists */ if((logfp=fopen(lognm,"a"))==(FILE *)NULL) { fprintf(stderr,"WARNING: (rotdiskcirc) can't open logfile `%s' for write.\n",lognm); logfp = stderr; } fprintf(logfp," Number of slices or planes: %d\n", Nz); fprintf(logfp," Number of xy-samples after binning: %d\n", Nxy); fprintf(logfp," Number of radial samples available: %d\n", Nr); fprintf(logfp," Oversampling (before binning) by: %d\n", ovrsmp); fprintf(logfp," binning factor: %d\n", bin); fprintf(logfp," distance between apertures: %E\n", distance); fprintf(logfp," aperture size(prebin,oversmpled): %f\n", size); fprintf(logfp," pupil support(prebin,oversmpled): %d\n", Pxy); fprintf(logfp," sample matrix:\n"); fprintf(logfp," Vxx: %f\n",Vxx); fprintf(logfp," Vxy: %f\n",Vxy); fprintf(logfp," Vyx: %f\n",Vyx); fprintf(logfp," Vyy: %f\n",Vyy); fprintf(logfp,"even symmetry in z is %s assumed.\n", symmetric ? "":"NOT"); sprintf(temp_string,"%s.info",psfnm); if((infofp=fopen(temp_string,"a"))==(FILE *)NULL) fprintf(stderr,"WARNING: can't open info file %s for write.\n",temp_string); else { fprintf(infofp," binning factor: %d\n", bin); fprintf(infofp," Distance between apertures: %E\n", distance); fprintf(infofp,"aperture diameter(prebin,oversamp): %f\n", size); fprintf(infofp," sample matrix:\n"); fprintf(infofp," Vxx: %f\n",Vxx); fprintf(infofp," Vxy: %f\n",Vxy); fprintf(infofp," Vyx: %f\n",Vyx); fprintf(infofp," Vyy: %f\n",Vyy); fclose(infofp); } fprintf(stderr,"check file %s for progress report.\n",lognm); if (size >= 1.0) { keeplog (logfp, "sampling pupil function "); /* first allocate memory for array pupil */ if((pupil=(float *)calloc(sizeof(float),((Pxy+2)*Pxy)))== (float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } ZeroOut(pupil,(Pxy+2)*Pxy); /* Get pupil function */ getpupil(pupil, size, Pxy, Pxy); add2columns(pupil,Pxy,Pxy); /* Fourier transform pupil function */ real_fft3d(pupil,Pxy,Pxy,1,Pxy+2,Pxy,FORWARD); Cpupil=(fcomplex *)pupil; /* If tandem scanning, the equivalent pinhole function is the convolution of the pinhole with itself */ if (tandem) mult3dcm (Cpupil, Cpupil, (Pxy+2)*Pxy/2); /* Normalize Cpupil array to avoid handling very small numbers */ peak = 0.0; norm3dcm (Cpupil, &peak, Pxy*(Pxy+2)/2); } /* allocate memory for psfobj. Notice size of array */ if((psfobj=(float *)calloc(sizeof(float),Mxy*Mxy))==(float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } ZeroOut(psfobj,Mxy*Mxy); printf("Mxy = %d and Pxy=%d\n",Mxy,Pxy); fflush(stdout); /* allocate memory for psfcond. Notice size of array */ if (Mxy > Pxy){ /* when ovrsmp < 5 then this is true */ printf("Mxy >Pxy\n"); if((psfcond=(float *)calloc(sizeof(float),(Mxy+2)*Mxy))==(float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } } else if((psfcond=(float *)calloc(sizeof(float),(Pxy+2)*Pxy))==(float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } ZeroOut(psfcond,(Pxy+2)*Pxy); otfcond=(fcomplex *)psfcond; /* allocate memory for psfbin. Notice size of array */ if((psfbin=(float *)calloc(sizeof(float),Nxy*Nxy))==(float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } ZeroOut(psfbin,Nxy*Nxy); /* allocate memory for psfbi1. Notice size of array */ if((psfbin1=(float *)calloc(sizeof(float),Lxy*Lxy))==(float *)NULL) { fprintf(stderr,"out of memory in %s\n",prognm); exit(1); } ZeroOut(psfbin1,Lxy*Lxy); for(iz=1;iz<=Zup;iz++){ if ( (iz-1)%1 == 0) { sprintf(temp_string,"iz = %d",iz); keeplog(logfp, temp_string); } /* ..Read on row of the rz or xz cross-section */ /* first have to go to the right place in file */ radpsf = (psfxz + (iz-1)*Nr); /* Calculate the objective PSF (iterpolate rz section into xyz sect)*/ r2xy(psfobj, radpsf, Mxy, Mxy, Nr, ratio); if (size >= 1.0) { /* Convolve condenser PSF and aperture */ /* interpolate w/o subsampling */ r2xy(psfcond, radpsf, Pxy, Pxy, Nr, 1.0); add2columns(psfcond,Pxy,Pxy); /* Fourier transform, multiply, inverse Fourier transform */ real_fft3d(psfcond,Pxy,Pxy,1,Pxy+2,Pxy,FORWARD); mult3dcm(otfcond, Cpupil, (Pxy+2)*Pxy/2); real_fft3d(psfcond,Pxy,Pxy,1,Pxy+2,Pxy,REVERSE); /* Copy first line of convolution to radpsf to use for illumination */ NetNr = Pxy/2+1; cp3dr(radpsf, psfcond, NetNr); } /* The following loops are based on the PSF's even symmetry in x and y */ /* Calculate the illumination distribution */ for(iy=1;iy<=Halfway;iy++){ y = (float)(iy-1)*nrm_deltaxy; for(ix=1;ix<=Halfway;ix++){ x = (float)(ix-1)*nrm_deltaxy; /*Initialize to use as accumulator */ illum = 0.0; /* ..Index n2 is for columns */ for(n2=Minn2;n2<=Maxn2;n2++){ dx = Vxy*n2; dy = Vyy*n2; /* ..These two lines assume Vxx is not zero */ Minn1=(int)(((float)-Mxy/distance-dx)/Vxx); Maxn1=(int)((2.0*(float)Mxy/distance-dx)/Vxx); /* ..Index n1 involves rows and columns */ for(n1=Minn1;n1<=Maxn1;n1++){ xprime = x - distance*(Vxx*(float)n1+dx); yprime = y - distance*(Vyx*(float)n1+dy); /* Rectangular sampling to check what's going on here */ /* xprime = x - distance*float(n2) yprime = y - distance*float(n1) */ rprime = (float)sqrt((double)(xprime*xprime) + (double)(yprime*yprime)); /* ..Normalize to the subsampled grid */ normRprime = rprime*ratio; ir = (int)normRprime + 1; /* ..Interpolate and accumulate */ if (ir < NetNr) { /* ..Interpolate between adjacent samples */ alpha = ir - normRprime; /*tmp = radpsf(ir)*alpha + radpsf(ir+1)*(1.0-alpha)*/ tmp = *(radpsf+ir-1) * ((float)ir-normRprime) + *(radpsf+ir+1-1) * (normRprime+1.0-(float)ir); } else { /*This should disapear when we become confident that the conservative guess for Maxir is OK */ if (ir > Maxir) { Maxir = ir; fprintf(stderr,"Found an ir > Maxir. \n"); fprintf(stderr,"Maxir set to %d\n",ir); fprintf(stderr,"check the code that guesses Maxir again.\n"); appod = 1.0/(Maxir-Nr); } /*..Apodize from last available sample to zero to avoid sharp edges in the PSF */ /* ..Apodize by linear interpolation to zero at ir = Maxir */ weight = (Maxir-(float)ir)*appod; tmp = *(radpsf+NetNr-1) * weight; } illum = illum + tmp; } /* end loop for n1 */ } /* end loop for n2 */ /* IMPORTANT The dimension of the array psfcond is now Mxy by Mxy, instead of (Pxy+2) by Pxy. */ /* psfcond(ix,iy) = illum */ *(psfcond+(ix-1)+(iy-1)*Mxy) = illum; } /* end loop for ix */ } /* end loop for iy */ /* ..Even symmetric replication into the other three quadrants */ /* IMPORTANT The dimension of the array psfcond is now Mxy by Mxy, instead of (Pxy+2) by Pxy. */ evenrepr(psfcond, Mxy, Mxy); /* ..Multiply illumination and objective psf (into psfobj) */ mult3drm(psfobj, psfcond, Mxy*Mxy); /* sum neighboring pixels to downsample the BIG PSF */ /* note dimension goes from Mxy to Lxy = Nxy*bin */ Sum4N4NToNN(psfobj,psfbin1,Lxy,osamp); /* routine is in rotsum.c */ /* Bin here */ /* If binning by 1 copy psf to psfbin */ if (bin == 1) /* Lxy = Nxy */ cp3dr (psfbin, psfbin1, Nxy*Nxy); else { /* Add pixels together */ /* Clean psfbin array to use as accumulator */ init3dr(psfbin, 0.0, Nxy*Nxy); /* binning */ for(ix=1;ix<=Lxy;ix++){ jx = ((ix-1)/bin) + 1; for(iy=1;iy<=Lxy;iy++){ jy = ((iy-1)/bin) + 1; *(psfbin+(jx-1)+(jy-1)*Nxy) += *(psfbin1+(ix-1)+(iy-1)*Lxy); } } } WritePlane(iz-1,outpsf,psfbin,Nxy,Nxy,Nxy,Nxy); if(symmetric && (iz > 1)) WritePlane(Nz-iz+1,outpsf,psfbin,Nxy,Nxy,Nxy,Nxy); } /* end of "for(iz=1;iz<=Zup;iz++)" */ keeplog (logfp, "DONE"); if(logfp != stderr) fclose(logfp); printf("don't free up the memory\n"); /*free(radpsf); free(psfbin); free(psfobj); free(psfcond); free(pupil);*/ }
//7.3.3 Slice header syntax void structure(slice_header)(h264_stream_t* h, bs_t* b) { slice_header_t* sh = h->sh; if( is_reading ) { memset(sh, 0, sizeof(slice_header_t)); } nal_t* nal = h->nal; value( sh->first_mb_in_slice, ue ); value( sh->slice_type, ue ); value( sh->pic_parameter_set_id, ue ); // TODO check existence, otherwise fail pps_t* pps = h->pps; sps_t* sps = h->sps; memcpy(h->pps_table[sh->pic_parameter_set_id], h->pps, sizeof(pps_t)); memcpy(h->sps_table[pps->seq_parameter_set_id], h->sps, sizeof(sps_t)); value( sh->frame_num, u(sps->log2_max_frame_num_minus4 + 4 ) ); // was u(v) if( !sps->frame_mbs_only_flag ) { value( sh->field_pic_flag, u1 ); if( sh->field_pic_flag ) { value( sh->bottom_field_flag, u1 ); } } if( nal->nal_unit_type == 5 ) { value( sh->idr_pic_id, ue ); } if( sps->pic_order_cnt_type == 0 ) { value( sh->pic_order_cnt_lsb, u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ) ); // was u(v) if( pps->pic_order_present_flag && !sh->field_pic_flag ) { value( sh->delta_pic_order_cnt_bottom, se ); } } if( sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag ) { value( sh->delta_pic_order_cnt[ 0 ], se ); if( pps->pic_order_present_flag && !sh->field_pic_flag ) { value( sh->delta_pic_order_cnt[ 1 ], se ); } } if( pps->redundant_pic_cnt_present_flag ) { value( sh->redundant_pic_cnt, ue ); } if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) ) { value( sh->direct_spatial_mv_pred_flag, u1 ); } if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_P ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) ) { value( sh->num_ref_idx_active_override_flag, u1 ); if( sh->num_ref_idx_active_override_flag ) { value( sh->num_ref_idx_l0_active_minus1, ue ); // FIXME does this modify the pps? if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) ) { value( sh->num_ref_idx_l1_active_minus1, ue ); } } } structure(ref_pic_list_reordering)(h, b); if( ( pps->weighted_pred_flag && ( is_slice_type( sh->slice_type, SH_SLICE_TYPE_P ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) ) ) || ( pps->weighted_bipred_idc == 1 && is_slice_type( sh->slice_type, SH_SLICE_TYPE_B ) ) ) { structure(pred_weight_table)(h, b); } if( nal->nal_ref_idc != 0 ) { structure(dec_ref_pic_marking)(h, b); } if( pps->entropy_coding_mode_flag && ! is_slice_type( sh->slice_type, SH_SLICE_TYPE_I ) && ! is_slice_type( sh->slice_type, SH_SLICE_TYPE_SI ) ) { value( sh->cabac_init_idc, ue ); } value( sh->slice_qp_delta, se ); if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) || is_slice_type( sh->slice_type, SH_SLICE_TYPE_SI ) ) { if( is_slice_type( sh->slice_type, SH_SLICE_TYPE_SP ) ) { value( sh->sp_for_switch_flag, u1 ); } value( sh->slice_qs_delta, se ); } if( pps->deblocking_filter_control_present_flag ) { value( sh->disable_deblocking_filter_idc, ue ); if( sh->disable_deblocking_filter_idc != 1 ) { value( sh->slice_alpha_c0_offset_div2, se ); value( sh->slice_beta_offset_div2, se ); } } if( pps->num_slice_groups_minus1 > 0 && pps->slice_group_map_type >= 3 && pps->slice_group_map_type <= 5) { int v = intlog2( pps->pic_size_in_map_units_minus1 + pps->slice_group_change_rate_minus1 + 1 ); value( sh->slice_group_change_cycle, u(v) ); // FIXME add 2? } }
//7.3.2.2 Picture parameter set RBSP syntax void structure(pic_parameter_set_rbsp)(h264_stream_t* h, bs_t* b) { pps_t* pps = h->pps; if( is_reading ) { memset(pps, 0, sizeof(pps_t)); } value( pps->pic_parameter_set_id, ue); value( pps->seq_parameter_set_id, ue ); value( pps->entropy_coding_mode_flag, u1 ); value( pps->pic_order_present_flag, u1 ); value( pps->num_slice_groups_minus1, ue ); if( pps->num_slice_groups_minus1 > 0 ) { value( pps->slice_group_map_type, ue ); if( pps->slice_group_map_type == 0 ) { for( int i_group = 0; i_group <= pps->num_slice_groups_minus1; i_group++ ) { value( pps->run_length_minus1[ i_group ], ue ); } } else if( pps->slice_group_map_type == 2 ) { for( int i_group = 0; i_group < pps->num_slice_groups_minus1; i_group++ ) { value( pps->top_left[ i_group ], ue ); value( pps->bottom_right[ i_group ], ue ); } } else if( pps->slice_group_map_type == 3 || pps->slice_group_map_type == 4 || pps->slice_group_map_type == 5 ) { value( pps->slice_group_change_direction_flag, u1 ); value( pps->slice_group_change_rate_minus1, ue ); } else if( pps->slice_group_map_type == 6 ) { value( pps->pic_size_in_map_units_minus1, ue ); for( int i = 0; i <= pps->pic_size_in_map_units_minus1; i++ ) { int v = intlog2( pps->num_slice_groups_minus1 + 1 ); value( pps->slice_group_id[ i ], u(v) ); } } } value( pps->num_ref_idx_l0_active_minus1, ue ); value( pps->num_ref_idx_l1_active_minus1, ue ); value( pps->weighted_pred_flag, u1 ); value( pps->weighted_bipred_idc, u(2) ); value( pps->pic_init_qp_minus26, se ); value( pps->pic_init_qs_minus26, se ); value( pps->chroma_qp_index_offset, se ); value( pps->deblocking_filter_control_present_flag, u1 ); value( pps->constrained_intra_pred_flag, u1 ); value( pps->redundant_pic_cnt_present_flag, u1 ); int have_more_data = 0; if( is_reading ) { have_more_data = more_rbsp_data(h, b); } if( is_writing ) { have_more_data = pps->transform_8x8_mode_flag | pps->pic_scaling_matrix_present_flag | pps->second_chroma_qp_index_offset != 0; } if( have_more_data ) { value( pps->transform_8x8_mode_flag, u1 ); value( pps->pic_scaling_matrix_present_flag, u1 ); if( pps->pic_scaling_matrix_present_flag ) { for( int i = 0; i < 6 + 2* pps->transform_8x8_mode_flag; i++ ) { value( pps->pic_scaling_list_present_flag[ i ], u1 ); if( pps->pic_scaling_list_present_flag[ i ] ) { if( i < 6 ) { structure(scaling_list)( b, pps->ScalingList4x4[ i ], 16, &( pps->UseDefaultScalingMatrix4x4Flag[ i ] ) ); } else { structure(scaling_list)( b, pps->ScalingList8x8[ i - 6 ], 64, &( pps->UseDefaultScalingMatrix8x8Flag[ i - 6 ] ) ); } } } } value( pps->second_chroma_qp_index_offset, se ); } structure(rbsp_trailing_bits)(h, b); if( is_reading ) { memcpy(h->pps, h->pps_table[pps->pic_parameter_set_id], sizeof(pps_t)); } }
int calcDimForFFT( int original_dim ) { int dim = original_dim * 2; if ( isPowerOfTwo( dim ) ) return dim; return 2 << intlog2( dim ); }
/// ブロック幅からレベル数に変換 static unsigned int blockToLevel(unsigned int w) { return _LEVEL-(intlog2(w-1)+1); }