Explorar el Código

vxor(): xor operations among vectors.

Tests included.
Michele Orrù hace 11 años
padre
commit
65843deb55

+ 2 - 0
src/questions/include/qstrings.h

@@ -3,4 +3,6 @@
 
 int is_vzero(const void *v, size_t len);
 
+void vxor(void *u, const void *v, const void *w, size_t len);
+
 #endif /* _QA_QSTRINGS_H_ */

+ 20 - 1
src/questions/qstrings.c

@@ -9,12 +9,31 @@
 #include "qa/questions/qstrings.h"
 
 
+/**
+ * \brief xor operations among vectors.
+ *
+ * Compute the xor operation for len bytes, between v and w, and places the result in u.
+ * Note: u can be any of v, w.
+ */
+void
+vxor(void *void_u, const void *void_v, const void *void_w, size_t len)
+{
+  char unsigned *u = (char unsigned *) void_u;
+  char unsigned *v = (char unsigned *) void_v;
+  char unsigned *w = (char unsigned *) void_w;
+
+  while (len--)
+    *(u++) = *(v++) ^ *(w++);
+}
+
+
 /**
  * \brief Check v the first len bits of v are filled with zeroes
  *
  * \return true if the first len bits of v are zero, false otherwise.
  */
-int is_vzero(const void *v, size_t len)
+int
+is_vzero(const void *v, size_t len)
 {
   char unsigned *s = (char unsigned *) v;
   while (len--)

+ 21 - 0
src/questions/tests/test_qstrings.c

@@ -12,8 +12,29 @@ void test_is_vzero(void)
   assert(!is_vzero(v, 6));
 }
 
+
+void test_vxor(void)
+{
+  size_t i;
+  char v[10] = "\0\1\0\1\0\1\0\1\0\1";
+  char w[10] = "\1\0\1\0\1\0\1\0\1\0";
+
+  vxor(v, v, w, 10);
+  assert(v[0] == 1);
+  assert(v[1] == 1);
+
+  vxor(v, v, w, 10);
+  assert(v[0] == 0);
+  vxor(v, v, v, 10);
+
+  for (i=0; i!=10; i++)
+    assert(v[i] == 0);
+
+}
 int main(int argc, char **argv)
 {
   test_is_vzero();
+  test_vxor();
+
   return 0;
 }