Debian Codegraph @

bwa-apache2/utils.h

raw
1 /* The MIT License
2
3 Copyright (c) 2018- Dana-Farber Cancer Institute
4 2009-2018 Broad Institute, Inc.
5 2008-2009 Genome Research Ltd. (GRL)
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 */
27 #ifndef LH3_UTILS_H
28 #define LH3_UTILS_H
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <zlib.h>
33
34 #ifdef __GNUC__
35 // Tell GCC to validate printf format string and args
36 #define ATTRIBUTE(list) __attribute__ (list)
37 #else
38 #define ATTRIBUTE(list)
39 #endif
40
41 #define err_fatal_simple(msg) _err_fatal_simple(__func__, msg)
42 #define err_fatal_simple_core(msg) _err_fatal_simple_core(__func__, msg)
43
44 #define xopen(fn, mode) err_xopen_core(__func__, fn, mode)
45 #define xreopen(fn, mode, fp) err_xreopen_core(__func__, fn, mode, fp)
46 #define xzopen(fn, mode) err_xzopen_core(__func__, fn, mode)
47
48 #define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg)
49
50 typedef struct {
51 uint64_t x, y;
52 } pair64_t;
53
54 typedef struct { size_t n, m; uint64_t *a; } uint64_v;
55 typedef struct { size_t n, m; pair64_t *a; } pair64_v;
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60
61 void err_fatal(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn));
62 void err_fatal_core(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn));
63 void _err_fatal_simple(const char *func, const char *msg) ATTRIBUTE((noreturn));
64 void _err_fatal_simple_core(const char *func, const char *msg) ATTRIBUTE((noreturn));
65 FILE *err_xopen_core(const char *func, const char *fn, const char *mode);
66 FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp);
67 gzFile err_xzopen_core(const char *func, const char *fn, const char *mode);
68 size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
69 size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream);
70
71 int err_gzread(gzFile file, void *ptr, unsigned int len);
72 int err_fseek(FILE *stream, long offset, int whence);
73 #define err_rewind(FP) err_fseek((FP), 0, SEEK_SET)
74 long err_ftell(FILE *stream);
75 int err_fprintf(FILE *stream, const char *format, ...)
76 ATTRIBUTE((format(printf, 2, 3)));
77 int err_printf(const char *format, ...)
78 ATTRIBUTE((format(printf, 1, 2)));
79 int err_fputc(int c, FILE *stream);
80 #define err_putchar(C) err_fputc((C), stdout)
81 int err_fputs(const char *s, FILE *stream);
82 int err_puts(const char *s);
83 int err_fflush(FILE *stream);
84 int err_fclose(FILE *stream);
85 int err_gzclose(gzFile file);
86
87 double cputime(void);
88 double realtime(void);
89 long peakrss(void);
90
91 void ks_introsort_64 (size_t n, uint64_t *a);
92 void ks_introsort_128(size_t n, pair64_t *a);
93
94 #ifdef __cplusplus
95 }
96 #endif
97
98 static inline uint64_t hash_64(uint64_t key)
99 {
100 key += ~(key << 32);
101 key ^= (key >> 22);
102 key += ~(key << 13);
103 key ^= (key >> 8);
104 key += (key << 3);
105 key ^= (key >> 15);
106 key += ~(key << 27);
107 key ^= (key >> 31);
108 return key;
109 }
110
111 #endif