Git Product home page Git Product logo

unrolled's Issues

Support unroll factors

This can be done without information on loop length.

proc searchChar(s: string, c: char): int =
  unroll(4): for i in 0 .. s.high:
    if s[i] == c: return i
  result = -1

should produce something like

proc searchChar(s: string, c: char): int =
  for i in countup(0, s.high, 4):  # unroll factor goes here
    if s[i] == c: return i
    if s[i+1] == c: return i+1
    if s[i+2] == c: return i+2
    if s[i+3] == c: return i+3

  let n = ((s.high - 0) div 4) * 4 + 0  # we can do better here
  for i in n..s.high:  # eventually left out elements
    if s[i] == c: return i
  result = -1

We can even employ a default value for when the loop length is not known in advance.

Can't nest loops even when only one of them is unrolled

This affects these examples, for instance

unrolled/tests/tests.nim

Lines 48 to 79 in a18ab5a

# BUG: !!!
# suite "Nested for-loops over slices":
# test "unroll works in inner nested loops":
# expandMacros:
# var total: int
# for i in 1..3:
# unroll for j in 1..3:
# total += i + j
# check total == (1 + 1) + (1 + 2) + (1 + 3) +
# (2 + 1) + (2 + 2) + (2 + 3) +
# (3 + 1) + (3 + 2) + (3 + 3)
# test "unroll works in outer nested loops":
# expandMacros:
# var total: int
# unroll for i in 1..3:
# for j in 1..3:
# total += i + j
# check total == (1 + 1) + (1 + 2) + (1 + 3) +
# (2 + 1) + (2 + 2) + (2 + 3) +
# (3 + 1) + (3 + 2) + (3 + 3)
# test "unroll works with multiple nested loops":
# expandMacros:
# var total: int
# unroll:
# for i in 1..3:
# for j in 1..3:
# total += i + j
# check total == (1 + 1) + (1 + 2) + (1 + 3) +
# (2 + 1) + (2 + 2) + (2 + 3) +
# (3 + 1) + (3 + 2) + (3 + 3)

Again (see #1), all three tests pass if we accept an untyped parameter in

unrolled/src/unrolled.nim

Lines 117 to 118 in a18ab5a

macro unroll*(x: typed): auto =
## Unroll for-loops.

Below the expanded code for the first example:

var total: int
block:
  for j in 1 .. 3:
    total += 1 + j
block:
  for j in 1 .. 3:
    total += 2 + j
block:
  for j in 1 .. 3:
    total += 3 + j

Wrong answer in a simple test when receiving a typed parameter

The following test

unrolled/tests/tests.nim

Lines 21 to 28 in a18ab5a

# BUG: !!!
# test "unroll works when variables are defined within the loop":
# expandMacros:
# var x: int
# unroll for i in 1..3:
# var j = i + 1
# x += j
# check x == (1 + 1) + (2 + 1) + (3 + 1)

works if unroll is defined to accept an untyped parameter, but gives the wrong result for typed, as defined below

unrolled/src/unrolled.nim

Lines 117 to 118 in a18ab5a

macro unroll*(x: typed): auto =
## Unroll for-loops.

Below the expanded macro for this example:

var x: int
block:
  var j = 2
  x += j
block:
  var j = 3
  x += j
block:
  var j = 4
  x += j

The problem seems to be that j doesn't get updated, stays as 2 the whole loop, and x is equal to 2 + 2 + 2 at the end.
By inspecting what is going on inside the loop, we see that i assumes values 2, 4, 6. But I can't figure out why from the treeRepr below

StmtList
  BlockStmt
    Empty
    StmtList
      VarSection
        IdentDefs
          Sym "j"
          Empty
          Infix
            Sym "+"
            IntLit 1
            IntLit 1
      Infix
        Sym "+="
        Sym "x"
        Sym "j"
  BlockStmt
    Empty
    StmtList
      VarSection
        IdentDefs
          Sym "j"
          Empty
          Infix
            Sym "+"
            IntLit 2
            IntLit 1
      Infix
        Sym "+="
        Sym "x"
        Sym "j"
  BlockStmt
    Empty
    StmtList
      VarSection
        IdentDefs
          Sym "j"
          Empty
          Infix
            Sym "+"
            IntLit 3
            IntLit 1
      Infix
        Sym "+="
        Sym "x"
        Sym "j"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.