1 /* 2 ** OSSP ui128 - 128-Bit Arithmetic 3 ** Copyright (c) 2002-2005 Ralf S. Engelschall <rse@engelschall.com> 4 ** Copyright (c) 2002-2005 The OSSP Project <http://www.ossp.org/> 5 ** 6 ** This file is part of OSSP ui128, a 128-bit arithmetic library 7 ** which can be found at http://www.ossp.org/pkg/lib/ui128/. 8 ** 9 ** Permission to use, copy, modify, and distribute this software for 10 ** any purpose with or without fee is hereby granted, provided that 11 ** the above copyright notice and this permission notice appear in all 12 ** copies. 13 ** 14 ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 15 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR 18 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 21 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 24 ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 ** SUCH DAMAGE. 26 ** 27 ** ui128.h: API declaration 28 */ 29 30 #ifndef __UI128_H__ 31 #define __UI128_H__ 32 33 #include <string.h> 34 35 #define UI128_PREFIX uuid_ 36 37 /* embedding support */ 38 #ifdef UI128_PREFIX 39 #if defined(__STDC__) || defined(__cplusplus) 40 #define __UI128_CONCAT(x,y) x ## y 41 #define UI128_CONCAT(x,y) __UI128_CONCAT(x,y) 42 #else 43 #define __UI128_CONCAT(x) x 44 #define UI128_CONCAT(x,y) __UI128_CONCAT(x)y 45 #endif 46 #define ui128_t UI128_CONCAT(UI128_PREFIX,ui128_t) 47 #define ui128_zero UI128_CONCAT(UI128_PREFIX,ui128_zero) 48 #define ui128_max UI128_CONCAT(UI128_PREFIX,ui128_max) 49 #define ui128_n2i UI128_CONCAT(UI128_PREFIX,ui128_n2i) 50 #define ui128_i2n UI128_CONCAT(UI128_PREFIX,ui128_i2n) 51 #define ui128_s2i UI128_CONCAT(UI128_PREFIX,ui128_s2i) 52 #define ui128_i2s UI128_CONCAT(UI128_PREFIX,ui128_i2s) 53 #define ui128_add UI128_CONCAT(UI128_PREFIX,ui128_add) 54 #define ui128_addn UI128_CONCAT(UI128_PREFIX,ui128_addn) 55 #define ui128_sub UI128_CONCAT(UI128_PREFIX,ui128_sub) 56 #define ui128_subn UI128_CONCAT(UI128_PREFIX,ui128_subn) 57 #define ui128_mul UI128_CONCAT(UI128_PREFIX,ui128_mul) 58 #define ui128_muln UI128_CONCAT(UI128_PREFIX,ui128_muln) 59 #define ui128_div UI128_CONCAT(UI128_PREFIX,ui128_div) 60 #define ui128_divn UI128_CONCAT(UI128_PREFIX,ui128_divn) 61 #define ui128_and UI128_CONCAT(UI128_PREFIX,ui128_and) 62 #define ui128_or UI128_CONCAT(UI128_PREFIX,ui128_or) 63 #define ui128_xor UI128_CONCAT(UI128_PREFIX,ui128_xor) 64 #define ui128_not UI128_CONCAT(UI128_PREFIX,ui128_not) 65 #define ui128_rol UI128_CONCAT(UI128_PREFIX,ui128_rol) 66 #define ui128_ror UI128_CONCAT(UI128_PREFIX,ui128_ror) 67 #define ui128_len UI128_CONCAT(UI128_PREFIX,ui128_len) 68 #define ui128_cmp UI128_CONCAT(UI128_PREFIX,ui128_cmp) 69 #endif 70 71 typedef struct { 72 unsigned char x[16]; /* x_0, ..., x_15 */ 73 } ui128_t; 74 75 #define ui128_cons(x15,x14,x13,x12,x11,x10,x9,x8,x7,x6,x5,x4,x3,x2,x1,x0) \ 76 { { 0x##x0, 0x##x1, 0x##x2, 0x##x3, 0x##x4, 0x##x5, 0x##x6, 0x##x7, \ 77 { { 0x##x8, 0x##x9, 0x##x10, 0x##x11, 0x##x12, 0x##x13, 0x##x14, 0x##x15 } } 78 79 /* particular values */ 80 extern ui128_t ui128_zero (void); 81 extern ui128_t ui128_max (void); 82 83 /* import and export via ISO-C "unsigned long" */ 84 extern ui128_t ui128_n2i (unsigned long n); 85 extern unsigned long ui128_i2n (ui128_t x); 86 87 /* import and export via ISO-C string of arbitrary base */ 88 extern ui128_t ui128_s2i (const char *str, char **end, int base); 89 extern char * ui128_i2s (ui128_t x, char *str, size_t len, int base); 90 91 /* arithmetical operations */ 92 extern ui128_t ui128_add (ui128_t x, ui128_t y, ui128_t *ov); 93 extern ui128_t ui128_addn (ui128_t x, int y, int *ov); 94 extern ui128_t ui128_sub (ui128_t x, ui128_t y, ui128_t *ov); 95 extern ui128_t ui128_subn (ui128_t x, int y, int *ov); 96 extern ui128_t ui128_mul (ui128_t x, ui128_t y, ui128_t *ov); 97 extern ui128_t ui128_muln (ui128_t x, int y, int *ov); 98 extern ui128_t ui128_div (ui128_t x, ui128_t y, ui128_t *ov); 99 extern ui128_t ui128_divn (ui128_t x, int y, int *ov); 100 101 /* bit operations */ 102 extern ui128_t ui128_and (ui128_t x, ui128_t y); 103 extern ui128_t ui128_or (ui128_t x, ui128_t y); 104 extern ui128_t ui128_xor (ui128_t x, ui128_t y); 105 extern ui128_t ui128_not (ui128_t x); 106 extern ui128_t ui128_rol (ui128_t x, int s, ui128_t *ov); 107 extern ui128_t ui128_ror (ui128_t x, int s, ui128_t *ov); 108 109 /* other operations */ 110 extern int ui128_len (ui128_t x); 111 extern int ui128_cmp (ui128_t x, ui128_t y); 112 113 #endif /* __UI128_H__ */ 114 115