PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rtyper/rfloat.py

https://bitbucket.org/pypy/pypy/
Python | 171 lines | 113 code | 47 blank | 11 comment | 1 complexity | 04b0cf65c9e3b5c95aed92cccaddf4ad MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  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. class FloatRepr(Repr):
  11. lowleveltype = Float
  12. def convert_const(self, value):
  13. if not isinstance(value, (int, base_int, float)): # can be bool too
  14. raise TyperError("not a float: %r" % (value,))
  15. return float(value)
  16. def get_ll_eq_function(self):
  17. return None
  18. get_ll_gt_function = get_ll_eq_function
  19. get_ll_lt_function = get_ll_eq_function
  20. get_ll_ge_function = get_ll_eq_function
  21. get_ll_le_function = get_ll_eq_function
  22. def get_ll_hash_function(self):
  23. return _hash_float
  24. def rtype_bool(_, hop):
  25. vlist = hop.inputargs(Float)
  26. return hop.genop('float_is_true', vlist, resulttype=Bool)
  27. def rtype_neg(_, hop):
  28. vlist = hop.inputargs(Float)
  29. return hop.genop('float_neg', vlist, resulttype=Float)
  30. def rtype_pos(_, hop):
  31. vlist = hop.inputargs(Float)
  32. return vlist[0]
  33. def rtype_abs(_, hop):
  34. vlist = hop.inputargs(Float)
  35. return hop.genop('float_abs', vlist, resulttype=Float)
  36. def rtype_int(_, hop):
  37. vlist = hop.inputargs(Float)
  38. # int(x) never raises in RPython, you need to use
  39. # rarithmetic.ovfcheck_float_to_int() if you want this
  40. hop.exception_cannot_occur()
  41. return hop.genop('cast_float_to_int', vlist, resulttype=Signed)
  42. def rtype_float(_, hop):
  43. vlist = hop.inputargs(Float)
  44. hop.exception_cannot_occur()
  45. return vlist[0]
  46. @jit.elidable
  47. def ll_str(self, f):
  48. from rpython.rlib.rfloat import formatd
  49. return llstr(formatd(f, 'f', 6))
  50. float_repr = FloatRepr()
  51. class __extend__(annmodel.SomeFloat):
  52. def rtyper_makerepr(self, rtyper):
  53. return float_repr
  54. def rtyper_makekey(self):
  55. return self.__class__,
  56. class __extend__(pairtype(FloatRepr, FloatRepr)):
  57. #Arithmetic
  58. def rtype_add(_, hop):
  59. return _rtype_template(hop, 'add')
  60. rtype_inplace_add = rtype_add
  61. def rtype_sub(_, hop):
  62. return _rtype_template(hop, 'sub')
  63. rtype_inplace_sub = rtype_sub
  64. def rtype_mul(_, hop):
  65. return _rtype_template(hop, 'mul')
  66. rtype_inplace_mul = rtype_mul
  67. def rtype_truediv(_, hop):
  68. return _rtype_template(hop, 'truediv')
  69. rtype_inplace_truediv = rtype_truediv
  70. # turn 'div' on floats into 'truediv'
  71. rtype_div = rtype_truediv
  72. rtype_inplace_div = rtype_inplace_truediv
  73. # 'floordiv' on floats not supported in RPython
  74. #comparisons: eq is_ ne lt le gt ge
  75. def rtype_eq(_, hop):
  76. return _rtype_compare_template(hop, 'eq')
  77. rtype_is_ = rtype_eq
  78. def rtype_ne(_, hop):
  79. return _rtype_compare_template(hop, 'ne')
  80. def rtype_lt(_, hop):
  81. return _rtype_compare_template(hop, 'lt')
  82. def rtype_le(_, hop):
  83. return _rtype_compare_template(hop, 'le')
  84. def rtype_gt(_, hop):
  85. return _rtype_compare_template(hop, 'gt')
  86. def rtype_ge(_, hop):
  87. return _rtype_compare_template(hop, 'ge')
  88. #Helpers FloatRepr,FloatRepr
  89. def _rtype_template(hop, func):
  90. vlist = hop.inputargs(Float, Float)
  91. return hop.genop('float_'+func, vlist, resulttype=Float)
  92. def _rtype_compare_template(hop, func):
  93. vlist = hop.inputargs(Float, Float)
  94. return hop.genop('float_'+func, vlist, resulttype=Bool)
  95. # ______________________________________________________________________
  96. # Support for r_singlefloat and r_longfloat from rpython.rlib.rarithmetic
  97. from rpython.rtyper.lltypesystem import lltype
  98. from rpython.rtyper.rmodel import Repr
  99. class __extend__(annmodel.SomeSingleFloat):
  100. def rtyper_makerepr(self, rtyper):
  101. return SingleFloatRepr()
  102. def rtyper_makekey(self):
  103. return self.__class__,
  104. class SingleFloatRepr(Repr):
  105. lowleveltype = lltype.SingleFloat
  106. def rtype_float(self, hop):
  107. v, = hop.inputargs(lltype.SingleFloat)
  108. hop.exception_cannot_occur()
  109. # we use cast_primitive to go between Float and SingleFloat.
  110. return hop.genop('cast_primitive', [v],
  111. resulttype = lltype.Float)
  112. class __extend__(annmodel.SomeLongFloat):
  113. def rtyper_makerepr(self, rtyper):
  114. return LongFloatRepr()
  115. def rtyper_makekey(self):
  116. return self.__class__,
  117. class LongFloatRepr(Repr):
  118. lowleveltype = lltype.LongFloat
  119. def rtype_float(self, hop):
  120. v, = hop.inputargs(lltype.LongFloat)
  121. hop.exception_cannot_occur()
  122. # we use cast_primitive to go between Float and LongFloat.
  123. return hop.genop('cast_primitive', [v],
  124. resulttype = lltype.Float)