/src/lib/numeric/integer_32.e

http://github.com/tybor/Liberty · Specman e · 246 lines · 173 code · 32 blank · 41 comment · 5 complexity · cfe2310396e787817ac79d4aa8e355b0 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class INTEGER_32
  5. insert
  6. INTEGER_GENERAL
  7. feature {ANY} -- Conversions:
  8. fit_integer_8: BOOLEAN
  9. -- Does `Current' fit in INTEGER_8?
  10. do
  11. if Current >= -128 then
  12. Result := Current <= 127
  13. end
  14. ensure
  15. Result = Current.in_range(-128, 127)
  16. end
  17. to_integer_8: INTEGER_8
  18. -- Explicit conversion to INTEGER_8.
  19. require
  20. fit_integer_8
  21. external "built_in"
  22. ensure
  23. Result = Current
  24. end
  25. fit_integer_16: BOOLEAN
  26. -- Does `Current' fit in INTEGER_16?
  27. do
  28. if Current >= -32768 then
  29. Result := Current <= 32767
  30. end
  31. ensure
  32. Result = Current.in_range(-32768, 32767)
  33. end
  34. to_integer_16: INTEGER_16
  35. -- Explicit conversion to INTEGER_16.
  36. require
  37. fit_integer_16
  38. external "built_in"
  39. ensure
  40. Result = Current
  41. end
  42. to_integer_64: INTEGER_64
  43. -- Explicit conversion to INTEGER_64.
  44. do
  45. Result := Current
  46. ensure
  47. Result = Current
  48. end
  49. fit_natural_8: BOOLEAN
  50. -- Does `Current' fit in NATURAL_8?
  51. do
  52. if Current >= 0 then
  53. Result := Current <= 255
  54. end
  55. ensure
  56. Result = Current.in_range(0, 255)
  57. end
  58. to_natural_8: NATURAL_8
  59. -- Explicit conversion to NATURAL_8.
  60. require
  61. fit_natural_8
  62. external "built_in"
  63. ensure
  64. Result.to_integer_16 = Current
  65. end
  66. fit_natural_16: BOOLEAN
  67. -- Does `Current' fit in NATURAL_16?
  68. do
  69. if Current >= 0 then
  70. Result := Current <= 65535
  71. end
  72. ensure
  73. Result = Current.in_range(0, 65535)
  74. end
  75. to_natural_16: NATURAL_16
  76. -- Explicit conversion to NATURAL_16.
  77. require
  78. fit_natural_16
  79. external "built_in"
  80. ensure
  81. Result.to_integer_32 = Current
  82. end
  83. to_natural_32: NATURAL_32
  84. -- Explicit conversion to NATURAL_32.
  85. require
  86. Current >= 0
  87. external "built_in"
  88. ensure
  89. Result.to_integer_32 = Current
  90. end
  91. to_natural_64: NATURAL_64
  92. -- Explicit conversion to NATURAL_64.
  93. require
  94. Current >= 0
  95. external "built_in"
  96. ensure
  97. Result.to_integer_32 = Current
  98. end
  99. force_to_real_32: REAL_32
  100. -- Forced conversion to REAL_32 (possible loss of precision).
  101. -- (See also `fit_real_32' and `to_real_32'.)
  102. external "built_in"
  103. end
  104. fit_real_32: BOOLEAN
  105. -- Does `Current' fit in REAL_32?
  106. do
  107. Result := integer_32_fit_real_32(Current)
  108. end
  109. to_real_32: REAL_32
  110. -- Explicit conversion to REAL_32. (See also `force_to_real_32'.)
  111. require
  112. fit_real_32
  113. do
  114. Result := force_to_real_32
  115. ensure
  116. Result.force_to_integer_32 = Current
  117. end
  118. to_real_64: REAL_64
  119. -- Explicit conversion to REAL_64.
  120. do
  121. Result := Current
  122. ensure
  123. Result = Current
  124. end
  125. to_number: NUMBER
  126. local
  127. number_tools: NUMBER_TOOLS
  128. do
  129. Result := number_tools.from_integer(Current)
  130. ensure then
  131. Result @= Current
  132. end
  133. decimal_digit: CHARACTER
  134. do
  135. Result := (Current + '0'.code).to_character
  136. end
  137. hexadecimal_digit: CHARACTER
  138. do
  139. if Current <= 9 then
  140. Result := (Current + '0'.code).to_character
  141. else
  142. Result := ('A'.code + (Current - 10)).to_character
  143. end
  144. end
  145. infix "|..|" (other: INTEGER): INTEGER_RANGE[INTEGER]
  146. require
  147. Current <= other
  148. do
  149. create Result.make(Current, other, integer_range_noop, integer_range_noop)
  150. end
  151. feature {}
  152. integer_range_noop: FUNCTION[TUPLE[INTEGER], INTEGER]
  153. once
  154. Result := agent (i: INTEGER): INTEGER do Result := i end (?)
  155. end
  156. feature {ANY}
  157. low_16: INTEGER_16
  158. -- The 16 low bits of `Current' (i.e. the right-most part).
  159. external "built_in"
  160. end
  161. high_16: INTEGER_16
  162. -- The 16 high bits of `Current' (i.e. the left-most part).
  163. do
  164. Result := (Current |>> 16).low_16
  165. end
  166. one: INTEGER_8 1
  167. zero: INTEGER_8 0
  168. hash_code: INTEGER_32
  169. do
  170. Result := Current & 0x7FFFFFFF
  171. end
  172. sqrt: REAL
  173. do
  174. Result := to_real_64.sqrt
  175. end
  176. log: REAL
  177. do
  178. Result := to_real_64.log
  179. end
  180. log10: REAL
  181. do
  182. Result := to_real_64.log10
  183. end
  184. bit_count: INTEGER_8 32
  185. feature {}
  186. integer_32_fit_real_32 (integer_32: INTEGER_32): BOOLEAN
  187. external "plug_in"
  188. alias "{
  189. location: "${sys}/runtime"
  190. module_name: "integer_fit_real"
  191. feature_name: "integer_32_fit_real_32"
  192. }"
  193. end
  194. end -- class INTEGER_32
  195. --
  196. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  197. --
  198. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  199. -- of this software and associated documentation files (the "Software"), to deal
  200. -- in the Software without restriction, including without limitation the rights
  201. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  202. -- copies of the Software, and to permit persons to whom the Software is
  203. -- furnished to do so, subject to the following conditions:
  204. --
  205. -- The above copyright notice and this permission notice shall be included in
  206. -- all copies or substantial portions of the Software.
  207. --
  208. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  209. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  210. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  211. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  212. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  213. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  214. -- THE SOFTWARE.