Convert static func naming to same style
Previously, some modules used BH_<Module> prefix in the names of static functions (some used just <Module> prefix). Now every static function should start from lowercase.
This commit is contained in:
@@ -27,7 +27,7 @@ typedef struct PakEntry
|
|||||||
} PakEntry;
|
} PakEntry;
|
||||||
|
|
||||||
|
|
||||||
static int ParseHeader(BH_IO *io,
|
static int parseHeader(BH_IO *io,
|
||||||
PakHeader *header)
|
PakHeader *header)
|
||||||
{
|
{
|
||||||
char buffer[HEADER_SIZE];
|
char buffer[HEADER_SIZE];
|
||||||
@@ -47,7 +47,7 @@ static int ParseHeader(BH_IO *io,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int ParseEntry(BH_IO *io,
|
static int parseEntry(BH_IO *io,
|
||||||
PakEntry *entry)
|
PakEntry *entry)
|
||||||
{
|
{
|
||||||
char buffer[ENTRY_SIZE];
|
char buffer[ENTRY_SIZE];
|
||||||
@@ -84,14 +84,14 @@ static BH_ArgsOption options[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void PrintHelp(void)
|
static void printHelp(void)
|
||||||
{
|
{
|
||||||
printf("Usage: PakReader [options...] <file>\n");
|
printf("Usage: PakReader [options...] <file>\n");
|
||||||
BH_ArgsHelp(options, 0);
|
BH_ArgsHelp(options, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int OptionsCallback(int key,
|
static int optionsCallback(int key,
|
||||||
char *arg,
|
char *arg,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
@@ -101,7 +101,7 @@ static int OptionsCallback(int key,
|
|||||||
{
|
{
|
||||||
case BH_ARGS_UNKNOWN: break;
|
case BH_ARGS_UNKNOWN: break;
|
||||||
case BH_ARGS_ARGUMENT: if (!config->file) config->file = arg; break;
|
case BH_ARGS_ARGUMENT: if (!config->file) config->file = arg; break;
|
||||||
case 'h': PrintHelp(); exit(0);
|
case 'h': printHelp(); exit(0);
|
||||||
case 'l': config->list = 1; break;
|
case 'l': config->list = 1; break;
|
||||||
case 'i': config->input = arg; break;
|
case 'i': config->input = arg; break;
|
||||||
case 'o': config->output = arg; break;
|
case 'o': config->output = arg; break;
|
||||||
@@ -112,7 +112,7 @@ static int OptionsCallback(int key,
|
|||||||
|
|
||||||
|
|
||||||
/* Copy data between two IO */
|
/* Copy data between two IO */
|
||||||
static int CopyData(BH_IO *from,
|
static int copyData(BH_IO *from,
|
||||||
BH_IO *to,
|
BH_IO *to,
|
||||||
size_t size)
|
size_t size)
|
||||||
{
|
{
|
||||||
@@ -137,7 +137,7 @@ static int CopyData(BH_IO *from,
|
|||||||
|
|
||||||
|
|
||||||
/* Process pack (list files or extract file) */
|
/* Process pack (list files or extract file) */
|
||||||
static int ProcessPack(Config *config,
|
static int processPack(Config *config,
|
||||||
BH_IO *io)
|
BH_IO *io)
|
||||||
{
|
{
|
||||||
PakHeader header;
|
PakHeader header;
|
||||||
@@ -146,7 +146,7 @@ static int ProcessPack(Config *config,
|
|||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
/* Read header and seek to begging of the file table */
|
/* Read header and seek to begging of the file table */
|
||||||
if (ParseHeader(io, &header))
|
if (parseHeader(io, &header))
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
|
|
||||||
if (BH_IOSeek(io, header.offset, BH_IO_SEEK_SET))
|
if (BH_IOSeek(io, header.offset, BH_IO_SEEK_SET))
|
||||||
@@ -155,7 +155,7 @@ static int ProcessPack(Config *config,
|
|||||||
/* Parse and output entries */
|
/* Parse and output entries */
|
||||||
for (i = header.size / 64; i; i--)
|
for (i = header.size / 64; i; i--)
|
||||||
{
|
{
|
||||||
if (ParseEntry(io, &entry))
|
if (parseEntry(io, &entry))
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
|
|
||||||
if (config->list)
|
if (config->list)
|
||||||
@@ -167,7 +167,7 @@ static int ProcessPack(Config *config,
|
|||||||
|
|
||||||
output = BH_FileNew(config->output, BH_FILE_WRITE | BH_FILE_TRUNCATE, NULL);
|
output = BH_FileNew(config->output, BH_FILE_WRITE | BH_FILE_TRUNCATE, NULL);
|
||||||
if (!output || BH_IOSeek(io, entry.offset, BH_IO_SEEK_SET) ||
|
if (!output || BH_IOSeek(io, entry.offset, BH_IO_SEEK_SET) ||
|
||||||
CopyData(io, output, entry.size))
|
copyData(io, output, entry.size))
|
||||||
{
|
{
|
||||||
BH_IOFree(output);
|
BH_IOFree(output);
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
@@ -193,9 +193,9 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
/* Parse arguments */
|
/* Parse arguments */
|
||||||
memset(&config, 0, sizeof(config));
|
memset(&config, 0, sizeof(config));
|
||||||
if (BH_ArgsParse(argc, argv, options, OptionsCallback, &config) || !config.file)
|
if (BH_ArgsParse(argc, argv, options, optionsCallback, &config) || !config.file)
|
||||||
{
|
{
|
||||||
PrintHelp();
|
printHelp();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +203,7 @@ int main(int argc, char **argv)
|
|||||||
if (!config.list && (!config.input || !config.output))
|
if (!config.list && (!config.input || !config.output))
|
||||||
{
|
{
|
||||||
printf("Specify input and output files\n");
|
printf("Specify input and output files\n");
|
||||||
PrintHelp();
|
printHelp();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +215,7 @@ int main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = ProcessPack(&config, io);
|
result = processPack(&config, io);
|
||||||
BH_IOFree(io);
|
BH_IOFree(io);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ typedef struct PakEntry
|
|||||||
} PakEntry;
|
} PakEntry;
|
||||||
|
|
||||||
|
|
||||||
static int ParseHeader(BH_IO *io,
|
static int parseHeader(BH_IO *io,
|
||||||
PakHeader *header)
|
PakHeader *header)
|
||||||
{
|
{
|
||||||
char buffer[HEADER_SIZE];
|
char buffer[HEADER_SIZE];
|
||||||
@@ -242,7 +242,7 @@ static int ParseHeader(BH_IO *io,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int ParseEntry(BH_IO *io,
|
static int parseEntry(BH_IO *io,
|
||||||
PakEntry *entry)
|
PakEntry *entry)
|
||||||
{
|
{
|
||||||
char buffer[ENTRY_SIZE];
|
char buffer[ENTRY_SIZE];
|
||||||
@@ -279,14 +279,14 @@ static BH_ArgsOption options[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void PrintHelp(void)
|
static void printHelp(void)
|
||||||
{
|
{
|
||||||
printf("Usage: PakReader [options...] <file>\n");
|
printf("Usage: PakReader [options...] <file>\n");
|
||||||
BH_ArgsHelp(options, 0);
|
BH_ArgsHelp(options, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int OptionsCallback(int key,
|
static int optionsCallback(int key,
|
||||||
char *arg,
|
char *arg,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
@@ -307,7 +307,7 @@ static int OptionsCallback(int key,
|
|||||||
|
|
||||||
|
|
||||||
/* Copy data between two IO */
|
/* Copy data between two IO */
|
||||||
static int CopyData(BH_IO *from,
|
static int copyData(BH_IO *from,
|
||||||
BH_IO *to,
|
BH_IO *to,
|
||||||
size_t size)
|
size_t size)
|
||||||
{
|
{
|
||||||
@@ -332,7 +332,7 @@ static int CopyData(BH_IO *from,
|
|||||||
|
|
||||||
|
|
||||||
/* Process pack (list files or extract file) */
|
/* Process pack (list files or extract file) */
|
||||||
static int ProcessPack(Config *config,
|
static int processPack(Config *config,
|
||||||
BH_IO *io)
|
BH_IO *io)
|
||||||
{
|
{
|
||||||
PakHeader header;
|
PakHeader header;
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ BH_Args - command line argument processing
|
|||||||
{0, NULL, 0, NULL}
|
{0, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int OptionsCallback(int key,
|
static int optionsCallback(int key,
|
||||||
char *arg,
|
char *arg,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ BH_Args - обработка аргументов командной строк
|
|||||||
{0, NULL, 0, NULL}
|
{0, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int OptionsCallback(int key,
|
static int optionsCallback(int key,
|
||||||
char *arg,
|
char *arg,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
|
|||||||
42
src/Algo.c
42
src/Algo.c
@@ -77,10 +77,10 @@ void *BH_Partition(void *pivot,
|
|||||||
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
static void BH_SortInsert(void *array,
|
static void sortInsert(void *array,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t element,
|
size_t element,
|
||||||
BH_EqualCallback equal)
|
BH_EqualCallback equal)
|
||||||
{
|
{
|
||||||
size_t i, j;
|
size_t i, j;
|
||||||
|
|
||||||
@@ -105,10 +105,10 @@ static void BH_SortInsert(void *array,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void BH_SortShell(void *array,
|
static void sortShell(void *array,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t element,
|
size_t element,
|
||||||
BH_EqualCallback equal)
|
BH_EqualCallback equal)
|
||||||
{
|
{
|
||||||
static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0};
|
static const size_t gaps[10] = {1750, 701, 301, 132, 57, 23, 10, 4, 1, 0};
|
||||||
const size_t *gap;
|
const size_t *gap;
|
||||||
@@ -136,10 +136,10 @@ static void BH_SortShell(void *array,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_SortHeap(void *array,
|
static void sortHeap(void *array,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t element,
|
size_t element,
|
||||||
BH_EqualCallback equal)
|
BH_EqualCallback equal)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@@ -149,11 +149,11 @@ static void BH_SortHeap(void *array,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_SortIntroR(void *array,
|
static void sortIntroR(void *array,
|
||||||
size_t size,
|
size_t size,
|
||||||
size_t element,
|
size_t element,
|
||||||
BH_EqualCallback equal,
|
BH_EqualCallback equal,
|
||||||
size_t depth)
|
size_t depth)
|
||||||
{
|
{
|
||||||
/* Introsort (with manual tail call optimization) */
|
/* Introsort (with manual tail call optimization) */
|
||||||
while (1)
|
while (1)
|
||||||
@@ -163,13 +163,13 @@ static void BH_SortIntroR(void *array,
|
|||||||
if (size < 16)
|
if (size < 16)
|
||||||
{
|
{
|
||||||
/* There are less then 16 elements left - use Shell/Insert sort */
|
/* There are less then 16 elements left - use Shell/Insert sort */
|
||||||
BH_SortShell(array, size, element, equal);
|
sortShell(array, size, element, equal);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else if (!depth)
|
else if (!depth)
|
||||||
{
|
{
|
||||||
/* Max depth reached - use heap sort */
|
/* Max depth reached - use heap sort */
|
||||||
BH_SortHeap(array, size, element, equal);
|
sortHeap(array, size, element, equal);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,7 +202,7 @@ static void BH_SortIntroR(void *array,
|
|||||||
middle = BH_Partition(pivot, array, size, element, equal);
|
middle = BH_Partition(pivot, array, size, element, equal);
|
||||||
|
|
||||||
/* Recursive call into first half */
|
/* Recursive call into first half */
|
||||||
BH_SortIntroR(array, (middle - start) / element, element, equal, depth - 1);
|
sortIntroR(array, (middle - start) / element, element, equal, depth - 1);
|
||||||
|
|
||||||
/* Setup array and size for the second half */
|
/* Setup array and size for the second half */
|
||||||
array = middle;
|
array = middle;
|
||||||
@@ -229,7 +229,7 @@ void BH_Sort(void *array,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Call main sorting function */
|
/* Call main sorting function */
|
||||||
BH_SortIntroR(array, size, element, equal, depth);
|
sortIntroR(array, size, element, equal, depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
48
src/Args.c
48
src/Args.c
@@ -5,14 +5,14 @@
|
|||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
|
||||||
|
|
||||||
static int BH_ArgsExtractArg(int argc,
|
static int extractArg(int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
BH_ArgsOption *option,
|
BH_ArgsOption *option,
|
||||||
BH_ArgsCallback callback,
|
BH_ArgsCallback callback,
|
||||||
void *data,
|
void *data,
|
||||||
int *i,
|
int *i,
|
||||||
int isLong,
|
int isLong,
|
||||||
char *next)
|
char *next)
|
||||||
{
|
{
|
||||||
if (isLong && *next == '=')
|
if (isLong && *next == '=')
|
||||||
return callback(option->key, next + 1, data);
|
return callback(option->key, next + 1, data);
|
||||||
@@ -30,12 +30,12 @@ static int BH_ArgsExtractArg(int argc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int BH_ArgsParseShort(int argc,
|
static int parseShort(int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
BH_ArgsOption *options,
|
BH_ArgsOption *options,
|
||||||
BH_ArgsCallback callback,
|
BH_ArgsCallback callback,
|
||||||
void *data,
|
void *data,
|
||||||
int *i)
|
int *i)
|
||||||
{
|
{
|
||||||
char *symbol;
|
char *symbol;
|
||||||
BH_ArgsOption *option;
|
BH_ArgsOption *option;
|
||||||
@@ -49,7 +49,7 @@ static int BH_ArgsParseShort(int argc,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (option->flags & BH_ARGS_VALUE)
|
if (option->flags & BH_ARGS_VALUE)
|
||||||
return BH_ArgsExtractArg(argc, argv, option, callback, data, i, 0, symbol + 1);
|
return extractArg(argc, argv, option, callback, data, i, 0, symbol + 1);
|
||||||
|
|
||||||
if (callback(option->key, NULL, data))
|
if (callback(option->key, NULL, data))
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
@@ -69,12 +69,12 @@ static int BH_ArgsParseShort(int argc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int BH_ArgsParseLong(int argc,
|
static int parseLong(int argc,
|
||||||
char **argv,
|
char **argv,
|
||||||
BH_ArgsOption *options,
|
BH_ArgsOption *options,
|
||||||
BH_ArgsCallback callback,
|
BH_ArgsCallback callback,
|
||||||
void *data,
|
void *data,
|
||||||
int *i)
|
int *i)
|
||||||
{
|
{
|
||||||
char *start, *end;
|
char *start, *end;
|
||||||
BH_ArgsOption *option;
|
BH_ArgsOption *option;
|
||||||
@@ -98,7 +98,7 @@ static int BH_ArgsParseLong(int argc,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (option->flags & BH_ARGS_VALUE)
|
if (option->flags & BH_ARGS_VALUE)
|
||||||
return BH_ArgsExtractArg(argc, argv, option, callback, data, i, 1, end);
|
return extractArg(argc, argv, option, callback, data, i, 1, end);
|
||||||
|
|
||||||
return callback(option->key, NULL, data);
|
return callback(option->key, NULL, data);
|
||||||
}
|
}
|
||||||
@@ -128,13 +128,13 @@ int BH_ArgsParse(int argc,
|
|||||||
/* Parse ingore, short or long option */
|
/* Parse ingore, short or long option */
|
||||||
if (arg[1] == '-')
|
if (arg[1] == '-')
|
||||||
{
|
{
|
||||||
if (arg[2] && BH_ArgsParseLong(argc, argv, options, callback, data, &i))
|
if (arg[2] && parseLong(argc, argv, options, callback, data, &i))
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
else if (!arg[2])
|
else if (!arg[2])
|
||||||
ignoreRest = 1;
|
ignoreRest = 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (arg[1] && BH_ArgsParseShort(argc, argv, options, callback, data, &i))
|
else if (arg[1] && parseShort(argc, argv, options, callback, data, &i))
|
||||||
return BH_ERROR;
|
return BH_ERROR;
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ struct BH_Hashmap
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void BH_HashmapInit(BH_Hashmap *hashmap,
|
static void hashmapInit(BH_Hashmap *hashmap,
|
||||||
BH_EqualCallback equal,
|
BH_EqualCallback equal,
|
||||||
BH_HashCallback hash)
|
BH_HashCallback hash)
|
||||||
{
|
{
|
||||||
memset(hashmap, 0, sizeof(*hashmap));
|
memset(hashmap, 0, sizeof(*hashmap));
|
||||||
hashmap->factor = 0.75f;
|
hashmap->factor = 0.75f;
|
||||||
@@ -34,7 +34,7 @@ static void BH_HashmapInit(BH_Hashmap *hashmap,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_HashmapDestroy(BH_Hashmap *hashmap)
|
static void hashmapDestroy(BH_Hashmap *hashmap)
|
||||||
{
|
{
|
||||||
if (hashmap->capacity)
|
if (hashmap->capacity)
|
||||||
{
|
{
|
||||||
@@ -44,10 +44,10 @@ static void BH_HashmapDestroy(BH_Hashmap *hashmap)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int BH_CalcCapacity(size_t size,
|
static int calcCapacity(size_t size,
|
||||||
float factor,
|
float factor,
|
||||||
size_t *capacity,
|
size_t *capacity,
|
||||||
size_t *threshold)
|
size_t *threshold)
|
||||||
{
|
{
|
||||||
/* Check if we need any capacity at all */
|
/* Check if we need any capacity at all */
|
||||||
if (!size)
|
if (!size)
|
||||||
@@ -78,8 +78,8 @@ static int BH_CalcCapacity(size_t size,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_CopyHashmap(BH_Hashmap *dest,
|
static void copyHashmap(BH_Hashmap *dest,
|
||||||
BH_Hashmap *src)
|
BH_Hashmap *src)
|
||||||
{
|
{
|
||||||
void *iter;
|
void *iter;
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
|
|||||||
|
|
||||||
result = malloc(sizeof(*result));
|
result = malloc(sizeof(*result));
|
||||||
if (result)
|
if (result)
|
||||||
BH_HashmapInit(result, equal, hash);
|
hashmapInit(result, equal, hash);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -113,7 +113,7 @@ BH_Hashmap *BH_HashmapNew(BH_EqualCallback equal,
|
|||||||
|
|
||||||
void BH_HashmapFree(BH_Hashmap *hashmap)
|
void BH_HashmapFree(BH_Hashmap *hashmap)
|
||||||
{
|
{
|
||||||
BH_HashmapDestroy(hashmap);
|
hashmapDestroy(hashmap);
|
||||||
free(hashmap);
|
free(hashmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,7 +137,7 @@ int BH_HashmapReserve(BH_Hashmap *hashmap,
|
|||||||
size = hashmap->size;
|
size = hashmap->size;
|
||||||
|
|
||||||
/* Calculate new capacity */
|
/* Calculate new capacity */
|
||||||
if (BH_CalcCapacity(size, hashmap->factor, &capacity, &threshold))
|
if (calcCapacity(size, hashmap->factor, &capacity, &threshold))
|
||||||
return BH_OOM;
|
return BH_OOM;
|
||||||
|
|
||||||
/* Prevent same size reallocation */
|
/* Prevent same size reallocation */
|
||||||
@@ -145,7 +145,7 @@ int BH_HashmapReserve(BH_Hashmap *hashmap,
|
|||||||
return BH_OK;
|
return BH_OK;
|
||||||
|
|
||||||
/* Initialize new hashmap */
|
/* Initialize new hashmap */
|
||||||
BH_HashmapInit(&other, hashmap->equal, hashmap->hash);
|
hashmapInit(&other, hashmap->equal, hashmap->hash);
|
||||||
other.factor = hashmap->factor;
|
other.factor = hashmap->factor;
|
||||||
|
|
||||||
if (capacity)
|
if (capacity)
|
||||||
@@ -170,11 +170,11 @@ int BH_HashmapReserve(BH_Hashmap *hashmap,
|
|||||||
memset(other.psls, 0, sizeof(size_t) * other.capacity);
|
memset(other.psls, 0, sizeof(size_t) * other.capacity);
|
||||||
|
|
||||||
/* Copy data from old hashmap to the new hashmap */
|
/* Copy data from old hashmap to the new hashmap */
|
||||||
BH_CopyHashmap(&other, hashmap);
|
copyHashmap(&other, hashmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Swap hashmaps */
|
/* Swap hashmaps */
|
||||||
BH_HashmapDestroy(hashmap);
|
hashmapDestroy(hashmap);
|
||||||
*hashmap = other;
|
*hashmap = other;
|
||||||
return BH_OK;
|
return BH_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ struct BH_ThreadContext
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void *BH_ThreadRun(void *context)
|
static void *threadRun(void *context)
|
||||||
{
|
{
|
||||||
BH_ThreadCallback callback;
|
BH_ThreadCallback callback;
|
||||||
void *data;
|
void *data;
|
||||||
@@ -27,10 +27,10 @@ static void *BH_ThreadRun(void *context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int BH_ThreadInit(BH_Thread *thread,
|
static int threadInit(BH_Thread *thread,
|
||||||
size_t stack,
|
size_t stack,
|
||||||
BH_ThreadCallback callback,
|
BH_ThreadCallback callback,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
struct BH_ThreadContext *context;
|
struct BH_ThreadContext *context;
|
||||||
pthread_attr_t attributes;
|
pthread_attr_t attributes;
|
||||||
@@ -46,13 +46,13 @@ static int BH_ThreadInit(BH_Thread *thread,
|
|||||||
pthread_attr_init(&attributes);
|
pthread_attr_init(&attributes);
|
||||||
|
|
||||||
if (!stack)
|
if (!stack)
|
||||||
result = pthread_create(&thread->handle, NULL, BH_ThreadRun, context);
|
result = pthread_create(&thread->handle, NULL, threadRun, context);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (stack < PTHREAD_STACK_MIN)
|
if (stack < PTHREAD_STACK_MIN)
|
||||||
stack = PTHREAD_STACK_MIN;
|
stack = PTHREAD_STACK_MIN;
|
||||||
pthread_attr_setstacksize(&attributes, stack);
|
pthread_attr_setstacksize(&attributes, stack);
|
||||||
result = pthread_create(&thread->handle, &attributes, BH_ThreadRun, context);
|
result = pthread_create(&thread->handle, &attributes, threadRun, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_attr_destroy(&attributes);
|
pthread_attr_destroy(&attributes);
|
||||||
@@ -67,7 +67,7 @@ BH_Thread *BH_ThreadNew(size_t stack,
|
|||||||
BH_Thread *thread;
|
BH_Thread *thread;
|
||||||
|
|
||||||
thread = malloc(sizeof(BH_Thread));
|
thread = malloc(sizeof(BH_Thread));
|
||||||
if (thread && BH_ThreadInit(thread, stack, callback, data))
|
if (thread && threadInit(thread, stack, callback, data))
|
||||||
{
|
{
|
||||||
free(thread);
|
free(thread);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ static int tssReady = 0;
|
|||||||
static pthread_key_t tssKey;
|
static pthread_key_t tssKey;
|
||||||
|
|
||||||
|
|
||||||
static void BH_TssKeyCleanup(void *data)
|
static void keyCleanup(void *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ static void BH_TssKeyCleanup(void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void **BH_TssDataFetch(void)
|
static void **dataFetch(void)
|
||||||
{
|
{
|
||||||
void **result;
|
void **result;
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ static void **BH_TssDataFetch(void)
|
|||||||
BH_SpinlockLock(&tssLock);
|
BH_SpinlockLock(&tssLock);
|
||||||
if (!tssReady)
|
if (!tssReady)
|
||||||
{
|
{
|
||||||
if (pthread_key_create(&tssKey, BH_TssKeyCleanup))
|
if (pthread_key_create(&tssKey, keyCleanup))
|
||||||
abort();
|
abort();
|
||||||
tssReady = 1;
|
tssReady = 1;
|
||||||
}
|
}
|
||||||
@@ -67,7 +67,7 @@ static void **BH_TssDataFetch(void)
|
|||||||
|
|
||||||
void BH_TssCleanup(void)
|
void BH_TssCleanup(void)
|
||||||
{
|
{
|
||||||
BH_TssKeyCleanup(BH_TssDataFetch());
|
keyCleanup(dataFetch());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -92,12 +92,12 @@ int BH_TssCreate(BH_GenericCallback callback)
|
|||||||
|
|
||||||
void *BH_TssRead(int index)
|
void *BH_TssRead(int index)
|
||||||
{
|
{
|
||||||
return BH_TssDataFetch()[index];
|
return dataFetch()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BH_TssWrite(int index,
|
void BH_TssWrite(int index,
|
||||||
void *value)
|
void *value)
|
||||||
{
|
{
|
||||||
BH_TssDataFetch()[index] = value;
|
dataFetch()[index] = value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ struct BH_ThreadContext
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static unsigned __stdcall BH_ThreadRun(void *context)
|
static unsigned __stdcall threadRun(void *context)
|
||||||
{
|
{
|
||||||
|
|
||||||
BH_ThreadCallback callback;
|
BH_ThreadCallback callback;
|
||||||
@@ -27,10 +27,10 @@ static unsigned __stdcall BH_ThreadRun(void *context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int BH_ThreadInit(BH_Thread *thread,
|
static int threadInit(BH_Thread *thread,
|
||||||
size_t stack,
|
size_t stack,
|
||||||
BH_ThreadCallback callback,
|
BH_ThreadCallback callback,
|
||||||
void *data)
|
void *data)
|
||||||
{
|
{
|
||||||
struct BH_ThreadContext *context;
|
struct BH_ThreadContext *context;
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ BH_Thread *BH_ThreadNew(size_t stack,
|
|||||||
BH_Thread *thread;
|
BH_Thread *thread;
|
||||||
|
|
||||||
thread = malloc(sizeof(BH_Thread));
|
thread = malloc(sizeof(BH_Thread));
|
||||||
if (thread && BH_ThreadInit(thread, stack, callback, data))
|
if (thread && threadInit(thread, stack, callback, data))
|
||||||
{
|
{
|
||||||
free(thread);
|
free(thread);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ static int tssReady = 0;
|
|||||||
static DWORD tssKey;
|
static DWORD tssKey;
|
||||||
|
|
||||||
|
|
||||||
static void __stdcall BH_TssKeyCleanup(void *data)
|
static void __stdcall keyCleanup(void *data)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ static void __stdcall BH_TssKeyCleanup(void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void **BH_TssDataFetch(void)
|
static void **dataFetch(void)
|
||||||
{
|
{
|
||||||
void **result;
|
void **result;
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ static void **BH_TssDataFetch(void)
|
|||||||
BH_SpinlockLock(&tssLock);
|
BH_SpinlockLock(&tssLock);
|
||||||
if (!tssReady)
|
if (!tssReady)
|
||||||
{
|
{
|
||||||
tssKey = FlsAlloc(BH_TssKeyCleanup);
|
tssKey = FlsAlloc(keyCleanup);
|
||||||
if (tssKey == FLS_OUT_OF_INDEXES)
|
if (tssKey == FLS_OUT_OF_INDEXES)
|
||||||
abort();
|
abort();
|
||||||
tssReady = 1;
|
tssReady = 1;
|
||||||
@@ -69,7 +69,7 @@ static void **BH_TssDataFetch(void)
|
|||||||
|
|
||||||
void BH_TssCleanup(void)
|
void BH_TssCleanup(void)
|
||||||
{
|
{
|
||||||
BH_TssKeyCleanup(BH_TssDataFetch());
|
keyCleanup(dataFetch());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -94,12 +94,12 @@ int BH_TssCreate(BH_GenericCallback callback)
|
|||||||
|
|
||||||
void *BH_TssRead(int index)
|
void *BH_TssRead(int index)
|
||||||
{
|
{
|
||||||
return BH_TssDataFetch()[index];
|
return dataFetch()[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BH_TssWrite(int index,
|
void BH_TssWrite(int index,
|
||||||
void *value)
|
void *value)
|
||||||
{
|
{
|
||||||
BH_TssDataFetch()[index] = value;
|
dataFetch()[index] = value;
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/Queue.c
14
src/Queue.c
@@ -13,20 +13,20 @@ struct BH_Queue
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void BH_QueueInit(BH_Queue *queue)
|
static void queueInit(BH_Queue *queue)
|
||||||
{
|
{
|
||||||
memset(queue, 0, sizeof(*queue));
|
memset(queue, 0, sizeof(*queue));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_QueueDestroy(BH_Queue *queue)
|
static void queueDestroy(BH_Queue *queue)
|
||||||
{
|
{
|
||||||
if (queue->capacity)
|
if (queue->capacity)
|
||||||
free(queue->data);
|
free(queue->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void BH_QueueCopy(BH_Queue *dest,
|
static void queueCopy(BH_Queue *dest,
|
||||||
BH_Queue *src)
|
BH_Queue *src)
|
||||||
{
|
{
|
||||||
void *iter;
|
void *iter;
|
||||||
@@ -47,7 +47,7 @@ BH_Queue *BH_QueueNew(void)
|
|||||||
|
|
||||||
result = malloc(sizeof(*result));
|
result = malloc(sizeof(*result));
|
||||||
if (result)
|
if (result)
|
||||||
BH_QueueInit(result);
|
queueInit(result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -55,7 +55,7 @@ BH_Queue *BH_QueueNew(void)
|
|||||||
|
|
||||||
void BH_QueueFree(BH_Queue *queue)
|
void BH_QueueFree(BH_Queue *queue)
|
||||||
{
|
{
|
||||||
BH_QueueDestroy(queue);
|
queueDestroy(queue);
|
||||||
free(queue);
|
free(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ int BH_QueueReserve(BH_Queue *queue,
|
|||||||
return BH_OK;
|
return BH_OK;
|
||||||
|
|
||||||
/* Prepare new empty queue */
|
/* Prepare new empty queue */
|
||||||
BH_QueueInit(&other);
|
queueInit(&other);
|
||||||
if (size)
|
if (size)
|
||||||
{
|
{
|
||||||
/* Allocate new capacity for the queue */
|
/* Allocate new capacity for the queue */
|
||||||
@@ -96,7 +96,7 @@ int BH_QueueReserve(BH_Queue *queue,
|
|||||||
return BH_OOM;
|
return BH_OOM;
|
||||||
|
|
||||||
/* Iterate over old queue and insert data into new queue */
|
/* Iterate over old queue and insert data into new queue */
|
||||||
BH_QueueCopy(&other, queue);
|
queueCopy(&other, queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If old queue had allocated data - free it */
|
/* If old queue had allocated data - free it */
|
||||||
|
|||||||
@@ -55,21 +55,21 @@ static void dragonFixup(struct DragonState *state,
|
|||||||
/* Account for unqual gaps */
|
/* Account for unqual gaps */
|
||||||
if (f == (((uint64_t)1) << 52))
|
if (f == (((uint64_t)1) << 52))
|
||||||
{
|
{
|
||||||
MpiLsh(&state->mp, 1, &state->mp);
|
mpiLsh(&state->mp, 1, &state->mp);
|
||||||
MpiLsh(&state->r, 1, &state->r);
|
mpiLsh(&state->r, 1, &state->r);
|
||||||
MpiLsh(&state->s, 1, &state->s);
|
mpiLsh(&state->s, 1, &state->s);
|
||||||
}
|
}
|
||||||
state->k = 0;
|
state->k = 0;
|
||||||
|
|
||||||
/* Burger/Dybvig approach */
|
/* Burger/Dybvig approach */
|
||||||
#ifndef BH_TWEAK_SHORT_BINT
|
#ifndef BH_TWEAK_SHORT_BINT
|
||||||
state->k = MpiClz((f >> 32) & MPI_MASK);
|
state->k = mpiClz((f >> 32) & MPI_MASK);
|
||||||
state->k += (state->k == 32) ? (MpiClz(f & MPI_MASK)) : (0);
|
state->k += (state->k == 32) ? (mpiClz(f & MPI_MASK)) : (0);
|
||||||
#else
|
#else
|
||||||
state->k = MpiClz((f >> 48) & MPI_MASK);
|
state->k = mpiClz((f >> 48) & MPI_MASK);
|
||||||
state->k += (state->k == 16) ? (MpiClz((f >> 32) & MPI_MASK)) : (0);
|
state->k += (state->k == 16) ? (mpiClz((f >> 32) & MPI_MASK)) : (0);
|
||||||
state->k += (state->k == 32) ? (MpiClz((f >> 16) & MPI_MASK)) : (0);
|
state->k += (state->k == 32) ? (mpiClz((f >> 16) & MPI_MASK)) : (0);
|
||||||
state->k += (state->k == 48) ? (MpiClz(f & MPI_MASK)) : (0);
|
state->k += (state->k == 48) ? (mpiClz(f & MPI_MASK)) : (0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* 77 / 256 is an approximation for Log(2) or 0.30102999 */
|
/* 77 / 256 is an approximation for Log(2) or 0.30102999 */
|
||||||
@@ -82,18 +82,18 @@ static void dragonFixup(struct DragonState *state,
|
|||||||
/* Scale numbers accordinaly */
|
/* Scale numbers accordinaly */
|
||||||
if (state->k < 0)
|
if (state->k < 0)
|
||||||
{
|
{
|
||||||
MpiPow10(&state->r, -state->k, &state->r, state->tmp);
|
mpiPow10(&state->r, -state->k, &state->r, state->tmp);
|
||||||
MpiPow10(&state->mm, -state->k, &state->mm, state->tmp);
|
mpiPow10(&state->mm, -state->k, &state->mm, state->tmp);
|
||||||
MpiPow10(&state->mp, -state->k, &state->mp, state->tmp);
|
mpiPow10(&state->mp, -state->k, &state->mp, state->tmp);
|
||||||
}
|
}
|
||||||
else if (state->k > 0)
|
else if (state->k > 0)
|
||||||
MpiPow10(&state->s, state->k, &state->s, state->tmp);
|
mpiPow10(&state->s, state->k, &state->s, state->tmp);
|
||||||
|
|
||||||
/* Scale S if we underestimated */
|
/* Scale S if we underestimated */
|
||||||
if (MpiCompare(&state->r, &state->s) >= 0)
|
if (mpiCompare(&state->r, &state->s) >= 0)
|
||||||
{
|
{
|
||||||
state->k += 1;
|
state->k += 1;
|
||||||
MpiMulDigit(&state->s, 10, &state->s);
|
mpiMulDigit(&state->s, 10, &state->s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find cutoff */
|
/* Find cutoff */
|
||||||
@@ -122,8 +122,8 @@ static void dragonRound(struct DragonState *state,
|
|||||||
/* Check if rounding up required */
|
/* Check if rounding up required */
|
||||||
if (high == low)
|
if (high == low)
|
||||||
{
|
{
|
||||||
MpiLsh(&state->r, 1, &state->tmp[0]);
|
mpiLsh(&state->r, 1, &state->tmp[0]);
|
||||||
i = MpiCompare(&state->tmp[0], &state->s);
|
i = mpiCompare(&state->tmp[0], &state->s);
|
||||||
if (i < 0) { low = 1; high = 0; }
|
if (i < 0) { low = 1; high = 0; }
|
||||||
else if (i > 0) { low = 0; high = 1; }
|
else if (i > 0) { low = 0; high = 1; }
|
||||||
else low = (((s - '0') & 0x1) == 0);
|
else low = (((s - '0') & 0x1) == 0);
|
||||||
@@ -185,12 +185,12 @@ static void dragon(double value,
|
|||||||
state.r.data[3] = (f >> 48) & MPI_MASK;
|
state.r.data[3] = (f >> 48) & MPI_MASK;
|
||||||
state.r.size = 4;
|
state.r.size = 4;
|
||||||
#endif
|
#endif
|
||||||
MpiTrim(&state.r);
|
mpiTrim(&state.r);
|
||||||
|
|
||||||
MpiLsh(&state.r, MAX(e - 53, 0), &state.r);
|
mpiLsh(&state.r, MAX(e - 53, 0), &state.r);
|
||||||
MpiLsh(&BInt1, MAX(0, -(e - 53)), &state.s);
|
mpiLsh(&BInt1, MAX(0, -(e - 53)), &state.s);
|
||||||
MpiLsh(&BInt1, MAX(e - 53, 0), &state.mm);
|
mpiLsh(&BInt1, MAX(e - 53, 0), &state.mm);
|
||||||
MpiLsh(&BInt1, MAX(e - 53, 0), &state.mp);
|
mpiLsh(&BInt1, MAX(e - 53, 0), &state.mp);
|
||||||
dragonFixup(&state, precision, mode, f, e);
|
dragonFixup(&state, precision, mode, f, e);
|
||||||
|
|
||||||
/* Main digit generation loop */
|
/* Main digit generation loop */
|
||||||
@@ -198,8 +198,8 @@ static void dragon(double value,
|
|||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
state.k -= 1;
|
state.k -= 1;
|
||||||
MpiMulDigit(&state.r, 10, &state.r);
|
mpiMulDigit(&state.r, 10, &state.r);
|
||||||
MpiDiv(&state.r, &state.s, &state.tmp[0], &state.r, &state.tmp[1]);
|
mpiDiv(&state.r, &state.s, &state.tmp[0], &state.r, &state.tmp[1]);
|
||||||
|
|
||||||
s = '0';
|
s = '0';
|
||||||
if (state.tmp[0].size)
|
if (state.tmp[0].size)
|
||||||
@@ -208,13 +208,13 @@ static void dragon(double value,
|
|||||||
|
|
||||||
if (mode == NORMAL)
|
if (mode == NORMAL)
|
||||||
{
|
{
|
||||||
MpiMulDigit(&state.mm, 10, &state.mm);
|
mpiMulDigit(&state.mm, 10, &state.mm);
|
||||||
MpiMulDigit(&state.mp, 10, &state.mp);
|
mpiMulDigit(&state.mp, 10, &state.mp);
|
||||||
MpiLsh(&state.r, 1, &state.tmp[1]);
|
mpiLsh(&state.r, 1, &state.tmp[1]);
|
||||||
MpiLsh(&state.s, 1, &state.tmp[2]);
|
mpiLsh(&state.s, 1, &state.tmp[2]);
|
||||||
MpiAdd(&state.tmp[1], &state.mp, &state.tmp[3]);
|
mpiAdd(&state.tmp[1], &state.mp, &state.tmp[3]);
|
||||||
low = MpiCompare(&state.tmp[1], &state.mm) < 0;
|
low = mpiCompare(&state.tmp[1], &state.mm) < 0;
|
||||||
high = MpiCompare(&state.tmp[3], &state.tmp[2]) > 0;
|
high = mpiCompare(&state.tmp[3], &state.tmp[2]) > 0;
|
||||||
if (low || high || state.k == state.cutoff || buffer->size >= BUFSIZE)
|
if (low || high || state.k == state.cutoff || buffer->size >= BUFSIZE)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -668,43 +668,43 @@ double BH_StringToDouble(const char *string,
|
|||||||
for (i = 0; i < count; i++)
|
for (i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
tmp[0].data[0] = buffer[i] - '0';
|
tmp[0].data[0] = buffer[i] - '0';
|
||||||
MpiMulDigit(&r, 10, &r);
|
mpiMulDigit(&r, 10, &r);
|
||||||
MpiAdd(&r, &tmp[0], &r);
|
mpiAdd(&r, &tmp[0], &r);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (e >= 0)
|
if (e >= 0)
|
||||||
MpiPow10(&r, e, &r, &tmp[0]);
|
mpiPow10(&r, e, &r, &tmp[0]);
|
||||||
else
|
else
|
||||||
MpiPow10(&s, -e, &s, &tmp[0]);
|
mpiPow10(&s, -e, &s, &tmp[0]);
|
||||||
|
|
||||||
/* Calculate required shift */
|
/* Calculate required shift */
|
||||||
shift = -52;
|
shift = -52;
|
||||||
if (MpiCompare(&r, &s) >= 0)
|
if (mpiCompare(&r, &s) >= 0)
|
||||||
{
|
{
|
||||||
MpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
|
mpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
|
||||||
shift += MpiLog2(&tmp[0]);
|
shift += mpiLog2(&tmp[0]);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
MpiDiv(&s, &r, &tmp[0], &tmp[1], &tmp[2]);
|
mpiDiv(&s, &r, &tmp[0], &tmp[1], &tmp[2]);
|
||||||
shift += -(MpiLog2(&tmp[0]) + 1);
|
shift += -(mpiLog2(&tmp[0]) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Shift */
|
/* Shift */
|
||||||
if (shift > 0)
|
if (shift > 0)
|
||||||
MpiLsh(&s, shift, &s);
|
mpiLsh(&s, shift, &s);
|
||||||
else if (shift < 0)
|
else if (shift < 0)
|
||||||
MpiLsh(&r, -shift, &r);
|
mpiLsh(&r, -shift, &r);
|
||||||
|
|
||||||
/* Calculate final exponent and 53 bit integer */
|
/* Calculate final exponent and 53 bit integer */
|
||||||
MpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
|
mpiDiv(&r, &s, &tmp[0], &tmp[1], &tmp[2]);
|
||||||
MpiRsh(&s, 1, &s);
|
mpiRsh(&s, 1, &s);
|
||||||
if (MpiCompare(&tmp[1], &s) > 0 || (MpiCompare(&tmp[1], &s) == 0 && (tmp[0].data[0] & 0x1)))
|
if (mpiCompare(&tmp[1], &s) > 0 || (mpiCompare(&tmp[1], &s) == 0 && (tmp[0].data[0] & 0x1)))
|
||||||
{
|
{
|
||||||
MpiAdd(&tmp[0], &BInt1, &tmp[0]);
|
mpiAdd(&tmp[0], &BInt1, &tmp[0]);
|
||||||
if (MpiCompare(&tmp[0], &BInt53) >= 0)
|
if (mpiCompare(&tmp[0], &BInt53) >= 0)
|
||||||
{
|
{
|
||||||
MpiRsh(&tmp[0], 1, &tmp[0]);
|
mpiRsh(&tmp[0], 1, &tmp[0]);
|
||||||
shift++;
|
shift++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ static const Mpi powLookup[] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static int MpiClz(MPI_TYPE value)
|
static int mpiClz(MPI_TYPE value)
|
||||||
{
|
{
|
||||||
if (value & 0xFF000000ul)
|
if (value & 0xFF000000ul)
|
||||||
return clzLookup[(value >> 24) & 0xFF];
|
return clzLookup[(value >> 24) & 0xFF];
|
||||||
@@ -110,7 +110,7 @@ static const Mpi powLookup[] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static int MpiClz(MPI_TYPE value)
|
static int mpiClz(MPI_TYPE value)
|
||||||
{
|
{
|
||||||
if (value & 0xFF00)
|
if (value & 0xFF00)
|
||||||
return clzLookup[(value >> 8) & 0xFF];
|
return clzLookup[(value >> 8) & 0xFF];
|
||||||
@@ -120,18 +120,18 @@ static int MpiClz(MPI_TYPE value)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int MpiLog2(const Mpi *in)
|
static int mpiLog2(const Mpi *in)
|
||||||
{
|
{
|
||||||
/* Preconditions */
|
/* Preconditions */
|
||||||
assert(in != NULL);
|
assert(in != NULL);
|
||||||
assert(in->size != 0);
|
assert(in->size != 0);
|
||||||
assert(in->data[in->size - 1] != 0);
|
assert(in->data[in->size - 1] != 0);
|
||||||
|
|
||||||
return (MPI_BITS - 1) - MpiClz(in->data[in->size - 1]) + MPI_BITS * (in->size - 1);
|
return (MPI_BITS - 1) - mpiClz(in->data[in->size - 1]) + MPI_BITS * (in->size - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiTrim(Mpi *in)
|
static void mpiTrim(Mpi *in)
|
||||||
{
|
{
|
||||||
/* Preconditions */
|
/* Preconditions */
|
||||||
assert(in != NULL);
|
assert(in != NULL);
|
||||||
@@ -141,7 +141,7 @@ static void MpiTrim(Mpi *in)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int MpiCompare(const Mpi *a,
|
static int mpiCompare(const Mpi *a,
|
||||||
const Mpi *b)
|
const Mpi *b)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -167,7 +167,7 @@ static int MpiCompare(const Mpi *a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiAdd(const Mpi *a,
|
static void mpiAdd(const Mpi *a,
|
||||||
const Mpi *b,
|
const Mpi *b,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -200,7 +200,7 @@ static void MpiAdd(const Mpi *a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiSub(const Mpi *a,
|
static void mpiSub(const Mpi *a,
|
||||||
const Mpi *b,
|
const Mpi *b,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -209,7 +209,7 @@ static void MpiSub(const Mpi *a,
|
|||||||
|
|
||||||
/* Preconditions */
|
/* Preconditions */
|
||||||
assert(a != NULL && b != NULL && out != NULL);
|
assert(a != NULL && b != NULL && out != NULL);
|
||||||
assert(MpiCompare(a, b) >= 0);
|
assert(mpiCompare(a, b) >= 0);
|
||||||
|
|
||||||
/* Main subtraction loop */
|
/* Main subtraction loop */
|
||||||
carry = 0;
|
carry = 0;
|
||||||
@@ -227,11 +227,11 @@ static void MpiSub(const Mpi *a,
|
|||||||
|
|
||||||
/* Trim leading zeros */
|
/* Trim leading zeros */
|
||||||
out->size = a->size;
|
out->size = a->size;
|
||||||
MpiTrim(out);
|
mpiTrim(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiMul(const Mpi *a,
|
static void mpiMul(const Mpi *a,
|
||||||
const Mpi *b,
|
const Mpi *b,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -261,11 +261,11 @@ static void MpiMul(const Mpi *a,
|
|||||||
|
|
||||||
/* Trim leading zeros */
|
/* Trim leading zeros */
|
||||||
out->size = a->size + b->size;
|
out->size = a->size + b->size;
|
||||||
MpiTrim(out);
|
mpiTrim(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiMulDigit(const Mpi *a,
|
static void mpiMulDigit(const Mpi *a,
|
||||||
MPI_TYPE b,
|
MPI_TYPE b,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -288,11 +288,11 @@ static void MpiMulDigit(const Mpi *a,
|
|||||||
|
|
||||||
/* Trim leading zeros */
|
/* Trim leading zeros */
|
||||||
out->size = a->size + 1;
|
out->size = a->size + 1;
|
||||||
MpiTrim(out);
|
mpiTrim(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiPow10(const Mpi *in,
|
static void mpiPow10(const Mpi *in,
|
||||||
int exponent,
|
int exponent,
|
||||||
Mpi *out,
|
Mpi *out,
|
||||||
Mpi *tmp)
|
Mpi *tmp)
|
||||||
@@ -309,14 +309,14 @@ static void MpiPow10(const Mpi *in,
|
|||||||
if (!(exponent & 0x1))
|
if (!(exponent & 0x1))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
MpiMul(&tmp[current], &powLookup[i], &tmp[1 - current]);
|
mpiMul(&tmp[current], &powLookup[i], &tmp[1 - current]);
|
||||||
current = 1 - current;
|
current = 1 - current;
|
||||||
}
|
}
|
||||||
*out = tmp[current];
|
*out = tmp[current];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiLsh(const Mpi *in,
|
static void mpiLsh(const Mpi *in,
|
||||||
int amount,
|
int amount,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -356,13 +356,13 @@ static void MpiLsh(const Mpi *in,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Trim leading zeros and zero out lower blocks */
|
/* Trim leading zeros and zero out lower blocks */
|
||||||
MpiTrim(out);
|
mpiTrim(out);
|
||||||
for (i = blocks; i; i--)
|
for (i = blocks; i; i--)
|
||||||
out->data[i - 1] = 0;
|
out->data[i - 1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiRsh(const Mpi *in,
|
static void mpiRsh(const Mpi *in,
|
||||||
int amount,
|
int amount,
|
||||||
Mpi *out)
|
Mpi *out)
|
||||||
{
|
{
|
||||||
@@ -404,11 +404,11 @@ static void MpiRsh(const Mpi *in,
|
|||||||
|
|
||||||
/* Trim leading zeros */
|
/* Trim leading zeros */
|
||||||
out->size = in->size - blocks;
|
out->size = in->size - blocks;
|
||||||
MpiTrim(out);
|
mpiTrim(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static MPI_TTYPE MpiGuess(const Mpi *a,
|
static MPI_TTYPE mpiGuess(const Mpi *a,
|
||||||
const Mpi *b)
|
const Mpi *b)
|
||||||
{
|
{
|
||||||
MPI_TTYPE tmp;
|
MPI_TTYPE tmp;
|
||||||
@@ -418,7 +418,7 @@ static MPI_TTYPE MpiGuess(const Mpi *a,
|
|||||||
assert(a->size > 0 && b->size > 0);
|
assert(a->size > 0 && b->size > 0);
|
||||||
assert((a->size == b->size) || ((a->size != b->size) && a->size > 1));
|
assert((a->size == b->size) || ((a->size != b->size) && a->size > 1));
|
||||||
|
|
||||||
if (MpiCompare(a, b) < 0)
|
if (mpiCompare(a, b) < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
tmp = a->data[a->size - 1];
|
tmp = a->data[a->size - 1];
|
||||||
@@ -429,7 +429,7 @@ static MPI_TTYPE MpiGuess(const Mpi *a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void MpiDiv(const Mpi *a,
|
static void mpiDiv(const Mpi *a,
|
||||||
const Mpi *b,
|
const Mpi *b,
|
||||||
Mpi *q,
|
Mpi *q,
|
||||||
Mpi *r,
|
Mpi *r,
|
||||||
@@ -443,7 +443,7 @@ static void MpiDiv(const Mpi *a,
|
|||||||
assert(b->size != 0);
|
assert(b->size != 0);
|
||||||
|
|
||||||
/* Handle case where a is less then b */
|
/* Handle case where a is less then b */
|
||||||
if (MpiCompare(a, b) < 0)
|
if (mpiCompare(a, b) < 0)
|
||||||
{
|
{
|
||||||
*r = *a;
|
*r = *a;
|
||||||
q->size = 0;
|
q->size = 0;
|
||||||
@@ -451,16 +451,16 @@ static void MpiDiv(const Mpi *a,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Normilize input to reduce tries */
|
/* Normilize input to reduce tries */
|
||||||
shift = MpiClz(b->data[b->size - 1]);
|
shift = mpiClz(b->data[b->size - 1]);
|
||||||
MpiLsh(a, shift, &tmp[0]);
|
mpiLsh(a, shift, &tmp[0]);
|
||||||
MpiLsh(b, shift, &tmp[1]);
|
mpiLsh(b, shift, &tmp[1]);
|
||||||
|
|
||||||
/* Prepare first step of the division */
|
/* Prepare first step of the division */
|
||||||
q->size = 0;
|
q->size = 0;
|
||||||
r->size = 0;
|
r->size = 0;
|
||||||
while (MpiCompare(r, &tmp[1]) < 0)
|
while (mpiCompare(r, &tmp[1]) < 0)
|
||||||
{
|
{
|
||||||
MpiLsh(r, MPI_BITS, r);
|
mpiLsh(r, MPI_BITS, r);
|
||||||
r->data[0] = tmp[0].data[--tmp[0].size];
|
r->data[0] = tmp[0].data[--tmp[0].size];
|
||||||
r->size += !r->size;
|
r->size += !r->size;
|
||||||
}
|
}
|
||||||
@@ -468,19 +468,19 @@ static void MpiDiv(const Mpi *a,
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
/* Make a guess and check */
|
/* Make a guess and check */
|
||||||
digit = MpiGuess(r, &tmp[1]);
|
digit = mpiGuess(r, &tmp[1]);
|
||||||
while (digit > MPI_MASK)
|
while (digit > MPI_MASK)
|
||||||
digit--;
|
digit--;
|
||||||
MpiMulDigit(&tmp[1], digit, &tmp[2]);
|
mpiMulDigit(&tmp[1], digit, &tmp[2]);
|
||||||
while (MpiCompare(r, &tmp[2]) < 0)
|
while (mpiCompare(r, &tmp[2]) < 0)
|
||||||
{
|
{
|
||||||
--digit;
|
--digit;
|
||||||
MpiSub(&tmp[2], &tmp[1], &tmp[2]);
|
mpiSub(&tmp[2], &tmp[1], &tmp[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store digit in quotient */
|
/* Store digit in quotient */
|
||||||
MpiSub(r, &tmp[2], r);
|
mpiSub(r, &tmp[2], r);
|
||||||
MpiLsh(q, MPI_BITS, q);
|
mpiLsh(q, MPI_BITS, q);
|
||||||
q->data[0] = digit;
|
q->data[0] = digit;
|
||||||
q->size += !q->size;
|
q->size += !q->size;
|
||||||
|
|
||||||
@@ -488,12 +488,12 @@ static void MpiDiv(const Mpi *a,
|
|||||||
if (!tmp[0].size)
|
if (!tmp[0].size)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
MpiLsh(r, MPI_BITS, r);
|
mpiLsh(r, MPI_BITS, r);
|
||||||
r->data[0] = tmp[0].data[--tmp[0].size];
|
r->data[0] = tmp[0].data[--tmp[0].size];
|
||||||
if (!r->size)
|
if (!r->size)
|
||||||
r->size = 1;
|
r->size = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Normilize remainder */
|
/* Normilize remainder */
|
||||||
MpiRsh(r, shift, r);
|
mpiRsh(r, shift, r);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ typedef struct BH_Unit
|
|||||||
static BH_Unit *root = NULL;
|
static BH_Unit *root = NULL;
|
||||||
|
|
||||||
|
|
||||||
static void BH_UnitCleanup(void)
|
static void cleanup(void)
|
||||||
{
|
{
|
||||||
BH_Unit *current;
|
BH_Unit *current;
|
||||||
|
|
||||||
@@ -75,6 +75,6 @@ int BH_UnitRun(void)
|
|||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
BH_UnitCleanup();
|
cleanup();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user