]> foleosoft.com Git - QAnsel.git/commitdiff
Thu Aug 15 06:51:01 PM EDT 2024
authormiha-q <>
Thu, 15 Aug 2024 22:51:01 +0000 (18:51 -0400)
committermiha-q <>
Thu, 15 Aug 2024 22:51:01 +0000 (18:51 -0400)
src/bytecode.c
src/context.c
src/context.h
src/hardware.c
src/hardware.h
src/qansel.h

index 1ae2df9f7232431c6b43cfa59ecbcd7ee0680699..550e6d728e363f916704e34adb5a0190638f9b7f 100644 (file)
@@ -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;
index caefb6b80950fad86ae5a8e6711e687830e9f154..916f02b3d80203332e6d4296493b60ca98559a13 100644 (file)
@@ -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;
 }
index a15b9f29fa82f38b550cc7298f84c29429507e39..40a4626e2d9a9b869c914a94b7d7a24a925fb694 100644 (file)
@@ -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;
 
index ab3c5ab5e258743d2e31ecbe52f171402a71109a..25921f9cd0562b78cb68ee13dd899a5bb96160d3 100644 (file)
@@ -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
index 31e0a8a61524bf2e184fac0c0585174691f43b1f..7e2ae2a525ac65176c0cee5b0dada30dac3e3ee7 100644 (file)
@@ -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
index a72eba2578ed11f29cc8d8a5baa835bb30980ab3..3059ddc19bbc758b3e529cb408a3e1deb0296228 100644 (file)
@@ -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