Skip to content

Commit

Permalink
fixed bug with mapq option
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Harry <e.f.harry@gmail.com>
  • Loading branch information
EdHarry committed May 16, 2019
1 parent 1382dd5 commit a1d9354
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 67 deletions.
189 changes: 129 additions & 60 deletions Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ SOFTWARE.
#ifndef HEADER_H
#define HEADER_H

#ifdef _WIN32
#include <windows.h>
#endif

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <intrin.h>
#else
#include <x86intrin.h>
#endif

#include "libdeflate.h"

Expand Down Expand Up @@ -94,38 +102,62 @@ typedef size_t memptr;
#define MainArgs s32 main(s32 ArgCount, const char *ArgBuffer[])
#define EndMain return(0)

#ifndef _WIN32
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#else
#define __atomic_fetch_add(x, y, z) _InterlockedExchangeAdd(x, y)
#define __atomic_add_fetch(x, y, z) (y + _InterlockedExchangeAdd(x, y))
#define __sync_fetch_and_add(x, y) _InterlockedExchangeAdd(x, y)
#define __sync_fetch_and_sub(x, y) _InterlockedExchangeAdd(x, -y)
#define __atomic_store(x, y, z) _InterlockedCompareExchange(x, *y, *x)
#endif

#ifndef _WIN32
#define ThreadFence __asm__ volatile("" ::: "memory")
#else
#define ThreadFence _mm_mfence()
#endif
#define FenceIn(x) ThreadFence; \
x; \
ThreadFence

typedef volatile u32 threadSig;

#ifndef _WIN32
typedef pthread_t thread;
typedef pthread_mutex_t mutex;
typedef pthread_cond_t cond;

typedef volatile u32 threadSig;

#define CreateThread(x) thread *x
#define CreateMutex(x) mutex *x
#define CreateCond(x) cond *x

#define InitialiseMutex(x) x = PTHREAD_MUTEX_INITIALIZER
#define InitialiseCond(x) x = PTHREAD_COND_INITIALIZER

#define LaunchThread(thread, func, dataIn) pthread_create(&thread, NULL, func, dataIn)
#define WaitForThread(x) pthread_join(*x, NULL)
#define DetachThread(thread) pthread_detach(thread)
#define PauseThread(thread) pthread_kill(thread, SIGUSR1)

#define LockMutex(x) pthread_mutex_lock(&x)
#define UnlockMutex(x) pthread_mutex_unlock(&x)
#define WaitOnCond(cond, mutex) pthread_cond_wait(&cond, &mutex)
#define SignalCondition(x) pthread_cond_signal(&x)
#define BroadcastCondition(x) pthread_cond_broadcast(&x)
#else
typedef HANDLE thread;
typedef CRITICAL_SECTION mutex;
typedef CONDITION_VARIABLE cond;

#define InitialiseMutex(x) InitializeCriticalSection(&x)
#define InitialiseCond(x) InitializeConditionVariable(&x)

#define LaunchThread(thread, func, dataIn) thread = CreateThread(NULL, 0, func, dataIn, 0, NULL)

#define LockMutex(x) EnterCriticalSection(&x)
#define UnlockMutex(x) LeaveCriticalSection(&x)
#define WaitOnCond(cond, mutex) SleepConditionVariableCS(&cond, &mutex, INFINITE)
#define SignalCondition(x) WakeConditionVariable(&x)
#define BroadcastCondition(x) WakeAllConditionVariable(&x)
#endif

struct
binary_semaphore
Expand Down Expand Up @@ -189,11 +221,6 @@ thread_context
#define MegaByte(x) 1024*KiloByte(x)
#define GigaByte(x) 1024*MegaByte(x)

#if 0
#include "zlib.h"
#define CHUNK KiloByte(256)
#endif

#define Default_Memory_Alignment_Pow2 4

struct
Expand Down Expand Up @@ -228,7 +255,13 @@ global_function
void
CreateMemoryArena_(memory_arena *arena, u64 size, u32 alignment_pow2 = Default_Memory_Alignment_Pow2)
{
#ifndef _WIN32
posix_memalign((void **)&arena->base, Pow2(alignment_pow2), size);
#else
#include <memoryapi.h>
(void)alignment_pow2;
arena->base = (u08 *)VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
#endif
arena->currentSize = 0;
arena->maxSize = size;
}
Expand Down Expand Up @@ -286,7 +319,7 @@ PushSize_(memory_arena *arena, u64 size, u32 alignment_pow2 = Default_Memory_Ali
if((size + arena->currentSize + padding + sizeof(u64)) > arena->maxSize)
{
result = 0;
#ifdef __APPLE__
#if defined(__APPLE__) || defined(_WIN32)
printf("Push of %llu bytes failed, out of memory.\n", size);
#else
printf("Push of %lu bytes failed, out of memory.\n", size);
Expand Down Expand Up @@ -347,22 +380,6 @@ global_variable
threadSig
Threads_KeepAlive;

global_variable
threadSig
Threads_OnHold;

global_function
void
ThreadHold(s32 id)
{
(void)id;
Threads_OnHold = 1;
while (Threads_OnHold)
{
sleep(1);
}
}

global_function
void
BinarySemaphoreInit(binary_semaphore *bsem, u32 value)
Expand Down Expand Up @@ -490,24 +507,17 @@ FreeThreadJob(job_queue *jobQueue, thread_job *job)
}

global_function
#ifndef _WIN32
void *
#else
DWORD WINAPI
#endif
ThreadFunc(void *in)
{
thread_context *context = (thread_context *)in;

thread_pool *pool = context->pool;

struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = ThreadHold;

if (sigaction(SIGUSR1, &act, NULL) == -1)
{
printf("\nThreadFunc(): cannot handle SIGUSR1\n");
*((threadSig *)0) = 0;
}

LockMutex(pool->threadCountLock);
pool->numThreadsAlive += 1;
UnlockMutex(pool->threadCountLock);
Expand Down Expand Up @@ -563,7 +573,9 @@ ThreadInit(memory_arena *arena, thread_pool *pool, thread_context **context, u32
(*context)->id = id;

LaunchThread((*context)->th, ThreadFunc, *context);
#ifndef _WIN32
DetachThread((*context)->th);
#endif
}

#define Number_Thread_Jobs 1024
Expand Down Expand Up @@ -636,7 +648,6 @@ global_function
thread_pool *
ThreadPoolInit(memory_arena *arena, u32 nThreads)
{
Threads_OnHold = 0;
Threads_KeepAlive = 1;

thread_pool *threadPool = PushStructP(arena, thread_pool);
Expand Down Expand Up @@ -698,6 +709,11 @@ ThreadPoolWait(thread_pool *threadPool)
UnlockMutex(threadPool->threadCountLock);
}

#ifdef _WIN32
#include <ctime>
#define sleep(x) Sleep(1000 * x)
#endif

global_function
void
ThreadPoolDestroy(thread_pool *threadPool)
Expand Down Expand Up @@ -727,25 +743,6 @@ ThreadPoolDestroy(thread_pool *threadPool)
}
}

global_function
void
ThreadPoolPause(thread_pool *threadPool)
{
for ( u32 index = 0;
index < threadPool->numThreadsAlive;
++index )
{
PauseThread(threadPool->threads[index]->th);
}
}

global_function
void
ThreadPoolUnPause()
{
Threads_OnHold = 0;
}

global_function
u32
AreNullTerminatedStringsEqual(u08 *string1, u08 *string2)
Expand Down Expand Up @@ -866,6 +863,31 @@ StringToInt(char *string)
return(result);
}

global_function
u32
StringToInt_Check(u08 *string, u32 *result)
{
u32 goodResult = 1;
*result = 0;

u32 strLen = 1;
while(*++string != '\0') ++strLen;

u32 pow = 1;

while(--strLen > 0 && goodResult)
{
*result += (u32)(*--string - '0') * pow;
goodResult = (*string >= '0' && *string <= '9');
pow *= 10;
}

*result += (u32)(*--string - '0') * pow;
goodResult = (goodResult && *string >= '0' && *string <= '9');

return(goodResult);
}

global_function
u32
SIMDTestEquali(__m128i a, __m128i b)
Expand Down Expand Up @@ -895,6 +917,7 @@ IntDivideCeil(u32 x, u32 y)
}

//https://github.com/ZilongTan/fast-hash/blob/master/fasthash.c
#ifndef _WIN32
#define HashMix(h) ({ \
(h) ^= (h) >> 23; \
(h) *= 0x2127599bf4325c37ULL; \
Expand Down Expand Up @@ -936,6 +959,52 @@ FastHash64(void *buf, u64 len, u64 seed)

return(HashMix(h));
}
#else
#define HashMix(h) { \
(h) ^= (h) >> 23; \
(h) *= 0x2127599bf4325c37ULL; \
(h) ^= (h) >> 47; }

global_function
u64
FastHash64(void *buf, u64 len, u64 seed)
{
u64 m = 0x880355f21e6d1965ULL;
u64 *pos = (u64 *)buf;
u64 *end = pos + (len / 8);
u08 *pos2;
u64 h = seed ^ (len * m);
u64 v;

while (pos != end)
{
v = *pos++;
HashMix(v);
h ^= v;
h *= m;
}

pos2 = (u08*)pos;
v = 0;

switch (len & 7)
{
case 7: v ^= (u64)pos2[6] << 48; [[clang::fallthrough]];
case 6: v ^= (u64)pos2[5] << 40; [[clang::fallthrough]];
case 5: v ^= (u64)pos2[4] << 32; [[clang::fallthrough]];
case 4: v ^= (u64)pos2[3] << 24; [[clang::fallthrough]];
case 3: v ^= (u64)pos2[2] << 16; [[clang::fallthrough]];
case 2: v ^= (u64)pos2[1] << 8; [[clang::fallthrough]];
case 1: v ^= (u64)pos2[0];
HashMix(v);
h ^= v;
h *= m;
}

HashMix(h);
return(h);
}
#endif

global_function
u32
Expand Down
Loading

0 comments on commit a1d9354

Please sign in to comment.