-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require C++11 and use
alignof()
to fix alignment issues (#149)
* Try requiring C11 * Implement `aligned_allocate()` and `aligned_void_deref()` These allow using `Rf_allocVector()` to allocate a RAWSXP with some buffer padding, along with a dereferencing helper to get an aligned pointer to the block of memory * Switch to C++11, since we know that is supported This now uses a single C++11 compilation unit to get access to `alignof()`, which we call on the two types we need it for, `long double` and our custom `struct mean_state_t`. Forward declaring it in `summary-core.h` is enough to give us access to those C++ functions from C. We forward declare rather than including `summary-core-align.hpp`, as that will attempt to compile `extern "C"` with a C compiler, which won't work. * NEWS bullet
- Loading branch information
1 parent
8cc244a
commit 2e597d7
Showing
13 changed files
with
171 additions
and
40 deletions.
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
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
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,38 @@ | ||
#ifndef SLIDER_ALIGN_H | ||
#define SLIDER_ALIGN_H | ||
|
||
/* | ||
* Following guidance of: | ||
* https://stackoverflow.com/questions/227897/how-to-allocate-aligned-memory-only-using-the-standard-library | ||
* | ||
* 1) Allocate enough space to shift the pointer | ||
* 2) Add to the pointer (p_x + buffer) | ||
* 3) Round down to the closest boundary using `& mask` | ||
*/ | ||
|
||
#include "slider.h" | ||
#include <stdint.h> // uintptr_t | ||
|
||
static | ||
inline | ||
SEXP | ||
aligned_allocate(R_xlen_t n_elements, | ||
size_t element_size, | ||
size_t element_align) { | ||
const size_t buffer = element_align - 1; | ||
const R_xlen_t size = n_elements * element_size + buffer; | ||
return Rf_allocVector(RAWSXP, size); | ||
} | ||
|
||
static | ||
inline | ||
void* | ||
aligned_void_deref(SEXP x, size_t element_align) { | ||
const size_t buffer = element_align - 1; | ||
uintptr_t mask = ~ (uintptr_t)buffer; | ||
uintptr_t p_x = (uintptr_t)RAW(x); | ||
uintptr_t p_aligned = (p_x + buffer) & mask; | ||
return (void*) p_aligned; | ||
} | ||
|
||
#endif |
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
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
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,21 @@ | ||
#include "summary-core-align.h" | ||
|
||
extern "C" { | ||
|
||
/* | ||
* `alignof()` is C++11 specific, so this single compilation unit requires | ||
* C++11, and we call these helpers from C in `summary-core.h`. | ||
* | ||
* Technically `alignof()` is also in C11, but it is unclear how well R supports | ||
* that. | ||
*/ | ||
|
||
size_t align_of_long_double() { | ||
return alignof(long double); | ||
} | ||
|
||
size_t align_of_mean_state_t() { | ||
return alignof(struct mean_state_t); | ||
} | ||
|
||
} // extern "C" |
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,15 @@ | ||
#ifndef SLIDER_SUMMARY_CORE_ALIGN | ||
#define SLIDER_SUMMARY_CORE_ALIGN | ||
|
||
#include <stddef.h> // size_t | ||
|
||
extern "C" { | ||
|
||
#include "summary-core-types.h" | ||
|
||
size_t align_of_long_double(); | ||
size_t align_of_mean_state_t(); | ||
|
||
} // extern "C" | ||
|
||
#endif |
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,11 @@ | ||
#ifndef SLIDER_SUMMARY_CORE_TYPES | ||
#define SLIDER_SUMMARY_CORE_TYPES | ||
|
||
#include <stdint.h> // uintptr_t | ||
|
||
struct mean_state_t { | ||
long double sum; | ||
uint64_t count; | ||
}; | ||
|
||
#endif |
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
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
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
Oops, something went wrong.