Skip to content

Commit 315d3c3

Browse files
SijaRX14
authored andcommittedJan 5, 2018
Remove _invariant macro from Float::Printer::* modules (#5435)
1 parent 1d52841 commit 315d3c3

File tree

4 files changed

+0
-52
lines changed

4 files changed

+0
-52
lines changed
 

‎src/float/printer/cached_powers.cr

-10
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,6 @@ module Float::Printer::CachedPowers
159159
k = ((min_exp + DiyFP::SIGNIFICAND_SIZE - 1) * D_1_LOG2_10).ceil
160160
index = ((CACHED_POWER_OFFSET + k.to_i - 1) / CACHED_EXP_STEP) + 1
161161
pow = PowCache[index]
162-
_invariant min_exp <= pow.binary_exp
163-
_invariant pow.binary_exp <= max_exp
164162
return DiyFP.new(pow.significand, pow.binary_exp), pow.decimal_exp.to_i
165163
end
166-
167-
private macro _invariant(exp, file = __FILE__, line = __LINE__)
168-
{% if !flag?(:release) %}
169-
unless {{exp}}
170-
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
171-
end
172-
{% end %}
173-
end
174164
end

‎src/float/printer/diy_fp.cr

-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ struct Float::Printer::DiyFP
5959
#
6060
# This result is not normalized.
6161
def -(other : DiyFP)
62-
_invariant self.exp == other.exp && frac >= other.frac
6362
self.class.new(frac - other.frac, exp)
6463
end
6564

@@ -93,7 +92,6 @@ struct Float::Printer::DiyFP
9392
end
9493

9594
def normalize
96-
_invariant frac != 0
9795
f = frac
9896
e = exp
9997

@@ -117,7 +115,6 @@ struct Float::Printer::DiyFP
117115
end
118116

119117
def self.from_f(d : Float64 | Float32)
120-
_invariant d > 0
121118
frac, exp = IEEE.frac_and_exp(d)
122119
new(frac, exp)
123120
end
@@ -139,12 +136,4 @@ struct Float::Printer::DiyFP
139136
e -= DiyFP::SIGNIFICAND_SIZE - IEEE::SIGNIFICAND_SIZE_64
140137
DiyFP.new(f, e)
141138
end
142-
143-
private macro _invariant(exp, file = __FILE__, line = __LINE__)
144-
{% if !flag?(:release) %}
145-
unless {{exp}}
146-
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
147-
end
148-
{% end %}
149-
end
150139
end

‎src/float/printer/grisu3.cr

-19
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ module Float::Printer::Grisu3
125125
# Conceptually rest ~= too_high - buffer
126126
# We need to do the following tests in this order to avoid over- and
127127
# underflows.
128-
_invariant rest <= unsafe_interval
129128
while (
130129
rest < small_distance && # Negated condition 1
131130
unsafe_interval - rest >= ten_kappa && # Negated condition 2
@@ -199,9 +198,6 @@ module Float::Printer::Grisu3
199198
# imprecision.
200199
def digit_gen(low : DiyFP, w : DiyFP, high : DiyFP, buffer_p) : {Bool, Int32, Int32}
201200
buffer = buffer_p.to_slice(128)
202-
_invariant low.exp == w.exp && w.exp == high.exp
203-
_invariant low.frac + 1 <= high.frac - 1
204-
_invariant CachedPowers::MIN_TARGET_EXP <= w.exp && w.exp <= CachedPowers::MAX_TARGET_EXP
205201
# low, w and high are imprecise, but by less than one ulp (unit in the last
206202
# place).
207203
# If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
@@ -245,7 +241,6 @@ module Float::Printer::Grisu3
245241
while kappa > 0
246242
digit = integrals / divisor
247243
# pp [digit, kappa]
248-
_invariant digit <= 9
249244
buffer[length] = 48_u8 + digit
250245
length += 1
251246
integrals %= divisor
@@ -273,15 +268,11 @@ module Float::Printer::Grisu3
273268
# data (like the interval or 'unit'), too.
274269
# Note that the multiplication by 10 does not overflow, because w.e >= -60
275270
# and thus one.e >= -60.
276-
_invariant one.exp >= -60
277-
_invariant fractionals < one.frac
278-
_invariant 0xFFFFFFFFFFFFFFFF / 10 >= one.frac
279271
loop do
280272
fractionals *= 10
281273
unit *= 10
282274
unsafe_interval = DiyFP.new(unsafe_interval.frac * 10, unsafe_interval.exp)
283275
digit = (fractionals >> -one.exp).to_i
284-
_invariant digit <= 9
285276
buffer[length] = 48_u8 + digit
286277
length += 1
287278
fractionals &= one.frac - 1
@@ -317,7 +308,6 @@ module Float::Printer::Grisu3
317308
# boundary_minus and boundary_plus will round to v when convert to a float.
318309
# Grisu3 will never output representations that lie exactly on a boundary.
319310
boundaries = IEEE.normalized_boundaries(v)
320-
_invariant boundaries[:plus].exp == w.exp
321311

322312
ten_mk, mk = CachedPowers.get_cached_power_for_binary_exponent(w.exp)
323313

@@ -331,7 +321,6 @@ module Float::Printer::Grisu3
331321
# In other words: let f = scaled_w.f() and e = scaled_w.e(), then
332322
# (f-1) * 2^e < w*10^k < (f+1) * 2^e
333323
scaled_w = w * ten_mk
334-
_invariant scaled_w.exp == boundaries[:plus].exp + ten_mk.exp + DiyFP::SIGNIFICAND_SIZE
335324

336325
# In theory it would be possible to avoid some recomputations by computing
337326
# the difference between w and boundary_minus/plus (a power of 2) and to
@@ -352,12 +341,4 @@ module Float::Printer::Grisu3
352341
decimal_exponent = -mk + kappa
353342
return result, decimal_exponent, length
354343
end
355-
356-
private macro _invariant(exp, file = __FILE__, line = __LINE__)
357-
{% if !flag?(:release) %}
358-
unless {{exp}}
359-
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
360-
end
361-
{% end %}
362-
end
363344
end

‎src/float/printer/ieee.cr

-12
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ module Float::Printer::IEEE
9393
# exponent as m_plus.
9494
# Precondition: the value encoded by this Flaot must be greater than 0.
9595
def normalized_boundaries(v : Float64)
96-
_invariant v > 0
9796
w = DiyFP.from_f(v)
9897
m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize
9998

@@ -122,7 +121,6 @@ module Float::Printer::IEEE
122121
end
123122

124123
def normalized_boundaries(v : Float32)
125-
_invariant v > 0
126124
w = DiyFP.from_f(v)
127125
m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize
128126

@@ -144,7 +142,6 @@ module Float::Printer::IEEE
144142

145143
def frac_and_exp(v : Float64)
146144
d64 = to_uint(v)
147-
_invariant (d64 & EXPONENT_MASK_64) != EXPONENT_MASK_64
148145

149146
if (d64 & EXPONENT_MASK_64) == 0 # denormal float
150147
frac = d64 & SIGNIFICAND_MASK_64
@@ -159,7 +156,6 @@ module Float::Printer::IEEE
159156

160157
def frac_and_exp(v : Float32)
161158
d32 = to_uint(v)
162-
_invariant (d32 & EXPONENT_MASK_32) != EXPONENT_MASK_32
163159

164160
if (d32 & EXPONENT_MASK_32) == 0 # denormal float
165161
frac = d32 & SIGNIFICAND_MASK_32
@@ -191,12 +187,4 @@ module Float::Printer::IEEE
191187
baised_e = ((d32 & EXPONENT_MASK_32) >> PHYSICAL_SIGNIFICAND_SIZE_32).to_i
192188
baised_e - EXPONENT_BIAS_32
193189
end
194-
195-
private macro _invariant(exp, file = __FILE__, line = __LINE__)
196-
{% if !flag?(:release) %}
197-
unless {{exp}}
198-
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
199-
end
200-
{% end %}
201-
end
202190
end

0 commit comments

Comments
 (0)
Please sign in to comment.