Confused about data types [SOLVED].

New to FreeBASIC? Post your questions here.
Post Reply
giorgos
Posts: 25
Joined: Oct 23, 2012 18:40

Confused about data types [SOLVED].

Post by giorgos »

Hi! :-)

I was trying to rewrite an older program with arrays, in a more modern way, but I got stuck on the way! :-)
To start with basics, lets take a tiny program, that reads 2 numbers (from keyboard), and adds them (and then displaying the result on screen).

Code: Select all

DIM AS UBYTE NR(2), Z, N
FOR N=1 TO 2
INPUT NR(N)
PRINT NR(N)
NEXT
PRINT "----------------"
PRINT NR(1)
PRINT NR(2)
PRINT "----------------"
PRINT "SUM"
Z=NR(1)+NR(2)
PRINT Z
So far, so good!
(At least, I think so)! :-)
The only problem is, that the above program, works with integers.

Lets rewrite it, so it will work with real numbers:

Code: Select all

DIM AS UBYTE N
DIM AS SINGLE NR(2), Z
FOR N=1 TO 2
INPUT NR(N)
PRINT NR(N)
NEXT
PRINT "----------------"
PRINT NR(1)
PRINT NR(2)
PRINT "----------------"
PRINT "SUM"
Z=NR(1)+NR(2)
PRINT Z
Nope! It doesn't adds. Just displaying the 1st number.
Ehm...Am I missing something???
TIA!!! :-)
G.
Last edited by giorgos on Jan 13, 2020 20:13, edited 1 time in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Confused about data types.

Post by MrSwiss »

I don't find a problem, if compiled with default language (-lang FB)?!?

If you are using another dialect, please specify ...

FB's standard data-types: CatPgStdDataTypes
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Confused about data types.

Post by badidea »

Works perfectly fine here as well:

Code: Select all

? 2.34
 2.34
? -13.7
-13.7
----------------
 2.34
-13.7
----------------
SUM
-11.36
The only problem I can think of is that you are not running the executable that belongs to that code.
Running a freebasic code requires 2 steps: 1. compilation 2. execution
If step 1 fails or is skipped for some reason (virus-scanner?), it can be that you keep running a previous compiled (buggy) version.
Try to delete the executable (e.g. hello.exe) and try compilation and run/execute again.
giorgos
Posts: 25
Joined: Oct 23, 2012 18:40

Re: Confused about data types.

Post by giorgos »

THANKS guys!!! :-)
I think I found, the source of the problem:
It was the command line argument -vec 2 (who was never troubled me, till today).
Switching it to 1, solved the problem.

I admit, I don't know the internals of FB!
Do you think, it's a bug?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Confused about data types [SOLVED].

Post by MrSwiss »

FB Manual wrote:-vec < level >

Set level of vector optimizations enabled by the compiler (default: 0)
I've never used this: Compiler Option
Dunno, what it is supposed to be good for, anyway. (don't think it's a bug, though)
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Confused about data types [SOLVED].

Post by fxm »

This is not the first time that fbc has encountered a problem with the use of '-fpu sse -vec 2':
See for example viewtopic.php?f=17&t=21960
giorgos
Posts: 25
Joined: Oct 23, 2012 18:40

Re: Confused about data types [SOLVED].

Post by giorgos »

Well...indeed it's a bug and it's already reported:
https://sourceforge.net/p/fbc/bugs/572/

No need for further actions.
Bye!!! :-)
G.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Confused about data types [SOLVED].

Post by fxm »

Yes, this bug report comes from this thread: viewtopic.php?f=3&t=19271
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Confused about data types [SOLVED].

Post by SARG »

-vec 2 enables intra-expression vectorization.

Not sure but it seems this functionnality is not completly implemented.
In the function extracted from ast-vectorize.bas (see end of post) only add operator appears, no sub or others. Why ?

So Z=NR(1)-NR(2) works perfectly.

Code: Select all

##Z=NR(1)-NR(2)
	movss xmm6, [ebp-16]
	subss xmm6, dword ptr [ebp-12]
	movss [ebp-56], xmm6
With -vec 2

Code: Select all

##Z=NR(1)+NR(2)
	movss xmm6, [ebp-16]
					<----- missing line that exists if vec 1, see below
	movss [ebp-56], xmm6
But with -vec 1

Code: Select all

##Z=NR(1)+NR(2)
	movss xmm6, [ebp-16]
	addss xmm6, dword ptr [ebp-12]
	movss [ebp-56], xmm6
With simple variables no problem : z=f1 + f2 but with arrays and udt fields (as reported in the bug list) it doesn't work.
After quickly looking at the code it could be an error when comparing addresses of the variables involved in the statement.
If I have the time I will try to confirm the cause of the bug.


Code: Select all

private function astIntraTreeVectorize _
	( _
		byval n as ASTNODE ptr _
	) as integer

	dim as ASTNODE ptr l = any
	dim as integer changed = FALSE

	if( n = NULL ) then return FALSE

	if( n->class = AST_NODECLASS_BOP ) then
		if( n->op.op = AST_OP_ADD ) then                       <--------------------------------------- Only add

			maxVectorWidth = 4
			vectorWidth = 0
			if( hMergeNode( n->l, n->r, FALSE ) ) then
				maxVectorWidth = 4
				vectorWidth = 0
				hMergeNode( n->l, n->r, TRUE )

				'' check for multiple HADDs
				l = n->l
				if( l->class = AST_NODECLASS_UOP ) then
					if( l->op.op = AST_OP_HADD ) then
						*n = *l
						'' remove this node
						astDelNode( l )
						n->vector = 0
						return TRUE
					end if
				end if

				astDelTree( n->r )
				n->r = NULL
				n->class = AST_NODECLASS_UOP
				n->op.op = AST_OP_HADD
				n->vector = 0

				return TRUE
			end if
		end if
	end if
	if( astIntraTreeVectorize( n->l ) = TRUE ) then
		changed = TRUE
	end if

	if( astIntraTreeVectorize( n->r ) = TRUE ) then
		changed = TRUE
	end if

	function = changed
end function
Post Reply