Implement mj_stackAlloc and mj_arenaAlloc alignment using bitwise-and.

This implementation explicitly avoids calling division instructions (which are generally expensive) even when link-time optimization is not active or if the alloc functions aren't inlined for some reason.

Also make mj_stackAlloc cheaper for external callers who may be linking against MuJoCo dynamically (and therefore cannot benefit from inlining).

PiperOrigin-RevId: 561299565
Change-Id: Iea4be5bf6d351ddf0c9d1fd8bd89a0c207b39309
This commit is contained in:
Saran Tunyasuvunakool
2023-08-30 04:28:17 -07:00
committed by Copybara-Service
parent 4c899b92cc
commit 215433f5f7
5 changed files with 14 additions and 7 deletions
+2 -2
View File
@@ -102,10 +102,10 @@ MJAPI void mj_resetDataDebug(const mjModel* m, mjData* d, unsigned char debug_va
// reset data, set fields from specified keyframe
MJAPI void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key);
// mjData arena allocate
// mjData arena allocate (alignment must be a power of 2)
MJAPI void* mj_arenaAlloc(mjData* d, size_t bytes, size_t alignment);
// mjData stack allocate
// mjData stack allocate (alignment must be a power of 2)
MJAPI void* mj_stackAlloc(mjData* d, size_t bytes, size_t alignment);
// mjData stack allocate for array of mjtNums