From 508dea5e012e40f2fee80da868fab934e43b71cc Mon Sep 17 00:00:00 2001 From: Cem Date: Sun, 5 Jul 2026 00:03:40 +0200 Subject: [PATCH] fixed: switch statement out of the benchmarking + parsing no longer accepts invalid floating numbers --- Implementierung/main.c | 127 +++++++++++++++++++++++++++++------------ 1 file changed, 92 insertions(+), 35 deletions(-) diff --git a/Implementierung/main.c b/Implementierung/main.c index b60eab4..af4891e 100644 --- a/Implementierung/main.c +++ b/Implementierung/main.c @@ -27,41 +27,61 @@ unsigned tests(void) { void benchmarking(size_t benchmarking_amount, int version_to_use, float complex complexA, size_t width, ssize_t height, float resolution, unsigned n, _Bool colored, unsigned char *tempArr) { struct timespec start; - clock_gettime(CLOCK_MONOTONIC, &start); - for (size_t i = 0; i < benchmarking_amount; i++) - { + struct timespec end; switch (version_to_use) { - case 0: - // Uses highly optimized SIMD and threaded version / main implementation - tricorn(complexA,width,height,resolution,n,colored,tempArr); + case 0: // Uses highly optimized SIMD and threaded version / main implementation + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; - case 1: - // Uses unoptimised comparison implementation - tricorn_V1(complexA,width,height,resolution,n,colored,tempArr); + case 1: // Uses unoptimised comparison implementation + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn_V1(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; - case 2: - // Uses optimized SISD version - tricorn_V2(complexA,width,height,resolution,n,colored,tempArr); + case 2: // Uses optimized SISD version + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn_V2(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; - case 3: - // Uses unoptimized SIMD version - tricorn_V3(complexA,width,height,resolution,n,colored,tempArr); + case 3: // Uses unoptimized SIMD version + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn_V3(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; - case 4: - // Uses highly optimized SIMD - tricorn_V4(complexA,width,height,resolution,n,colored,tempArr); + case 4: // Uses highly optimized SIMD + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn_V4(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; - case 5: - // Experimental cleanups and inlines / for testing - tricorn_V5(complexA,width,height,resolution,n,colored,tempArr); + case 5: // Experimental cleanups and inlines / for testing + clock_gettime(CLOCK_MONOTONIC, &start); + for (size_t i = 0; i < benchmarking_amount; i++) + { + tricorn_V5(complexA,width,height,resolution,n,colored,tempArr); + } + clock_gettime(CLOCK_MONOTONIC, &end); break; default: - break; + printf("Invalid version to use"); + return; } - } - struct timespec end; - clock_gettime(CLOCK_MONOTONIC, &end); double time = end.tv_sec- start.tv_sec + 1e-9 * (end.tv_nsec- start.tv_nsec); printf("The total amount of time spent for %zu function calls: %lf\n", benchmarking_amount,time); double average = time / benchmarking_amount; @@ -138,6 +158,7 @@ int main(int argc, char** argv) int version_to_use = 0; // actual initial value _Bool colored = 0; // actual initial value + _Bool will_benchmark = false; // actual initial value int amount_function_called = 0; // initial values, so that the compiler doesn't complain unsigned n = 0; @@ -147,7 +168,6 @@ int main(int argc, char** argv) ssize_t height = 0; float resolution = 0; char* output_data = 0; - _Bool will_benchmark = false; opt_long = getopt_long(argc,argv,optstring, long_options, NULL); @@ -168,7 +188,7 @@ int main(int argc, char** argv) case 'B': // done, function needed // wird die Laufzeit der angegebenen Implementierung gemessen und ausgegeben (do this later) - int amount_function_called = strtol(optarg,NULL,10); + amount_function_called = strtol(optarg,NULL,10); if (amount_function_called <= 0) { printf("Invalid benchmarking input, the value should be greater than 0\n"); return 1; @@ -180,23 +200,53 @@ int main(int argc, char** argv) // Das Argument dieser Option gibt die Anzahl an Wiederholungen des Funktionsaufrufs an, z. B.-B 5 . break; - case 's': - char* imaginary_string; + case 's': { + char *imaginary_string; + char *end; + starting_point_real = strtof(optarg,&imaginary_string); // split into two - starting_point_imaginary = strtof(imaginary_string + 1,NULL); // this is horrible + if (imaginary_string == optarg || *imaginary_string != ',') + { + fprintf(stderr, "Invalid first number or missing comma: %s\n",optarg); + return EXIT_FAILURE; + } + + starting_point_imaginary = strtof(imaginary_string + 1, &end); // second part, the imaginary part + if (end == imaginary_string + 1 || *end != '\0') + { + fprintf(stderr, "Invalid second number or extra characters: %s\n",optarg); + return EXIT_FAILURE; + } + printf("real %f, img %f\n", starting_point_real,starting_point_imaginary); // s , — Startpunkt der Berechnung break; + } - case 'd': + case 'd': { char* height_string; + char *end; width = strtol(optarg,&height_string,10); - height = strtol(height_string + 1,NULL,10); + + if (height_string == optarg || *height_string != ',') + { + fprintf(stderr, "Invalid width or missing comma: %s\n",optarg); + return EXIT_FAILURE; + } + + height = strtol(height_string + 1,&end,10); + + if (end == height_string + 1 || *end != '\0') + { + fprintf(stderr, "Invalid height or extra characters: %s\n",optarg); + return EXIT_FAILURE; + } printf("width %zu, height %zd\n", width,height); // -d , —Breite und Höhe des Bildausschnitts break; + } case 'n': n = strtol(optarg,NULL,10); @@ -204,11 +254,18 @@ int main(int argc, char** argv) // -n —MaximaleAnzahlanIterationen pro Bildpixel break; - case 'r': - resolution = strtof(optarg,NULL); + case 'r': { + char *end; + resolution = strtof(optarg,&end); + if (end == optarg || *end != '\0') + { + fprintf(stderr, "Invalid floating point number: %s\n",optarg); + return EXIT_FAILURE; + } printf("r:%f\n", resolution); // r —Schrittweite pro Bildpixel break; + } case 'o': output_data = malloc(strlen(optarg) + 1); @@ -218,7 +275,7 @@ int main(int argc, char** argv) break; case 'C': // done - colored = 1; + colored = true; printf("colored:%d\n",colored); // C|--color —Farbausgabe break;