Bug in Circle function?

For other topics related to the FreeBASIC project or its community.
UEZ
Posts: 688
Joined: May 05, 2017 19:59
Location: Germany

Bug in Circle function?

Postby UEZ » Jan 21, 2021 15:12

I have an odd phenomenon when I compile this code as x64:

Code: Select all

'Ported https://www.dwitter.net/d/21330 by rodrigo.siqueira to FB by UEZ build 2021-01-21

#Include "fbgfx.bi"
#Include "crt/math.bi"
Using FB

Dim As Integer w = 1920 Shr 0, h = 1080 Shr 0, w2 = w Shr 1, h2 = h Shr 1

Screenres w, h, 32, 2, GFX_ALWAYS_ON_TOP Or GFX_ALPHA_PRIMITIVES Or GFX_NO_SWITCH Or GFX_NO_FRAME 'Or GFX_FULLSCREEN
Screenset 1, 0
Color &hFF, &hFF000000
Cls

#Define Min(a, b)   (Iif(a < b, a, b))
#Define Max(a, b)   (Iif(a > b, a, b))
#Define Map(Val, source_start, source_stop, dest_start, dest_stop)   ((Val - source_start) * (dest_stop - dest_start) / (source_stop - source_start) + dest_start)
      
#Define f23 (2 / 3)
#Define f13 (1 / 3)
#Define f16 (1 / 6)
#Define to255(v)   (Max(0, Min(255, 256 * v)))

Function HUE2RGB(p As Single, q As Single, t As Single) As Single
   If t < 0 Then t += 1
   If t > 1 Then t -= 1
   If t < f16 Then Return p + (q - p) * 6 * t
   If t < 0.5 Then Return q
   If t < f23 Then Return p + (q - p) * (f23 - t) * 6
   Return p
End Function

Function HSL2RGB(H As Single, S As Single, L As Single, a As Ubyte = &hFF) As Ulong
   Dim As Single r, g, b
   If S = 0 Then
      r = L : g = L : b = L
   Else
      Dim As Single p, q
      q = Iif(L < 0.5, L * (1 + S), L + S - L * S)
      p = 2 * L - q
      r = HUE2RGB(p, q, H + f13)
      g = HUE2RGB(p, q, H)
      b = HUE2RGB(p, q, H - f13)
   End If
   Return a Shl 24 Or to255(r) Shl 16 Or to255(g) Shl 8 Or to255(b) Shl 0
End Function

Randomize

#Define h74 (h * 0.74)
Dim As Double t = 0
Dim As Single k, x, y, z = 1
Dim As Integer i


Do
   If z > 0.5 Then
      For i = 3e3 To 0 Step -1
         x = w2 + i * (Cos(i * i + t) * Cos(i / t) ^ 0.3)
         y = h74 - i * (Sin(i * i + t) * Sin(i / t) ^ 0.3)
         z = 9 / t
         k = Sin(i * i + t / 5) * 340
         'Line (x, y) - (x + z, y + z), HSL2RGB(fmod(120 + k, 360) / 360, 0.99, (k / 4) / 100, 15), BF
         Circle (x, y), z, HSL2RGB(fmod(120 + k, 360) / 360, 0.99, (k / 4) / 100, 15),,,,F
      Next
      t += 0.016
   Endif
   Flip
   Sleep(1)
Loop Until Len(Inkey())


Instead of circles I see also horizontal stripes - with x86 no problems.

x86:
Image

x64:
Image


Is this a bug?
Last edited by UEZ on Jan 21, 2021 19:42, edited 1 time in total.
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in Circle function?

Postby fxm » Jan 21, 2021 17:09

The fault appears while at the same time circles are drawn entirely outside the screen (dangerous behavior to be corrected in my opinion).
If we remove the drawing of the circles which necessarily are completely outside the screen, it seems to work well:
If x - z < w And y - z < h Then Circle (x, y), z, HSL2RGB(fmod(120 + k, 360) / 360, 0.99, (k / 4) / 100, 15),,,,F
UEZ
Posts: 688
Joined: May 05, 2017 19:59
Location: Germany

Re: Bug in Circle function?

Postby UEZ » Jan 21, 2021 19:40

fxm wrote:The fault appears while circles are drawn entirely outside the screen (dangerous behavior to be corrected in my opinion).
If we remove the drawing of the circles which necessarily are completely outside the screen, it seems to work well:
If x - z < w And y - z < h Then Circle (x, y), z, HSL2RGB(fmod(120 + k, 360) / 360, 0.99, (k / 4) / 100, 15),,,,F

That's a workaround but with x86 it works properly but with x64 it doesn't work properly. Thus, there must be a difference in the implementation of the circle functions. I would expect that the circle function covers all possibilities before it set a pixel to the screen. That means when a pixel is outside the visible area then the pixel shouldn't be set. This kind of checks reduce the speed of execution.
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in Circle function?

Postby fxm » Jan 21, 2021 20:03

I have the impression that it is the concordance with the use of the 'F' flag of 'CIRCLE' that produces this behavior.
UEZ
Posts: 688
Joined: May 05, 2017 19:59
Location: Germany

Re: Bug in Circle function?

Postby UEZ » Jan 21, 2021 21:25

fxm wrote:I have the impression that it is the concordance with the use of the 'F' flag of 'CIRCLE' that produces this behavior.

That's correct - only with fill the issue appears.
SARG
Posts: 1230
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Bug in Circle function?

Postby SARG » Jan 22, 2021 1:19

Reduced code for showing the bug.
When x=1.#INF
gas32 --> nothing drawed
gas64/gcc64 --> weird lines

Code: Select all

screen 18,32
randomize
dim As Single  x, y=300, z = 50
Do
   for i as integer =5 to 0 step -1
       x=50.0/i
       circle (x, y), z, rgb(rnd*255,rnd*255,rnd*255),,,,F
       print x:sleep 1000:cls
   Next
loop Until Len(Inkey())
sleep


A workaround for your code : add ' if x<0 then continue for' after the calculation of x.
UEZ
Posts: 688
Joined: May 05, 2017 19:59
Location: Germany

Re: Bug in Circle function?

Postby UEZ » Jan 22, 2021 6:52

SARG wrote:Reduced code for showing the bug.
When x=1.#INF
gas32 --> nothing drawed
gas64/gcc64 --> weird lines

Code: Select all

screen 18,32
randomize
dim As Single  x, y=300, z = 50
Do
   for i as integer =5 to 0 step -1
       x=50.0/i
       circle (x, y), z, rgb(rnd*255,rnd*255,rnd*255),,,,F
       print x:sleep 1000:cls
   Next
loop Until Len(Inkey())
sleep


A workaround for your code : add ' if x<0 then continue for' after the calculation of x.

Thanks SARG for minimizing the issue to reproduce it better. My intension was not to find a workaround to use the code also for x64 rather to be sure that there is a difference between x86 / X64 and probably a bug.

An interesting aspect is that it never occurred since I'm working with FB.
fxm
Posts: 10211
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Bug in Circle function?

Postby fxm » Jan 22, 2021 8:44

Well seen SARG:
- The problem also occurs for 'x=-1.#INF'
- On the other hand, the problem does not occur on 'y', but only on 'x' (and with the 'F' flag)

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 9 guests