schneiderfelipe / unrolled Goto Github PK
View Code? Open in Web Editor NEW🧻 Unroll for-loops at compile-time.
Home Page: http://schneiderfelipe.xyz/unrolled/
License: MIT License
🧻 Unroll for-loops at compile-time.
Home Page: http://schneiderfelipe.xyz/unrolled/
License: MIT License
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.
This affects these examples, for instance
Lines 48 to 79 in a18ab5a
Again (see #1), all three tests pass if we accept an untyped
parameter in
Lines 117 to 118 in a18ab5a
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
The following test
Lines 21 to 28 in a18ab5a
unroll
is defined to accept an untyped
parameter, but gives the wrong result for typed
, as defined belowLines 117 to 118 in a18ab5a
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"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.