PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/src/lib/numeric/integer_64.e

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