1 /*
2 **  OSSP ui64 - 64-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 ui64, a 64-bit arithmetic library
7 **  which can be found at http://www.ossp.org/pkg/lib/ui64/.
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 **  ui64.h: API declaration
28 */
29 
30 #ifndef __UI64_H__
31 #define __UI64_H__
32 
33 #include <string.h>
34 
35 #define UI64_PREFIX uuid_
36 
37 /* embedding support */
38 #ifdef UI64_PREFIX
39 #if defined(__STDC__) || defined(__cplusplus)
40 #define __UI64_CONCAT(x,y) x ## y
41 #define UI64_CONCAT(x,y) __UI64_CONCAT(x,y)
42 #else
43 #define __UI64_CONCAT(x) x
44 #define UI64_CONCAT(x,y) __UI64_CONCAT(x)y
45 #endif
46 #define ui64_t     UI64_CONCAT(UI64_PREFIX,ui64_t)
47 #define ui64_zero  UI64_CONCAT(UI64_PREFIX,ui64_zero)
48 #define ui64_max   UI64_CONCAT(UI64_PREFIX,ui64_max)
49 #define ui64_n2i   UI64_CONCAT(UI64_PREFIX,ui64_n2i)
50 #define ui64_i2n   UI64_CONCAT(UI64_PREFIX,ui64_i2n)
51 #define ui64_s2i   UI64_CONCAT(UI64_PREFIX,ui64_s2i)
52 #define ui64_i2s   UI64_CONCAT(UI64_PREFIX,ui64_i2s)
53 #define ui64_add   UI64_CONCAT(UI64_PREFIX,ui64_add)
54 #define ui64_addn  UI64_CONCAT(UI64_PREFIX,ui64_addn)
55 #define ui64_sub   UI64_CONCAT(UI64_PREFIX,ui64_sub)
56 #define ui64_subn  UI64_CONCAT(UI64_PREFIX,ui64_subn)
57 #define ui64_mul   UI64_CONCAT(UI64_PREFIX,ui64_mul)
58 #define ui64_muln  UI64_CONCAT(UI64_PREFIX,ui64_muln)
59 #define ui64_div   UI64_CONCAT(UI64_PREFIX,ui64_div)
60 #define ui64_divn  UI64_CONCAT(UI64_PREFIX,ui64_divn)
61 #define ui64_and   UI64_CONCAT(UI64_PREFIX,ui64_and)
62 #define ui64_or    UI64_CONCAT(UI64_PREFIX,ui64_or)
63 #define ui64_xor   UI64_CONCAT(UI64_PREFIX,ui64_xor)
64 #define ui64_not   UI64_CONCAT(UI64_PREFIX,ui64_not)
65 #define ui64_rol   UI64_CONCAT(UI64_PREFIX,ui64_rol)
66 #define ui64_ror   UI64_CONCAT(UI64_PREFIX,ui64_ror)
67 #define ui64_len   UI64_CONCAT(UI64_PREFIX,ui64_len)
68 #define ui64_cmp   UI64_CONCAT(UI64_PREFIX,ui64_cmp)
69 #endif
70 
71 typedef struct {
72     unsigned char x[8]; /* x_0, ..., x_7 */
73 } ui64_t;
74 
75 #define ui64_cons(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 
78 /* particular values */
79 extern ui64_t        ui64_zero (void);
80 extern ui64_t        ui64_max  (void);
81 
82 /* import and export via ISO-C "unsigned long" */
83 extern ui64_t        ui64_n2i  (unsigned long n);
84 extern unsigned long ui64_i2n  (ui64_t x);
85 
86 /* import and export via ISO-C string of arbitrary base */
87 extern ui64_t        ui64_s2i  (const char *str, char **end, int base);
88 extern char *        ui64_i2s  (ui64_t x, char *str, size_t len, int base);
89 
90 /* arithmetical operations */
91 extern ui64_t        ui64_add  (ui64_t x, ui64_t y, ui64_t *ov);
92 extern ui64_t        ui64_addn (ui64_t x, int    y, int    *ov);
93 extern ui64_t        ui64_sub  (ui64_t x, ui64_t y, ui64_t *ov);
94 extern ui64_t        ui64_subn (ui64_t x, int    y, int    *ov);
95 extern ui64_t        ui64_mul  (ui64_t x, ui64_t y, ui64_t *ov);
96 extern ui64_t        ui64_muln (ui64_t x, int    y, int    *ov);
97 extern ui64_t        ui64_div  (ui64_t x, ui64_t y, ui64_t *ov);
98 extern ui64_t        ui64_divn (ui64_t x, int    y, int    *ov);
99 
100 /* bit operations */
101 extern ui64_t        ui64_and  (ui64_t x, ui64_t y);
102 extern ui64_t        ui64_or   (ui64_t x, ui64_t y);
103 extern ui64_t        ui64_xor  (ui64_t x, ui64_t y);
104 extern ui64_t        ui64_not  (ui64_t x);
105 extern ui64_t        ui64_rol  (ui64_t x, int s, ui64_t *ov);
106 extern ui64_t        ui64_ror  (ui64_t x, int s, ui64_t *ov);
107 
108 /* other operations */
109 extern int           ui64_len  (ui64_t x);
110 extern int           ui64_cmp  (ui64_t x, ui64_t y);
111 
112 #endif /* __UI64_H__ */
113 
114