gpujs / gpujs/gpu.js

Using `continue` in a loop has inconsistent behavior.

Open
#611 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
15.5k
Forks
663
PR merge metrics
No merged PRs in 30d

Description

![A GIF or MEME to give some spice of the internet](url)

## *What* is wrong?

`continue` acts as `break` when loop is based off an input variable

## *Where* does it happen?

In a kernel or added function, where JS is interpreted, within a loop.

## *How* do we replicate the issue?

A - fixed iteration (i < 4), with or without `continue`, expected behavior
```
k = gpu.createKernel(function() {
var a = 0
for(var i = 0; i < 4; i++){
if(i == this.thread.x) { continue }
else { a += i }
}
return a
}).setOutput([4])
console.log(k())
VM3228:9 Float32Array(4) [6, 5, 4, 3]
```
B - variable iteration (i < n), with `continue`, completely unexpected behavior
```
k = gpu.createKernel(function(n) {
var a = 0
for(var i = 0; i < n; i++){
if(i == this.thread.x) { continue }
else { a += i }
}
return a
}).setOutput([4])
console.log(k(4))
VM3217:9 Float32Array(4) [0, 0, 1, 3]
undefined
```
C - variable iteration (i < n) without `continue`, a valid workaround
```
k = gpu.createKernel(function(n) {
var a = 0
for(var i = 0; i < n; i++){
if(i == this.thread.x) { }
else { a += i }
}
return a
}).setOutput([4])
console.log(k(4))
VM3207:9 Float32Array(4) [6, 5, 4, 3]
undefined
```

## *How* important is this (1-5)?

5

## Expected behavior (i.e. solution)

I would expect scenarios A, B, and C to behave identically.

## Other Comments

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.