summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorArne Döring <arne.doering@gmx.net>2019-08-27 23:18:46 +0200
committerAndreas Rumpf <rumpf_a@web.de>2019-08-27 23:18:46 +0200
commitd564130a3b2596161847d695329ad009e693358a (patch)
tree14ced0de16093871d709b6170a2018bbee897d19
parenteff0837ff40e4a5f5659ff02a56d9936bcbd7bcd (diff)
downloadNim-d564130a3b2596161847d695329ad009e693358a.tar.gz
Fix to int to biggest int (#12066)
* fix to(Biggest)Int
* kill toFloat magics as well
-rw-r--r--compiler/ast.nim4
-rw-r--r--compiler/ccgexprs.nim10
-rw-r--r--compiler/jsgen.nim8
-rw-r--r--compiler/semfold.nim3
-rw-r--r--compiler/sigmatch.nim2
-rw-r--r--compiler/vmgen.nim3
-rw-r--r--lib/nimbase.h12
-rw-r--r--lib/system.nim34
-rw-r--r--tests/system/tsystem_misc.nim15
-rw-r--r--tools/dochack/fuzzysearch.nim16
10 files changed, 43 insertions, 64 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 45c8d5ace..ddf81c874 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -622,8 +622,6 @@ type
     mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot,
     mUnaryPlusI, mBitnotI,
     mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
-    mToFloat, mToBiggestFloat,
-    mToInt, mToBiggestInt,
     mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
     mStrToStr, mEnumToStr,
     mAnd, mOr,
@@ -692,8 +690,6 @@ const
     mEqRef, mEqProc, mEqUntracedRef, mLePtr, mLtPtr, mEqCString, mXor,
     mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI,
     mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
-    mToFloat, mToBiggestFloat,
-    mToInt, mToBiggestInt,
     mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
     mStrToStr, mEnumToStr,
     mAnd, mOr,
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index 6e203dad5..a882a3cee 100644
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -666,14 +666,6 @@ proc unaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
   of mAbsF64:
     applyFormat("($1 < 0? -($1) : ($1))")
     # BUGFIX: fabs() makes problems for Tiny C
-  of mToFloat:
-    applyFormat("((double) ($1))")
-  of mToBiggestFloat:
-    applyFormat("((double) ($1))")
-  of mToInt:
-    applyFormat("float64ToInt32($1)")
-  of mToBiggestInt:
-    applyFormat("float64ToInt64($1)")
   else:
     assert false, $op
 
@@ -2094,7 +2086,7 @@ proc genEnumToStr(p: BProc, e: PNode, d: var TLoc) =
 proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
   case op
   of mOr, mAnd: genAndOr(p, e, d, op)
-  of mNot..mToBiggestInt: unaryArith(p, e, d, op)
+  of mNot..mAbsF64: unaryArith(p, e, d, op)
   of mUnaryMinusI..mAbsI: unaryArithOverflow(p, e, d, op)
   of mAddF64..mDivF64: binaryFloatArith(p, e, d, op)
   of mShrI..mXor: binaryArith(p, e, d, op)
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index f858bd32c..5391a451b 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -431,10 +431,6 @@ const # magic checked op; magic unchecked op;
     ["", ""], # UnaryPlusF64
     ["", ""], # UnaryMinusF64
     ["", ""], # AbsF64
-    ["", ""],     # ToFloat
-    ["", ""],     # ToBiggestFloat
-    ["", ""], # ToInt
-    ["", ""], # ToBiggestInt
     ["nimCharToStr", "nimCharToStr"],
     ["nimBoolToStr", "nimBoolToStr"],
     ["cstrToNimstr", "cstrToNimstr"],
@@ -612,10 +608,6 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
   of mUnaryPlusF64: applyFormat("+($1)", "+($1)")
   of mUnaryMinusF64: applyFormat("-($1)", "-($1)")
   of mAbsF64: applyFormat("Math.abs($1)", "Math.abs($1)")
-  of mToFloat: applyFormat("$1", "$1")
-  of mToBiggestFloat: applyFormat("$1", "$1")
-  of mToInt: applyFormat("Math.trunc($1)", "Math.trunc($1)")
-  of mToBiggestInt: applyFormat("Math.trunc($1)", "Math.trunc($1)")
   of mCharToStr: applyFormat("nimCharToStr($1)", "nimCharToStr($1)")
   of mBoolToStr: applyFormat("nimBoolToStr($1)", "nimBoolToStr($1)")
   of mIntToStr: applyFormat("cstrToNimstr(($1)+\"\")", "cstrToNimstr(($1)+\"\")")
diff --git a/compiler/semfold.nim b/compiler/semfold.nim
index 564d7f067..bfb5077d6 100644
--- a/compiler/semfold.nim
+++ b/compiler/semfold.nim
@@ -196,10 +196,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
     else:
       result = newIntNodeT(toInt128(sonsLen(a)), n, g)
   of mUnaryPlusI, mUnaryPlusF64: result = a # throw `+` away
-  of mToFloat, mToBiggestFloat:
-    result = newFloatNodeT(toFloat64(getInt(a)), n, g)
   # XXX: Hides overflow/underflow
-  of mToInt, mToBiggestInt: result = newIntNodeT(system.toInt(getFloat(a)), n, g)
   of mAbsF64: result = newFloatNodeT(abs(getFloat(a)), n, g)
   of mAbsI: result = foldAbs(getInt(a), n, g)
   of mUnaryLt: result = foldSub(getOrdValue(a), One, n, g)
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 327537595..e4bf13e60 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -873,7 +873,7 @@ proc inferStaticParam*(c: var TCandidate, lhs: PNode, rhs: BiggestInt): bool =
     of mUnaryMinusI:
       return inferStaticParam(c, lhs[1], -rhs)
 
-    of mUnaryPlusI, mToInt, mToBiggestInt:
+    of mUnaryPlusI:
       return inferStaticParam(c, lhs[1], rhs)
 
     else: discard
diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim
index 9cd3b841e..15ddfe496 100644
--- a/compiler/vmgen.nim
+++ b/compiler/vmgen.nim
@@ -1058,8 +1058,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
     let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
     if t.kind in {tyUInt8..tyUInt32} or (t.kind == tyUInt and t.size < 8):
       c.gABC(n, opcNarrowU, dest, TRegister(t.size*8))
-  of mToFloat, mToBiggestFloat, mToInt,
-     mToBiggestInt, mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr,
+  of mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr,
      mFloatToStr, mCStrToStr, mStrToStr, mEnumToStr:
     genConv(c, n, n.sons[1], dest)
   of mEqStr, mEqCString: genBinaryABC(c, n, dest, opcEqStr)
diff --git a/lib/nimbase.h b/lib/nimbase.h
index a4bde0bd4..eadc34f66 100644
--- a/lib/nimbase.h
+++ b/lib/nimbase.h
@@ -374,18 +374,6 @@ typedef char* NCSTRING;
 #  define NIM_IMAN 0
 #endif
 
-static N_INLINE(NI, float64ToInt32)(double x) {
-  /* nowadays no hack necessary anymore */
-  return x >= 0 ? (NI)(x+0.5) : (NI)(x-0.5);
-}
-
-static N_INLINE(NI32, float32ToInt32)(float x) {
-  /* nowadays no hack necessary anymore */
-  return x >= 0 ? (NI32)(x+0.5) : (NI32)(x-0.5);
-}
-
-#define float64ToInt64(x) ((NI64) (x))
-
 #define NIM_STRLIT_FLAG ((NU)(1) << ((NIM_INTBITS) - 2)) /* This has to be the same as system.strlitFlag! */
 
 #define STRING_LITERAL(name, str, length) \
diff --git a/lib/system.nim b/lib/system.nim
index 09280d607..974c39a67 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -1132,57 +1132,57 @@ proc chr*(u: range[0..255]): char {.magic: "Chr", noSideEffect.}
 # built-in operators
 
 when defined(nimNoZeroExtendMagic):
-  proc ze*(x: int8): int =
+  proc ze*(x: int8): int {.deprecated.} =
     ## zero extends a smaller integer type to ``int``. This treats `x` as
     ## unsigned.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
 
     cast[int](uint(cast[uint8](x)))
 
-  proc ze*(x: int16): int =
+  proc ze*(x: int16): int {.deprecated.} =
     ## zero extends a smaller integer type to ``int``. This treats `x` as
     ## unsigned.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int](uint(cast[uint16](x)))
 
-  proc ze64*(x: int8): int64 =
+  proc ze64*(x: int8): int64 {.deprecated.} =
     ## zero extends a smaller integer type to ``int64``. This treats `x` as
     ## unsigned.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int64](uint64(cast[uint8](x)))
 
-  proc ze64*(x: int16): int64 =
+  proc ze64*(x: int16): int64 {.deprecated.} =
     ## zero extends a smaller integer type to ``int64``. This treats `x` as
     ## unsigned.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int64](uint64(cast[uint16](x)))
 
-  proc ze64*(x: int32): int64 =
+  proc ze64*(x: int32): int64 {.deprecated.} =
     ## zero extends a smaller integer type to ``int64``. This treats `x` as
     ## unsigned.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int64](uint64(cast[uint32](x)))
 
-  proc ze64*(x: int): int64 =
+  proc ze64*(x: int): int64 {.deprecated.} =
     ## zero extends a smaller integer type to ``int64``. This treats `x` as
     ## unsigned. Does nothing if the size of an ``int`` is the same as ``int64``.
     ## (This is the case on 64 bit processors.)
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int64](uint64(cast[uint](x)))
 
-  proc toU8*(x: int): int8 =
+  proc toU8*(x: int): int8 {.deprecated.} =
     ## treats `x` as unsigned and converts it to a byte by taking the last 8 bits
     ## from `x`.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int8](x)
 
-  proc toU16*(x: int): int16 =
+  proc toU16*(x: int): int16 {.deprecated.} =
     ## treats `x` as unsigned and converts it to an ``int16`` by taking the last
     ## 16 bits from `x`.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
     cast[int16](x)
 
-  proc toU32*(x: int64): int32 =
+  proc toU32*(x: int64): int32 {.deprecated.} =
     ## treats `x` as unsigned and converts it to an ``int32`` by taking the
     ## last 32 bits from `x`.
     ## **Deprecated since version 0.19.9**: Use unsigned integers instead.
@@ -2281,8 +2281,7 @@ type # these work for most platforms:
   PInt64* = ptr int64        ## An alias for ``ptr int64``.
   PInt32* = ptr int32        ## An alias for ``ptr int32``.
 
-proc toFloat*(i: int): float {.
-  magic: "ToFloat", noSideEffect, importc: "toFloat".}
+proc toFloat*(i: int): float {.noSideEffect, inline.} =
   ## Converts an integer `i` into a ``float``.
   ##
   ## If the conversion fails, `ValueError` is raised.
@@ -2294,13 +2293,13 @@ proc toFloat*(i: int): float {.
   ##     b = 3.7
   ##
   ##   echo a.toFloat + b # => 5.7
+  float(i)
 
-proc toBiggestFloat*(i: BiggestInt): BiggestFloat {.
-  magic: "ToBiggestFloat", noSideEffect, importc: "toBiggestFloat".}
+proc toBiggestFloat*(i: BiggestInt): BiggestFloat {.noSideEffect, inline.} =
   ## Same as `toFloat <#toFloat,int>`_ but for ``BiggestInt`` to ``BiggestFloat``.
+  BiggestFloat(i)
 
-proc toInt*(f: float): int {.
-  magic: "ToInt", noSideEffect, importc: "toInt".}
+proc toInt*(f: float): int {.noSideEffect.} =
   ## Converts a floating point number `f` into an ``int``.
   ##
   ## Conversion rounds `f` half away from 0, see
@@ -2314,10 +2313,11 @@ proc toInt*(f: float): int {.
   ##   doAssert toInt(0.49) == 0
   ##   doAssert toInt(0.5) == 1
   ##   doAssert toInt(-0.5) == -1 # rounding is symmetrical
+  if f >= 0: int(f+0.5) else: int(f-0.5)
 
-proc toBiggestInt*(f: BiggestFloat): BiggestInt {.
-  magic: "ToBiggestInt", noSideEffect, importc: "toBiggestInt".}
+proc toBiggestInt*(f: BiggestFloat): BiggestInt {.noSideEffect.} =
   ## Same as `toInt <#toInt,float>`_ but for ``BiggestFloat`` to ``BiggestInt``.
+  if f >= 0: BiggestInt(f+0.5) else: BiggestInt(f-0.5)
 
 proc addQuitProc*(quitProc: proc() {.noconv.}) {.
   importc: "atexit", header: "<stdlib.h>".}
diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim
index a20e6b3bf..9dcd9ac9f 100644
--- a/tests/system/tsystem_misc.nim
+++ b/tests/system/tsystem_misc.nim
@@ -159,3 +159,18 @@ block: # `$`*[T: tuple|object](x: T)
     x2:float
   doAssert $Foo(x:2) == "(x: 2, x2: 0.0)"
   doAssert $() == "()"
+
+
+# this is a call indirection to prevent `toInt` to be resolved at compile time.
+proc testToInt(arg: float64, a: int, b: BiggestInt) =
+  doAssert toInt(arg) == a
+  doAssert toBiggestInt(arg) == b
+
+testToInt(0.45, 0, 0)    # should round towards 0
+testToInt(-0.45, 0, 0)   # should round towards 0
+testToInt(0.5, 1, 1)     # should round away from 0
+testToInt(-0.5, -1, -1)  # should round away from 0
+testToInt(13.37, 13, 13)    # should round towards 0
+testToInt(-13.37, -13, -13) # should round towards 0
+testToInt(7.8, 8, 8)     # should round away from 0
+testToInt(-7.8, -8, -8)  # should round away from 0
diff --git a/tools/dochack/fuzzysearch.nim b/tools/dochack/fuzzysearch.nim
index 69f9fce3c..40575b998 100644
--- a/tools/dochack/fuzzysearch.nim
+++ b/tools/dochack/fuzzysearch.nim
@@ -17,8 +17,8 @@ const
   ## This is to weight function signatures and descriptions over module titles.
 
 
-type 
-  ScoreCard = enum 
+type
+  ScoreCard = enum
     StartMatch           = -100 ## Start matching.
     LeadingCharDiff      = -3   ## An unmatched, leading character was found.
     CharDiff             = -1   ## An unmatched character was found.
@@ -58,19 +58,19 @@ proc fuzzyMatch*(pattern, str: cstring) : tuple[score: int, matched: bool] =
     if strChar in {'_', ' ', '.'}:
       strIndex += 1
       continue
-    
+
     # Since this algorithm will be used to search against Nim documentation,
     # the below logic prioritizes headers.
     if not headerMatched and strChar == ':':
       headerMatched = true
       scoreState = StartMatch
-      score = toInt(floor(HeadingScaleFactor * toFloat(score)))
+      score = int(floor(HeadingScaleFactor * float(score)))
       patIndex = 0
       strIndex += 1
       continue
 
     if strChar == patternChar:
-      case scoreState 
+      case scoreState
       of StartMatch, WordBoundryMatch:
         scoreState = LeadingCharMatch
 
@@ -84,7 +84,7 @@ proc fuzzyMatch*(pattern, str: cstring) : tuple[score: int, matched: bool] =
 
         if scoreState == LeadingCharMatch:
           score += ord(LeadingCharMatch)
-          
+
         var onBoundary = (patIndex == high(pattern))
         if not onBoundary and strIndex < high(str):
           let
@@ -95,7 +95,7 @@ proc fuzzyMatch*(pattern, str: cstring) : tuple[score: int, matched: bool] =
             nextStrChar notin {'a'..'z'} and
             nextStrChar != nextPatternChar
           )
-        
+
         if onBoundary:
           transition(WordBoundryMatch)
 
@@ -115,7 +115,7 @@ proc fuzzyMatch*(pattern, str: cstring) : tuple[score: int, matched: bool] =
       patIndex += 1
 
     else:
-      case scoreState 
+      case scoreState
       of StartMatch:
         transition(LeadingCharDiff)