PageRenderTime 31ms CodeModel.GetById 25ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/numeric/integer_16.e

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