ascjulia.c and ascbrot.c

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

ascjulia.c and ascbrot.c

Post by veekoo »

Fist steps with Gigatron programming with LCC.
At some point this or other version of this code draw some waves.
Now it's stuck on giving floating point overflow.
The compiler doesn't warn it, but gtsim and actual Gigatron report that.
Any idea what to fix?

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |      ascii-julia.c -- demonstrate fractal in text / quick and dirty  |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define HEIGHT 120
#define WIDTH 160
#define SCALE 1.0f
#define ITERATIONS 150
#define THRESH 100
#define NUM_THREADS 8
#define CX -0.8f
#define CY 0.156f

int julia(float x, float y) {
  float z,xtemp;
  int i,n;
  for(i = 0; i < ITERATIONS; i++) {
    xtemp = x * x - y * y;
    y = 2.0f * x * y  + CY;
    z = xtemp + CX;
    n = 47 + i * 9 / ITERATIONS;
    if(abs(z) > THRESH){
     putchar(n);
     return 0;
    } 
  }
  return 1;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
  for(y = 0; y < HEIGHT; y = y + NUM_THREADS ) {
    for(x = 0; x < WIDTH; x = x + 6 ) {
      sx = SCALE * (WIDTH/2.0f - x) / (WIDTH/2.0f);
      sy = SCALE * (HEIGHT/2.0f - y) / (HEIGHT/2.0f);
      data = julia(sx, sy);
      offset = ( x + y * WIDTH);
    }
  }
}
Last edited by veekoo on 12 Sep 2021, 10:56, edited 2 times in total.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

This is optimized now for Gigatron 26x15 screen. Seems to be working now that I don't do all iterations.
I was curious how this runs at x86-pc with virtual ubuntu linux. I compiled with gcc and executable runs very fast compared actual gt1. Current speed is one screen in 2hrs.

I attached GT1 file which was compiled at default settings. So it runs on rom v5a?
Last edited by veekoo on 11 Sep 2021, 12:27, edited 1 time in total.
lb3361
Posts: 77
Joined: 17 Feb 2021, 23:07

Re: ascii-julia.c

Post by lb3361 »

Glcc by default compiles for romv5a. Command glcc -info tells you exactly what is selected.

The runtime by default generates an exception on overflow. This is because the MS floating point format does not support nans or infinities. From your message it is not clear to me whether the floating exception arises because a floating point calculation is incorrect or because this is a genuine overflow.

* In the case of a genuine overflow, you could use signal(SIGFPE, SIG_IGN) to ignore the overflow. The overflowing code will simply return HUGE_VAR which will exceeed the threshold anyway. Alternatively you can define your own signal handler and set an overflow flag that you can test later. Whatever the signal handler returns (assuming it returns a float) will be returned by the overflowing instruction (see https://github.com/lb3361/gigatron-lcc/ ... STsignal.c for examples involving division by zero with ints and longs).

* On the other hand, if this arises because the runtime compiles something incorrect, then I have to investigate why. Can you send me a code that triggers the exception (the one above does not).

A x86 processor, even a virtual one, is considerably faster than the Gigatron. In addition the GLCC floating point is entirely written in software (vCPUs ource code at https://github.com/lb3361/gigatron-lcc/ ... me/rt_fp.s). In contrast, a modern x86 has this in hardware with a rather efficient pipeline.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

The problem with floating point overflow was caused by programming error. In the screen there should be put one character per "pixel'. I didn't exit the loop and put out all iterations on the screen.

Currently what I get from the program is far from fractal. Maybe some mistake mixing complex math with regular?
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

This days work.

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |      ascii-julia.c -- demonstrate fractal in text / quick and dirty  |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define HEIGHT 120
#define WIDTH 160
#define SCALE 1.0f
#define ITERATIONS 75
#define THRESH 100
#define YSTEP 9
#define XSTEP 6
#define CX -0.8f
#define CY 0.156f

int julia(float x, float y) {
  float z,xtemp;
  int i,n;
  
  for(i = 0; i < ITERATIONS; i++) {
    xtemp = x * x - y * y;
    y = 2.0f * x * y  + CY;
    z = xtemp + CX;
    n = 48 + i * 9 / ITERATIONS;
    if(abs(z) > THRESH){
     putchar(n);
     return 0;
    } 
  }
  putchar(' ');
  return 1;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = SCALE * (WIDTH/2.0f - x) / (WIDTH/2.0f);
      sy = SCALE * (HEIGHT/2.0f - y) / (HEIGHT/2.0f);
      data = julia(sx, sy);
      offset = ( x + y * WIDTH);
    }
  }
}
Last edited by veekoo on 11 Sep 2021, 12:28, edited 1 time in total.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

Little bit better if I put

#define SCALE 2.1f
#define ITERATIONS 10
#define THRESH 2

It seems to zoom out and the empty gap is minimized. It takes a couple of minutes to draw a screen. I doubt that the equations are correct, because it doesn't look like the picture
Attachments
Julia_-0.8_0.156.png
Julia_-0.8_0.156.png (201.79 KiB) Viewed 230 times
Last edited by veekoo on 11 Sep 2021, 11:41, edited 1 time in total.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

I doubt the resolution (26x15) of Gigatron allows this julia to be shown. I first need it to get working on bigger screen/terminal (132x43). Here is what I tried with linux.

#define HEIGHT 43
#define WIDTH 132
#define SCALE 2.1f
#define ITERATIONS 10
#define THRESH 2
#define YSTEP 1
#define XSTEP 1
#define CX -0.8f
#define CY 0.156f
Attachments
julia132_43.PNG
julia132_43.PNG (65.78 KiB) Viewed 213 times
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

This time with Gigatron 26x15. It takes 2 minutes to draw the screen.

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |      ascii-julia.c -- demonstrate fractal in text / quick and dirty  |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define HEIGHT 15
#define WIDTH 26
#define SCALE 2.1f
#define ITERATIONS 10
#define THRESH 2
#define YSTEP 1
#define XSTEP 1
#define CX -0.8f
#define CY 0.156f

int julia(float x, float y) {
  float z,xtemp;
  int i,n;
  
  for(i = 0; i < ITERATIONS; i++) {
    xtemp = x * x - y * y;
    y = 2.0f * x * y  + CY;
    z = xtemp + CX;
    n = 48 + i * 9 / ITERATIONS;
    if(abs(z) > THRESH){
     putchar(n);
     return 0;
    } 
  }
  putchar(' ');
  return 1;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = SCALE * (WIDTH/2.0f - x) / (WIDTH/2.0f);
      sy = SCALE * (HEIGHT/2.0f - y) / (HEIGHT/2.0f);
      data = julia(sx, sy);
      offset = ( x + y * WIDTH);
    }
  }
}
Attachments
IMG_0915.JPG
IMG_0915.JPG (4.55 MiB) Viewed 210 times
Last edited by veekoo on 12 Sep 2021, 08:35, edited 1 time in total.
lb3361
Posts: 77
Joined: 17 Feb 2021, 23:07

Re: ascii-julia.c

Post by lb3361 »

Now with graphics?

Code: Select all

#include <gigatron/console.h>

void drawPixel(int x, int y, int color)
{
    screenMemory[y][x] = color;
}
Note: the code above assumes the screen has not been scrolled -- default videoTable.
Otherwise you can include <gigatron/console.h> and call console_clear_screen(void) to reset it.
veekoo
Posts: 25
Joined: 07 Jun 2021, 07:07

Re: ascii-julia.c

Post by veekoo »

lb3361 wrote: 12 Sep 2021, 03:03 Now with graphics?

Code: Select all

#include <gigatron/console.h>

void drawPixel(int x, int y, int color)
{
    screenMemory[y][x] = color;
}
Note: the code above assumes the screen has not been scrolled -- default videoTable.
Otherwise you can include <gigatron/console.h> and call console_clear_screen(void) to reset it.
Yes logical step is to do next with graphics. I had lot to correct with code and I realized I had to do mandelbrot first to check the functionality of julia. So here is ascbrot.c and ascjulia.c. It takes 3 minutes to draw the fractals.

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |     ascbrot.c -- demonstrate fractal in text / quick and dirty       |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>

#define HEIGHT 15
#define WIDTH 26
#define SCALE 1.4
#define YSTEP 1
#define XSTEP 1

int mandelbrot(float x, float y) {
 float zz;
 float a;
 float b;
 float a2;
 float b2;
 float atemp;
 int i;
  
  a = 0;
  b = 0;  
  i = 47;
  while (i < 58)
  {
    a2 = a * a;
    b2 = b * b;
    zz = a2 + b2;
    if(zz > 32) break;
    
    atemp = a2 - b2 + x;
    b = 2.0 * a * b + y;
    a = atemp;
    i++;
  }
  if(x+y < 4) { 
    putchar(i);
    return 0;
  }
  putchar(' ');
  return 1;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = -0.7 + SCALE * (WIDTH/2.0 - x) / (WIDTH/2.0);
      sy = SCALE * (HEIGHT/2.0 - y) / (HEIGHT/2.0);
      data = mandelbrot(sx, sy);
      offset = ( x + y * WIDTH);
    }
  }
}

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |      ascjulia.c -- demonstrate fractal in text / quick and dirty     |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>

#define HEIGHT 15
#define WIDTH 26
#define SCALE 1.7
#define YSTEP 1
#define XSTEP 1
#define CX -0.8
#define CY 0.156

int julia(float x, float y) {
 float zz;
 float a;
 float b;
 float a2;
 float b2;
 float atemp;
 int i;
  
  a = x;
  b = y;  
  i = 47;
  while (i < 58)
  {
    a2 = a * a;
    b2 = b * b;
    zz = a2 + b2;
    if(zz > 32) break;
    
    atemp = a2 - b2 + CX;
    b = 2.0 * a * b + CY;
    a = atemp;
    i++;
  }
  if(x+y < 4) { 
    putchar(i);
    return 0;
  }
  putchar(' ');
  return 1;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = SCALE * (WIDTH/2.0 - x) / (WIDTH/2.0);
      sy = SCALE * (HEIGHT/2.0 - y) / (HEIGHT/2.0);
      data = julia(sx, sy);
      offset = ( x + y * WIDTH);
    }
  }
}
Development future is in github / veekooFIN
Attachments
IMG_0919.JPG
IMG_0919.JPG (4.18 MiB) Viewed 180 times
IMG_0918.JPG
IMG_0918.JPG (4.29 MiB) Viewed 180 times
ascjulia.gt1
(6.63 KiB) Downloaded 3 times
ascbrot.gt1
(6.64 KiB) Downloaded 3 times
Post Reply