void CoverBond::apply(Particle *p) const { Bond bd(p); core::XYZ ea(bd.get_bonded(0)), eb(bd.get_bonded(1)); core::XYZR r(p); r.set_coordinates(.5*(ea.get_coordinates()+ eb.get_coordinates())); r.set_radius((r.get_coordinates()- ea.get_coordinates()).get_magnitude()); }
void CoverBond::apply_index(Model *m, ParticleIndex pi) const { Bond bd(m, pi); core::XYZ ea(bd.get_bonded(0)), eb(bd.get_bonded(1)); core::XYZR r(m, pi); r.set_coordinates(.5 * (ea.get_coordinates() + eb.get_coordinates())); r.set_radius((r.get_coordinates() - ea.get_coordinates()).get_magnitude()); }
std::vector<Point> EllipticalArc::pointAndDerivatives(Coord t, unsigned int n) const { unsigned int nn = n+1; // nn represents the size of the result vector. std::vector<Point> result; result.reserve(nn); double angle = map_unit_interval_on_circular_arc(t, initialAngle(), finalAngle(), _sweep); std::auto_ptr<EllipticalArc> ea( static_cast<EllipticalArc*>(duplicate()) ); ea->_center = Point(0,0); unsigned int m = std::min(nn, 4u); for ( unsigned int i = 0; i < m; ++i ) { result.push_back( ea->pointAtAngle(angle) ); angle += (_sweep ? M_PI/2 : -M_PI/2); if ( !(angle < 2*M_PI) ) angle -= 2*M_PI; } m = nn / 4; for ( unsigned int i = 1; i < m; ++i ) { for ( unsigned int j = 0; j < 4; ++j ) result.push_back( result[j] ); } m = nn - 4 * m; for ( unsigned int i = 0; i < m; ++i ) { result.push_back( result[i] ); } if ( !result.empty() ) // nn != 0 result[0] = pointAtAngle(angle); return result; }
int main(int argc, char *argv[]) { long m, p, r, L, R; ArgMapping argmap; MDL::Timer timer; argmap.arg("m", m, "m"); argmap.arg("L", L, "L"); argmap.arg("p", p, "p"); argmap.arg("r", r, "r"); argmap.arg("R", R, "R"); argmap.parse(argc, argv); timer.start(); FHEcontext context(m, p, r); buildModChain(context, L); FHESecKey sk(context); sk.GenSecKey(64); addSome1DMatrices(sk); FHEPubKey pk = sk; auto G = context.alMod.getFactorsOverZZ()[0]; EncryptedArray ea(context, G); timer.end(); printf("slots %ld\n", ea.size()); printf("Key Gen %f\n", timer.second()); auto data = load_csv("adult.data", R); benchmark(ea, pk, sk, data); }
/** Merging a plane (xxi) and an axix (oof) constraint may result in: <ul> <li> prismatic : cylindar's axis lying in the plane <li> revolute : plane perpendicular to the cylindar's axis <li> fixed : all other cases </ul> */ Joint meld_xxi_oof(const Joint& plane, const Joint& axis, const bool flip = false) { //log4cpp::CategoryStream& log = plane.log_cf.infoStream(); // isis_LOG(lg, isis_FILE, isis_INFO) << (flip ? "meld_oof_xxi" : "meld_xxi_oof"); e3ga::vector x1 = axis.location; e3ga::vector b3 = e3ga::unit(axis.orientation); e3ga::vector b2 = e3ga::unit(plane.orientation); if ( e3ga::zero((b2 ^ b3), DEFAULT_TOLERANCE) ) { // axis perpendicular to plane = revolute /** Find the intersection between the axis and the plane. http://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection */ e3ga::vector x2 = plane.location; double d = ((x2 - x1) % b2) / (b3 % b2); e3ga::vector r3 = (d * b3) + x1; Joint result(REVOLUTE, r3, b3, 0.0, plane, axis); return result; } /** http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Rotation_matrix_.E2.86.94_Euler_axis.2Fangle */ e3ga::vector b1 = e3ga::unit( b2 << *b3 ); double theta = acos(0.5* (b1.get_e1() + b2.get_e2() + b3.get_e3() - 1.0)); if ( e3ga::zero( (b2 % b3) , DEFAULT_TOLERANCE) ) { // axis parallel to plane = prismatic // is the axis or the plane the reference location? // if (flip) // b? is the projection of the axis onto the plane. // return Joint(PRISMATIC, x1, b?, -theta, plane, axis); return Joint(PRISMATIC, x1, b3, theta, plane, axis); } double half_csc_theta = 0.5 / sin( theta ); // compute the euler axis e3ga::vector ea(e3ga::vector::coord_e1_e2_e3, half_csc_theta * (b3.get_e2() - b2.get_e3()), half_csc_theta * (b1.get_e3() - b3.get_e1()), half_csc_theta * (b2.get_e1() - b1.get_e2()) ); // compute the location for the fixed joint // 1 - intersection between the axis and the plane. // 2 - the location of the axis. // 3 - the location of the plane. // // 1 is probably the best choice but 2 is easier. e3ga::vector x2 = axis.location; return Joint(FIXED, x2, ea, theta, plane, axis); }
void TransformToConfig(const RigidTransform& T,Vector& q) { q.resize(6); T.t.get(q[0],q[1],q[2]); EulerAngleRotation ea(q[3],q[4],q[5]); ea.setMatrixZYX(T.R); ea.get(q[3],q[4],q[5]); }
void MatrixZuzka(Int_t idDataSet = 0,Double_t nu01=0.3, TList *lg=0) { // fixne Double_t r0x=2; Double_t r0y=4; Double_t nu10=0.1; Double_t mxy=0.01; Double_t myx=0.01; // old // if (idDataSet == 1) { // r0x=1.0; // r0y=1.5; // nu10=0.1; // mxy=2.0; // myx=1.0; // } TMatrixD a(4,4); a(0,0)=r0x-nu01; a(0,1)=nu10; a(0,2)=0; a(0,3)=0; a(1,0)=nu01; a(1,1)=-nu10-mxy; a(1,2)=0; a(1,3)=myx; a(2,0)=0; a(2,1)=0; a(2,2)=r0y-nu01; a(2,3)=nu10; a(3,0)=0; a(3,1)=mxy; a(3,2)=nu01; a(3,3)=-nu10-myx; TMatrixDEigen ea(a); const TVectorD ear = ea.GetEigenValuesRe(); const TVectorD eai = ea.GetEigenValuesIm(); TGraph *g1 = (TGraph*) lg->At(0); g1->SetTitle(Form("4x4 matrix blok, constant parameters: r_{0x}=%.1f, r_{0y}=%.1f, #nu_{10}=%.2f, #mu_{xy}=%.2f, #mu_{yx}=%.2f",r0x,r0y,nu10,mxy,myx)); TGraph *g2 = (TGraph*) lg->At(1); TGraph *g3 = (TGraph*) lg->At(2); TGraph *g4 = (TGraph*) lg->At(3); g1->SetPoint(g1->GetN(),nu01,ear(0)); g2->SetPoint(g2->GetN(),nu01,ear(1)); g3->SetPoint(g3->GetN(),nu01,ear(2)); g4->SetPoint(g4->GetN(),nu01,ear(3)); Printf("%f %f %f %f", ear(0), ear(1), ear(2), ear(3)); }
bool CheckPEHeaders::parseResponse(QPointer<RpcData> rd) { if (!ICommand::parseResponse(rd)) return false; try { rpc::CheckPEHeadersResult result; if (!hlp::protobuf::parseBigMessage(result, rd->response->rpc_result())) { msg("%s: rpc::CheckPEHeadersResult::ParseFromString() failed\n", __FUNCTION__); return false; } peValid = result.pe_valid(); if (peValid) { const auto& exps = result.exps(); for (auto it = exps.begin(), end = exps.end(); it != end; ++it) { ExportItem item = { it->ea(), it->ord(), it->name() }; exports.append(item); } const auto& sects = result.sections(); for (auto it = sects.begin(), end = sects.end(); it != end; ++it) { Section s = { it->name(), it->va(), it->v_size(), it->raw(), it->raw_size(), it->characteristics() }; sections.append(s); } } return true; } catch (std::runtime_error e) { msg("%s: Runtime error: %s\n", __FUNCTION__, e.what()); } catch (...) { msg("%s: Unable to parse CheckPEHeadersRequest response\n", __FUNCTION__); } return false; }
void filesrv::init (cb_t c) { assert (!cb); cb = c; for (size_t i = 0; i < fstab.size (); i++) fstab[i].parent = &fstab[path2fsidx (fstab[i].path_mntpt, i)]; ref<erraccum> ea (New refcounted<erraccum> (wrap (this, &filesrv::gotroots))); for (size_t i = 0; i < fstab.size (); i++) findfs (NULL, fstab[i].path_root, wrap (this, &filesrv::gotroot, ea, i), FINDFS_NOSFS); }
// Block until an event of the given type is received. // Note: The calling function is responsible for deleting the returned // SVEvent afterwards! SVEvent* ScrollView::AwaitEvent(SVEventType type) { // Initialize the waiting semaphore. SVSemaphore* sem = new SVSemaphore(); std::pair<ScrollView*, SVEventType> ea(this, type); mutex_waiting->Lock(); waiting_for_events[ea] = std::pair<SVSemaphore*, SVEvent*> (sem, NULL); mutex_waiting->Unlock(); // Wait on it, but first flush. stream_->Flush(); sem->Wait(); // Process the event we got woken up for (its in waiting_for_events pair). mutex_waiting->Lock(); SVEvent* ret = waiting_for_events[ea].second; waiting_for_events.erase(ea); mutex_waiting->Unlock(); return ret; }
// Block until any event on any window is received. // No event is returned here! SVEvent* ScrollView::AwaitEventAnyWindow() { // Initialize the waiting semaphore. SVSemaphore* sem = new SVSemaphore(); std::pair<ScrollView*, SVEventType> ea((ScrollView*)nullptr, SVET_ANY); waiting_for_events_mu->Lock(); waiting_for_events[ea] = std::pair<SVSemaphore*, SVEvent*> (sem, (SVEvent*)nullptr); waiting_for_events_mu->Unlock(); // Wait on it. stream_->Flush(); sem->Wait(); // Process the event we got woken up for (its in waiting_for_events pair). waiting_for_events_mu->Lock(); SVEvent* ret = waiting_for_events[ea].second; waiting_for_events.erase(ea); waiting_for_events_mu->Unlock(); return ret; }
void test_ExpandingAllocator_allocate_searches_for_new_holes() { immix::ExpandingAllocator ea(gc->block_allocator()); immix::Block& block = ea.current_block(); block.mark_line(1); ea.resync_position(); memory::Address small = ea.allocate(24); TS_ASSERT(!small.is_null()); memory::Address addr = ea.allocate(156); TS_ASSERT(!addr.is_null()); immix::Block& block2 = ea.current_block(); TS_ASSERT(&block == &block2); }
void filesrv::gotroots (bool ok) { if (!ok) { (*cb) (false); return; } ref<erraccum> ea (New refcounted<erraccum> (wrap (this, &filesrv::gotmps))); for (size_t i = 0; i < fstab.size (); i++) { lookupfh3 (fstab[i].c, fstab[i].fh_root, "", wrap (this, &filesrv::gotrootattr, ea, i)); lookupfh3 (fstab[i].parent->c, fstab[i].parent->fh_root, substr (fstab[i].path_mntpt, fstab[i].parent->path_mntpt.len ()), wrap (this, &filesrv::gotmp, ea, i)); } }
void filesrv::gotmps (bool ok) { if (!ok) { (*cb) (false); return; } ref<erraccum> ea (New refcounted<erraccum> (wrap (this, &filesrv::gotdds))); for (size_t i = 0; i < fstab.size (); i++) for (int mp = 0; mp < 2; mp++) { ref<readdir3res> res (New refcounted<readdir3res>); rpc_clear (*res); res->resok->reply.entries.alloc (); rpc_clear (*res->resok->reply.entries); gotrdres (ea, res, i, mp, RPC_SUCCESS); } }
int main() { std::vector<Node *> nodes; std::vector<Edge *> edges; N na("a"); N nb("b"); N nc("c"); N nd("d"); E ea(&na, &nb, 6); E eb(&nb, &nc, 5); E ec(&nc, &nd, 4); E ed(&nd, &na, 3); E ee(&na, &nc, 2); E ef(&nb, &nd, 1); nodes.push_back(&na); nodes.push_back(&nb); nodes.push_back(&nc); nodes.push_back(&nd); edges.push_back(&ea); edges.push_back(&eb); edges.push_back(&ec); edges.push_back(&ed); edges.push_back(&ee); edges.push_back(&ef); Kruskal k(nodes, edges); float w = k.solve(); std::cout << "Tree weight: " << std::endl << w << std::endl; std::cout << "Edges: " << std::endl; std::vector<Edge *> mst = k.getEdges(); for (std::vector<Edge *>::iterator it = mst.begin(); it != mst.end(); it++) { N *a = (N *) (*it)->a; N *b = (N *) (*it)->b; std::cout << a->name << " - " << b->name << std::endl; } return 0; }
// Apply a permutation network to a ciphertext // FIXME: Do we need to also give an EncryptedArray object as paramter? void PermNetwork::applyToCtxt(Ctxt& c) const { const PAlgebra& al = c.getContext().zMStar; EncryptedArray ea(c.getContext()); // Use G(X)=X for this ea object, this works since we only have 0/1 entries // Apply the layers, one at a time for (long i=0; i<layers.length(); i++) { const PermNetLayer& lyr = layers[i]; if (lyr.isID) continue; // this layer is the identity permutation // This layer is shifted via powers of g^e mod m long g2e = PowerMod(al.ZmStarGen(lyr.genIdx), lyr.e, al.getM()); Vec<long> unused = lyr.shifts; // copy to a new vector vector<long> mask(lyr.shifts.length()); // buffer to hold masks Ctxt sum(c.getPubKey(), c.getPtxtSpace()); // an empty ciphertext long shamt = 0; bool frst = true; while (true) { pair<long,bool> ret=makeMask(mask, unused, shamt); // compute mask if (ret.second) { // non-empty mask Ctxt tmp = c; ZZX maskPoly; ea.encode(maskPoly, mask); // encode mask as polynomial tmp.multByConstant(maskPoly); // multiply by mask if (shamt!=0) // rotate if the shift amount is nonzero tmp.smartAutomorph(PowerMod(g2e, shamt, al.getM())); if (frst) { sum = tmp; frst = false; } else sum += tmp; } if (ret.first >= 0) shamt = unused[ret.first]; // next shift amount to use else break; // unused is all-zero, done with this layer } c = sum; // update the cipehrtext c before the next layer } }
void test_ExpandingAllocator_allocate_pulls_new_blocks() { immix::ExpandingAllocator ea(gc->block_allocator()); immix::Block& block = ea.current_block(); for(int i = 0; i < immix::cLineTableSize; i++) { block.mark_line(i); } block.free_line(1); ea.resync_position(); memory::Address small = ea.allocate(24); TS_ASSERT(!small.is_null()); memory::Address addr = ea.allocate(156); TS_ASSERT(!addr.is_null()); immix::Block& block2 = ea.current_block(); TS_ASSERT(&block != &block2); }
void ItemHandle::doLaunch(Helper::ItemLaunchHelperI* helper) { preLaunchCheck(); bool needElevation = false; size_t x=0; while (g_ElevatedGames[x].isOk()) { if (getItemInfo()->getId() == g_ElevatedGames[x] || getItemInfo()->getParentId() == g_ElevatedGames[x]) { needElevation = true; break; } x++; } UserCore::Item::Misc::ExeInfoI* ei = getItemInfo()->getActiveExe(); gcString args; gcString ea(ei->getExeArgs()); gcString ua(ei->getUserArgs()); if (ea.size() > 0) args += " " + ea; if (ua.size() > 0) args += " " + ua; m_pUserCore->getItemManager()->setRecent(getItemInfo()->getId()); bool res = UTIL::WIN::launchExe(ei->getExe(), args.c_str(), needElevation, m_pUserCore->getMainWindowHandle()); if (!res) throw gcException(ERR_LAUNCH, GetLastError(), gcString("Failed to create {0} process. [{1}: {2}].\n", getItemInfo()->getName(), GetLastError(), ei->getExe())); }
main() { const unsigned POP_SIZE = 8, CHROM_SIZE = 16; unsigned i; // a chromosome randomizer eoBinRandom<Chrom> random; // the populations: eoPop<Chrom> pop; // Evaluation eoEvalFuncPtr<Chrom> eval( binary_value ); for (i = 0; i < POP_SIZE; ++i) { Chrom chrom(CHROM_SIZE); random(chrom); eval(chrom); pop.push_back(chrom); } std::cout << "population:" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; // selection eoLottery<Chrom> lottery; // breeder eoBinBitFlip<Chrom> bitflip; eoBinCrossover<Chrom> xover; eoProportionalOpSel<Chrom> propSel; eoBreeder<Chrom> breeder( propSel ); propSel.addOp(bitflip, 0.25); propSel.addOp(xover, 0.75); // replacement eoInclusion<Chrom> inclusion; // Terminators eoFitTerm<Chrom> term( pow(2.0, CHROM_SIZE), 1 ); // GA generation eoEasyEA<Chrom> ea(lottery, breeder, inclusion, eval, term); // evolution try { ea(pop); } catch (std::exception& e) { std::cout << "exception: " << e.what() << std::endl;; exit(EXIT_FAILURE); } std::cout << "pop" << std::endl; for (i = 0; i < pop.size(); ++i) std::cout << "\t" << pop[i] << " " << pop[i].fitness() << std::endl; return 0; }
int test_generic_executor_ref() { // std::cout << BOOST_CONTEXTOF << std::endl; { try { { boost::basic_thread_pool ea(4); submit_some( ea); { boost::future<int> t1 = boost::async(ea, &f1); boost::future<int> t2 = boost::async(ea, &f1); // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; // std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl; } submit_some(ea); { boost::basic_thread_pool ea3(1); boost::future<int> t1 = boost::async(ea3, &f1); boost::future<int> t2 = boost::async(ea3, &f1); //boost::future<int> t2 = boost::async(ea3, f2, 1); // todo this doesn't compiles yet on C++11 //boost::future<int> t2 = boost::async(ea3, boost::bind(f2, 1)); // todo this doesn't compiles yet on C++98 // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; // std::cout << BOOST_CONTEXTOF << " t2= " << t2.get() << std::endl; } submit_some(ea); } // std::cout << BOOST_CONTEXTOF << std::endl; { boost::loop_executor ea2; submit_some( ea2); ea2.run_queued_closures(); } #if ! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) // std::cout << BOOST_CONTEXTOF << std::endl; { boost::basic_thread_pool ea1(4); boost::serial_executor ea2(ea1); submit_some(ea2); } #endif // std::cout << BOOST_CONTEXTOF << std::endl; { boost::inline_executor ea1; submit_some(ea1); } // std::cout << BOOST_CONTEXTOF << std::endl; { //boost::thread_executor ea1; //submit_some(ea1); } // std::cout << BOOST_CONTEXTOF << std::endl; { boost::basic_thread_pool ea(4, at_th_entry); boost::future<int> t1 = boost::async(ea, &f1); // std::cout << BOOST_CONTEXTOF << " t1= " << t1.get() << std::endl; } } catch (std::exception& ex) { std::cout << "ERROR= " << ex.what() << "" << std::endl; return 1; } catch (...) { std::cout << " ERROR= exception thrown" << std::endl; return 2; } } // std::cout << BOOST_CONTEXTOF << std::endl; return 0; }
static void do_emu(struct info * info) { unsigned short code; temp_real tmp; char * address; if (I387.cwd & I387.swd & 0x3f) I387.swd |= 0x8000; else I387.swd &= 0x7fff; ORIG_EIP = EIP; /* 0x0007 means user code space */ if (CS != 0x000F) { printk("math_emulate: %04x:%08x\n\r",CS,EIP); panic("Math emulation needed in kernel"); } code = get_fs_word((unsigned short *) EIP); bswapw(code); code &= 0x7ff; I387.fip = EIP; *(unsigned short *) &I387.fcs = CS; *(1+(unsigned short *) &I387.fcs) = code; EIP += 2; switch (code) { case 0x1d0: /* fnop */ return; case 0x1d1: case 0x1d2: case 0x1d3: case 0x1d4: case 0x1d5: case 0x1d6: case 0x1d7: math_abort(info,1<<(SIGILL-1)); case 0x1e0: ST(0).exponent ^= 0x8000; return; case 0x1e1: ST(0).exponent &= 0x7fff; return; case 0x1e2: case 0x1e3: math_abort(info,1<<(SIGILL-1)); case 0x1e4: ftst(PST(0)); return; case 0x1e5: printk("fxam not implemented\n\r"); math_abort(info,1<<(SIGILL-1)); case 0x1e6: case 0x1e7: math_abort(info,1<<(SIGILL-1)); case 0x1e8: fpush(); ST(0) = CONST1; return; case 0x1e9: fpush(); ST(0) = CONSTL2T; return; case 0x1ea: fpush(); ST(0) = CONSTL2E; return; case 0x1eb: fpush(); ST(0) = CONSTPI; return; case 0x1ec: fpush(); ST(0) = CONSTLG2; return; case 0x1ed: fpush(); ST(0) = CONSTLN2; return; case 0x1ee: fpush(); ST(0) = CONSTZ; return; case 0x1ef: math_abort(info,1<<(SIGILL-1)); case 0x1f0: case 0x1f1: case 0x1f2: case 0x1f3: case 0x1f4: case 0x1f5: case 0x1f6: case 0x1f7: case 0x1f8: case 0x1f9: case 0x1fa: case 0x1fb: case 0x1fc: case 0x1fd: case 0x1fe: case 0x1ff: printk("%04x fxxx not implemented\n\r",code + 0xc800); math_abort(info,1<<(SIGILL-1)); case 0x2e9: fucom(PST(1),PST(0)); fpop(); fpop(); return; case 0x3d0: case 0x3d1: return; case 0x3e2: I387.swd &= 0x7f00; return; case 0x3e3: I387.cwd = 0x037f; I387.swd = 0x0000; I387.twd = 0x0000; return; case 0x3e4: return; case 0x6d9: fcom(PST(1),PST(0)); fpop(); fpop(); return; case 0x7e0: *(short *) &EAX = I387.swd; return; } switch (code >> 3) { case 0x18: fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x19: fmul(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x1a: fcom(PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x1b: fcom(PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); fpop(); return; case 0x1c: real_to_real(&ST(code & 7),&tmp); tmp.exponent ^= 0x8000; fadd(PST(0),&tmp,&tmp); real_to_real(&tmp,&ST(0)); return; case 0x1d: ST(0).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x1e: fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x1f: fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; case 0x38: fpush(); ST(0) = ST((code & 7)+1); return; case 0x39: fxchg(&ST(0),&ST(code & 7)); return; case 0x3b: ST(code & 7) = ST(0); fpop(); return; case 0x98: fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0x99: fmul(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0x9a: fcom(PST(code & 7),PST(0)); return; case 0x9b: fcom(PST(code & 7),PST(0)); fpop(); return; case 0x9c: ST(code & 7).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0x9d: real_to_real(&ST(0),&tmp); tmp.exponent ^= 0x8000; fadd(PST(code & 7),&tmp,&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0x9e: fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0x9f: fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); return; case 0xb8: printk("ffree not implemented\n\r"); math_abort(info,1<<(SIGILL-1)); case 0xb9: fxchg(&ST(0),&ST(code & 7)); return; case 0xba: ST(code & 7) = ST(0); return; case 0xbb: ST(code & 7) = ST(0); fpop(); return; case 0xbc: fucom(PST(code & 7),PST(0)); return; case 0xbd: fucom(PST(code & 7),PST(0)); fpop(); return; case 0xd8: fadd(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xd9: fmul(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xda: fcom(PST(code & 7),PST(0)); fpop(); return; case 0xdc: ST(code & 7).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xdd: real_to_real(&ST(0),&tmp); tmp.exponent ^= 0x8000; fadd(PST(code & 7),&tmp,&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xde: fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xdf: fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return; case 0xf8: printk("ffree not implemented\n\r"); math_abort(info,1<<(SIGILL-1)); fpop(); return; case 0xf9: fxchg(&ST(0),&ST(code & 7)); return; case 0xfa: case 0xfb: ST(code & 7) = ST(0); fpop(); return; } switch ((code>>3) & 0xe7) { case 0x22: put_short_real(PST(0),info,code); return; case 0x23: put_short_real(PST(0),info,code); fpop(); return; case 0x24: address = ea(info,code); for (code = 0 ; code < 7 ; code++) { ((long *) & I387)[code] = get_fs_long((unsigned long *) address); address += 4; } return; case 0x25: address = ea(info,code); *(unsigned short *) &I387.cwd = get_fs_word((unsigned short *) address); return; case 0x26: address = ea(info,code); verify_area(address,28); for (code = 0 ; code < 7 ; code++) { put_fs_long( ((long *) & I387)[code], (unsigned long *) address); address += 4; } return; case 0x27: address = ea(info,code); verify_area(address,2); put_fs_word(I387.cwd,(short *) address); return; case 0x62: put_long_int(PST(0),info,code); return; case 0x63: put_long_int(PST(0),info,code); fpop(); return; case 0x65: fpush(); get_temp_real(&tmp,info,code); real_to_real(&tmp,&ST(0)); return; case 0x67: put_temp_real(PST(0),info,code); fpop(); return; case 0xa2: put_long_real(PST(0),info,code); return; case 0xa3: put_long_real(PST(0),info,code); fpop(); return; case 0xa4: address = ea(info,code); for (code = 0 ; code < 27 ; code++) { ((long *) & I387)[code] = get_fs_long((unsigned long *) address); address += 4; } return; case 0xa6: address = ea(info,code); verify_area(address,108); for (code = 0 ; code < 27 ; code++) { put_fs_long( ((long *) & I387)[code], (unsigned long *) address); address += 4; } I387.cwd = 0x037f; I387.swd = 0x0000; I387.twd = 0x0000; return; case 0xa7: address = ea(info,code); verify_area(address,2); put_fs_word(I387.swd,(short *) address); return; case 0xe2: put_short_int(PST(0),info,code); return; case 0xe3: put_short_int(PST(0),info,code); fpop(); return; case 0xe4: fpush(); get_BCD(&tmp,info,code); real_to_real(&tmp,&ST(0)); return; case 0xe5: fpush(); get_longlong_int(&tmp,info,code); real_to_real(&tmp,&ST(0)); return; case 0xe6: put_BCD(PST(0),info,code); fpop(); return; case 0xe7: put_longlong_int(PST(0),info,code); fpop(); return; } switch (code >> 9) { case 0: get_short_real(&tmp,info,code); break; case 1: get_long_int(&tmp,info,code); break; case 2: get_long_real(&tmp,info,code); break; case 4: get_short_int(&tmp,info,code); } switch ((code>>3) & 0x27) { case 0: fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; case 1: fmul(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; case 2: fcom(&tmp,PST(0)); return; case 3: fcom(&tmp,PST(0)); fpop(); return; case 4: tmp.exponent ^= 0x8000; fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; case 5: ST(0).exponent ^= 0x8000; fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; case 6: fdiv(PST(0),&tmp,&tmp); real_to_real(&tmp,&ST(0)); return; case 7: fdiv(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return; } if ((code & 0x138) == 0x100) { fpush(); real_to_real(&tmp,&ST(0)); return; } printk("Unknown math-insns: %04x:%08x %04x\n\r",CS,EIP,code); math_abort(info,1<<(SIGFPE-1)); }
int main() { Eris::Logged.connect(sigc::ptr_fun(writeLog)); Eris::setLogLevel(Eris::LOG_DEBUG); { TestConnection con("name", "localhost", 6767, true); TestAccount acc(&con); std::string fake_char_id("1"); TestAvatar ea(&acc, fake_char_id); acc.setup_insertActiveCharacters(&ea); TestEntity* char_ent = new TestEntity(fake_char_id, 0, ea.getView()); ea.setup_setEntity(char_ent); typedef std::map<std::string, Atlas::Message::Element> ElementStore; ElementStore modTypes; ElementStore shapes; ElementStore levelMods; Atlas::Message::MapType levelMod1; levelMod1["type"] = Atlas::Message::Element("levelmod"); levelMods["1"] = levelMod1; Atlas::Message::MapType levelMod2(levelMod1); levelMod2["height"] = 20; levelMods["2"] = levelMod2; Atlas::Message::MapType levelMod3(levelMod1); levelMod2["heightoffset"] = 30; levelMods["3"] = levelMod3; ElementStore adjustMods; Atlas::Message::MapType adjustMod1; adjustMod1["type"] = Atlas::Message::Element("adjustmod"); adjustMods["1"] = levelMod1; Atlas::Message::MapType adjustMod2(adjustMod1); adjustMod2["height"] = 20; adjustMods["2"] = adjustMod2; Atlas::Message::MapType adjustMod3(adjustMod1); adjustMod2["heightoffset"] = 30; adjustMods["3"] = adjustMod3; Atlas::Message::MapType craterMod1; craterMod1["type"] = Atlas::Message::Element("cratermod"); ElementStore slopeMods; Atlas::Message::MapType slopeMod1; slopeMod1["type"] = Atlas::Message::Element("slopemod"); Atlas::Message::ListType slopes; slopes.push_back(10); slopes.push_back(20); slopeMod1["slopes"] = slopes; slopeMods["1"] = slopeMod1; Atlas::Message::MapType shapeCircle; shapeCircle["radius"] = 15; shapeCircle["position"] = Atlas::Message::ListType(2, 0.); shapeCircle["type"] = "ball"; shapes["ball"] = shapeCircle; Atlas::Message::MapType shapePolygon; Atlas::Message::ListType points; points.push_back(WFMath::Point<2>(0,0).toAtlas()); points.push_back(WFMath::Point<2>(10,0).toAtlas()); points.push_back(WFMath::Point<2>(10,10).toAtlas()); points.push_back(WFMath::Point<2>(0,10).toAtlas()); shapePolygon["points"] = points; shapePolygon["type"] = "polygon"; shapes["polygon"] = shapePolygon; shapes["empty"] = Atlas::Message::MapType(); //no terrain mod info { Atlas::Message::MapType emptyElement; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", emptyElement); Eris::TerrainModObserver mod(mod_ent); assert(!mod.init()); } //no shape { Atlas::Message::MapType modElement = levelMod1; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", modElement); Eris::TerrainModObserver mod(mod_ent); assert(!mod.init()); } //test level mod for (ElementStore::iterator I = levelMods.begin(); I != levelMods.end(); ++I) { for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) { std::cout << "Testing level mod " << I->first << " with " << J->first << std::endl; Atlas::Message::Element modElement = I->second; modElement.asMap()["shape"] = J->second; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", modElement); Eris::TerrainModObserver mod(mod_ent); if (J->first == "empty") { assert(!mod.init()); } else { assert(mod.init()); } } } //test adjust mod for (ElementStore::iterator I = adjustMods.begin(); I != adjustMods.end(); ++I) { for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) { std::cout << "Testing adjust mod " << I->first << " with " << J->first << std::endl; Atlas::Message::Element modElement = I->second; modElement.asMap()["shape"] = J->second; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", modElement); Eris::TerrainModObserver mod(mod_ent); if (J->first == "empty") { assert(!mod.init()); } else { assert(mod.init()); } } } //test slope mod for (ElementStore::iterator I = slopeMods.begin(); I != slopeMods.end(); ++I) { for (ElementStore::iterator J = shapes.begin(); J != shapes.end(); ++J) { std::cout << "Testing slope mod " << I->first << " with " << J->first << std::endl; Atlas::Message::Element modElement = I->second; modElement.asMap()["shape"] = J->second; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", modElement); Eris::TerrainModObserver mod(mod_ent); if (J->first == "empty") { assert(!mod.init()); } else { assert(mod.init()); } } } Atlas::Message::MapType shapeBall; shapeBall["radius"] = 15; shapeBall["position"] = Atlas::Message::ListType(3, 0.); shapeBall["type"] = "ball"; //test crater mod { Atlas::Message::MapType modElement = craterMod1; modElement["shape"] = shapeCircle; TestEntity* mod_ent = new TestEntity("2", 0, ea.getView()); mod_ent->setup_setAttr("terrainmod", modElement); Eris::TerrainModObserver mod(mod_ent); assert(mod.init()); } } return 0; }
void QDial::repaintScreen( const QRect *cr ) { QPainter p; p.begin( this ); bool resetClipping = FALSE; // calculate clip-region for erasing background if ( cr ) { p.setClipRect( *cr ); } else if ( !d->onlyOutside && d->eraseAreaValid ) { QRegion reg = d->eraseArea; double a; reg = reg.subtract( calcArrow( a ) ); p.setClipRegion( reg ); resetClipping = TRUE; } QRect br( calcDial() ); p.setPen( NoPen ); // if ( style() == MotifStyle ) // p.setBrush( colorGroup().brush( QColorGroup::Mid ) ); // else { QBrush b; if ( colorGroup().brush( QColorGroup::Light ).pixmap() ) b = QBrush( colorGroup().brush( QColorGroup::Light ) ); else b = QBrush( colorGroup().light(), Dense4Pattern ); p.setBrush( b ); p.setBackgroundMode( OpaqueMode ); // } QRect te = br; te.setWidth(te.width()+2); te.setHeight(te.height()+2); // erase background of dial if ( !d->onlyOutside ) { p.drawEllipse( te ); } // erase remaining space around the dial QRegion remaining( 0, 0, width(), height() ); remaining = remaining.subtract( QRegion( te, QRegion::Ellipse ) ); if ( p.hasClipping() ) remaining = remaining.intersect( p.clipRegion() ); erase(remaining); if ( resetClipping ) { if ( cr ) p.setClipRect( *cr ); else p.setClipRect( QRect( 0, 0, width(), height() ) ); } // draw notches if ( d->showNotches ) { calcLines(); p.setPen( colorGroup().foreground() ); p.drawLineSegments( d->lines ); } // calculate and paint arrow p.setPen( QPen( colorGroup().dark() ) ); p.drawArc( te, 60 * 16, 180 * 16 ); p.setPen( QPen( colorGroup().light() ) ); p.drawArc( te, 240 * 16, 180 * 16 ); double a; QPointArray arrow( calcArrow( a ) ); QRect ea( arrow.boundingRect() ); d->eraseArea = ea; d->eraseAreaValid = TRUE; p.setPen( NoPen ); p.setBrush( colorGroup().brush( QColorGroup::Button ) ); if ( !d->onlyOutside ) p.drawPolygon( arrow ); a = angle( QPoint( width() / 2, height() / 2 ), arrow[ 0 ] ); p.setBrush( Qt::NoBrush ); // that's still a hack... if ( a <= 0 || a > 200 ) { p.setPen( colorGroup().light() ); p.drawLine( arrow[ 2 ], arrow[ 0 ] ); p.drawLine( arrow[ 1 ], arrow[ 2 ] ); p.setPen( colorGroup().dark() ); p.drawLine( arrow[ 0 ], arrow[ 1 ] ); } else if ( a > 0 && a < 45 ) { p.setPen( colorGroup().light() ); p.drawLine( arrow[ 2 ], arrow[ 0 ] ); p.setPen( colorGroup().dark() ); p.drawLine( arrow[ 1 ], arrow[ 2 ] ); p.drawLine( arrow[ 0 ], arrow[ 1 ] ); } else if ( a >= 45 && a < 135 ) { p.setPen( colorGroup().dark() ); p.drawLine( arrow[ 2 ], arrow[ 0 ] ); p.drawLine( arrow[ 1 ], arrow[ 2 ] ); p.setPen( colorGroup().light() ); p.drawLine( arrow[ 0 ], arrow[ 1 ] ); } else if ( a >= 135 && a < 200 ) { p.setPen( colorGroup().dark() ); p.drawLine( arrow[ 2 ], arrow[ 0 ] ); p.setPen( colorGroup().light() ); p.drawLine( arrow[ 0 ], arrow[ 1 ] ); p.drawLine( arrow[ 1 ], arrow[ 2 ] ); } // draw focus rect around the dial if ( hasFocus() ) { p.setClipping( FALSE ); br.setWidth( br.width() + 2 ); br.setHeight( br.height() + 2 ); if ( d->showNotches ) { int r = QMIN( width(), height() ) / 2; br.moveBy( -r / 6, - r / 6 ); br.setWidth( br.width() + r / 3 ); br.setHeight( br.height() + r / 3 ); } // strange, but else we get redraw errors on Windows p.end(); p.begin( this ); p.save(); p.setPen( QPen( colorGroup().background() ) ); p.setBrush( NoBrush ); p.drawRect( br ); p.restore(); style().drawPrimitive( QStyle::PE_FocusRect, &p, br, colorGroup()); } p.end(); }
static int math_emulate(struct trapframe * info) { unsigned short code; temp_real tmp; char * address; u_long oldeip; /* ever used fp? */ if ((((struct pcb *)curproc->p_addr)->pcb_flags & FP_SOFTFP) == 0) { ((struct pcb *)curproc->p_addr)->pcb_flags |= FP_SOFTFP; I387.cwd = 0x037f; I387.swd = 0x0000; I387.twd = 0x0000; } if (I387.cwd & I387.swd & 0x3f) I387.swd |= 0x8000; else I387.swd &= 0x7fff; oldeip = info->tf_eip; /* 0x001f means user code space */ if ((u_short)info->tf_cs != 0x001F) { printf("math_emulate: %04x:%08lx\n", (u_short)info->tf_cs, oldeip); panic("?Math emulation needed in kernel?"); } code = get_fs_word((unsigned short *) oldeip); bswapw(code); code &= 0x7ff; I387.fip = oldeip; *(unsigned short *) &I387.fcs = (u_short) info->tf_cs; *(1+(unsigned short *) &I387.fcs) = code; info->tf_eip += 2; switch (code) { case 0x1d0: /* fnop */ return(0); case 0x1d1: case 0x1d2: case 0x1d3: /* fst to 32-bit mem */ case 0x1d4: case 0x1d5: case 0x1d6: case 0x1d7: math_abort(info,SIGILL); case 0x1e0: /* fchs */ ST(0).exponent ^= 0x8000; return(0); case 0x1e1: /* fabs */ ST(0).exponent &= 0x7fff; return(0); case 0x1e2: case 0x1e3: math_abort(info,SIGILL); case 0x1e4: /* ftst */ ftst(PST(0)); return(0); case 0x1e5: /* fxam */ printf("fxam not implemented\n"); math_abort(info,SIGILL); case 0x1e6: case 0x1e7: /* fldenv */ math_abort(info,SIGILL); case 0x1e8: /* fld1 */ fpush(); ST(0) = CONST1; return(0); case 0x1e9: /* fld2t */ fpush(); ST(0) = CONSTL2T; return(0); case 0x1ea: /* fld2e */ fpush(); ST(0) = CONSTL2E; return(0); case 0x1eb: /* fldpi */ fpush(); ST(0) = CONSTPI; return(0); case 0x1ec: /* fldlg2 */ fpush(); ST(0) = CONSTLG2; return(0); case 0x1ed: /* fldln2 */ fpush(); ST(0) = CONSTLN2; return(0); case 0x1ee: /* fldz */ fpush(); ST(0) = CONSTZ; return(0); case 0x1ef: math_abort(info,SIGILL); case 0x1f0: /* f2xm1 */ case 0x1f1: /* fyl2x */ case 0x1f2: /* fptan */ case 0x1f3: /* fpatan */ case 0x1f4: /* fxtract */ case 0x1f5: /* fprem1 */ case 0x1f6: /* fdecstp */ case 0x1f7: /* fincstp */ case 0x1f8: /* fprem */ case 0x1f9: /* fyl2xp1 */ case 0x1fa: /* fsqrt */ case 0x1fb: /* fsincos */ case 0x1fe: /* fsin */ case 0x1ff: /* fcos */ uprintf( "math_emulate: instruction %04x not implemented\n", code + 0xd800); math_abort(info,SIGILL); case 0x1fc: /* frndint */ frndint(PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x1fd: /* fscale */ /* incomplete and totally inadequate -wfj */ Fscale(PST(0), PST(1), &tmp); real_to_real(&tmp,&ST(0)); return(0); /* 19 Sep 92*/ case 0x2e9: /* ????? */ /* if this should be a fucomp ST(0),ST(1) , it must be a 0x3e9 ATS */ fucom(PST(1),PST(0)); fpop(); fpop(); return(0); case 0x3d0: case 0x3d1: /* fist ?? */ return(0); case 0x3e2: /* fclex */ I387.swd &= 0x7f00; return(0); case 0x3e3: /* fninit */ I387.cwd = 0x037f; I387.swd = 0x0000; I387.twd = 0x0000; return(0); case 0x3e4: return(0); case 0x6d9: /* fcompp */ fcom(PST(1),PST(0)); fpop(); fpop(); return(0); case 0x7e0: /* fstsw ax */ *(short *) &info->tf_eax = I387.swd; return(0); } switch (code >> 3) { case 0x18: /* fadd */ fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x19: /* fmul */ fmul(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x1a: /* fcom */ fcom(PST(code & 7),PST(0)); return(0); case 0x1b: /* fcomp */ fcom(PST(code & 7),PST(0)); fpop(); return(0); case 0x1c: /* fsubr */ real_to_real(&ST(code & 7),&tmp); tmp.exponent ^= 0x8000; fadd(PST(0),&tmp,&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x1d: /* fsub */ ST(0).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x1e: /* fdivr */ fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x1f: /* fdiv */ fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 0x38: /* fld */ fpush(); ST(0) = ST((code & 7)+1); /* why plus 1 ????? ATS */ return(0); case 0x39: /* fxch */ fxchg(&ST(0),&ST(code & 7)); return(0); case 0x3b: /* ??? ??? wrong ???? ATS */ ST(code & 7) = ST(0); fpop(); return(0); case 0x98: /* fadd */ fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0x99: /* fmul */ fmul(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0x9a: /* ???? , my manual don't list a direction bit for fcom , ??? ATS */ fcom(PST(code & 7),PST(0)); return(0); case 0x9b: /* same as above , ATS */ fcom(PST(code & 7),PST(0)); fpop(); return(0); case 0x9c: /* fsubr */ ST(code & 7).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0x9d: /* fsub */ real_to_real(&ST(0),&tmp); tmp.exponent ^= 0x8000; fadd(PST(code & 7),&tmp,&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0x9e: /* fdivr */ fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0x9f: /* fdiv */ fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); return(0); case 0xb8: /* ffree */ printf("ffree not implemented\n"); math_abort(info,SIGILL); case 0xb9: /* fstp ???? where is the pop ? ATS */ fxchg(&ST(0),&ST(code & 7)); return(0); case 0xba: /* fst */ ST(code & 7) = ST(0); return(0); case 0xbb: /* ????? encoding of fstp to mem ? ATS */ ST(code & 7) = ST(0); fpop(); return(0); case 0xbc: /* fucom */ fucom(PST(code & 7),PST(0)); return(0); case 0xbd: /* fucomp */ fucom(PST(code & 7),PST(0)); fpop(); return(0); case 0xd8: /* faddp */ fadd(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xd9: /* fmulp */ fmul(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xda: /* ??? encoding of ficom with 16 bit mem ? ATS */ fcom(PST(code & 7),PST(0)); fpop(); return(0); case 0xdc: /* fsubrp */ ST(code & 7).exponent ^= 0x8000; fadd(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xdd: /* fsubp */ real_to_real(&ST(0),&tmp); tmp.exponent ^= 0x8000; fadd(PST(code & 7),&tmp,&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xde: /* fdivrp */ fdiv(PST(0),PST(code & 7),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xdf: /* fdivp */ fdiv(PST(code & 7),PST(0),&tmp); real_to_real(&tmp,&ST(code & 7)); fpop(); return(0); case 0xf8: /* fild 16-bit mem ???? ATS */ printf("ffree not implemented\n"); math_abort(info,SIGILL); fpop(); return(0); case 0xf9: /* ????? ATS */ fxchg(&ST(0),&ST(code & 7)); return(0); case 0xfa: /* fist 16-bit mem ? ATS */ case 0xfb: /* fistp 16-bit mem ? ATS */ ST(code & 7) = ST(0); fpop(); return(0); } switch ((code>>3) & 0xe7) { case 0x22: put_short_real(PST(0),info,code); return(0); case 0x23: put_short_real(PST(0),info,code); fpop(); return(0); case 0x24: address = ea(info,code); for (code = 0 ; code < 7 ; code++) { ((long *) & I387)[code] = get_fs_long((unsigned long *) address); address += 4; } return(0); case 0x25: address = ea(info,code); *(unsigned short *) &I387.cwd = get_fs_word((unsigned short *) address); return(0); case 0x26: address = ea(info,code); /*verify_area(address,28);*/ for (code = 0 ; code < 7 ; code++) { put_fs_long( ((long *) & I387)[code], (unsigned long *) address); address += 4; } return(0); case 0x27: address = ea(info,code); /*verify_area(address,2);*/ put_fs_word(I387.cwd,(short *) address); return(0); case 0x62: put_long_int(PST(0),info,code); return(0); case 0x63: put_long_int(PST(0),info,code); fpop(); return(0); case 0x65: fpush(); get_temp_real(&tmp,info,code); real_to_real(&tmp,&ST(0)); return(0); case 0x67: put_temp_real(PST(0),info,code); fpop(); return(0); case 0xa2: put_long_real(PST(0),info,code); return(0); case 0xa3: put_long_real(PST(0),info,code); fpop(); return(0); case 0xa4: address = ea(info,code); for (code = 0 ; code < 27 ; code++) { ((long *) & I387)[code] = get_fs_long((unsigned long *) address); address += 4; } return(0); case 0xa6: address = ea(info,code); /*verify_area(address,108);*/ for (code = 0 ; code < 27 ; code++) { put_fs_long( ((long *) & I387)[code], (unsigned long *) address); address += 4; } I387.cwd = 0x037f; I387.swd = 0x0000; I387.twd = 0x0000; return(0); case 0xa7: address = ea(info,code); /*verify_area(address,2);*/ put_fs_word(I387.swd,(short *) address); return(0); case 0xe2: put_short_int(PST(0),info,code); return(0); case 0xe3: put_short_int(PST(0),info,code); fpop(); return(0); case 0xe4: fpush(); get_BCD(&tmp,info,code); real_to_real(&tmp,&ST(0)); return(0); case 0xe5: fpush(); get_longlong_int(&tmp,info,code); real_to_real(&tmp,&ST(0)); return(0); case 0xe6: put_BCD(PST(0),info,code); fpop(); return(0); case 0xe7: put_longlong_int(PST(0),info,code); fpop(); return(0); } switch (code >> 9) { case 0: get_short_real(&tmp,info,code); break; case 1: get_long_int(&tmp,info,code); break; case 2: get_long_real(&tmp,info,code); break; case 4: get_short_int(&tmp,info,code); } switch ((code>>3) & 0x27) { case 0: fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 1: fmul(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 2: fcom(&tmp,PST(0)); return(0); case 3: fcom(&tmp,PST(0)); fpop(); return(0); case 4: tmp.exponent ^= 0x8000; fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 5: ST(0).exponent ^= 0x8000; fadd(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); case 6: fdiv(PST(0),&tmp,&tmp); real_to_real(&tmp,&ST(0)); return(0); case 7: fdiv(&tmp,PST(0),&tmp); real_to_real(&tmp,&ST(0)); return(0); } if ((code & 0x138) == 0x100) { fpush(); real_to_real(&tmp,&ST(0)); return(0); } printf("Unknown math-insns: %04x:%08x %04x\n",(u_short)info->tf_cs, info->tf_eip,code); math_abort(info,SIGFPE); }
int main(){ cout << "This experiment check for different values of parameters, how much time it takes to encrypt and decrypt a \"full\" matrix (nslots*nslots), and multply 2 encrypted matrices\nAlso check for what size of matrices, the multiplication in the server on the encrypted data is faster than for the user than do all the work on his machine. Using this formula: N > n(P)*(2*Enc(P)+Dec(P)) when:\nP is the parameters\nn(P) is the nslots value for these values\nEnc(P) and Dec(P) is the time it takes to encrypt and decrypt the matrics in size nslots*nslots\nNOTE: this formula don't take into account the time it takes to send and recieve the data to and from the server, and the time it took to the server to do the actual multiplication\n" << endl; /* long m=0, r=1; // Native plaintext space int p = 65539; // Computations will be 'modulo p' long L=16; // Levels long c=3; // Columns in key switching matrix long w=64; // Hamming weight of secret key long d=0; long s = 0; //minimum number of slots [ default=0 ] long security = 128;*/ long m, r, p, L, c, w, s, d, security, enc1, enc2, dec, encMul, ptMul, recommended; char tempChar; bool toEncMult = false, toPrint = false, debugMul = false, toSave = false; //Scan parameters cout << "Enter HElib's keys paramter. Enter zero for the recommended values" << endl; while(true){ cout << "Enter the field of the computations (a prime number): "; cin >> p; if(isPrime(p)) break; cout << "Error! p must be a prime number! " << endl; } while(true){ recommended = 1; cout << "Enter r (recommended " << recommended <<"): "; cin >> r; if(r == 0) r = recommended; if(r > 0) break; cout << "Error! r must be a positive number!" << endl; } while(true){ recommended = 16; cout << "Enter L (recommended " << recommended <<"): "; cin >> L; if(L == 0) L = recommended; if(L > 1) break; cout << "Error! L must be a positive number!" << endl; } while(true){ recommended = 3; cout << "Enter c (recommended " << recommended <<"): "; cin >> c; if(c == 0) c = recommended; if(c > 1) break; cout << "Error! c must be a positive number!" << endl; } while(true){ recommended = 64; cout << "Enter w (recommended " << recommended <<"): "; cin >> w; if(w == 0) w = recommended; if(w > 1) break; cout << "Error! w must be a positive number!" << endl; } while(true){ recommended = 0; cout << "Enter d (recommended " << recommended <<"): "; cin >> d; if(d >= 0) break; cout << "Error! d must be a positive or zero!" << endl; } while(true){ recommended = 0; cout << "Enter s (recommended " << recommended <<"): "; cin >> s; if(s >= 0) break; cout << "Error! s must be a positive or zero!" << endl; } while(true){ recommended = 128; cout << "Enter security (recommended " << recommended << "): "; cin >> security; if(security == 0) security = recommended; if(security >= 1) break; cout << "Error! security must be a positive number " << endl; } ZZX G; m = FindM(security,L,c,p, d, s, 0); FHEcontext context(m, p, r); // initialize context buildModChain(context, L, c); // modify the context, adding primes to the modulus chain FHESecKey secretKey(context); // construct a secret key structure const FHEPubKey& publicKey = secretKey; // an "upcast": FHESecKey is a subclass of FHEPubKey //if(0 == d) G = context.alMod.getFactorsOverZZ()[0]; secretKey.GenSecKey(w); // actually generate a secret key with Hamming weight w addSome1DMatrices(secretKey); EncryptedArray ea(context, G); // constuct an Encrypted array object ea that is // associated with the given context and the polynomial G long nslots = ea.size(), field = power(p,r); cout << "nslots: " << nslots << endl ; cout << "Computations will be modulo " << field << endl; cout << "m: " << m << endl; unsigned int sz1, sz2, sz3; while(true){ cout << "Enter number of rows in the first matrix: "; cin >> sz1; if(sz1 > 1 && sz1 <= nslots) break; cout << "Error! the value must be between 1 to " << nslots << "!" << endl; } while(true){ cout << "Enter number of rows in the first matrix: "; cin >> sz2; if(sz1 > 2 && sz2 <= nslots) break; cout << "Error! the value must be between 1 to " << nslots << "!" << endl; } while(true){ cout << "Enter number of rows in the first matrix: "; cin >> sz3; if(sz1 > 3 && sz3 <= nslots) break; cout << "Error! the value must be between 1 to " << nslots << "!" << endl; } PTMatrix PTmat1(MatSize(sz1, sz2),field), PTmat2(MatSize(sz2, sz3), field); //random matrix in size origSize1 while(true){ cout << "To multiply the encrypted matrices? Not affecting the formula, just for statistic" << endl; cout << "Y for yes, N for no: "; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ toEncMult = true; break; } if(tempChar == 'N' || tempChar == 'n'){ toEncMult = false; break; } cout << "Error! invalid input!" << endl; } while(toEncMult){ cout << "Debug the multiplication steps?\nY for yesm N for no :"; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ debugMul = true; break; } if(tempChar == 'N' || tempChar == 'n'){ debugMul = false; break; } cout << "Error! invalid input!" << endl; } while(true){ cout << "Print the matrices?" << endl; cout << "Y for yes, N for no: "; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ toPrint = true; break; } if(tempChar == 'N' || tempChar == 'n'){ toPrint = false; break; } cout << "Error! invalid input!" << endl; } while(true){ cout << "Save the matrices?" << endl; cout << "Y for yes, N for no: "; cin >> tempChar; if(tempChar == 'Y' || tempChar == 'y'){ toSave = true; break; } if(tempChar == 'N' || tempChar == 'n'){ toSave = false; break; } cout << "Error! invalid input!" << endl; } if(toPrint){ PTmat1.print(); PTmat2.print(); } if(toSave){ ofstream out_mat1("mat1.txt"), out_mat2("mat2.txt"); PTmat1.save(out_mat1); PTmat2.save(out_mat2); out_mat1.close(); out_mat2.close(); } //encryptions cout << "Encrypting the first matrices..." << endl; resetTimers(); EncryptedMatrix encMat1 = PTmat1.encrypt(ea, publicKey); enc1 = stopTimers("to encrypt the first matrix"); cout << "Encrypting the second matrices..." << endl; resetTimers(); EncryptedMatrix encMat2 = PTmat2.encrypt(ea, publicKey); enc2 = stopTimers("to encrypt the second matrix"); //multiplication if(toEncMult){ cout << "Multiplying the matrices..." << endl; resetTimers(); if(debugMul) encMat1 = encMat1.debugMul(encMat2); //same as encMat1 *= encMat2 but print progress update else encMat1 *= encMat2; encMul = stopTimers("to multiply the matrices"); } cout << "Decrypting the result..." << endl; resetTimers(); PTMatrix res = encMat1.decrypt(ea, secretKey); dec = stopTimers("to decrypt the result"); if(toPrint) res.print("Solution: "); resetTimers(); PTMatrix PTres = PTmat1.mulWithMod(PTmat2,field); //like (PTmat1*PTmat2)%p but do modulu after each multiplication to avoid overflow ptMul = stopTimers("to multiply the regular matrices"); if(toSave){ ofstream out_res("mat_res.txt"), out_ptRes("mat_pt_res.txt"); res.save(out_res); PTres.save(out_ptRes); out_res.close(); out_ptRes.close(); } //PTres.print("pt result: "); cout << "\n\n----------------------------------------Summary------------------------------ " << endl; cout << "p: " << p << ", r: " << r << ", L: " << L << ", c: " << c << ", w: " << w << ", d: " << d << ", s: " << s << ", security: " << security << endl; cout << "nslots: " << nslots << "\nm: " << m << endl; cout << "It took " << enc1 << " clock ticks to encrypt the first matrix" << endl; cout << "It took " << enc2 << " clock ticks to encrypt the second matrix" << endl; cout << "It took " << dec << " clock ticks to decrypt the result" << endl; cout << "It took " << ptMul << " clock ticks to multiply the regular matrices" << endl; if(toEncMult){ cout << "It took " << encMul << " clock ticks to multiply the encrypted matrices" << endl; cout << "is correct? " << (res==PTres) << endl; } long N = nslots*(enc1+enc2+dec)/ptMul; cout << "N should be greater than " << N << endl; return 0; }
void TestIt(long c, long k, long w, long L, long m, long n) { FHEcontext context(m, 2, 1); // p = 2, r = 1 long d = context.zMStar.getOrdP(); buildModChain(context, L, c); context.zMStar.printout(); cerr << endl; #ifdef DEBUG cerr << context << endl; #endif FHESecKey secretKey(context); const FHEPubKey& publicKey = secretKey; secretKey.GenSecKey(w); // A Hamming-weight-w secret key ZZX G; G = makeIrredPoly(2, d); // G = context.alMod.getFactorsOverZZ()[0]; cerr << "generating key-switching matrices... "; addFrbMatrices(secretKey); addSome1DMatrices(secretKey); cerr << "done\n"; cerr << "computing masks and tables for rotation..."; EncryptedArray ea(context, G); cerr << "done\n"; long nslots = ea.size(); if (n <= 0 || n > d) n = d; vector<ZZX> v; v.resize(nslots); for (long i = 0; i < nslots; i++) { GF2X f; random(f, n); conv(v[i], f); } printBits(v, n); Ctxt ctxt(publicKey); ea.encrypt(ctxt, publicKey, v); // ctxt encrypts a vector where each slots is a random // polynomial of degree < n Ctxt* res[n]; for (long j = 0; j < n; j++) res[j] = new Ctxt(publicKey); // allocate resetAllTimers(); incrementalZeroTest(res, ea, ctxt, n); for (long j = 0; j < n; j++) { vector<ZZX> v1; ea.decrypt(*res[j], secretKey, v1); printBits(v1, n); } for (long j = 0; j < n; j++) delete res[j]; // cleanup }
int main(int argc, char* argv[]) { if (argc < 2) { printf("Usage: %s <program name>\n", argv[0]); exit(0); } filename = argv[1]; FILE* fp = fopen(filename, "rb"); if (fp == 0) error("opening"); ram = (Byte*)malloc(0x10000); memset(ram, 0, 0x10000); if (ram == 0) { fprintf(stderr, "Out of memory\n"); exit(1); } if (fseek(fp, 0, SEEK_END) != 0) error("seeking"); length = ftell(fp); if (length == -1) error("telling"); if (fseek(fp, 0, SEEK_SET) != 0) error("seeking"); if (length > 0x10000 - 0x100) { fprintf(stderr, "%s is too long to be a .com file\n", filename); exit(1); } if (fread(&ram[0x100], length, 1, fp) != 1) error("reading"); fclose(fp); Word segment = 0x1000; setAX(0x0000); setCX(0x00FF); setDX(segment); registers[3] = 0x0000; setSP(0xFFFE); registers[5] = 0x091C; setSI(0x0100); setDI(0xFFFE); for (int i = 0; i < 4; ++i) registers[8 + i] = segment; Byte* byteData = (Byte*)®isters[0]; int bigEndian = (byteData[2] == 0 ? 1 : 0); int byteNumbers[8] = {0, 2, 4, 6, 1, 3, 5, 7}; for (int i = 0 ; i < 8; ++i) byteRegisters[i] = &byteData[byteNumbers[i] ^ bigEndian]; bool prefix = false; for (int i = 0; i < 1000000000; ++i) { if (!repeating) { if (!prefix) { segmentOverride = -1; rep = 0; } prefix = false; opcode = fetchByte(); } wordSize = ((opcode & 1) != 0); bool sourceIsRM = ((opcode & 2) != 0); int operation = (opcode >> 3) & 7; bool jump; switch (opcode) { case 0x00: case 0x01: case 0x02: case 0x03: case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x10: case 0x11: case 0x12: case 0x13: case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x20: case 0x21: case 0x22: case 0x23: case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x30: case 0x31: case 0x32: case 0x33: case 0x38: case 0x39: case 0x3a: case 0x3b: // alu rmv,rmv data = readEA(); if (!sourceIsRM) { destination = data; source = getReg(); } else { destination = getReg(); source = data; } aluOperation = operation; doALUOperation(); if (aluOperation != 7) { if (!sourceIsRM) finishWriteEA(data); else setReg(data); } break; case 0x04: case 0x05: case 0x0c: case 0x0d: case 0x14: case 0x15: case 0x1c: case 0x1d: case 0x24: case 0x25: case 0x2c: case 0x2d: case 0x34: case 0x35: case 0x3c: case 0x3d: // alu accum,i destination = getAccum(); source = !wordSize ? fetchByte() : fetchWord(); aluOperation = operation; doALUOperation(); if (aluOperation != 7) setAccum(); break; case 0x06: case 0x0e: case 0x16: case 0x1e: // PUSH segreg push(registers[operation + 8]); break; case 0x07: case 0x17: case 0x1f: // POP segreg registers[operation + 8] = pop(); break; case 0x26: case 0x2e: case 0x36: case 0x3e: // segment override segmentOverride = operation; prefix = true; break; case 0x27: case 0x2f: // DA if (af() || (al() & 0x0f) > 9) { data = al() + (opcode == 0x27 ? 6 : -6); setAL(data); setAF(true); if ((data & 0x100) != 0) setCF(true); } setCF(cf() || al() > 0x9f); if (cf()) setAL(al() + (opcode == 0x27 ? 0x60 : -0x60)); wordSize = false; data = al(); setPZS(); break; case 0x37: case 0x3f: // AA if (af() || (al() & 0xf) > 9) { setAL(al() + (opcode == 0x37 ? 6 : -6)); setAH(ah() + (opcode == 0x37 ? 1 : -1)); setCA(); } else clearCA(); setAL(al() & 0x0f); break; case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47: case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f: // incdec rw destination = rw(); wordSize = true; setRW(incdec((opcode & 8) != 0)); break; case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: // PUSH rw push(rw()); break; case 0x58: case 0x59: case 0x5a: case 0x5b: case 0x5c: case 0x5d: case 0x5e: case 0x5f: // POP rw setRW(pop()); break; case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67: case 0x68: case 0x69: case 0x6a: case 0x6b: case 0x6c: case 0x6d: case 0x6e: case 0x6f: case 0xc0: case 0xc1: case 0xc8: case 0xc9: // invalid case 0xcc: case 0xf0: case 0xf1: case 0xf4: // INT 3, LOCK, HLT case 0x9b: case 0xce: case 0x0f: // WAIT, INTO, POP CS case 0xd8: case 0xd9: case 0xda: case 0xdb: case 0xdc: case 0xdd: case 0xde: case 0xdf: // escape case 0xe4: case 0xe5: case 0xe6: case 0xe7: case 0xec: case 0xed: case 0xee: case 0xef: // IN, OUT fprintf(stderr, "Invalid opcode %02x", opcode); runtimeError(""); break; case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: case 0x78: case 0x79: case 0x7a: case 0x7b: case 0x7c: case 0x7d: case 0x7e: case 0x7f: // Jcond cb switch (opcode & 0x0e) { case 0x00: jump = of(); break; case 0x02: jump = cf(); break; case 0x04: jump = zf(); break; case 0x06: jump = cf() || zf(); break; case 0x08: jump = sf(); break; case 0x0a: jump = pf(); break; case 0x0c: jump = sf() != of(); break; default: jump = sf() != of() || zf(); break; } jumpShort(fetchByte(), jump == ((opcode & 1) == 0)); break; case 0x80: case 0x81: case 0x82: case 0x83: // alu rmv,iv destination = readEA(); data = fetch(opcode == 0x81); if (opcode != 0x83) source = data; else source = signExtend(data); aluOperation = modRMReg(); doALUOperation(); if (aluOperation != 7) finishWriteEA(data); break; case 0x84: case 0x85: // TEST rmv,rv data = readEA(); test(data, getReg()); break; case 0x86: case 0x87: // XCHG rmv,rv data = readEA(); finishWriteEA(getReg()); setReg(data); break; case 0x88: case 0x89: // MOV rmv,rv ea(); finishWriteEA(getReg()); break; case 0x8a: case 0x8b: // MOV rv,rmv setReg(readEA()); break; case 0x8c: // MOV rmw,segreg ea(); wordSize = 1; finishWriteEA(registers[modRMReg() + 8]); break; case 0x8d: // LEA address = ea(); if (!useMemory) runtimeError("LEA needs a memory address"); setReg(address); break; case 0x8e: // MOV segreg,rmw wordSize = 1; data = readEA(); registers[modRMReg() + 8] = data; break; case 0x8f: // POP rmw writeEA(pop()); break; case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: // XCHG AX,rw data = ax(); setAX(rw()); setRW(data); break; case 0x98: // CBW setAX(signExtend(al())); break; case 0x99: // CWD setDX((ax() & 0x8000) == 0 ? 0x0000 : 0xffff); break; case 0x9a: // CALL cp savedIP = fetchWord(); savedCS = fetchWord(); farCall(); break; case 0x9c: // PUSHF push((flags & 0x0fd7) | 0xf000); break; case 0x9d: // POPF flags = pop() | 2; break; case 0x9e: // SAHF flags = (flags & 0xff02) | ah(); break; case 0x9f: // LAHF setAH(flags & 0xd7); break; case 0xa0: case 0xa1: // MOV accum,xv data = read(fetchWord()); setAccum(); break; case 0xa2: case 0xa3: // MOV xv,accum write(getAccum(), fetchWord()); break; case 0xa4: case 0xa5: // MOVSv stoS(lodS()); doRep(); break; case 0xa6: case 0xa7: // CMPSv lodDIS(); source = data; sub(); doRep(); break; case 0xa8: case 0xa9: // TEST accum,iv data = fetch(wordSize); test(getAccum(), data); break; case 0xaa: case 0xab: // STOSv stoS(getAccum()); doRep(); break; case 0xac: case 0xad: // LODSv data = lodS(); setAccum(); doRep(); break; case 0xae: case 0xaf: // SCASv lodDIS(); destination = getAccum(); source = data; sub(); doRep(); break; case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7: setRB(fetchByte()); break; case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf: // MOV rv,iv setRW(fetchWord()); break; case 0xc2: case 0xc3: case 0xca: case 0xcb: // RET savedIP = pop(); savedCS = (opcode & 8) == 0 ? cs() : pop(); if (!wordSize) setSP(sp() + fetchWord()); farJump(); break; case 0xc4: case 0xc5: // LES/LDS ea(); farLoad(); *modRMRW() = savedIP; registers[8 + (!wordSize ? 0 : 3)] = savedCS; break; case 0xc6: case 0xc7: // MOV rmv,iv ea(); finishWriteEA(fetch(wordSize)); break; case 0xcd: data = fetchByte(); if (data != 0x21) { fprintf(stderr, "Unknown interrupt 0x%02x", data); runtimeError(""); } switch (ah()) { case 2: printf("%c", dl()); break; case 0x4c: printf("*** Bytes: %i\n", length); printf("*** Cycles: %i\n", ios); printf("*** EXIT code %i\n", al()); exit(0); break; default: fprintf(stderr, "Unknown DOS call 0x%02x", data); runtimeError(""); } break; case 0xcf: ip = pop(); setCS(pop()); flags = pop() | 2; break; case 0xd0: case 0xd1: case 0xd2: case 0xd3: // rot rmv,n data = readEA(); if ((opcode & 2) == 0) source = 1; else source = cl(); while (source != 0) { destination = data; switch (modRMReg()) { case 0: // ROL data <<= 1; doCF(); data |= (cf() ? 1 : 0); setOFRotate(); break; case 1: // ROR setCF((data & 1) != 0); data >>= 1; if (cf()) data |= (!wordSize ? 0x80 : 0x8000); setOFRotate(); break; case 2: // RCL data = (data << 1) | (cf() ? 1 : 0); doCF(); setOFRotate(); break; case 3: // RCR data >>= 1; if (cf()) data |= (!wordSize ? 0x80 : 0x8000); setCF((destination & 1) != 0); setOFRotate(); break; case 4: // SHL case 6: data <<= 1; doCF(); setOFRotate(); setPZS(); break; case 5: // SHR setCF((data & 1) != 0); data >>= 1; setOFRotate(); setAF(true); setPZS(); break; case 7: // SAR setCF((data & 1) != 0); data >>= 1; if (!wordSize) data |= (destination & 0x80); else data |= (destination & 0x8000); setOFRotate(); setAF(true); setPZS(); break; } --source; } finishWriteEA(data); break; case 0xd4: // AAM data = fetchByte(); if (data == 0) divideOverflow(); setAH(al() / data); setAL(al() % data); wordSize = true; setPZS(); break; case 0xd5: // AAD data = fetchByte(); setAL(al() + ah()*data); setAH(0); setPZS(); break; case 0xd6: // SALC setAL(cf() ? 0xff : 0x00); break; case 0xd7: // XLATB setAL(readByte(bx() + al())); break; case 0xe0: case 0xe1: case 0xe2: // LOOPc cb setCX(cx() - 1); jump = (cx() != 0); switch (opcode) { case 0xe0: if (zf()) jump = false; break; case 0xe1: if (!zf()) jump = false; break; } jumpShort(fetchByte(), jump); break; case 0xe3: // JCXZ cb jumpShort(fetchByte(), cx() == 0); break; case 0xe8: // CALL cw call(ip + fetchWord()); break; case 0xe9: // JMP cw ip += fetchWord(); break; case 0xea: // JMP cp savedIP = fetchWord(); savedCS = fetchWord(); farJump(); break; case 0xeb: // JMP cb jumpShort(fetchByte(), true); break; case 0xf2: case 0xf3: // REP rep = opcode == 0xf2 ? 1 : 2; prefix = true; break; case 0xf5: // CMC flags ^= 1; break; case 0xf6: case 0xf7: // math rmv data = readEA(); switch (modRMReg()) { case 0: case 1: // TEST rmv,iv test(data, fetch(wordSize)); break; case 2: // NOT iv finishWriteEA(~data); break; case 3: // NEG iv source = data; destination = 0; sub(); finishWriteEA(data); break; case 4: case 5: // MUL rmv, IMUL rmv source = data; destination = getAccum(); data = destination; setSF(); setPF(); data *= source; setAX(data); if (!wordSize) { if (modRMReg() == 4) setCF(ah() != 0); else { if ((source & 0x80) != 0) setAH(ah() - destination); if ((destination & 0x80) != 0) setAH(ah() - source); setCF(ah() == ((al() & 0x80) == 0 ? 0 : 0xff)); } } else { setDX(data >> 16); if (modRMReg() == 4) { data |= dx(); setCF(dx() != 0); } else { if ((source & 0x8000) != 0) setDX(dx() - destination); if ((destination & 0x8000) != 0) setDX(dx() - source); data |= dx(); setCF(dx() == ((ax() & 0x8000) == 0 ? 0 : 0xffff)); } } setZF(); setOF(cf()); break; case 6: case 7: // DIV rmv, IDIV rmv source = data; if (source == 0) divideOverflow(); if (!wordSize) { destination = ax(); if (modRMReg() == 6) { div(); if (data > 0xff) divideOverflow(); } else { destination = ax(); if ((destination & 0x8000) != 0) destination |= 0xffff0000; source = signExtend(source); div(); if (data > 0x7f && data < 0xffffff80) divideOverflow(); } setAH(remainder); setAL(data); } else { destination = (dx() << 16) + ax(); div(); if (modRMReg() == 6) { if (data > 0xffff) divideOverflow(); } else { if (data > 0x7fff && data < 0xffff8000) divideOverflow(); } setDX(remainder); setAX(data); } break; } break; case 0xf8: case 0xf9: // STC/CLC setCF(wordSize); break; case 0xfa: case 0xfb: // STI/CLI setIF(wordSize); break; case 0xfc: case 0xfd: // STD/CLD setDF(wordSize); break; case 0xfe: case 0xff: // misc ea(); if ((!wordSize && modRMReg() >= 2 && modRMReg() <= 6) || modRMReg() == 7) { fprintf(stderr, "Invalid instruction %02x %02x", opcode, modRM); runtimeError(""); } switch (modRMReg()) { case 0: case 1: // incdec rmv destination = readEA2(); finishWriteEA(incdec(modRMReg() != 0)); break; case 2: // CALL rmv call(readEA2()); break; case 3: // CALL mp farLoad(); farCall(); break; case 4: // JMP rmw ip = readEA2(); break; case 5: // JMP mp farLoad(); farJump(); break; case 6: // PUSH rmw push(readEA2()); break; } break; } } runtimeError("Timed out"); }
void writeEA(Word data) { ea(); finishWriteEA(data); }
Word readEA() { address = ea(); return readEA2(); }
void TestIt(long R, long p, long r, long d, long c, long k, long w, long L, long m) { cerr << "\n\n******** TestIt: R=" << R << ", p=" << p << ", r=" << r << ", d=" << d << ", c=" << c << ", k=" << k << ", w=" << w << ", L=" << L << ", m=" << m << endl; FHEcontext context(m, p, r); buildModChain(context, L, c); // context.lazy = false; if (context.lazy) cerr << "LAZY REDUCTIONS\n"; else cerr << "NON-LAZY REDUCTIONS\n"; context.zMStar.printout(); cerr << endl; #ifdef DEBUG cerr << context << endl; #endif FHESecKey secretKey(context); const FHEPubKey& publicKey = secretKey; secretKey.GenSecKey(w); // A Hamming-weight-w secret key ZZX G; if (d == 0) { G = context.alMod.getFactorsOverZZ()[0]; d = deg(G); } else G = makeIrredPoly(p, d); cerr << "G = " << G << "\n"; cerr << "generating key-switching matrices... "; addSome1DMatrices(secretKey); // compute key-switching matrices that we need addFrbMatrices(secretKey); // compute key-switching matrices that we need cerr << "done\n"; cerr << "computing masks and tables for rotation..."; EncryptedArray ea(context, G); cerr << "done\n"; long nslots = ea.size(); // L selects even coefficients vector<ZZX> LM(d); for (long j = 0; j < d; j++) if (j % 2 == 0) LM[j] = ZZX(j, 1); vector<ZZX> C; ea.buildLinPolyCoeffs(C, LM); PlaintextArray p0(ea); p0.random(); Ctxt c0(publicKey); ea.encrypt(c0, publicKey, p0); Ctxt res(c0); applyLinPoly1(ea, res, C); PlaintextArray pp0(ea); ea.decrypt(res, secretKey, pp0); p0.print(cout); cout << "\n"; pp0.print(cout); cout << "\n"; }