2011-01-14 09:52:12 +08:00
|
|
|
/* The MIT License
|
|
|
|
|
|
2020-07-02 11:02:01 +08:00
|
|
|
Copyright (c) 2018- Dana-Farber Cancer Institute
|
|
|
|
|
2009-2018 Broad Institute, Inc.
|
|
|
|
|
2008-2009 Genome Research Ltd. (GRL)
|
2011-01-14 09:52:12 +08:00
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
|
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef LH3_UTILS_H
|
|
|
|
|
#define LH3_UTILS_H
|
|
|
|
|
|
2013-02-12 22:50:28 +08:00
|
|
|
#include <stdint.h>
|
2011-01-14 09:52:12 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <zlib.h>
|
2024-06-24 18:11:54 +08:00
|
|
|
#include <immintrin.h>
|
|
|
|
|
#include "profiling.h"
|
2023-12-25 13:58:15 +08:00
|
|
|
|
2024-02-23 01:09:08 +08:00
|
|
|
#undef MAX
|
|
|
|
|
#undef MIN
|
|
|
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
|
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
|
|
2024-06-24 18:11:54 +08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
#define LIKELY(x) __builtin_expect((x), 1)
|
|
|
|
|
#define UNLIKELY(x) __builtin_expect((x), 0)
|
|
|
|
|
#else
|
|
|
|
|
#define LIKELY(x) (x)
|
|
|
|
|
#define UNLIKELY(x) (x)
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-05-04 21:46:50 +08:00
|
|
|
#ifdef __GNUC__
|
|
|
|
|
// Tell GCC to validate printf format string and args
|
|
|
|
|
#define ATTRIBUTE(list) __attribute__ (list)
|
|
|
|
|
#else
|
|
|
|
|
#define ATTRIBUTE(list)
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-16 18:05:32 +08:00
|
|
|
#define err_fatal_simple(msg) _err_fatal_simple(__func__, msg)
|
|
|
|
|
#define err_fatal_simple_core(msg) _err_fatal_simple_core(__func__, msg)
|
2013-03-01 17:37:46 +08:00
|
|
|
|
2011-01-14 09:52:12 +08:00
|
|
|
#define xopen(fn, mode) err_xopen_core(__func__, fn, mode)
|
2011-01-14 09:54:10 +08:00
|
|
|
#define xreopen(fn, mode, fp) err_xreopen_core(__func__, fn, mode, fp)
|
2011-01-14 09:52:12 +08:00
|
|
|
#define xzopen(fn, mode) err_xzopen_core(__func__, fn, mode)
|
2012-12-16 18:05:32 +08:00
|
|
|
|
|
|
|
|
#define xassert(cond, msg) if ((cond) == 0) _err_fatal_simple_core(__func__, msg)
|
|
|
|
|
|
2024-03-24 04:40:09 +08:00
|
|
|
#if defined(__GNUC__) && __GNUC__ < 11 && !defined(__clang__)
|
|
|
|
|
#if defined(__i386__)
|
|
|
|
|
static inline unsigned long long __rdtsc(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned long long int x;
|
|
|
|
|
__asm__ volatile(".byte 0x0f, 0x31" : "=A"(x));
|
|
|
|
|
return x;
|
|
|
|
|
}
|
|
|
|
|
#elif defined(__x86_64__)
|
|
|
|
|
static inline unsigned long long __rdtsc(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned hi, lo;
|
|
|
|
|
__asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi));
|
|
|
|
|
return ((unsigned long long)lo) | (((unsigned long long)hi) << 32);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-08-03 17:26:32 +08:00
|
|
|
#define log_file(fd, M, ...) \
|
|
|
|
|
fprintf(fd, M "\n", ##__VA_ARGS__); \
|
|
|
|
|
fflush(fd)
|
|
|
|
|
|
2013-02-12 22:50:28 +08:00
|
|
|
typedef struct {
|
|
|
|
|
uint64_t x, y;
|
|
|
|
|
} pair64_t;
|
|
|
|
|
|
|
|
|
|
typedef struct { size_t n, m; uint64_t *a; } uint64_v;
|
2024-04-02 07:42:37 +08:00
|
|
|
typedef struct { size_t n, m; uint32_t *a; } uint32_v;
|
2013-02-12 22:50:28 +08:00
|
|
|
typedef struct { size_t n, m; pair64_t *a; } pair64_v;
|
|
|
|
|
|
2024-03-07 18:23:21 +08:00
|
|
|
typedef struct { size_t m; uint8_t *addr; } buf_t;
|
|
|
|
|
|
2011-01-14 09:52:12 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-12-16 18:05:32 +08:00
|
|
|
void err_fatal(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn));
|
|
|
|
|
void err_fatal_core(const char *header, const char *fmt, ...) ATTRIBUTE((noreturn));
|
|
|
|
|
void _err_fatal_simple(const char *func, const char *msg) ATTRIBUTE((noreturn));
|
|
|
|
|
void _err_fatal_simple_core(const char *func, const char *msg) ATTRIBUTE((noreturn));
|
2011-01-14 09:52:12 +08:00
|
|
|
FILE *err_xopen_core(const char *func, const char *fn, const char *mode);
|
2011-01-14 09:54:10 +08:00
|
|
|
FILE *err_xreopen_core(const char *func, const char *fn, const char *mode, FILE *fp);
|
2011-01-14 09:52:12 +08:00
|
|
|
gzFile err_xzopen_core(const char *func, const char *fn, const char *mode);
|
2011-05-04 21:46:50 +08:00
|
|
|
size_t err_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
|
2012-12-16 18:05:32 +08:00
|
|
|
size_t err_fread_noeof(void *ptr, size_t size, size_t nmemb, FILE *stream);
|
2024-02-22 01:26:57 +08:00
|
|
|
uint64_t fread_fix(FILE *fp, uint64_t size, void *a);
|
2012-12-16 18:05:32 +08:00
|
|
|
|
|
|
|
|
int err_gzread(gzFile file, void *ptr, unsigned int len);
|
|
|
|
|
int err_fseek(FILE *stream, long offset, int whence);
|
|
|
|
|
#define err_rewind(FP) err_fseek((FP), 0, SEEK_SET)
|
|
|
|
|
long err_ftell(FILE *stream);
|
2011-05-04 21:46:50 +08:00
|
|
|
int err_fprintf(FILE *stream, const char *format, ...)
|
|
|
|
|
ATTRIBUTE((format(printf, 2, 3)));
|
|
|
|
|
int err_printf(const char *format, ...)
|
|
|
|
|
ATTRIBUTE((format(printf, 1, 2)));
|
2013-01-09 22:43:36 +08:00
|
|
|
int err_fputc(int c, FILE *stream);
|
|
|
|
|
#define err_putchar(C) err_fputc((C), stdout)
|
|
|
|
|
int err_fputs(const char *s, FILE *stream);
|
2014-08-26 23:07:24 +08:00
|
|
|
int err_puts(const char *s);
|
2011-05-04 21:46:50 +08:00
|
|
|
int err_fflush(FILE *stream);
|
|
|
|
|
int err_fclose(FILE *stream);
|
2013-01-04 00:57:37 +08:00
|
|
|
int err_gzclose(gzFile file);
|
2011-01-14 09:52:12 +08:00
|
|
|
|
2021-02-23 12:26:03 +08:00
|
|
|
double cputime(void);
|
|
|
|
|
double realtime(void);
|
2024-03-24 04:40:09 +08:00
|
|
|
uint64_t realtime_msec(void);
|
2021-02-23 12:26:03 +08:00
|
|
|
long peakrss(void);
|
2011-11-13 05:50:58 +08:00
|
|
|
|
2013-02-12 22:50:28 +08:00
|
|
|
void ks_introsort_64 (size_t n, uint64_t *a);
|
|
|
|
|
void ks_introsort_128(size_t n, pair64_t *a);
|
|
|
|
|
|
2024-08-03 17:26:32 +08:00
|
|
|
int memcpy_bwamem(void *dest, size_t dmax, const void *src, size_t smax, char *file_name, int line_num);
|
|
|
|
|
|
2011-01-14 09:52:12 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-02-13 01:09:36 +08:00
|
|
|
static inline uint64_t hash_64(uint64_t key)
|
|
|
|
|
{
|
|
|
|
|
key += ~(key << 32);
|
|
|
|
|
key ^= (key >> 22);
|
|
|
|
|
key += ~(key << 13);
|
|
|
|
|
key ^= (key >> 8);
|
|
|
|
|
key += (key << 3);
|
|
|
|
|
key ^= (key >> 15);
|
|
|
|
|
key += ~(key << 27);
|
|
|
|
|
key ^= (key >> 31);
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-14 09:52:12 +08:00
|
|
|
#endif
|