Skip to content

Commit

Permalink
Test RANGE variables used as pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc committed Sep 11, 2024
1 parent f332ec5 commit 421e692
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/usecases/verbatim/internal_function.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NEURON {
NEURON {
SUFFIX internal_function
THREADSAFE
}
Expand Down
36 changes: 36 additions & 0 deletions test/usecases/verbatim/pointer_in_double.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
NEURON {
SUFFIX pointer_in_double
THREADSAFE
}

ASSIGNED {
ptr
}

VERBATIM
struct SomeDouble {
SomeDouble(double _value) : _value(_value) {}

double value() const {
return _value;
}

double _value;
};

static std::vector<SomeDouble> some_doubles;
ENDVERBATIM

INITIAL {
VERBATIM
some_doubles.reserve(10);
some_doubles.push_back(SomeDouble(double(some_doubles.size())));
*reinterpret_cast<SomeDouble**>(&ptr) = &some_doubles.back();
ENDVERBATIM
}

FUNCTION use_pointer() {
VERBATIM
return (*reinterpret_cast<SomeDouble**>(&ptr))->value();
ENDVERBATIM
}
22 changes: 22 additions & 0 deletions test/usecases/verbatim/test_pointer_in_double.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from neuron import h, gui

# Since a double is 64-bits and a pointer is also 32 or 64-bits, we can store
# the bits of a pointer in the memory that was allocated for use as doubles.
# Concretely, we, using VERBATIM, safe pointers in ASSIGNED or RANGE variables.
#
# The pattern is found in the builtin mod file: `apcount.mod`.

def test_pointer_in_double():
s = h.Section()
s.nseg = 3
s.insert("pointer_in_double")

h.finitialize()
for i, x in enumerate([0.25, 0.5, 0.75]):
actual = s(x).pointer_in_double.use_pointer()
expected = i
assert actual == expected, f"{actual} != {expected}"


if __name__ == "__main__":
test_pointer_in_double()

0 comments on commit 421e692

Please sign in to comment.