pastebin - collaborative debugging tool
rovema.kpaste.net RSS


rov_int_mandelbrot_test1.c - math test module for |template <typename T> class rov_int_template|
Posted by Anonymous on Wed 18th Mar 2026 19:26
raw | new post
view followups (newest first): rov_int_mandelbrot_test1.c - math test module for |template <typename T> class rov_int_template| by Anonymous

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

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at