| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Common definitions for OCB and related modes |
| 4 | * |
| 5 | * (c) 2018 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 12 | * Catacomb is free software: you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU Library General Public License as published |
| 14 | * by the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * Catacomb is distributed in the hope that it will be useful, but |
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with Catacomb. If not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 25 | * USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include "config.h" |
| 31 | |
| 32 | #include "ocb.h" |
| 33 | |
| 34 | /*----- Main code ---------------------------------------------------------*/ |
| 35 | |
| 36 | /* --- @ocb_ctz@, @ocb_ctzl@ --- * |
| 37 | * |
| 38 | * Arguments: @unsigned i@ or @unsigned long i@ = operand, assumed nonzero |
| 39 | * |
| 40 | * Returns: The number of trailing zero bits in @i@, or nonsense if |
| 41 | * %$i = 0$%. |
| 42 | */ |
| 43 | |
| 44 | unsigned ocb_ctz(unsigned i) |
| 45 | { |
| 46 | unsigned n = 0; |
| 47 | |
| 48 | if (!(i&0x00ff)) { n += 8; i >>= 8; } |
| 49 | if (!(i&0x000f)) { n += 4; i >>= 4; } |
| 50 | if (!(i&0x0003)) { n += 2; i >>= 2; } |
| 51 | if (!(i&0x0001)) { n += 1; i >>= 1; } |
| 52 | return (n); |
| 53 | } |
| 54 | |
| 55 | unsigned ocb_ctzl(unsigned long i) |
| 56 | { |
| 57 | unsigned n = 0; |
| 58 | |
| 59 | #if ULONG_BITS > 64 |
| 60 | while (!(i&0xfffffffffffffffful)) { n += 64; i >>= 64; } |
| 61 | #endif |
| 62 | #if ULONG_BITS > 32 |
| 63 | if (!(i&0xffffffff)) { n += 32; i >>= 32; } |
| 64 | #endif |
| 65 | if (!(i&0xffff)) { n += 16; i >>= 16; } |
| 66 | if (!(i&0x00ff)) { n += 8; i >>= 8; } |
| 67 | if (!(i&0x000f)) { n += 4; i >>= 4; } |
| 68 | if (!(i&0x0003)) { n += 2; i >>= 2; } |
| 69 | if (!(i&0x0001)) { n += 1; i >>= 1; } |
| 70 | return (n); |
| 71 | } |
| 72 | |
| 73 | /*----- That's all, folks -------------------------------------------------*/ |