From 18fccd316bd640023b8391b7392d77afa50c7009 Mon Sep 17 00:00:00 2001
From: Martin Larralde <martin.larralde@embl.de>
Date: Sun, 16 Jun 2024 19:45:53 +0200
Subject: [PATCH] Fix incorrect alignment handling in `DenseMatrix`

---
 lightmotif/src/dense.rs | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lightmotif/src/dense.rs b/lightmotif/src/dense.rs
index edae74d..9ec9c98 100644
--- a/lightmotif/src/dense.rs
+++ b/lightmotif/src/dense.rs
@@ -87,7 +87,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C,
 
         // compute offset to aligned memory
         m.offset = 0;
-        while m.data[m.offset..].as_ptr() as usize % c > 0 {
+        while m.data[m.offset..].as_ptr() as usize & (A::USIZE - 1) > 0 {
             m.offset += 1
         }
 
@@ -168,7 +168,7 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> DenseMatrix<T, C,
             self.data.resize_with((rows + 1) * c, T::default);
             // Compute offset to aligned memory
             self.offset = 0;
-            while self.data[self.offset..].as_ptr() as usize % c > 0 {
+            while self.data[self.offset..].as_ptr() as usize & (A::USIZE - 1) > 0 {
                 self.offset += 1
             }
             // Copy data in case alignment offset changed
@@ -232,7 +232,9 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> Index<usize>
         let c = self.stride();
         let row = self.offset + c * index;
         debug_assert!(row + C::USIZE <= self.data.len());
-        &self.data[row..row + C::USIZE]
+        let row = &self.data[row..row + C::USIZE];
+        debug_assert_eq!(row.as_ptr() as usize & (A::USIZE - 1), 0);
+        row
     }
 }
 
@@ -244,7 +246,9 @@ impl<T: Default + Copy, C: Unsigned, A: Unsigned + PowerOfTwo> IndexMut<usize>
         let c = self.stride();
         let row = self.offset + c * index;
         debug_assert!(row + C::USIZE <= self.data.len());
-        &mut self.data[row..row + C::USIZE]
+        let row = &mut self.data[row..row + C::USIZE];
+        debug_assert_eq!(row.as_ptr() as usize & (A::USIZE - 1), 0);
+        row
     }
 }
 
-- 
GitLab