Example #1
0
def test_float_array_at(monkeypatch):
    space, interp, image, reader = read_image("Squeak4.3.image")
    prim_res = []
    st_res = []


    w_FloatArray = space.smalltalk_at("FloatArray")
    w_ary = W_WordsObject(space, w_FloatArray, 1)

    from rpython.rlib.longlong2float import singlefloat2uint, uint2singlefloat
    from rpython.rlib.rarithmetic import r_singlefloat
    w_ary.setword(0, singlefloat2uint(r_singlefloat(1.5)))

    prim_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)]))
    prim_res.append(interp.perform(w_ary, "at:put:", w_arguments=[space.w(1), space.w(12.5)]))
    prim_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)]))
    assert [space.unwrap_float(w) for w in prim_res] == [1.5, 12.5, 12.5]

    from rsqueakvm.primitives.control import ExternalPlugins
    from rsqueakvm.plugins.float_array_plugin import FloatArrayPlugin

    for p in ExternalPlugins:
        if p is FloatArrayPlugin:
            monkeypatch.delitem(p.primitives, "primitiveAt")
            monkeypatch.delitem(p.primitives, "primitiveAtPut")
            break
    try:
        w_ary.setword(0, singlefloat2uint(r_singlefloat(1.5)))
        st_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)]))
        st_res.append(interp.perform(w_ary, "at:put:", w_arguments=[space.w(1), space.w(12.5)]))
        st_res.append(interp.perform(w_ary, "at:", w_arguments=[space.w(1)]))
        assert [space.unwrap_float(w) for w in st_res] == [1.5, 12.5, 12.5]
    finally:
        monkeypatch.undo()
def primitiveAtPut(interp, s_frame, words, index0, w_float):
    value = interp.space.unwrap_float(w_float)
    try:
        words[index0] = r_uint(singlefloat2uint(r_singlefloat(value)))
    except IndexError:
        raise PrimitiveFailedError
    return w_float
Example #3
0
def primitiveAtPut(interp, s_frame, words, index0, w_float):
    value = interp.space.unwrap_float(w_float)
    try:
        words[index0] = r_uint(singlefloat2uint(r_singlefloat(value)))
    except IndexError:
        raise PrimitiveFailedError
    return w_float
Example #4
0
def pack_float(pack_obj, fmtdesc, count):
    for _ in xrange(count):
        value = pack_obj.space.float_w(pack_obj.pop_arg())
        floatval = r_singlefloat(value)
        value = longlong2float.singlefloat2uint(floatval)
        value = widen(value)

        for i in range(fmtdesc.size):
            pack_obj.result.append(chr(value & 0xff))
            value >>= 8
Example #5
0
def pack_float(fmtiter):
    doubleval = fmtiter.accept_float_arg()
    floatval = r_singlefloat(doubleval)
    if std.pack_fastpath(rffi.FLOAT)(fmtiter, floatval):
        return
    # slow path
    value = longlong2float.singlefloat2uint(floatval)
    value = widen(value)
    value = intmask(value)
    pack_float_to_buffer(fmtiter.wbuf, fmtiter.pos, value, 4, fmtiter.bigendian)
    fmtiter.advance(4)
Example #6
0
def pack_float(fmtiter):
    doubleval = fmtiter.accept_float_arg()
    floatval = r_singlefloat(doubleval)
    value = longlong2float.singlefloat2uint(floatval)
    value = widen(value)
    if fmtiter.bigendian:
        for i in range_4_unroll:
            x = (value >> (8 * i)) & 0xff
            fmtiter.result.append(chr(x))
    else:
        for i in range_4_unroll:
            fmtiter.result.append(chr(value & 0xff))
            value >>= 8
Example #7
0
def pack_float(fmtiter):
    doubleval = fmtiter.accept_float_arg()
    floatval = r_singlefloat(doubleval)
    value = longlong2float.singlefloat2uint(floatval)
    value = widen(value)
    if fmtiter.bigendian:
        for i in range_4_unroll:
            x = (value >> (8*i)) & 0xff
            fmtiter.result.append(chr(x))
    else:
        for i in range_4_unroll:
            fmtiter.result.append(chr(value & 0xff))
            value >>= 8
Example #8
0
def test_call_stubs_single_float():
    from rpython.rlib.longlong2float import uint2singlefloat, singlefloat2uint
    from rpython.rlib.rarithmetic import r_singlefloat, intmask
    #
    c0 = GcCache(False)
    ARGS = [lltype.SingleFloat, lltype.SingleFloat, lltype.SingleFloat]
    RES = lltype.SingleFloat

    def f(a, b, c):
        a = float(a)
        b = float(b)
        c = float(c)
        x = a - (b / c)
        return r_singlefloat(x)

    fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f)
    descr2 = get_call_descr(c0, ARGS, RES)
    a = intmask(singlefloat2uint(r_singlefloat(-10.0)))
    b = intmask(singlefloat2uint(r_singlefloat(3.0)))
    c = intmask(singlefloat2uint(r_singlefloat(2.0)))
    res = descr2.call_stub_i(rffi.cast(lltype.Signed, fnptr),
                             [a, b, c], [], [])
    assert float(uint2singlefloat(rffi.r_uint(res))) == -11.5
Example #9
0
def test_call_stubs_single_float():
    from rpython.rlib.longlong2float import uint2singlefloat, singlefloat2uint
    from rpython.rlib.rarithmetic import r_singlefloat, intmask
    #
    c0 = GcCache(False)
    ARGS = [lltype.SingleFloat, lltype.SingleFloat, lltype.SingleFloat]
    RES = lltype.SingleFloat

    def f(a, b, c):
        a = float(a)
        b = float(b)
        c = float(c)
        x = a - (b / c)
        return r_singlefloat(x)

    fnptr = llhelper(lltype.Ptr(lltype.FuncType(ARGS, RES)), f)
    descr2 = get_call_descr(c0, ARGS, RES)
    a = intmask(singlefloat2uint(r_singlefloat(-10.0)))
    b = intmask(singlefloat2uint(r_singlefloat(3.0)))
    c = intmask(singlefloat2uint(r_singlefloat(2.0)))
    res = descr2.call_stub_i(rffi.cast(lltype.Signed, fnptr),
                             [a, b, c], [], [])
    assert float(uint2singlefloat(rffi.r_uint(res))) == -11.5
Example #10
0
def byteswap(arg):
    """ Convert little->big endian and the opposite
    """
    from rpython.rtyper.lltypesystem import lltype, rffi
    from rpython.rlib.longlong2float import longlong2float, float2longlong,\
         uint2singlefloat, singlefloat2uint

    T = lltype.typeOf(arg)
    if T == lltype.SingleFloat:
        arg = singlefloat2uint(arg)
    elif T == lltype.Float:
        arg = float2longlong(arg)
    elif T == lltype.LongFloat:
        assert False
    else:
        # we cannot do arithmetics on small ints
        arg = widen(arg)

    if rffi.sizeof(T) == 1:
        res = arg
    elif rffi.sizeof(T) == 2:
        a, b = arg & 0xFF, arg & 0xFF00
        res = (a << 8) | (b >> 8)
    elif rffi.sizeof(T) == 4:
        FF = r_uint(0xFF)
        arg = r_uint(arg)
        a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
                      arg & (FF << 24))
        res = (a << 24) | (b << 8) | (c >> 8) | (d >> 24)
    elif rffi.sizeof(T) == 8:
        FF = r_ulonglong(0xFF)
        arg = r_ulonglong(arg)
        a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
                      arg & (FF << 24))
        e, f, g, h = (arg & (FF << 32), arg & (FF << 40), arg & (FF << 48),
                      arg & (FF << 56))
        res = ((a << 56) | (b << 40) | (c << 24) | (d << 8) | (e >> 8) |
               (f >> 24) | (g >> 40) | (h >> 56))
    else:
        assert False  # unreachable code

    if T == lltype.SingleFloat:
        return uint2singlefloat(rffi.cast(rffi.UINT, res))
    if T == lltype.Float:
        return longlong2float(rffi.cast(rffi.LONGLONG, res))
    return rffi.cast(T, res)
Example #11
0
def byteswap(arg):
    """ Convert little->big endian and the opposite
    """
    from rpython.rtyper.lltypesystem import lltype, rffi
    from rpython.rlib.longlong2float import longlong2float, float2longlong,\
         uint2singlefloat, singlefloat2uint

    T = lltype.typeOf(arg)
    if T == lltype.SingleFloat:
        arg = singlefloat2uint(arg)
    elif T == lltype.Float:
        arg = float2longlong(arg)
    elif T == lltype.LongFloat:
        assert False
    else:
        # we cannot do arithmetics on small ints
        arg = widen(arg)

    if rffi.sizeof(T) == 1:
        res = arg
    elif rffi.sizeof(T) == 2:
        a, b = arg & 0xFF, arg & 0xFF00
        res = (a << 8) | (b >> 8)
    elif rffi.sizeof(T) == 4:
        FF = r_uint(0xFF)
        arg = r_uint(arg)
        a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
                      arg & (FF << 24))
        res = (a << 24) | (b << 8) | (c >> 8) | (d >> 24)
    elif rffi.sizeof(T) == 8:
        FF = r_ulonglong(0xFF)
        arg = r_ulonglong(arg)
        a, b, c, d = (arg & FF, arg & (FF << 8), arg & (FF << 16),
                      arg & (FF << 24))
        e, f, g, h = (arg & (FF << 32), arg & (FF << 40), arg & (FF << 48),
                      arg & (FF << 56))
        res = ((a << 56) | (b << 40) | (c << 24) | (d << 8) | (e >> 8) |
               (f >> 24) | (g >> 40) | (h >> 56))
    else:
        assert False # unreachable code

    if T == lltype.SingleFloat:
        return uint2singlefloat(rffi.cast(rffi.UINT, res))
    if T == lltype.Float:
        return longlong2float(rffi.cast(rffi.LONGLONG, res))
    return rffi.cast(T, res)
Example #12
0
def fnsingle(f1):
    sf1 = r_singlefloat(f1)
    ii = singlefloat2uint(sf1)
    sf2 = uint2singlefloat(ii)
    f2 = float(sf2)
    return f2
Example #13
0
    def float2bits(flt):

        # See note above for bits2float. We're doing the reverse:
        # Python Float (64-bit) -> r_singlefloat -> r_uint32 -> Python Int (64-bit)
        bits = widen(singlefloat2uint(rffi.cast(lltype.SingleFloat, flt)))
        return bits
Example #14
0
def singlefloat2int(x):
    x = longlong2float.singlefloat2uint(x)
    return rffi.cast(lltype.Signed, x)
Example #15
0
def fnsingle(f1):
    sf1 = r_singlefloat(f1)
    ii = singlefloat2uint(sf1)
    sf2 = uint2singlefloat(ii)
    f2 = float(sf2)
    return f2
Example #16
0
def singlefloat2int(x):
    x = longlong2float.singlefloat2uint(x)
    return rffi.cast(lltype.Signed, x)
Example #17
0
    def float2bits(flt):

        # See note above for bits2float. We're doing the reverse:
        # Python Float (64-bit) -> r_singlefloat -> r_uint32 -> Python Int (64-bit)
        bits = widen(singlefloat2uint(rffi.cast(lltype.SingleFloat, flt)))
        return bits