From 739829b30e93299ee4485acb80c2104f9220b109 Mon Sep 17 00:00:00 2001 From: miha-q <> Date: Thu, 15 Aug 2024 18:51:01 -0400 Subject: [PATCH] Thu Aug 15 06:51:01 PM EDT 2024 --- src/bytecode.c | 4 ++-- src/context.c | 8 ++++---- src/context.h | 3 +-- src/hardware.c | 9 +++++---- src/hardware.h | 2 +- src/qansel.h | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/bytecode.c b/src/bytecode.c index 1ae2df9..550e6d7 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -65,9 +65,9 @@ float qansel_rand_t(QAnselContext* ctx) case QANSEL_HARDWARE_QUANTIS: r = ctx->chunk[ctx->pointer]; ctx->pointer += 1; - if (ctx->pointer >= QANSEL_QUANTIS_CHUNK) + if (ctx->pointer >= QANSEL_QUANTIS_CHUNK_SIZE) { - qansel_quantis_chunk(ctx->chunk); + qansel_quantis_chunk(&(ctx->chunk)); ctx->pointer = 0; } break; diff --git a/src/context.c b/src/context.c index caefb6b..916f02b 100644 --- a/src/context.c +++ b/src/context.c @@ -43,6 +43,7 @@ int qanselContextValidate(QAnselContext* ctx) int qanselContextBegin(QAnselContext* ctx) { + ctx->chunk = NULL; if (ctx->memory_limit == 0) ctx->memory_limit = QANSEL_QUBITS_MAX; if (ctx->memory_limit > QANSEL_QUBITS_MAX) { @@ -89,16 +90,15 @@ int qanselContextBegin(QAnselContext* ctx) else { if (ctx->verbose) printf("QAnsel: Hardware `TrueRNG` selected.\n"); - strcpy(ctx->random_path, "/dev/TrueRNG0"); ctx->hardware_rng = QANSEL_HARDWARE_TRUERNG; } } else { + fclose(ctx->random_file); if (ctx->verbose) printf("QAnsel: Hardware `Quantis` selected.\n"); ctx->hardware_rng = QANSEL_HARDWARE_QUANTIS; - ctx->pointer = 0; - qansel_quantis_chunk(ctx->chunk); + ctx->pointer = QUANTIS_CHUNK_SIZE; } } if (ctx->optimization_level & QANSEL_MODE_METAL) @@ -122,6 +122,6 @@ int qanselContextEnd(QAnselContext* ctx) { if (ctx->random_file != NULL) fclose(ctx->random_file); if (ctx->optimization_level & QANSEL_MODE_METAL) cpx_mtx_clean(); - if (ctx->hardware_rng == QANSEL_HARDWARE_QUANTIS) free(ctx->chunk); + if (ctx->chunk != NULL) free(ctx->chunk); return 1; } diff --git a/src/context.h b/src/context.h index a15b9f2..40a4626 100644 --- a/src/context.h +++ b/src/context.h @@ -20,8 +20,7 @@ typedef struct int qubit_count; int bit_count; int pointer; - char* chunk; - char random_path[32]; + unsigned char* chunk; FILE* random_file; } QAnselContext; diff --git a/src/hardware.c b/src/hardware.c index ab3c5ab..25921f9 100644 --- a/src/hardware.c +++ b/src/hardware.c @@ -125,16 +125,17 @@ float qansel_hardware_getseed() //return seed; } -void qansel_quantis_chunk(unsigned char* chunk) +void qansel_quantis_chunk(unsigned char** chunk) { - chunk = malloc(QUANTIS_CHUNK_SIZE); + if ((*chunk) != NULL) free(*chunk); + *chunk = malloc(QUANTIS_CHUNK_SIZE); FILE* f = fopen("/dev/qrandom0", "r"); unsigned char c = fgetc(f); while (c == 0x00) c = fgetc(f); - chunk[0] = c; + (*chunk)[0] = c; for (int i = 0; i < QUANTIS_CHUNK_SIZE - 1; i++) { - chunk[i + 1] = fgetc(f); + (*chunk)[i + 1] = fgetc(f); } fclose(f); } \ No newline at end of file diff --git a/src/hardware.h b/src/hardware.h index 31e0a8a..7e2ae2a 100644 --- a/src/hardware.h +++ b/src/hardware.h @@ -21,6 +21,6 @@ unsigned long int qansel_get_time(); int qansel_hardware_rand_supported(); unsigned char qansel_hardware_rand(); float qansel_hardware_getseed(); -void qansel_quantis_chunk(unsigned char* chunk); +void qansel_quantis_chunk(unsigned char** chunk); #endif diff --git a/src/qansel.h b/src/qansel.h index a72eba2..3059ddc 100644 --- a/src/qansel.h +++ b/src/qansel.h @@ -16,7 +16,7 @@ #define QANSEL_HARDWARE_TRUERNG 2 #define QANSEL_HARDWARE_RDSEED 3 #define QANSEL_HARDWARE_QUANTIS 4 -#define QANSEL_QUANTIS_CHUNK 4096 +#define QANSEL_QUANTIS_CHUNK_SIZE 4096 #define QANSEL_INSTRUCTION_X 0x10 #define QANSEL_INSTRUCTION_Y 0x11 #define QANSEL_INSTRUCTION_Z 0x12 -- 2.39.5