Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fusion/pointers: add pointer arithmetic operators #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
- Added `jssets` module, Set for the JavaScript target
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
- Added `jsheaders` module for [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers) for the JavaScript target.
- Added module `pointers` containing `toUncheckedArray` and pointer arithmetic operators and indexing
34 changes: 34 additions & 0 deletions src/fusion/pointers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,37 @@ proc toUncheckedArray*[T](a: ptr T): ptr UncheckedArray[T] {.inline.} =
pa[0] += 5
doAssert a[1] == 105
cast[ptr UncheckedArray[T]](a)

template `+`*[T](p: ptr T, off: int): ptr T =
## Unsafe.
runnableExamples:
var a = @[10, 11, 12]
let pa = a[0].addr
doAssert (pa + 1)[] == 11
doAssert pa[2] == 12
pa[1] = 2
doAssert a[1] == 2
type T = typeof(p[]) # pending https://github.com/nim-lang/Nim/issues/13527
cast[ptr T](cast[ByteAddress](p) +% off * sizeof(T))
Copy link
Member

@Araq Araq Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my own low level code I never needed the * sizeof(T) part. It's not intuitive and probably error-prone. Why try to outsmart the programmer who chose to operate on a very low level for a reason?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


template `-`*[T](p: ptr T, off: int): ptr T =
## Unsafe.
type T = typeof(p[])
cast[ptr T](cast[ByteAddress](p) -% off * sizeof(T))

template `[]`*[T](p: ptr T, off: int): T =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single pointer is not an array.

Copy link
Member Author

@timotheecour timotheecour Oct 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about this; slightly more verbose than [], []= but still short enough:

# `[]` =>
template `at`*[T](p: ptr T, off: int): T = ...
# `[]=` =>
template `at=`*[T](p: ptr T, off: int, val: T) = ...

it would still allow this:

echo p.at(1)
p.at(2) = 2
p.at(2) -= 3

(at naming precedent: http://www.cplusplus.com/reference/vector/vector/at/ or https://riptutorial.com/opencv/example/6394/access-individual-pixel-values-with-cv--mat--at-t---)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have the toUncheckedArray proc to offer array indexing.

Copy link
Member Author

@timotheecour timotheecour Oct 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toUncheckedArray is useful but not as a replacement for that; this module is about convenience when dealing with low-level code so you use cast less often and make the intent clearer

p[1]
vs
p.toUncheckedArray[1]

That's why it's been implemented in so many places (with more or less good implementations eg many implementations have issues eg are not safe wrt multiple template argument evaluation bugs) and requested also in many places as shown here #21 (comment)

Among other things, it makes it in particular easy to adapt C/C++ code to nim with the simplest possible syntax.

## Unsafe.
(p + off)[]

template `[]=`*[T](p: ptr T, off: int, val: T) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single pointer is not an array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## Unsafe.
(p + off)[] = val

proc `+=`*[T](p: var ptr T, off: int) {.inline.} =
## Unsafe.
# not a template to avoid double evaluation issues
p = p + off

proc `-=`*[T](p: var ptr T, off: int) {.inline.} =
## Unsafe.
p = p - off
19 changes: 19 additions & 0 deletions tests/tpointers.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fusion/pointers

block:
var a = @[10, 11, 12]
let pa = a[0].addr
let pb = pa + 1
doAssert pb[] == 11
doAssert (pb - 1)[] == 10
pa[] = 100
doAssert a[0] == 100
doAssert pa[1] == 11

var pc = pa
pc += 1
doAssert pc[] == 11
doAssert pc[0] == 11
doAssert pc == pb
pc -= 1
doAssert pc == pa