-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test RANGE variables used as pointers.
- Loading branch information
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
NEURON { | ||
NEURON { | ||
SUFFIX internal_function | ||
THREADSAFE | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |