pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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

    
11
/*
22
 * MIT License
33
 *
44
 * Copyright (c) 2026 Roland Mainz <roland.mainz@nrubsig.org>
55
 *
66
 * Permission is hereby granted, free of charge, to any person obtaining a copy
77
 * of this software and associated documentation files (the "Software"), to deal
88
 * in the Software without restriction, including without limitation the rights
99
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1010
 * copies of the Software, and to permit persons to whom the Software is
1111
 * furnished to do so, subject to the following conditions:
1212
 *
1313
 * The above copyright notice and this permission notice shall be included in all
1414
 * copies or substantial portions of the Software.
1515
 *
1616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1717
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1818
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1919
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2020
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
 * SOFTWARE.
2323
 */
2424
2525
/*
2626
 * rov_int_mandelbrot_test1.c - math test module for |template <typename T> class rov_int_template|
2727
 *
2828
 * WARNING: This module must be compileable with ISO C and ISO C++ compilers
2929
 * for benchmarking and testing purposes!
3030
 */
3131
3232
#include <stdio.h>
3333
#include <stdlib.h>
3434
#include <stdint.h>
3535
36+
#include <iostream>
3737
38+
#define USE_ROV_INT_TEMPLATE002_TYPES 1
3840
#ifdef USE_ROV_INT_TEMPLATE002_TYPES
3941
/*
4042
 * Compile with ISO C++ integer wrapper class
4143
 */
4244
#include "rov_int_template002.h"
4345
4446
typedef rov_int_template<int32_t> test_int32_t;
4547
typedef rov_int_template<int64_t> test_int64_t;
4648
#else
4749
/*
4850
 * Compile with ISO C99/C11 compiler
4951
 */
5052
#error XXX
5153
typedef int32_t test_int32_t;
5254
typedef int64_t test_int64_t;
5355
#endif /* USE_ROV_INT_TEMPLATE002_TYPES */
5456
5557
/* Mandelbrot parameters */
5658
#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)
5961
6062
/* Fixed-point math (fpx) scale */
6163
#define FPX_SCALE	(1000LL)  /* fpx scale: 1000 <--> 1.0 */
6264
6365
/*
6466
 * |fpx_mul()| - fpx multiply: (a*b)/FPX_SCALE using 64-bit
6567
 */
6668
static inline
6769
test_int32_t fpx_mul(test_int32_t a, test_int32_t b)
6870
{
6971
    return (test_int32_t)(((test_int64_t)a * (test_int64_t)b) / FPX_SCALE);
7072
}
7173
7274
/*
7375
 * Iterations && ASCII "palette"
7476
 * - FIXME: Make this a cmdline argument
7577
 */
7678
static const char mandelbrot_palette[] =
77-
    " .:-+*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#";
79+
    " .':;-~=+*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%&$#";
7880
const size_t mandelbrot_palette_len = sizeof(mandelbrot_palette) - 1;
7981
8082
static
8183
void do_fxmath_mandelbrot(void)
8284
{
8385
    /*
8486
     * Mandelbrot view window:
8587
     * fpx: x in [-2, 1], y range adjusted for aspect
8688
     */
8789
    const test_int32_t x_min   = -2000;  /* was: -2.000 * FPX_SCALE */
8890
    const test_int32_t x_max   =  1000;  /* was: 1.000 * FPX_SCALE */
8991
    const test_int32_t x_range = x_max - x_min;
9092
9193
    /* y_range = x_range * MANDELBROT_VIEW_HEIGHT / MANDELBROT_VIEW_WIDTH, centered around 0 */
9294
    const test_int32_t y_range =
9395
    	(test_int32_t)(((test_int64_t)x_range * MANDELBROT_VIEW_HEIGHT) / MANDELBROT_VIEW_WIDTH);
9496
    const test_int32_t y_min   = -(y_range / 2);
9597
    const test_int32_t y_max   =  (y_range - y_range / 2);
9698
9799
    /* Precompute escape radius squared, scaled: (2*FPX_SCALE)^2 */
98100
    const test_int64_t esc2 = (test_int64_t)((2LL * FPX_SCALE) * (2LL * FPX_SCALE));
99101
100102
    for (int j = 0; j < MANDELBROT_VIEW_HEIGHT; j++) {
101103
        /* cy = y_min + j * (y_max - y_min) / (MANDELBROT_VIEW_HEIGHT-1) */
102104
        test_int32_t cy = y_min;
103105
104106
        test_int64_t t = (test_int64_t)(y_max - y_min);
105107
        t = (t * (test_int64_t)j) / (test_int64_t)(MANDELBROT_VIEW_HEIGHT - 1);
106108
        cy = (test_int32_t)((test_int64_t)y_min + t);
107109
108110
        for (int i = 0; i < MANDELBROT_VIEW_WIDTH; i++) {
109111
            /* cx = x_min + i * x_range / (MANDELBROT_VIEW_WIDTH-1) */
110112
            test_int32_t cx = x_min;
111113
112114
            test_int64_t t = (test_int64_t)x_range;
113115
            t = (t * (test_int64_t)i) / (test_int64_t)(MANDELBROT_VIEW_WIDTH - 1);
114116
            cx = (test_int32_t)((test_int64_t)x_min + t);
115117
116118
            test_int32_t zx = 0, zy = 0;
117119
            int iter = 0;
118120
119121
            /* Loop in fpx: z_{n+1} = z_n^2 + c */
120122
            for (;;) {
121123
                test_int64_t sumsq = (test_int64_t)zx * (test_int64_t)zx + (test_int64_t)zy * (test_int64_t)zy;
122124
                if (sumsq > esc2)
123125
			break;
124126
                if (iter >= MANDELBROT_VIEW_MAX_ITER)
125127
			break;
126128
127129
                test_int32_t zx2 = fpx_mul(zx, zx);
128130
                test_int32_t zy2 = fpx_mul(zy, zy);
129131
                test_int32_t xy  = fpx_mul(zx, zy);
130132
131133
                test_int32_t nx = (zx2 - zy2) + cx;
132134
                test_int32_t ny = (test_int32_t)((test_int64_t)xy * 2) + cy;
133135
134136
                zx = nx;
135137
                zy = ny;
136138
                iter++;
137139
            }
138140
139141
            /* Map mandelbrot iterations to palette index */
140142
            size_t idx = (iter >= MANDELBROT_VIEW_MAX_ITER)
141143
		? (mandelbrot_palette_len - 1)
142144
		: (int)(((test_int64_t)iter * (mandelbrot_palette_len - 1)) / MANDELBROT_VIEW_MAX_ITER);
143145
            (void)putchar(mandelbrot_palette[idx]);
144146
        }
145147
146148
        (void)putchar('\n');
147149
    }
148150
}
149151
150152
static
151153
void do_unit_tests1(void)
152154
{
153155
	/*
154156
	 * FIXME: clang++ throws SIGFPE on Debian Bulleye, we need to add code to catch this
155157
	 */
156158
	{
157159
		rov_int_template<int32_t> x = 0;
158160
		rov_int_template<int32_t> y = 0;
161+
		try {
159162
		(void)printf("res (x/y)=%d\n", (int)(x/y));
163+
			std::cerr << "Test FAILED for " << "(x/y)\n";
160164
	}
165+
		catch (const std::exception& e) {
161169
	{
162170
		rov_int_template<int32_t> x = 0;
171+
		try {
163172
		(void)printf("res (x/0)=%d\n", (int)(x/0));
173+
			std::cerr << "Test FAILED for " << "(x/0)\n";
164174
	}
175+
		catch (const std::exception& e) {
165179
	{
166180
		rov_int_template<int32_t> x = 0;
167181
		rov_int_template<int32_t> y = 0;
182+
		try {
168183
		(void)printf("res (x/=y)=%d\n", (int)(x/=y));
184+
			std::cerr << "Test FAILED for " << "(x/=y)\n";
169185
	}
186+
		catch (const std::exception& e) {
170188
}
189+
	}
171191
172-
int main(int ac, char *av[])
192+
static
193+
void do_unit_tests2_template_nesting(void)
173194
{
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+
}
190211
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
197218
198-
219+
#if 1
199-
220+
	do_fxmath_mandelbrot();
200-
221+
#endif
201-
222+
	return EXIT_SUCCESS;
202-
223+
}
203224
204225

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