Refactor code for multi architecture SIMD operations

This commit is contained in:
balhau@balhau.net 2021-12-25 14:53:20 +00:00
parent 55f0152b01
commit 8b82d09726
No known key found for this signature in database
GPG key ID: 1E666F326A121830
30 changed files with 685 additions and 418 deletions

View file

@ -1,222 +0,0 @@
# GNU Make project makefile autogenerated by Premake
ifndef config
config=debug
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild prelink
ifeq ($(config),debug)
RESCOMP = windres
TARGETDIR = bin/Debug
TARGET = $(TARGETDIR)/CppCmds
OBJDIR = obj/Debug
DEFINES += -DDEBUG -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__
INCLUDES += -I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.0 -I/usr/include/wx-3.0
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -g
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -g
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS += -lwx_gtk3u_xrc-3.0 -lwx_gtk3u_html-3.0 -lwx_gtk3u_adv-3.0 -lwx_gtk3u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0 -lglfw3
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS)
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
ifeq ($(config),release)
RESCOMP = windres
TARGETDIR = bin/Release
TARGET = $(TARGETDIR)/CppCmds
OBJDIR = obj/Release
DEFINES += -DNDEBUG -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__
INCLUDES += -I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.0 -I/usr/include/wx-3.0
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O2
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O2
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS += -lwx_gtk3u_xrc-3.0 -lwx_gtk3u_html-3.0 -lwx_gtk3u_adv-3.0 -lwx_gtk3u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0 -lglfw3
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -s
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
OBJECTS := \
$(OBJDIR)/complex.o \
$(OBJDIR)/complex1.o \
$(OBJDIR)/math.o \
$(OBJDIR)/math1.o \
$(OBJDIR)/avx2.o \
$(OBJDIR)/sse.o \
$(OBJDIR)/naive.o \
$(OBJDIR)/sse1.o \
$(OBJDIR)/gui1.o \
$(OBJDIR)/ex1.o \
$(OBJDIR)/ex2.o \
$(OBJDIR)/model.o \
$(OBJDIR)/volatile.o \
$(OBJDIR)/IndexBuffer.o \
$(OBJDIR)/Renderer.o \
$(OBJDIR)/Shader.o \
$(OBJDIR)/VertexArray.o \
$(OBJDIR)/VertexBuffer.o \
$(OBJDIR)/VertexBufferLayout.o \
$(OBJDIR)/gl1.o \
$(OBJDIR)/gl2.o \
$(OBJDIR)/gl3.o \
$(OBJDIR)/utils.o \
RESOURCES := \
CUSTOMFILES := \
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
$(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) | $(TARGETDIR)
@echo Linking CppCmds
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(CUSTOMFILES): | $(OBJDIR)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning CppCmds
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild:
$(PREBUILDCMDS)
prelink:
$(PRELINKCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) $(PCH) | $(OBJDIR)
$(GCH): $(PCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
else
$(OBJECTS): | $(OBJDIR)
endif
$(OBJDIR)/complex.o: src/bmath/complex.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/complex1.o: src/bmath/demos/complex.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/math.o: src/bmath/demos/math.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/math1.o: src/bmath/math.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/avx2.o: src/cpu/avx2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/sse.o: src/cpu/demos/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/naive.o: src/cpu/naive.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/sse1.o: src/cpu/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/gui1.o: src/gui/gui1.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/ex1.o: src/misc/dec/ex1.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/ex2.o: src/misc/dec/ex2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/model.o: src/misc/memory/model.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/volatile.o: src/misc/memory/volatile.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/IndexBuffer.o: src/opengl/IndexBuffer.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/Renderer.o: src/opengl/Renderer.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/Shader.o: src/opengl/Shader.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/VertexArray.o: src/opengl/VertexArray.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/VertexBuffer.o: src/opengl/VertexBuffer.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/VertexBufferLayout.o: src/opengl/VertexBufferLayout.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/gl1.o: src/opengl/gl1.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/gl2.o: src/opengl/gl2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/gl3.o: src/opengl/gl3.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/utils.o: src/opengl/utils.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(OBJDIR)/$(notdir $(PCH)).d
endif

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../../include/misc/memory/util.h"
#include "../memory/util.h"
#define AUTO_FREE __attribute__((cleanup(scoped_free)))

View file

@ -1,6 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../../include/misc/memory/util.h"
#include "../memory/util.h"
void stack_frame2(void)
{

View file

@ -1,5 +1,5 @@
#include <iostream>
#include "../complex.hpp"
#include "../bmath/complex.hpp"
using BMath::Complex;

View file

@ -6,12 +6,12 @@
#include <signal.h>
#include <stdlib.h>
#include "Shader.h"
#include "Renderer.h"
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexArray.h"
#include "utils.h"
#include "../opengl/Shader.h"
#include "../opengl/Renderer.h"
#include "../opengl/VertexBuffer.h"
#include "../opengl/IndexBuffer.h"
#include "../opengl/VertexArray.h"
#include "../opengl/utils.h"
const std::string SHADERS_PATH = "src/opengl/res/shaders/Basic.shader";

View file

@ -74,8 +74,8 @@ void printString(char *s)
void display(void)
{
GLfloat white[3] = {1.0, 1.0, 1.0};
const char* p1="LINDA MERDA";
const char* p2="PUTA MERDA";
const char* p1="KIE KIE";
const char* p2="DIRI RIIR";
glClear(GL_COLOR_BUFFER_BIT);
glColor3fv(white);

View file

@ -1,4 +1,4 @@
#include "../math.hpp"
#include "../bmath/math.hpp"
#include <iostream>
using namespace BMath;

View file

@ -1,7 +1,7 @@
#include <iostream>
#include "../utils.hpp"
#include "../sse.hpp"
#include "../naive.hpp"
#include "../src/cpu/utils.hpp"
#include "../src/cpu/x86/sse.hpp"
#include "../src/cpu/naive.hpp"
#include <sys/time.h>
using namespace cpu;
@ -17,6 +17,7 @@ long int gettime(){
#define MAX_ITER 1000*1000*100
int main(int argc, char** argcv){
SSE sse = SSE();
UInt v1_128[INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
UInt v2_128[INT_LEN_128] = { 0x1, 0x2,0x3,0x4 };
@ -26,7 +27,7 @@ int main(int argc, char** argcv){
Utils::int128BitToLong(v1_128,v1_128_l);
Utils::int128BitToLong(v2_128,v2_128_l);
SSE::paddw(v1_128,v2_128);
sse.add_128(v1_128,v2_128);
long int start,end;
@ -45,14 +46,14 @@ int main(int argc, char** argcv){
start = gettime();
for(int i=0;i<MAX_ITER;i++){
SSE::paddw(v1_128,v2_128);
sse.add_128(v1_128,v2_128);
}
end = gettime();
cout << "SSE Approach paddw: " << end-start << endl;
start = gettime();
for(int i=0;i<MAX_ITER;i++){
SSE::paddd(v1_128_l,v2_128_l);
sse.add_128(v1_128_l,v2_128_l);
}
end = gettime();
cout << "SSE Approach paddd: " << end-start << endl;

150
premake/bmath.make Normal file
View file

@ -0,0 +1,150 @@
# GNU Make project makefile autogenerated by Premake
ifndef config
config=debug
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild prelink
ifeq ($(config),debug)
RESCOMP = windres
TARGETDIR = ../lib/Debug
TARGET = $(TARGETDIR)/libbmath.so
OBJDIR = ../obj/Debug/bmath
DEFINES += -DDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -fPIC -g
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -fPIC -g
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -shared -Wl,-soname=libbmath.so
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
ifeq ($(config),release)
RESCOMP = windres
TARGETDIR = ../lib/Release
TARGET = $(TARGETDIR)/libbmath.so
OBJDIR = ../obj/Release/bmath
DEFINES += -DNDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O2 -fPIC
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O2 -fPIC
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -shared -Wl,-soname=libbmath.so -s
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
OBJECTS := \
$(OBJDIR)/complex.o \
$(OBJDIR)/math.o \
$(OBJDIR)/avx2.o \
$(OBJDIR)/naive.o \
$(OBJDIR)/sse.o \
RESOURCES := \
CUSTOMFILES := \
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
$(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) | $(TARGETDIR)
@echo Linking bmath
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(CUSTOMFILES): | $(OBJDIR)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning bmath
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild:
$(PREBUILDCMDS)
prelink:
$(PRELINKCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) $(PCH) | $(OBJDIR)
$(GCH): $(PCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
else
$(OBJECTS): | $(OBJDIR)
endif
$(OBJDIR)/complex.o: ../src/bmath/complex.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/math.o: ../src/bmath/math.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/avx2.o: ../src/cpu/avx2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/naive.o: ../src/cpu/naive.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/sse.o: ../src/cpu/x86/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(OBJDIR)/$(notdir $(PCH)).d
endif

23
premake/libbmath.lua Normal file
View file

@ -0,0 +1,23 @@
project "bmath"
kind "SharedLib"
language "c++"
targetdir "../lib/%{cfg.buildcfg}"
objdir "../obj"
--files {"**.hpp","**.cpp"}
files {
"../src/bmath/**.hpp",
"../src/bmath/**.cpp",
"../src/cpu/**.hpp",
"../src/cpu/**.cpp"
}
filter "configurations:Debug"
defines { "DEBUG" }
symbols "On"
filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
filter { "system:linux" }

150
premake/libmath.make Normal file
View file

@ -0,0 +1,150 @@
# GNU Make project makefile autogenerated by Premake
ifndef config
config=debug
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild prelink
ifeq ($(config),debug)
RESCOMP = windres
TARGETDIR = ../lib/Debug
TARGET = $(TARGETDIR)/liblibmath.so
OBJDIR = ../obj/Debug/libmath
DEFINES += -DDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -fPIC -g
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -fPIC -g
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -shared -Wl,-soname=liblibmath.so
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
ifeq ($(config),release)
RESCOMP = windres
TARGETDIR = ../lib/Release
TARGET = $(TARGETDIR)/liblibmath.so
OBJDIR = ../obj/Release/libmath
DEFINES += -DNDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O2 -fPIC
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O2 -fPIC
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -shared -Wl,-soname=liblibmath.so -s
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
OBJECTS := \
$(OBJDIR)/complex.o \
$(OBJDIR)/math.o \
$(OBJDIR)/avx2.o \
$(OBJDIR)/naive.o \
$(OBJDIR)/sse.o \
RESOURCES := \
CUSTOMFILES := \
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
$(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) | $(TARGETDIR)
@echo Linking libmath
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(CUSTOMFILES): | $(OBJDIR)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning libmath
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild:
$(PREBUILDCMDS)
prelink:
$(PRELINKCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) $(PCH) | $(OBJDIR)
$(GCH): $(PCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
else
$(OBJECTS): | $(OBJDIR)
endif
$(OBJDIR)/complex.o: ../src/bmath/complex.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/math.o: ../src/bmath/math.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/avx2.o: ../src/cpu/avx2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/naive.o: ../src/cpu/naive.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/sse.o: ../src/cpu/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(OBJDIR)/$(notdir $(PCH)).d
endif

26
premake/sse.lua Normal file
View file

@ -0,0 +1,26 @@
project "sse"
kind "ConsoleApp"
language "c++"
targetdir "../bin/%{cfg.buildcfg}"
objdir "../obj"
--files {"**.hpp","**.cpp"}
files {
"../app/sse.cpp",
"../src/bmath/**.hpp",
"../src/bmath/**.cpp",
"../src/misc/**.hpp",
"../src/misc/**.cpp",
"../src/cpu/**.hpp",
"../src/cpu/**.cpp"
}
filter "configurations:Debug"
defines { "DEBUG" }
symbols "On"
filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
filter { "system:linux" }

158
premake/sse.make Normal file
View file

@ -0,0 +1,158 @@
# GNU Make project makefile autogenerated by Premake
ifndef config
config=debug
endif
ifndef verbose
SILENT = @
endif
.PHONY: clean prebuild prelink
ifeq ($(config),debug)
RESCOMP = windres
TARGETDIR = ../bin/Debug
TARGET = $(TARGETDIR)/sse
OBJDIR = ../obj/Debug/sse
DEFINES += -DDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -g
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -g
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS)
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
ifeq ($(config),release)
RESCOMP = windres
TARGETDIR = ../bin/Release
TARGET = $(TARGETDIR)/sse
OBJDIR = ../obj/Release/sse
DEFINES += -DNDEBUG
INCLUDES +=
FORCE_INCLUDE +=
ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES)
ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O2
ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O2
ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES)
LIBS +=
LDDEPS +=
ALL_LDFLAGS += $(LDFLAGS) -s
LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS)
define PREBUILDCMDS
endef
define PRELINKCMDS
endef
define POSTBUILDCMDS
endef
all: prebuild prelink $(TARGET)
@:
endif
OBJECTS := \
$(OBJDIR)/sse.o \
$(OBJDIR)/complex.o \
$(OBJDIR)/math.o \
$(OBJDIR)/avx2.o \
$(OBJDIR)/naive.o \
$(OBJDIR)/sse1.o \
$(OBJDIR)/model.o \
RESOURCES := \
CUSTOMFILES := \
SHELLTYPE := posix
ifeq (.exe,$(findstring .exe,$(ComSpec)))
SHELLTYPE := msdos
endif
$(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) | $(TARGETDIR)
@echo Linking sse
$(SILENT) $(LINKCMD)
$(POSTBUILDCMDS)
$(CUSTOMFILES): | $(OBJDIR)
$(TARGETDIR):
@echo Creating $(TARGETDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(TARGETDIR)
else
$(SILENT) mkdir $(subst /,\\,$(TARGETDIR))
endif
$(OBJDIR):
@echo Creating $(OBJDIR)
ifeq (posix,$(SHELLTYPE))
$(SILENT) mkdir -p $(OBJDIR)
else
$(SILENT) mkdir $(subst /,\\,$(OBJDIR))
endif
clean:
@echo Cleaning sse
ifeq (posix,$(SHELLTYPE))
$(SILENT) rm -f $(TARGET)
$(SILENT) rm -rf $(OBJDIR)
else
$(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET))
$(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR))
endif
prebuild:
$(PREBUILDCMDS)
prelink:
$(PRELINKCMDS)
ifneq (,$(PCH))
$(OBJECTS): $(GCH) $(PCH) | $(OBJDIR)
$(GCH): $(PCH) | $(OBJDIR)
@echo $(notdir $<)
$(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<"
else
$(OBJECTS): | $(OBJDIR)
endif
$(OBJDIR)/sse.o: ../app/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/complex.o: ../src/bmath/complex.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/math.o: ../src/bmath/math.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/avx2.o: ../src/cpu/avx2.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/naive.o: ../src/cpu/naive.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/sse1.o: ../src/cpu/x86/sse.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
$(OBJDIR)/model.o: ../src/misc/memory/model.cpp
@echo $(notdir $<)
$(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
-include $(OBJECTS:%.o=%.d)
ifneq (,$(PCH))
-include $(OBJDIR)/$(notdir $(PCH)).d
endif

View file

@ -2,41 +2,5 @@ workspace "CppLib"
configurations {"Debug", "Release" }
system "linux"
project "CppCmds"
kind "ConsoleApp"
language "c++"
targetdir "bin/%{cfg.buildcfg}"
files {"**.hpp","**.cpp"}
filter "configurations:Debug"
defines { "DEBUG" }
symbols "On"
filter "configurations:Release"
defines { "NDEBUG" }
optimize "On"
filter { "system:linux" }
links {
"wx_gtk3u_xrc-3.0",
"wx_gtk3u_html-3.0",
"wx_gtk3u_adv-3.0",
"wx_gtk3u_core-3.0" ,
"wx_baseu_xml-3.0",
"wx_baseu_net-3.0",
"wx_baseu-3.0",
"glfw3"
}
includedirs {
"/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.0" ,
"/usr/include/wx-3.0"
}
defines {
"_FILE_OFFSET_BITS=64",
"WXUSINGDLL",
"__WXGTK__"
}
include("premake/sse.lua")
include("premake/libbmath.lua")

View file

@ -1,11 +1,11 @@
#pragma once
namespace cpu
{
class avx2
{
public:
class avx2
{
public:
static int const AVX2_INT_SIZE = 16;
static void avx_sum(int *a, int *b);
static void printAVX2Num(int *num);
};
};
};

View file

@ -11,8 +11,8 @@ void cpu::Naive::sum_128_long(UInt *a,UInt *b){
a1=a1+b1;
a2=a2+b2;
a[0]=a1>>32;
a[1]=a1;
a[2]=a2>>32;
a[3]=a2;
a[0]=(UInt)a1;
a[1]=(UInt)(a1>>32);
a[2]=(UInt)a2;
a[3]=(UInt)(a2>>32);
};

View file

@ -3,10 +3,10 @@
#include "types.hpp"
namespace cpu
{
class Naive
{
public:
//Methods
class Naive
{
public:
// Methods
static void sum_128_long(UInt *a, UInt *b);
};
};
};

13
src/cpu/simd.hpp Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include "types.hpp"
namespace cpu
{
class SIMD
{
public:
virtual void add_128(UInt *a, UInt *b)=0;
virtual void add_128(ULong *a, ULong *b)=0;
};
};

View file

@ -1,14 +0,0 @@
#pragma once
#include "types.hpp"
namespace cpu
{
class SSE
{
public:
static void paddw(UInt *a, UInt *b);
static void paddd(ULong *a, ULong *b);
};
};

View file

@ -1,58 +1,64 @@
#pragma once
#include "../platform/platform.hpp"
/*
* Types for x86 architectures
*/
#if defined(__x86_64__)
//Signed type alias
typedef int Int;
typedef long Long;
typedef char Char;
typedef short Short;
#ifdef ARCH_X86
// Signed type alias
typedef int Int;
typedef long Long;
typedef char Char;
typedef short Short;
typedef union {
typedef union
{
struct
{
Int value : 31;
Int signal : 1;
} int_with_msb;
Int signed_int;
} SignedInt;
} SignedInt;
typedef union {
struct {
typedef union
{
struct
{
Long value : 63;
Int signal : 1;
} long_with_msb;
} SignedLong;
} SignedLong;
//IEEE floating point alias
typedef float Float;
typedef double Double;
// IEEE floating point alias
typedef float Float;
typedef double Double;
//Unsigned type alias
typedef unsigned char UChar;
typedef unsigned short UShort;
typedef unsigned int UInt;
typedef unsigned long ULong;
// Unsigned type alias
typedef unsigned char UChar;
typedef unsigned short UShort;
typedef unsigned int UInt;
typedef unsigned long ULong;
//SSE DataTypes
#define INT_LEN_64 2
#define INT_LEN_128 4
#define INT_LEN_256 8
#define INT_LEN_512 16
// SSE DataTypes
#define INT_LEN_64 2
#define INT_LEN_128 4
#define INT_LEN_256 8
#define INT_LEN_512 16
#define LONG_LEN_64 1
#define LONG_LEN_128 2
#define LONG_LEN_256 4
#define LONG_LEN_512 8
#define LONG_LEN_64 1
#define LONG_LEN_128 2
#define LONG_LEN_256 4
#define LONG_LEN_512 8
//Masks
#define MASK_32 0xFFFFFFFF
#define MASK_16 0xFFFF
#define MASK_8 0xFF
// Masks
#define MASK_32 0xFFFFFFFF
#define MASK_16 0xFFFF
#define MASK_8 0xFF
//Shifts
#define SHIFT_32 32
#define SHIFT_16 16
#define SHIFT_8 8
// Shifts
#define SHIFT_32 32
#define SHIFT_16 16
#define SHIFT_8 8
#endif

View file

@ -7,9 +7,9 @@ using namespace std;
namespace cpu
{
class Utils
{
public:
class Utils
{
public:
template <typename Number>
static void printHex(Number *num, Int len)
{
@ -38,13 +38,13 @@ public:
*/
static void long128BitToInt(ULong *packedULong, UInt *packedUInteger)
{
//Unpack first long
// Unpack first long
packedUInteger[0] = packedULong[0] & MASK_32;
packedUInteger[1] = packedULong[0] >> SHIFT_32;
//Unpack second long
// Unpack second long
packedUInteger[2] = packedULong[1] & MASK_32;
packedUInteger[3] = packedULong[1] >> SHIFT_32;
};
};
};
}; // namespace Cpu

View file

@ -1,8 +1,9 @@
#include "sse.hpp"
#ifdef ARCH_X86
#include <iostream>
//X86 Assembly to add two 128 bit numbers in the form of packed integers 32bit
void cpu::SSE::paddw(UInt *a,UInt *b) {
void cpu::SSE::add_128(UInt *a,UInt *b) {
asm(
"movdqa %0, %%xmm1\n"
"paddw %1, %%xmm1\n"
@ -14,7 +15,7 @@ void cpu::SSE::paddw(UInt *a,UInt *b) {
//X86 Assembly to add two 128 bit numbers in the form of packed long 64bit
void cpu::SSE::paddd(ULong *a,ULong *b) {
void cpu::SSE::add_128(ULong *a,ULong *b) {
asm(
"movdqa %0, %%xmm1\n"
"paddd %1, %%xmm1\n"
@ -23,3 +24,4 @@ void cpu::SSE::paddd(ULong *a,ULong *b) {
: "m"(*b)
);
};
#endif

18
src/cpu/x86/sse.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include "../../platform/platform.hpp"
#ifdef ARCH_X86
#include "../types.hpp"
#include "../simd.hpp"
namespace cpu
{
class SSE: public SIMD
{
public:
void add_128(UInt *a, UInt *b);
void add_128(ULong *a, ULong *b);
};
};
#endif

View file

@ -74,30 +74,3 @@ template <typename Object>
void printBoxContent(Box<Object> box){
print("Box: Content: " << *box << ":" << box << ":" << box.getRefCounter())
}
int main(void)
{
print("Stack memory management")
Point2D<int> p1={1,2};
printPointByValue(p1);
print("")
print("Heap memory management")
Point2D<int> *p2 = new Point2D<int>{3, 4};
printPointByRef(p2);
delete p2;
print("")
print("Heap/Stack memory management")
Point2D<int> *ptrPt3 = new Point2D<int>(10,20);
Box<Point2D<int>> box1(ptrPt3);
print(box1)
print("")print("")
printBoxContent(box1);
Box<Point2D<int>> box2=box1;
printBoxContent(box2);
Atomic<int> atomic_int(12);
return 0;
}

19
src/platform/platform.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
// Check for architecture
#ifdef __i386
#define ARCH_X86
#endif
#ifdef __x86__
#define ARCH_X86
#endif
#ifdef __x64__
#define ARCH_X86
#endif
#ifdef __x86_64__
#define ARCH_X86
#endif