| 1 |
from rpython.annotator import model as annmodel |
| 2 |
from rpython.rlib.objectmodel import _hash_float |
| 3 |
from rpython.rlib.rarithmetic import base_int |
| 4 |
from rpython.rlib import jit |
| 5 |
from rpython.rtyper.annlowlevel import llstr |
| 6 |
from rpython.rtyper.error import TyperError |
| 7 |
from rpython.rtyper.lltypesystem.lltype import (Signed, Bool, Float) |
| 8 |
from rpython.rtyper.rmodel import Repr |
| 9 |
from rpython.tool.pairtype import pairtype |
| 10 |
|
| 11 |
class FloatRepr(Repr): |
| 12 |
lowleveltype = Float |
| 13 |
|
| 14 |
def convert_const(self, value): |
| 15 |
if not isinstance(value, (int, base_int, float)): |
| 16 |
raise TyperError("not a float: %r" % (value,)) |
| 17 |
return float(value) |
| 18 |
|
| 19 |
def get_ll_eq_function(self): |
| 20 |
return None |
| 21 |
get_ll_gt_function = get_ll_eq_function |
| 22 |
get_ll_lt_function = get_ll_eq_function |
| 23 |
get_ll_ge_function = get_ll_eq_function |
| 24 |
get_ll_le_function = get_ll_eq_function |
| 25 |
|
| 26 |
def get_ll_hash_function(self): |
| 27 |
return _hash_float |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
def rtype_bool(_, hop): |
| 33 |
vlist = hop.inputargs(Float) |
| 34 |
return hop.genop('float_is_true', vlist, resulttype=Bool) |
| 35 |
|
| 36 |
def rtype_neg(_, hop): |
| 37 |
vlist = hop.inputargs(Float) |
| 38 |
return hop.genop('float_neg', vlist, resulttype=Float) |
| 39 |
|
| 40 |
def rtype_pos(_, hop): |
| 41 |
vlist = hop.inputargs(Float) |
| 42 |
return vlist[0] |
| 43 |
|
| 44 |
def rtype_abs(_, hop): |
| 45 |
vlist = hop.inputargs(Float) |
| 46 |
return hop.genop('float_abs', vlist, resulttype=Float) |
| 47 |
|
| 48 |
def rtype_int(_, hop): |
| 49 |
vlist = hop.inputargs(Float) |
| 50 |
|
| 51 |
|
| 52 |
hop.exception_cannot_occur() |
| 53 |
return hop.genop('cast_float_to_int', vlist, resulttype=Signed) |
| 54 |
|
| 55 |
def rtype_float(_, hop): |
| 56 |
vlist = hop.inputargs(Float) |
| 57 |
hop.exception_cannot_occur() |
| 58 |
return vlist[0] |
| 59 |
|
| 60 |
@jit.elidable |
| 61 |
def ll_str(self, f): |
| 62 |
from rpython.rlib.rfloat import formatd |
| 63 |
return llstr(formatd(f, 'f', 6)) |
| 64 |
|
| 65 |
float_repr = FloatRepr() |
| 66 |
|
| 67 |
class __extend__(annmodel.SomeFloat): |
| 68 |
def rtyper_makerepr(self, rtyper): |
| 69 |
return float_repr |
| 70 |
|
| 71 |
def rtyper_makekey(self): |
| 72 |
return self.__class__, |
| 73 |
|
| 74 |
|
| 75 |
class __extend__(pairtype(FloatRepr, FloatRepr)): |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
def rtype_add(_, hop): |
| 80 |
return _rtype_template(hop, 'add') |
| 81 |
|
| 82 |
rtype_inplace_add = rtype_add |
| 83 |
|
| 84 |
def rtype_sub(_, hop): |
| 85 |
return _rtype_template(hop, 'sub') |
| 86 |
|
| 87 |
rtype_inplace_sub = rtype_sub |
| 88 |
|
| 89 |
def rtype_mul(_, hop): |
| 90 |
return _rtype_template(hop, 'mul') |
| 91 |
|
| 92 |
rtype_inplace_mul = rtype_mul |
| 93 |
|
| 94 |
def rtype_truediv(_, hop): |
| 95 |
return _rtype_template(hop, 'truediv') |
| 96 |
|
| 97 |
rtype_inplace_truediv = rtype_truediv |
| 98 |
|
| 99 |
|
| 100 |
rtype_div = rtype_truediv |
| 101 |
rtype_inplace_div = rtype_inplace_truediv |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def rtype_eq(_, hop): |
| 108 |
return _rtype_compare_template(hop, 'eq') |
| 109 |
|
| 110 |
rtype_is_ = rtype_eq |
| 111 |
|
| 112 |
def rtype_ne(_, hop): |
| 113 |
return _rtype_compare_template(hop, 'ne') |
| 114 |
|
| 115 |
def rtype_lt(_, hop): |
| 116 |
return _rtype_compare_template(hop, 'lt') |
| 117 |
|
| 118 |
def rtype_le(_, hop): |
| 119 |
return _rtype_compare_template(hop, 'le') |
| 120 |
|
| 121 |
def rtype_gt(_, hop): |
| 122 |
return _rtype_compare_template(hop, 'gt') |
| 123 |
|
| 124 |
def rtype_ge(_, hop): |
| 125 |
return _rtype_compare_template(hop, 'ge') |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
def _rtype_template(hop, func): |
| 130 |
vlist = hop.inputargs(Float, Float) |
| 131 |
return hop.genop('float_'+func, vlist, resulttype=Float) |
| 132 |
|
| 133 |
def _rtype_compare_template(hop, func): |
| 134 |
vlist = hop.inputargs(Float, Float) |
| 135 |
return hop.genop('float_'+func, vlist, resulttype=Bool) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
from rpython.rtyper.lltypesystem import lltype |
| 142 |
from rpython.rtyper.rmodel import Repr |
| 143 |
|
| 144 |
class __extend__(annmodel.SomeSingleFloat): |
| 145 |
def rtyper_makerepr(self, rtyper): |
| 146 |
return SingleFloatRepr() |
| 147 |
def rtyper_makekey(self): |
| 148 |
return self.__class__, |
| 149 |
|
| 150 |
class SingleFloatRepr(Repr): |
| 151 |
lowleveltype = lltype.SingleFloat |
| 152 |
|
| 153 |
def rtype_float(self, hop): |
| 154 |
v, = hop.inputargs(lltype.SingleFloat) |
| 155 |
hop.exception_cannot_occur() |
| 156 |
|
| 157 |
return hop.genop('cast_primitive', [v], |
| 158 |
resulttype = lltype.Float) |
| 159 |
|
| 160 |
class __extend__(annmodel.SomeLongFloat): |
| 161 |
def rtyper_makerepr(self, rtyper): |
| 162 |
return LongFloatRepr() |
| 163 |
def rtyper_makekey(self): |
| 164 |
return self.__class__, |
| 165 |
|
| 166 |
class LongFloatRepr(Repr): |
| 167 |
lowleveltype = lltype.LongFloat |
| 168 |
|
| 169 |
def rtype_float(self, hop): |
| 170 |
v, = hop.inputargs(lltype.LongFloat) |
| 171 |
hop.exception_cannot_occur() |
| 172 |
|
| 173 |
return hop.genop('cast_primitive', [v], |
| 174 |
resulttype = lltype.Float) |