2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Moinak Ghosh. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2012-07-07 16:48:29 +00:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2012-05-28 14:49:29 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
|
|
|
*
|
|
|
|
* This program includes partly-modified public domain source
|
|
|
|
* code from the LZMA SDK: http://www.7-zip.org/sdk.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _UTILS_H
|
|
|
|
#define _UTILS_H
|
|
|
|
|
|
|
|
#include <arpa/nameser_compat.h>
|
|
|
|
#include <sys/types.h>
|
2012-08-15 14:43:40 +00:00
|
|
|
#include <stdint.h>
|
2012-10-21 15:33:07 +00:00
|
|
|
#include <inttypes.h>
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2012-07-10 14:44:23 +00:00
|
|
|
#define DATA_TEXT 1
|
|
|
|
#define DATA_BINARY 2
|
|
|
|
|
2012-05-31 12:31:34 +00:00
|
|
|
#if !defined(sun) && !defined(__sun)
|
2012-05-28 14:49:29 +00:00
|
|
|
#define uchar_t u_char
|
2012-05-31 12:31:34 +00:00
|
|
|
#endif
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
#if ULONG_MAX == 4294967295UL
|
|
|
|
# ifndef UINT64_C
|
|
|
|
# define UINT64_C(n) n ## ULL
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifndef UINT64_C
|
|
|
|
# define UINT64_C(n) n ## UL
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
typedef unsigned long uintptr_t;
|
2012-12-09 04:45:06 +00:00
|
|
|
typedef int64_t bsize_t;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
#undef WORDS_BIGENDIAN
|
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
|
|
|
# define WORDS_BIGENDIAN
|
|
|
|
# ifndef htonll
|
|
|
|
# define htonll(x) (x)
|
|
|
|
# endif
|
|
|
|
# ifndef ntonll
|
|
|
|
# define ntohll(x) (x)
|
|
|
|
# endif
|
|
|
|
#else
|
2012-05-31 12:31:34 +00:00
|
|
|
# if !defined(sun) && !defined (__sun)
|
2012-05-28 14:49:29 +00:00
|
|
|
# ifndef htonll
|
|
|
|
# define htonll(x) __bswap_64(x)
|
|
|
|
# endif
|
|
|
|
# ifndef ntohll
|
|
|
|
# define ntohll(x) __bswap_64(x)
|
|
|
|
# endif
|
2012-05-31 12:31:34 +00:00
|
|
|
# endif
|
2012-05-28 14:49:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// These allow helping the compiler in some often-executed branches, whose
|
|
|
|
// result is almost always the same.
|
|
|
|
#ifdef __GNUC__
|
|
|
|
# define likely(expr) __builtin_expect(expr, 1)
|
|
|
|
# define unlikely(expr) __builtin_expect(expr, 0)
|
|
|
|
# define ATOMIC_ADD(var, val) __sync_fetch_and_add(&var, val)
|
|
|
|
# define ATOMIC_SUB(var, val) __sync_fetch_and_sub(&var, val)
|
|
|
|
#else
|
|
|
|
# define likely(expr) (expr)
|
|
|
|
# define unlikely(expr) (expr)
|
|
|
|
# if defined(sun) || defined (__sun)
|
|
|
|
# include <atomic.h>
|
|
|
|
# define ATOMIC_ADD(var, val) atomic_add_int(&var, val)
|
|
|
|
# define ATOMIC_SUB(var, val) atomic_add_int(&var, -val)
|
|
|
|
# else
|
|
|
|
// Dunno what to do
|
|
|
|
# define ATOMIC_ADD(var, val) var += val
|
|
|
|
# define ATOMIC_SUB(var, val) var -= val
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-07-10 14:44:23 +00:00
|
|
|
#define ISP2(x) ((x != 0) && ((x & (~x + 1)) == x))
|
|
|
|
|
2012-08-15 14:43:40 +00:00
|
|
|
#ifdef DEBUG_STATS
|
|
|
|
#define DEBUG_STAT_EN(...) __VA_ARGS__;
|
|
|
|
#else
|
|
|
|
#define DEBUG_STAT_EN(...)
|
|
|
|
#endif
|
|
|
|
|
2012-12-22 19:14:56 +00:00
|
|
|
#define BYTES_TO_MB(x) ((x) / (1024 * 1024))
|
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t buf_extra;
|
|
|
|
int compress_mt_capable;
|
|
|
|
int decompress_mt_capable;
|
2012-08-27 16:21:55 +00:00
|
|
|
int single_chunk_mt_capable;
|
|
|
|
int is_single_chunk;
|
2012-08-18 04:50:52 +00:00
|
|
|
int nthreads;
|
|
|
|
int c_max_threads;
|
|
|
|
int d_max_threads;
|
2012-12-13 15:48:16 +00:00
|
|
|
int delta2_span;
|
2012-08-18 04:50:52 +00:00
|
|
|
} algo_props_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
COMPRESS_THREADS = 1,
|
|
|
|
DECOMPRESS_THREADS
|
|
|
|
} algo_threads_type_t;
|
|
|
|
|
2012-10-06 15:21:23 +00:00
|
|
|
typedef enum {
|
|
|
|
PROC_BIGENDIAN_GENERIC = 1,
|
|
|
|
PROC_LITENDIAN_GENERIC,
|
|
|
|
PROC_X64_INTEL,
|
|
|
|
PROC_X64_AMD
|
|
|
|
} proc_type_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int sse_level;
|
|
|
|
int avx_level;
|
|
|
|
proc_type_t proc_type;
|
|
|
|
} processor_info_t;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
extern void err_exit(int show_errno, const char *format, ...);
|
|
|
|
extern const char *get_execname(const char *);
|
2012-12-09 04:45:06 +00:00
|
|
|
extern int parse_numeric(int64_t *val, const char *str);
|
2012-05-31 16:06:33 +00:00
|
|
|
extern char *bytes_to_size(uint64_t bytes);
|
2012-12-09 04:45:06 +00:00
|
|
|
extern int64_t Read(int fd, void *buf, uint64_t count);
|
|
|
|
extern int64_t Read_Adjusted(int fd, uchar_t *buf, uint64_t count,
|
|
|
|
int64_t *rabin_count, void *ctx);
|
|
|
|
extern int64_t Write(int fd, const void *buf, uint64_t count);
|
2012-08-18 04:50:52 +00:00
|
|
|
extern void set_threadcounts(algo_props_t *props, int *nthreads, int nprocs,
|
|
|
|
algo_threads_type_t typ);
|
2012-10-17 18:02:35 +00:00
|
|
|
extern uint64_t get_total_ram();
|
2012-12-22 19:14:56 +00:00
|
|
|
extern double get_wtime_millis(void);
|
|
|
|
extern double get_mb_s(uint64_t bytes, double strt, double en);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
|
|
|
/* Pointer type for compress and decompress functions. */
|
2012-12-09 04:45:06 +00:00
|
|
|
typedef int (*compress_func_ptr)(void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *destlen, int level, uchar_t chdr, void *data);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-11-22 15:32:50 +00:00
|
|
|
typedef enum {
|
|
|
|
COMPRESS,
|
|
|
|
DECOMPRESS
|
|
|
|
} compress_op_t;
|
|
|
|
|
2012-07-01 16:14:02 +00:00
|
|
|
/* Pointer type for algo specific init/deinit/stats functions. */
|
2012-12-09 04:45:06 +00:00
|
|
|
typedef int (*init_func_ptr)(void **data, int *level, int nthreads, int64_t chunksize,
|
2012-11-22 15:32:50 +00:00
|
|
|
int file_version, compress_op_t op);
|
2012-07-01 16:14:02 +00:00
|
|
|
typedef int (*deinit_func_ptr)(void **data);
|
|
|
|
typedef void (*stats_func_ptr)(int show);
|
2012-12-09 04:45:06 +00:00
|
|
|
typedef void (*props_func_ptr)(algo_props_t *data, int level, int64_t chunksize);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Roundup v to the nearest power of 2. From Bit Twiddling Hacks:
|
|
|
|
* http://graphics.stanford.edu/~seander/bithacks.html
|
|
|
|
*/
|
|
|
|
static inline unsigned int
|
|
|
|
roundup_pow_two(unsigned int v) {
|
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
v++;
|
|
|
|
return (v);
|
|
|
|
}
|
|
|
|
|
2012-07-29 09:32:51 +00:00
|
|
|
/*
|
|
|
|
* Hash function for 64Bit pointers/numbers that generates
|
|
|
|
* a 32Bit hash value.
|
|
|
|
* Taken from Thomas Wang's Integer hashing paper:
|
|
|
|
* http://www.cris.com/~Ttwang/tech/inthash.htm
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
hash6432shift(uint64_t key)
|
|
|
|
{
|
|
|
|
key = (~key) + (key << 18); // key = (key << 18) - key - 1;
|
|
|
|
key = key ^ (key >> 31);
|
|
|
|
key = key * 21; // key = (key + (key << 2)) + (key << 4);
|
|
|
|
key = key ^ (key >> 11);
|
|
|
|
key = key + (key << 6);
|
|
|
|
key = key ^ (key >> 22);
|
|
|
|
return (uint32_t) key;
|
|
|
|
}
|
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
static void
|
|
|
|
init_algo_props(algo_props_t *props)
|
|
|
|
{
|
|
|
|
props->buf_extra = 0;
|
|
|
|
props->compress_mt_capable = 0;
|
|
|
|
props->decompress_mt_capable = 0;
|
2012-08-27 16:21:55 +00:00
|
|
|
props->single_chunk_mt_capable = 0;
|
|
|
|
props->is_single_chunk = 0;
|
2012-08-18 04:50:52 +00:00
|
|
|
props->nthreads = 1;
|
|
|
|
props->c_max_threads = 1;
|
|
|
|
props->d_max_threads = 1;
|
2012-12-13 15:48:16 +00:00
|
|
|
props->delta2_span = 0;
|
2012-08-18 04:50:52 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|