Help converting short p5.js program to Purebasic

Just starting out? Need help? Post your questions and find answers here.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Help converting short p5.js program to Purebasic

Post by wilbert »

SMaag wrote: Wed Jul 10, 2024 1:50 pm I tried to calculate the BayerMatrix
This one uses recursion

Code: Select all

Global Dim M(15, 15)

Procedure InitBayer(x, y, s, v, st)
  If s = 1
    M(y, x) = v
  Else
    s >> 1
    InitBayer(x,   y,   s, v,       st<<2)
    InitBayer(x+s, y+s, s, v+st,    st<<2)
    InitBayer(x+s, y,   s, v+st<<1, st<<2)
    InitBayer(x,   y+s, s, v+st*3,  st<<2)
  EndIf
EndProcedure

Debug "DataSection"
Debug ""
s = 2
While s <= 16
  InitBayer(0, 0, s, 0, 1)
  Debug "  ; "+Str(s)+"x"+Str(s)+" Bayer Matrix"
  Debug "  Bayer"+Str(s)+"x"+Str(s)+":"
  For y = 0 To s-1
    ds.s = "  Data.a "
    For x = 0 To s-2
      ds + RSet(Str(M(y, x)),3)+", "
    Next
    ds + RSet(Str(M(y, s-1)),3)
    Debug ds
  Next
  Debug "  Bayer"+Str(s)+"x"+Str(s)+"_end:"
  Debug ""
  s << 1
Wend
Debug "EndDataSection"
Output:

Code: Select all

DataSection

  ; 2x2 Bayer Matrix
  Bayer2x2:
  Data.a   0,   2
  Data.a   3,   1
  Bayer2x2_end:

  ; 4x4 Bayer Matrix
  Bayer4x4:
  Data.a   0,   8,   2,  10
  Data.a  12,   4,  14,   6
  Data.a   3,  11,   1,   9
  Data.a  15,   7,  13,   5
  Bayer4x4_end:

  ; 8x8 Bayer Matrix
  Bayer8x8:
  Data.a   0,  32,   8,  40,   2,  34,  10,  42
  Data.a  48,  16,  56,  24,  50,  18,  58,  26
  Data.a  12,  44,   4,  36,  14,  46,   6,  38
  Data.a  60,  28,  52,  20,  62,  30,  54,  22
  Data.a   3,  35,  11,  43,   1,  33,   9,  41
  Data.a  51,  19,  59,  27,  49,  17,  57,  25
  Data.a  15,  47,   7,  39,  13,  45,   5,  37
  Data.a  63,  31,  55,  23,  61,  29,  53,  21
  Bayer8x8_end:

  ; 16x16 Bayer Matrix
  Bayer16x16:
  Data.a   0, 128,  32, 160,   8, 136,  40, 168,   2, 130,  34, 162,  10, 138,  42, 170
  Data.a 192,  64, 224,  96, 200,  72, 232, 104, 194,  66, 226,  98, 202,  74, 234, 106
  Data.a  48, 176,  16, 144,  56, 184,  24, 152,  50, 178,  18, 146,  58, 186,  26, 154
  Data.a 240, 112, 208,  80, 248, 120, 216,  88, 242, 114, 210,  82, 250, 122, 218,  90
  Data.a  12, 140,  44, 172,   4, 132,  36, 164,  14, 142,  46, 174,   6, 134,  38, 166
  Data.a 204,  76, 236, 108, 196,  68, 228, 100, 206,  78, 238, 110, 198,  70, 230, 102
  Data.a  60, 188,  28, 156,  52, 180,  20, 148,  62, 190,  30, 158,  54, 182,  22, 150
  Data.a 252, 124, 220,  92, 244, 116, 212,  84, 254, 126, 222,  94, 246, 118, 214,  86
  Data.a   3, 131,  35, 163,  11, 139,  43, 171,   1, 129,  33, 161,   9, 137,  41, 169
  Data.a 195,  67, 227,  99, 203,  75, 235, 107, 193,  65, 225,  97, 201,  73, 233, 105
  Data.a  51, 179,  19, 147,  59, 187,  27, 155,  49, 177,  17, 145,  57, 185,  25, 153
  Data.a 243, 115, 211,  83, 251, 123, 219,  91, 241, 113, 209,  81, 249, 121, 217,  89
  Data.a  15, 143,  47, 175,   7, 135,  39, 167,  13, 141,  45, 173,   5, 133,  37, 165
  Data.a 207,  79, 239, 111, 199,  71, 231, 103, 205,  77, 237, 109, 197,  69, 229, 101
  Data.a  63, 191,  31, 159,  55, 183,  23, 151,  61, 189,  29, 157,  53, 181,  21, 149
  Data.a 255, 127, 223,  95, 247, 119, 215,  87, 253, 125, 221,  93, 245, 117, 213,  85
  Bayer16x16_end:

EndDataSection
Last edited by wilbert on Wed Jul 10, 2024 3:22 pm, edited 3 times in total.
Windows (x64)
Raspberry Pi OS (Arm64)
miskox
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Re: Help converting short p5.js program to Purebasic

Post by miskox »

wilbert wrote: Tue Jul 09, 2024 9:13 pm
miskox wrote: Tue Jul 09, 2024 6:22 pm Wouldn't this be faster:

Code: Select all

M(x,y)=temp/d/d
No, the opposite.
A division is a lot slower as a multiplication.
If you are aiming for performance, try to avoid the division (and modulo) operator as much as possible.
In this case the M array is only 8x8 so it's not that important but if you would have a very big array, the best thing would be to calculate 1/(d*d) before the loop and multiply temp with that.
Thanks for clarification. I thought that parantheses would cause a delay...looks like I was wrong.

Thanks again.
Saso
Post Reply