/[eiffelstudio]/FreeELKS/trunk/library/kernel/integer_overflow_checker.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/integer_overflow_checker.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91477 - (show annotations)
Sun Jan 14 09:47:13 2007 UTC (13 years ago) by ericb
File size: 4062 byte(s)
Synchronized with ISE 6.0.65740
1 indexing
2 description: "String - Integer/Natural conversion overflow checker"
3 library: "Free implementation of ELKS library"
4 copyright: "Copyright (c) 1986-2006, Eiffel Software and others"
5 license: "Eiffel Forum License v2 (see forum.txt)"
6 date: "$Date$"
7 revision: "$Revision$"
8
9 class
10 INTEGER_OVERFLOW_CHECKER
11
12 inherit
13 NUMERIC_INFORMATION
14
15 create
16 make
17
18 feature{NONE} -- Initialization
19
20 make is
21 -- Initialize.
22 do
23 create integer_overflow_state1.make (1, type_count * 2)
24 create integer_overflow_state2.make (1, type_count * 2)
25
26 integer_overflow_state1.put (({INTEGER_8}.max_value // 10).to_natural_64, 1)
27 integer_overflow_state2.put (({INTEGER_8}.max_value \\ 10).to_natural_64, 1)
28 integer_overflow_state1.put (({INTEGER_16}.max_value // 10).to_natural_64, 2)
29 integer_overflow_state2.put (({INTEGER_16}.max_value \\ 10).to_natural_64, 2)
30 integer_overflow_state1.put (({INTEGER}.max_value // 10).to_natural_64, 3)
31 integer_overflow_state2.put (({INTEGER}.max_value \\ 10).to_natural_64, 3)
32 integer_overflow_state1.put (({INTEGER_64}.max_value // 10).to_natural_64, 4)
33 integer_overflow_state2.put (({INTEGER_64}.max_value \\ 10).to_natural_64, 4)
34
35 integer_overflow_state1.put ((-({INTEGER_8}.min_value // 10)).to_natural_64, 5)
36 integer_overflow_state2.put ((-({INTEGER_8}.min_value \\ 10)).to_natural_64, 5)
37 integer_overflow_state1.put ((-({INTEGER_16}.min_value // 10)).to_natural_64, 6)
38 integer_overflow_state2.put ((-({INTEGER_16}.min_value \\ 10)).to_natural_64, 6)
39 integer_overflow_state1.put ((-({INTEGER}.min_value // 10)).to_natural_64, 7)
40 integer_overflow_state2.put ((-({INTEGER}.min_value \\ 10)).to_natural_64, 7)
41 integer_overflow_state1.put ((-({INTEGER_64}.min_value // 10)).to_natural_64, 8)
42 integer_overflow_state2.put ((-({INTEGER_64}.min_value \\ 10)).to_natural_64, 8)
43
44 create natural_overflow_state1.make (1, type_count)
45 create natural_overflow_state2.make (1, type_count)
46
47 natural_overflow_state1.put (({NATURAL_8}.max_value // 10).to_natural_64, 1)
48 natural_overflow_state2.put (({NATURAL_8}.max_value \\ 10).to_natural_64, 1)
49 natural_overflow_state1.put (({NATURAL_16}.max_value // 10).to_natural_64, 2)
50 natural_overflow_state2.put (({NATURAL_16}.max_value \\ 10).to_natural_64, 2)
51 natural_overflow_state1.put (({NATURAL_32}.max_value // 10).to_natural_64, 3)
52 natural_overflow_state2.put (({NATURAL_32}.max_value \\ 10).to_natural_64, 3)
53 natural_overflow_state1.put (({NATURAL_64}.max_value // 10).to_natural_64, 4)
54 natural_overflow_state2.put (({NATURAL_64}.max_value \\ 10).to_natural_64, 4)
55 end
56
57 feature -- Overflow checking
58
59 will_overflow (part1: like max_natural_type; part2: like max_natural_type; type: INTEGER; sign: INTEGER): BOOLEAN is
60 -- Will `part1' * 10 + `part2' with `sign' overflow
61 -- if we convert it to an number of `type'?
62 require
63 type_valid: integer_natural_type_valid (type)
64 local
65 l_index: INTEGER
66 do
67 if type = type_no_limitation then
68 Result := False
69 else
70 if (type = type_integer_8) or
71 (type = type_integer_16) or
72 (type = type_integer_32) or
73 (type = type_integer_64)
74 then
75 l_index := sign * 4 + type
76 Result := (part1 > integer_overflow_state1.item (l_index)) or
77 ((part1 = integer_overflow_state1.item (l_index)) and
78 (part2 > integer_overflow_state2.item (l_index)))
79 else
80 l_index := type - type_integer_natural_separator
81 if sign = 1 then
82 Result := (part1 > 0) or (part2 > 0)
83 else
84
85 Result := (part1 > natural_overflow_state1.item (l_index)) or
86 ((part1 = natural_overflow_state1.item (l_index)) and
87 (part2 > natural_overflow_state2.item (l_index)))
88 end
89 end
90 end
91 end
92
93 feature{NONE} -- Implementation
94
95 integer_overflow_state1: ARRAY [like max_natural_type]
96 integer_overflow_state2: ARRAY [like max_natural_type]
97 natural_overflow_state1: ARRAY [like max_natural_type]
98 natural_overflow_state2: ARRAY [like max_natural_type]
99 -- Arrays to check conversion overflow
100
101 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23