Skip to content

Commit

Permalink
Remove _invariant macro from Float::Printer::* modules (#5435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and RX14 committed Jan 5, 2018
1 parent 1d52841 commit 315d3c3
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 52 deletions.
10 changes: 0 additions & 10 deletions src/float/printer/cached_powers.cr
Expand Up @@ -159,16 +159,6 @@ module Float::Printer::CachedPowers
k = ((min_exp + DiyFP::SIGNIFICAND_SIZE - 1) * D_1_LOG2_10).ceil
index = ((CACHED_POWER_OFFSET + k.to_i - 1) / CACHED_EXP_STEP) + 1
pow = PowCache[index]
_invariant min_exp <= pow.binary_exp
_invariant pow.binary_exp <= max_exp
return DiyFP.new(pow.significand, pow.binary_exp), pow.decimal_exp.to_i
end

private macro _invariant(exp, file = __FILE__, line = __LINE__)
{% if !flag?(:release) %}
unless {{exp}}
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
end
{% end %}
end
end
11 changes: 0 additions & 11 deletions src/float/printer/diy_fp.cr
Expand Up @@ -59,7 +59,6 @@ struct Float::Printer::DiyFP
#
# This result is not normalized.
def -(other : DiyFP)
_invariant self.exp == other.exp && frac >= other.frac
self.class.new(frac - other.frac, exp)
end

Expand Down Expand Up @@ -93,7 +92,6 @@ struct Float::Printer::DiyFP
end

def normalize
_invariant frac != 0
f = frac
e = exp

Expand All @@ -117,7 +115,6 @@ struct Float::Printer::DiyFP
end

def self.from_f(d : Float64 | Float32)
_invariant d > 0
frac, exp = IEEE.frac_and_exp(d)
new(frac, exp)
end
Expand All @@ -139,12 +136,4 @@ struct Float::Printer::DiyFP
e -= DiyFP::SIGNIFICAND_SIZE - IEEE::SIGNIFICAND_SIZE_64
DiyFP.new(f, e)
end

private macro _invariant(exp, file = __FILE__, line = __LINE__)
{% if !flag?(:release) %}
unless {{exp}}
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
end
{% end %}
end
end
19 changes: 0 additions & 19 deletions src/float/printer/grisu3.cr
Expand Up @@ -125,7 +125,6 @@ module Float::Printer::Grisu3
# Conceptually rest ~= too_high - buffer
# We need to do the following tests in this order to avoid over- and
# underflows.
_invariant rest <= unsafe_interval
while (
rest < small_distance && # Negated condition 1
unsafe_interval - rest >= ten_kappa && # Negated condition 2
Expand Down Expand Up @@ -199,9 +198,6 @@ module Float::Printer::Grisu3
# imprecision.
def digit_gen(low : DiyFP, w : DiyFP, high : DiyFP, buffer_p) : {Bool, Int32, Int32}
buffer = buffer_p.to_slice(128)
_invariant low.exp == w.exp && w.exp == high.exp
_invariant low.frac + 1 <= high.frac - 1
_invariant CachedPowers::MIN_TARGET_EXP <= w.exp && w.exp <= CachedPowers::MAX_TARGET_EXP
# low, w and high are imprecise, but by less than one ulp (unit in the last
# place).
# If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
Expand Down Expand Up @@ -245,7 +241,6 @@ module Float::Printer::Grisu3
while kappa > 0
digit = integrals / divisor
# pp [digit, kappa]
_invariant digit <= 9
buffer[length] = 48_u8 + digit
length += 1
integrals %= divisor
Expand Down Expand Up @@ -273,15 +268,11 @@ module Float::Printer::Grisu3
# data (like the interval or 'unit'), too.
# Note that the multiplication by 10 does not overflow, because w.e >= -60
# and thus one.e >= -60.
_invariant one.exp >= -60
_invariant fractionals < one.frac
_invariant 0xFFFFFFFFFFFFFFFF / 10 >= one.frac
loop do
fractionals *= 10
unit *= 10
unsafe_interval = DiyFP.new(unsafe_interval.frac * 10, unsafe_interval.exp)
digit = (fractionals >> -one.exp).to_i
_invariant digit <= 9
buffer[length] = 48_u8 + digit
length += 1
fractionals &= one.frac - 1
Expand Down Expand Up @@ -317,7 +308,6 @@ module Float::Printer::Grisu3
# boundary_minus and boundary_plus will round to v when convert to a float.
# Grisu3 will never output representations that lie exactly on a boundary.
boundaries = IEEE.normalized_boundaries(v)
_invariant boundaries[:plus].exp == w.exp

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

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

# In theory it would be possible to avoid some recomputations by computing
# the difference between w and boundary_minus/plus (a power of 2) and to
Expand All @@ -352,12 +341,4 @@ module Float::Printer::Grisu3
decimal_exponent = -mk + kappa
return result, decimal_exponent, length
end

private macro _invariant(exp, file = __FILE__, line = __LINE__)
{% if !flag?(:release) %}
unless {{exp}}
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
end
{% end %}
end
end
12 changes: 0 additions & 12 deletions src/float/printer/ieee.cr
Expand Up @@ -93,7 +93,6 @@ module Float::Printer::IEEE
# exponent as m_plus.
# Precondition: the value encoded by this Flaot must be greater than 0.
def normalized_boundaries(v : Float64)
_invariant v > 0
w = DiyFP.from_f(v)
m_plus = DiyFP.new((w.frac << 1) + 1, w.exp - 1).normalize

Expand Down Expand Up @@ -122,7 +121,6 @@ module Float::Printer::IEEE
end

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

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

def frac_and_exp(v : Float64)
d64 = to_uint(v)
_invariant (d64 & EXPONENT_MASK_64) != EXPONENT_MASK_64

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

def frac_and_exp(v : Float32)
d32 = to_uint(v)
_invariant (d32 & EXPONENT_MASK_32) != EXPONENT_MASK_32

if (d32 & EXPONENT_MASK_32) == 0 # denormal float
frac = d32 & SIGNIFICAND_MASK_32
Expand Down Expand Up @@ -191,12 +187,4 @@ module Float::Printer::IEEE
baised_e = ((d32 & EXPONENT_MASK_32) >> PHYSICAL_SIGNIFICAND_SIZE_32).to_i
baised_e - EXPONENT_BIAS_32
end

private macro _invariant(exp, file = __FILE__, line = __LINE__)
{% if !flag?(:release) %}
unless {{exp}}
raise "Assertion Failed #{{{file}}}:#{{{line}}}"
end
{% end %}
end
end

0 comments on commit 315d3c3

Please sign in to comment.