Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
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)])
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
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))
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
 def uncall(self):
     return wrapList([DoubleObject(self._d)])
Exemplo n.º 7
0
 def testAddDouble(self):
     i = IntObject(32)
     result = i.call(u"add", [DoubleObject(1.1)])
     self.assertAlmostEqual(result.getDouble(), 33.1)
Exemplo n.º 8
0
 def testSubtract(self):
     d = DoubleObject(5.5)
     result = d.call(u"subtract", [DoubleObject(1.3)])
     self.assertAlmostEqual(result.getDouble(), 4.2)
Exemplo n.º 9
0
 def testAddInt(self):
     d = DoubleObject(3.2)
     result = d.call(u"add", [IntObject(1)])
     self.assertAlmostEqual(result.getDouble(), 4.2)
Exemplo n.º 10
0
 def testHashEqual(self):
     a = DoubleObject(42)
     b = DoubleObject(42)
     self.assertEqual(a.hash(), b.hash())
Exemplo n.º 11
0
 def testSubtract(self):
     d = DoubleObject(5.5)
     result = d.call(u"subtract", [DoubleObject(1.3)])
     self.assertAlmostEqual(result.getDouble(), 4.2)
Exemplo n.º 12
0
 def testAddInt(self):
     d = DoubleObject(3.2)
     result = d.call(u"add", [IntObject(1)])
     self.assertAlmostEqual(result.getDouble(), 4.2)
Exemplo n.º 13
0
 def testPromoteToDoublePromise(self):
     with scopedVat(testingVat()):
         p = makeNear(DoubleObject(4.2))
         self.assertAlmostEqual(promoteToDouble(p), 4.2)
Exemplo n.º 14
0
 def testNaN(self):
     d = DoubleObject(float("nan"))
     self.assertTrue(d.isSettled())
Exemplo n.º 15
0
 def testDoubleEqualityNaN(self):
     first = DoubleObject(float("nan"))
     second = DoubleObject(float("nan"))
     self.assertEqual(optSame(first, second), EQUAL)
Exemplo n.º 16
0
 def testDoubleEquality(self):
     first = DoubleObject(4.2)
     second = DoubleObject(4.2)
     self.assertEqual(optSame(first, second), EQUAL)
Exemplo n.º 17
0
 def testNaN(self):
     d = DoubleObject(float("nan"))
     self.assertTrue(isSettled(d))
Exemplo n.º 18
0
 def testNaNFail(self):
     # Found by accident.
     first = DoubleObject(float("nan"))
     second = IntObject(42)
     self.assertEqual(optSame(first, second), INEQUAL)
Exemplo n.º 19
0
 def visitDoubleExpr(self, d, span):
     return self.dest.LiveExpr(DoubleObject(d), span)
Exemplo n.º 20
0
 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)]
Exemplo n.º 21
0
 def testFloorDivide(self):
     d = DoubleObject(3.2)
     result = d.call(u"floorDivide", [IntObject(2)])
     self.assertEqual(result.getInt(), 1)
Exemplo n.º 22
0
 def testSin(self):
     d = DoubleObject(math.pi / 2.0)
     result = d.call(u"sin", [])
     self.assertAlmostEqual(result.getDouble(), 1.0)
Exemplo n.º 23
0
 def testHashInequal(self):
     a = DoubleObject(42.0)
     b = DoubleObject(5.0)
     self.assertNotEqual(a.samenessHash(), b.samenessHash())
Exemplo n.º 24
0
 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())
Exemplo n.º 25
0
 def testApproxDivideDouble(self):
     bi = BigInt(rbigint.fromint(1))
     result = bi.call(u"approxDivide", [DoubleObject(32.0)])
     self.assertAlmostEqual(result._d, 1.0 / 32.0)
Exemplo n.º 26
0
 def testHashInequal(self):
     a = DoubleObject(42)
     b = DoubleObject(5)
     self.assertNotEqual(a.hash(), b.hash())
Exemplo n.º 27
0
 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))))
Exemplo n.º 28
0
 def testSin(self):
     d = DoubleObject(math.pi / 2.0)
     result = d.call(u"sin", [])
     self.assertAlmostEqual(result.getDouble(), 1.0)
Exemplo n.º 29
0
 def testOpCmpDouble(self):
     i = IntObject(2)
     result = i.call(u"op__cmp", [DoubleObject(2.0)])
     self.assertEqual(result.getInt(), 0)
Exemplo n.º 30
0
 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())
Exemplo n.º 31
0
 def testHashEqual(self):
     a = DoubleObject(42)
     b = DoubleObject(42)
     self.assertEqual(a.hash(), b.hash())
Exemplo n.º 32
0
 def testHashInequal(self):
     a = DoubleObject(42.0)
     b = DoubleObject(5.0)
     self.assertNotEqual(a.samenessHash(), b.samenessHash())
Exemplo n.º 33
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,
Exemplo n.º 34
0
 def testSubtractDouble(self):
     i = IntObject(5)
     result = i.call(u"subtract", [DoubleObject(1.5)])
     self.assertAlmostEqual(result.getDouble(), 3.5)
Exemplo n.º 35
0
 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)
Exemplo n.º 36
0
 def testHashInequal(self):
     a = DoubleObject(42)
     b = DoubleObject(5)
     self.assertNotEqual(a.hash(), b.hash())
Exemplo n.º 37
0
 def testFloorDivide(self):
     d = DoubleObject(3.2)
     result = d.call(u"floorDivide", [IntObject(2)])
     self.assertEqual(result.getInt(), 1)