def recv(self, atom, args): if atom is RUN_1: return DoubleObject(float(unwrapStr(args[0]).encode('utf-8'))) if atom is FROMBYTES_1: data = unwrapList(args[0]) x = unpack_float("".join([chr(unwrapInt(byte)) for byte in data]), True) return DoubleObject(x) raise Refused(self, atom, args)
def bench(obj, name): name = unwrapStr(name).encode("utf-8") if not benchmarkSettings.enabled: debug_print("Not running benchmark", name, "since benchmarking is disabled") return ConstList([IntObject(0), DoubleObject(0.0)]) debug_print("Benchmarking", name) # Step 1: Calibrate timing loop. debug_print("Calibrating timing loop...") # Unroll do-while iteration. loops = 1 debug_print("Trying 1 loop...") taken = time.time() obj.call(u"run", []) taken = time.time() - taken while taken < 1.0 and loops < 100000000: debug_print("Took", taken, "seconds to run", loops, "loops") loops *= 10 debug_print("Trying", loops, "loops...") acc = 0 taken = time.time() while acc < loops: acc += 1 obj.call(u"run", []) taken = time.time() - taken debug_print("Okay! Will take", loops, "loops at", taken, "seconds") # Step 2: Take trials. debug_print("Taking trials...") trialCount = 3 - 1 # Unroll first iteration to get maximum. acc = 0 taken = time.time() while acc < loops: acc += 1 obj.call(u"run", []) taken = time.time() - taken result = taken while trialCount: trialCount -= 1 acc = 0 taken = time.time() while acc < loops: acc += 1 obj.call(u"run", []) taken = time.time() - taken if taken < result: result = taken # All done! return ConstList([IntObject(loops), DoubleObject(taken)])
def recv(self, atom, args): from typhon.objects.collections.maps import EMPTY_MAP if atom is FROMNOW_1: duration = promoteToDouble(args[0]) p, r = makePromise() vat = currentVat.get() uv_timer = ruv.alloc_timer(vat.uv_loop) # Stash the resolver. ruv.stashTimer(uv_timer, (vat, r)) # repeat of 0 means "don't repeat" ruv.timerStart(uv_timer, resolveTimer, int(duration * 1000), 0) assert ruv.isActive(uv_timer), "Timer isn't active!?" return p if atom is TRIAL_1: obj = args[0] then = time.time() obj.call(u"run", []) now = time.time() # We can't give the value up immediately, due to determinism # requirements. Instead, provide it as a promise which will be # available on subsequent turns. rv = DoubleObject(now - then) p, r = makePromise() vat = currentVat.get() vat.sendOnly(r, RESOLVE_1, [rv], EMPTY_MAP) return p raise Refused(self, atom, args)
def resolveTimer(uv_timer): vat, (resolver, then) = ruv.unstashTimer(uv_timer) now = ruv.now(vat.uv_loop) assert isinstance(resolver, LocalResolver) # Convert from ms to s. d = intmask(now - then) / 1000.0 resolver.resolve(DoubleObject(d))
def testMulDouble(self): """ Ints are promoted by doubles during multiplication. """ i = IntObject(4) result = i.call(u"multiply", [DoubleObject(2.1)]) self.assertTrue(isinstance(result, DoubleObject)) self.assertEqual(result.getDouble(), 8.4)
def uncall(self): return wrapList([DoubleObject(self._d)])
def testAddDouble(self): i = IntObject(32) result = i.call(u"add", [DoubleObject(1.1)]) self.assertAlmostEqual(result.getDouble(), 33.1)
def testSubtract(self): d = DoubleObject(5.5) result = d.call(u"subtract", [DoubleObject(1.3)]) self.assertAlmostEqual(result.getDouble(), 4.2)
def testAddInt(self): d = DoubleObject(3.2) result = d.call(u"add", [IntObject(1)]) self.assertAlmostEqual(result.getDouble(), 4.2)
def testHashEqual(self): a = DoubleObject(42) b = DoubleObject(42) self.assertEqual(a.hash(), b.hash())
def testPromoteToDoublePromise(self): with scopedVat(testingVat()): p = makeNear(DoubleObject(4.2)) self.assertAlmostEqual(promoteToDouble(p), 4.2)
def testNaN(self): d = DoubleObject(float("nan")) self.assertTrue(d.isSettled())
def testDoubleEqualityNaN(self): first = DoubleObject(float("nan")) second = DoubleObject(float("nan")) self.assertEqual(optSame(first, second), EQUAL)
def testDoubleEquality(self): first = DoubleObject(4.2) second = DoubleObject(4.2) self.assertEqual(optSame(first, second), EQUAL)
def testNaN(self): d = DoubleObject(float("nan")) self.assertTrue(isSettled(d))
def testNaNFail(self): # Found by accident. first = DoubleObject(float("nan")) second = IntObject(42) self.assertEqual(optSame(first, second), INEQUAL)
def visitDoubleExpr(self, d, span): return self.dest.LiveExpr(DoubleObject(d), span)
def run(self): before = time.time() result = self.f.call(u"run", []) after = time.time() elapsed = max(after - before, 0.0) return [result, DoubleObject(elapsed)]
def testFloorDivide(self): d = DoubleObject(3.2) result = d.call(u"floorDivide", [IntObject(2)]) self.assertEqual(result.getInt(), 1)
def testSin(self): d = DoubleObject(math.pi / 2.0) result = d.call(u"sin", []) self.assertAlmostEqual(result.getDouble(), 1.0)
def testHashInequal(self): a = DoubleObject(42.0) b = DoubleObject(5.0) self.assertNotEqual(a.samenessHash(), b.samenessHash())
def testCmpNaN(self): a = DoubleObject(float("nan")) comparison = a.call(u"op__cmp", [a]) self.assertFalse(comparison.call(u"aboveZero", []).isTrue()) self.assertFalse(comparison.call(u"belowZero", []).isTrue()) self.assertFalse(comparison.call(u"isZero", []).isTrue())
def testApproxDivideDouble(self): bi = BigInt(rbigint.fromint(1)) result = bi.call(u"approxDivide", [DoubleObject(32.0)]) self.assertAlmostEqual(result._d, 1.0 / 32.0)
def testHashInequal(self): a = DoubleObject(42) b = DoubleObject(5) self.assertNotEqual(a.hash(), b.hash())
def testFloorDivideDouble(self): bi = BigInt(rbigint.fromint(2).pow(rbigint.fromint(65))) result = bi.call(u"floorDivide", [DoubleObject(2.0)]) self.assertTrue( result.bi.eq(rbigint.fromint(2).pow(rbigint.fromint(64))))
def testOpCmpDouble(self): i = IntObject(2) result = i.call(u"op__cmp", [DoubleObject(2.0)]) self.assertEqual(result.getInt(), 0)
This guard admits any near value, as well as any resolved reference to any near value. This guard is unretractable. """ specimen = resolution(specimen) if isinstance(specimen, Promise): msg = u"Specimen is in non-near state %s" % specimen.state().repr throw(ej, StrObject(msg)) return specimen # Prebuild, since building on-the-fly floats from strings doesn't work in # RPython. Infinity = DoubleObject(float("inf")) NaN = DoubleObject(float("nan")) def safeScope(): return finalize({ u"null": NullObject, u"Infinity": Infinity, u"NaN": NaN, u"false": wrapBool(False), u"true": wrapBool(True), u"Any": anyGuard, u"Binding": BindingGuard(), u"DeepFrozen": deepFrozenGuard, u"Near": nearGuard(), u"Same": sameGuardMaker,
def testSubtractDouble(self): i = IntObject(5) result = i.call(u"subtract", [DoubleObject(1.5)]) self.assertAlmostEqual(result.getDouble(), 3.5)
def sendTimestamp(self, obj): from typhon.objects.collections.maps import EMPTY_MAP vat = currentVat.get() now = intmask(ruv.now(vat.uv_loop)) / 1000.0 return vat.send(obj, RUN_1, [DoubleObject(now)], EMPTY_MAP)