symm/ocb1.h, symm/pmac1.h, ...: Implement PMAC1 and OCB1.
[catacomb] / symm / ocb.c
diff --git a/symm/ocb.c b/symm/ocb.c
new file mode 100644 (file)
index 0000000..1c97215
--- /dev/null
@@ -0,0 +1,73 @@
+/* -*-c-*-
+ *
+ * Common definitions for OCB and related modes
+ *
+ * (c) 2018 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Library General Public License as published
+ * by the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb.  If not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ * USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "config.h"
+
+#include "ocb.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @ocb_ctz@, @ocb_ctzl@ --- *
+ *
+ * Arguments:  @unsigned i@ or @unsigned long i@ = operand, assumed nonzero
+ *
+ * Returns:    The number of trailing zero bits in @i@, or nonsense if
+ *             %$i = 0$%.
+ */
+
+unsigned ocb_ctz(unsigned i)
+{
+  unsigned n = 0;
+
+  if (!(i&0x00ff)) { n +=  8; i >>=  8; }
+  if (!(i&0x000f)) { n +=  4; i >>=  4; }
+  if (!(i&0x0003)) { n +=  2; i >>=  2; }
+  if (!(i&0x0001)) { n +=  1; i >>=  1; }
+  return (n);
+}
+
+unsigned ocb_ctzl(unsigned long i)
+{
+  unsigned n = 0;
+
+#if ULONG_BITS > 64
+  while (!(i&0xfffffffffffffffful)) { n += 64; i >>= 64; }
+#endif
+#if ULONG_BITS > 32
+  if (!(i&0xffffffff)) { n += 32; i >>= 32; }
+#endif
+  if (!(i&0xffff)) { n += 16; i >>= 16; }
+  if (!(i&0x00ff)) { n +=  8; i >>=  8; }
+  if (!(i&0x000f)) { n +=  4; i >>=  4; }
+  if (!(i&0x0003)) { n +=  2; i >>=  2; }
+  if (!(i&0x0001)) { n +=  1; i >>=  1; }
+  return (n);
+}
+
+/*----- That's all, folks -------------------------------------------------*/