Difference between
modified post 7bfc by Anonymous on Fri 27th Mar 2026 13:22
original post 3f7376c09 by Anonymous on Wed 18th Mar 2026 19:26
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | /* | |
| 2 | 2 | * MIT License | |
| 3 | 3 | * | |
| 4 | 4 | * Copyright (c) 2026 Roland Mainz <roland.mainz@nrubsig.org> | |
| 5 | 5 | * | |
| 6 | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 7 | 7 | * of this software and associated documentation files (the "Software"), to deal | |
| 8 | 8 | * in the Software without restriction, including without limitation the rights | |
| 9 | 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 10 | 10 | * copies of the Software, and to permit persons to whom the Software is | |
| 11 | 11 | * furnished to do so, subject to the following conditions: | |
| 12 | 12 | * | |
| 13 | 13 | * The above copyright notice and this permission notice shall be included in all | |
| 14 | 14 | * copies or substantial portions of the Software. | |
| 15 | 15 | * | |
| 16 | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 17 | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 18 | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 19 | 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 20 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 22 | 22 | * SOFTWARE. | |
| 23 | 23 | */ | |
| 24 | 24 | ||
| 25 | 25 | /* | |
| 26 | 26 | * rov_int_mandelbrot_test1.c - math test module for |template <typename T> class rov_int_template| | |
| 27 | 27 | * | |
| 28 | 28 | * WARNING: This module must be compileable with ISO C and ISO C++ compilers | |
| 29 | 29 | * for benchmarking and testing purposes! | |
| 30 | 30 | */ | |
| 31 | 31 | ||
| 32 | 32 | #include <stdio.h> | |
| 33 | 33 | #include <stdlib.h> | |
| 34 | 34 | #include <stdint.h> | |
| 35 | 35 | ||
| 36 | + | #include <iostream> | |
| 37 | 37 | ||
| 38 | + | #define USE_ROV_INT_TEMPLATE002_TYPES 1 | |
| 38 | 40 | #ifdef USE_ROV_INT_TEMPLATE002_TYPES | |
| 39 | 41 | /* | |
| 40 | 42 | * Compile with ISO C++ integer wrapper class | |
| 41 | 43 | */ | |
| 42 | 44 | #include "rov_int_template002.h" | |
| 43 | 45 | ||
| 44 | 46 | typedef rov_int_template<int32_t> test_int32_t; | |
| 45 | 47 | typedef rov_int_template<int64_t> test_int64_t; | |
| 46 | 48 | #else | |
| 47 | 49 | /* | |
| 48 | 50 | * Compile with ISO C99/C11 compiler | |
| 49 | 51 | */ | |
| 50 | 52 | #error XXX | |
| 51 | 53 | typedef int32_t test_int32_t; | |
| 52 | 54 | typedef int64_t test_int64_t; | |
| 53 | 55 | #endif /* USE_ROV_INT_TEMPLATE002_TYPES */ | |
| 54 | 56 | ||
| 55 | 57 | /* Mandelbrot parameters */ | |
| 56 | 58 | #define MANDELBROT_VIEW_WIDTH (132LL) /*72*/ | |
| 57 | - | #define MANDELBROT_VIEW_HEIGHT (50LL) | |
| 59 | + | #define MANDELBROT_VIEW_HEIGHT (3*25LL) | |
| 58 | - | #define MANDELBROT_VIEW_MAX_ITER (100) | |
| 60 | + | #define MANDELBROT_VIEW_MAX_ITER (75) | |
| 59 | 61 | ||
| 60 | 62 | /* Fixed-point math (fpx) scale */ | |
| 61 | 63 | #define FPX_SCALE (1000LL) /* fpx scale: 1000 <--> 1.0 */ | |
| 62 | 64 | ||
| 63 | 65 | /* | |
| 64 | 66 | * |fpx_mul()| - fpx multiply: (a*b)/FPX_SCALE using 64-bit | |
| 65 | 67 | */ | |
| 66 | 68 | static inline | |
| 67 | 69 | test_int32_t fpx_mul(test_int32_t a, test_int32_t b) | |
| 68 | 70 | { | |
| 69 | 71 | return (test_int32_t)(((test_int64_t)a * (test_int64_t)b) / FPX_SCALE); | |
| 70 | 72 | } | |
| 71 | 73 | ||
| 72 | 74 | /* | |
| 73 | 75 | * Iterations && ASCII "palette" | |
| 74 | 76 | * - FIXME: Make this a cmdline argument | |
| 75 | 77 | */ | |
| 76 | 78 | static const char mandelbrot_palette[] = | |
| 77 | - | " .:-+*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#"; | |
| 79 | + | " .':;-~=+*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%&$#"; | |
| 78 | 80 | const size_t mandelbrot_palette_len = sizeof(mandelbrot_palette) - 1; | |
| 79 | 81 | ||
| 80 | 82 | static | |
| 81 | 83 | void do_fxmath_mandelbrot(void) | |
| 82 | 84 | { | |
| 83 | 85 | /* | |
| 84 | 86 | * Mandelbrot view window: | |
| 85 | 87 | * fpx: x in [-2, 1], y range adjusted for aspect | |
| 86 | 88 | */ | |
| 87 | 89 | const test_int32_t x_min = -2000; /* was: -2.000 * FPX_SCALE */ | |
| 88 | 90 | const test_int32_t x_max = 1000; /* was: 1.000 * FPX_SCALE */ | |
| 89 | 91 | const test_int32_t x_range = x_max - x_min; | |
| 90 | 92 | ||
| 91 | 93 | /* y_range = x_range * MANDELBROT_VIEW_HEIGHT / MANDELBROT_VIEW_WIDTH, centered around 0 */ | |
| 92 | 94 | const test_int32_t y_range = | |
| 93 | 95 | (test_int32_t)(((test_int64_t)x_range * MANDELBROT_VIEW_HEIGHT) / MANDELBROT_VIEW_WIDTH); | |
| 94 | 96 | const test_int32_t y_min = -(y_range / 2); | |
| 95 | 97 | const test_int32_t y_max = (y_range - y_range / 2); | |
| 96 | 98 | ||
| 97 | 99 | /* Precompute escape radius squared, scaled: (2*FPX_SCALE)^2 */ | |
| 98 | 100 | const test_int64_t esc2 = (test_int64_t)((2LL * FPX_SCALE) * (2LL * FPX_SCALE)); | |
| 99 | 101 | ||
| 100 | 102 | for (int j = 0; j < MANDELBROT_VIEW_HEIGHT; j++) { | |
| 101 | 103 | /* cy = y_min + j * (y_max - y_min) / (MANDELBROT_VIEW_HEIGHT-1) */ | |
| 102 | 104 | test_int32_t cy = y_min; | |
| 103 | 105 | ||
| 104 | 106 | test_int64_t t = (test_int64_t)(y_max - y_min); | |
| 105 | 107 | t = (t * (test_int64_t)j) / (test_int64_t)(MANDELBROT_VIEW_HEIGHT - 1); | |
| 106 | 108 | cy = (test_int32_t)((test_int64_t)y_min + t); | |
| 107 | 109 | ||
| 108 | 110 | for (int i = 0; i < MANDELBROT_VIEW_WIDTH; i++) { | |
| 109 | 111 | /* cx = x_min + i * x_range / (MANDELBROT_VIEW_WIDTH-1) */ | |
| 110 | 112 | test_int32_t cx = x_min; | |
| 111 | 113 | ||
| 112 | 114 | test_int64_t t = (test_int64_t)x_range; | |
| 113 | 115 | t = (t * (test_int64_t)i) / (test_int64_t)(MANDELBROT_VIEW_WIDTH - 1); | |
| 114 | 116 | cx = (test_int32_t)((test_int64_t)x_min + t); | |
| 115 | 117 | ||
| 116 | 118 | test_int32_t zx = 0, zy = 0; | |
| 117 | 119 | int iter = 0; | |
| 118 | 120 | ||
| 119 | 121 | /* Loop in fpx: z_{n+1} = z_n^2 + c */ | |
| 120 | 122 | for (;;) { | |
| 121 | 123 | test_int64_t sumsq = (test_int64_t)zx * (test_int64_t)zx + (test_int64_t)zy * (test_int64_t)zy; | |
| 122 | 124 | if (sumsq > esc2) | |
| 123 | 125 | break; | |
| 124 | 126 | if (iter >= MANDELBROT_VIEW_MAX_ITER) | |
| 125 | 127 | break; | |
| 126 | 128 | ||
| 127 | 129 | test_int32_t zx2 = fpx_mul(zx, zx); | |
| 128 | 130 | test_int32_t zy2 = fpx_mul(zy, zy); | |
| 129 | 131 | test_int32_t xy = fpx_mul(zx, zy); | |
| 130 | 132 | ||
| 131 | 133 | test_int32_t nx = (zx2 - zy2) + cx; | |
| 132 | 134 | test_int32_t ny = (test_int32_t)((test_int64_t)xy * 2) + cy; | |
| 133 | 135 | ||
| 134 | 136 | zx = nx; | |
| 135 | 137 | zy = ny; | |
| 136 | 138 | iter++; | |
| 137 | 139 | } | |
| 138 | 140 | ||
| 139 | 141 | /* Map mandelbrot iterations to palette index */ | |
| 140 | 142 | size_t idx = (iter >= MANDELBROT_VIEW_MAX_ITER) | |
| 141 | 143 | ? (mandelbrot_palette_len - 1) | |
| 142 | 144 | : (int)(((test_int64_t)iter * (mandelbrot_palette_len - 1)) / MANDELBROT_VIEW_MAX_ITER); | |
| 143 | 145 | (void)putchar(mandelbrot_palette[idx]); | |
| 144 | 146 | } | |
| 145 | 147 | ||
| 146 | 148 | (void)putchar('\n'); | |
| 147 | 149 | } | |
| 148 | 150 | } | |
| 149 | 151 | ||
| 150 | 152 | static | |
| 151 | 153 | void do_unit_tests1(void) | |
| 152 | 154 | { | |
| 153 | 155 | /* | |
| 154 | 156 | * FIXME: clang++ throws SIGFPE on Debian Bulleye, we need to add code to catch this | |
| 155 | 157 | */ | |
| 156 | 158 | { | |
| 157 | 159 | rov_int_template<int32_t> x = 0; | |
| 158 | 160 | rov_int_template<int32_t> y = 0; | |
| 161 | + | try { | |
| 159 | 162 | (void)printf("res (x/y)=%d\n", (int)(x/y)); | |
| 163 | + | std::cerr << "Test FAILED for " << "(x/y)\n"; | |
| 160 | 164 | } | |
| 165 | + | catch (const std::exception& e) { | |
| 161 | 169 | { | |
| 162 | 170 | rov_int_template<int32_t> x = 0; | |
| 171 | + | try { | |
| 163 | 172 | (void)printf("res (x/0)=%d\n", (int)(x/0)); | |
| 173 | + | std::cerr << "Test FAILED for " << "(x/0)\n"; | |
| 164 | 174 | } | |
| 175 | + | catch (const std::exception& e) { | |
| 165 | 179 | { | |
| 166 | 180 | rov_int_template<int32_t> x = 0; | |
| 167 | 181 | rov_int_template<int32_t> y = 0; | |
| 182 | + | try { | |
| 168 | 183 | (void)printf("res (x/=y)=%d\n", (int)(x/=y)); | |
| 184 | + | std::cerr << "Test FAILED for " << "(x/=y)\n"; | |
| 169 | 185 | } | |
| 186 | + | catch (const std::exception& e) { | |
| 170 | 188 | } | |
| 189 | + | } | |
| 171 | 191 | ||
| 172 | - | int main(int ac, char *av[]) | |
| 192 | + | static | |
| 193 | + | void do_unit_tests2_template_nesting(void) | |
| 173 | 194 | { | |
| 174 | - | #if 0 | |
| 195 | + | #ifdef DISABLED_FOR_NOW | |
| 175 | - | do_unit_tests1(); | |
| 196 | + | { | |
| 176 | - | #endif | |
| 197 | + | /* | |
| 177 | - | ||
| 198 | + | * Nested template testcase | |
| 178 | - | #if 1 | |
| 199 | + | * We use this for: | |
| 179 | - | do_fxmath_mandelbrot(); | |
| 200 | + | * 1. compiler testing (some compilers like g++ and clang++ have | |
| 180 | - | #endif | |
| 201 | + | * issues with this | |
| 181 | - | return EXIT_SUCCESS; | |
| 202 | + | * 2. benchmarking, because we can get extra levels of | |
| 182 | - | } | |
| 203 | + | * indirection easily over all affected components, except if we hit [1] | |
| 183 | - | ||
| 204 | + | */ | |
| 184 | - | ||
| 205 | + | rov_int_template<rov_int_template<rov_int_template<rov_int_template<int32_t>>>> x = 1; | |
| 185 | - | ||
| 206 | + | rov_int_template<rov_int_template<rov_int_template<rov_int_template<int32_t>>>> y = 2; | |
| 186 | - | ||
| 207 | + | (void)printf("res (x+y)=%d\n", int((x+y).get().get().get())); | |
| 187 | - | ||
| 208 | + | } | |
| 188 | - | ||
| 209 | + | #endif /* DISABLED_FOR_NOW */ | |
| 189 | - | ||
| 210 | + | } | |
| 190 | 211 | ||
| 191 | - | ||
| 212 | + | int main(int ac, char *av[]) | |
| 192 | - | ||
| 213 | + | { | |
| 193 | - | ||
| 214 | + | #if 1 | |
| 194 | - | ||
| 215 | + | do_unit_tests1(); | |
| 195 | - | ||
| 216 | + | do_unit_tests2_template_nesting(); | |
| 196 | - | ||
| 217 | + | #endif | |
| 197 | 218 | ||
| 198 | - | ||
| 219 | + | #if 1 | |
| 199 | - | ||
| 220 | + | do_fxmath_mandelbrot(); | |
| 200 | - | ||
| 221 | + | #endif | |
| 201 | - | ||
| 222 | + | return EXIT_SUCCESS; | |
| 202 | - | ||
| 223 | + | } | |
| 203 | 224 | ||
| 204 | 225 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
rovema.kpaste.net RSS