playing with the C compiler explorer

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

playing with the C compiler explorer

Post by srvaldez »

just for fun go to https://godbolt.org/ and once you are in, select x86-64 clang 15.0.0 as the compiler and for the compiler options: -O3
click on the cogwheel and put a check on Intel asm syntax and maybe also on Demangle identifiers
then paste in the following

Code: Select all

int m4(__m128 *result, __m128 *x, __m128 *y) {
*result =(*x)/(*y);
return 0;
}
the output will be something like this

Code: Select all

m4(float __vector(4)*, float __vector(4)*, float __vector(4)*):                      # @m4(float __vector(4)*, float __vector(4)*, float __vector(4)*)
        vmovaps xmm0, xmmword ptr [rsi]
        vdivps  xmm0, xmm0, xmmword ptr [rdx]
        vmovaps xmmword ptr [rdi], xmm0
        xor     eax, eax
        ret
to use the code in FB you need to replace rdx with r8, rsi with rdx and rdi with rcx
see https://andreaspk.github.io/posts/2019- ... ntion.html and https://cmcenroe.me/2017/06/29/sysv-abi-reference.html
here's the FB code

Code: Select all

#Define NULL 0

Extern "c"
	Declare Function _aligned_malloc(Byval size As Uinteger, Byval alignment As Uinteger) As Any Ptr
	Declare Sub _aligned_free(Byval memblock As Any Ptr)
End Extern

	Dim As Single Ptr x, y, z
	Dim alignment As Uinteger
	alignment = 16
	'' x
	x = _aligned_malloc(16, alignment)
	If x = NULL Then
		Print "Error allocation aligned memory."
		End -1
	End If
	If (Cuint(x) Mod alignment) = 0 Then
		Print "pointer x, ";Hex(x,16);", is aligned on ";alignment
	Else
		Print "pointer x, ";Hex(x,16);", is not aligned on ";alignment
	End If

	'' y
	y = _aligned_malloc(16, alignment)
	If y = NULL Then
		Print "Error allocation aligned memory."
		End -1
	End If
	If (Cuint(y) Mod alignment) = 0 Then
		Print "pointer y, ";Hex(y,16);", is aligned on ";alignment
	Else
		Print "pointer y, ";Hex(y,16);", is not aligned on ";alignment
	End If

	'' z
	z = _aligned_malloc(16, alignment)
	If z = NULL Then
		Print "Error allocation aligned memory."
		End -1
	End If
	If (Cuint(z) Mod alignment) = 0 Then
		Print "pointer z, ";Hex(z,16);", is aligned on ";alignment
	Else
		Print "pointer z, ";Hex(z,16);", is not aligned on ";alignment
	End If

	x[0]=3.141593 : x[1]=2.718282 : x[2]=8888888 : x[3]=9999999
	y[0]=2 : y[1]=0.5 : y[2]=2 : y[3]=3

	Sub m128f_div Naked Cdecl(Byval result_rcx As Single Ptr, Byval num_rdx As Single Ptr, Byval den_r8 As Single Ptr)
		Asm
			vmovaps xmm0, xmmword Ptr [rdx]
			vdivps  xmm0, xmm0, xmmword Ptr [r8]
			vmovaps xmmword Ptr [rcx], xmm0
			Xor     eax, eax
			ret
		End Asm
	End Sub

	m128f_div(z, x, y)

	Print x[0];"/";y[0];" = ";z[0]
	Print x[1];"/";y[1];" = ";z[1]
	Print x[2];"/";y[2];" = ";z[2]
	Print x[3];"/";y[3];" = ";z[3]

	_aligned_free(z)
	_aligned_free(y)
	_aligned_free(x)
	Sleep
output

Code: Select all

pointer x, 0000000000FF1C20, is aligned on 16
pointer y, 0000000000FF1CB0, is aligned on 16
pointer z, 0000000000FF1CE0, is aligned on 16
 3.141593/ 2 =  1.570796
 2.718282/ 0.5 =  5.436564
 8888888/ 2 =  4444444
 9999999/ 3 =  3333333
you can do a similar thing with

Code: Select all

int m8(__m256 *result, __m256 *x, __m256 *y) {
*result =(*x)/(*y);
return 0;
}
which will divide 8 singles by 8 singles
<< edit >>
if you have a big screen then I suggest that you click on +Add new and select Clone Compiler, then you can have different compilers and options side by side, I have the gcc compiler on one and the clang on the other
Post Reply