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

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