summaryrefslogtreecommitdiff
path: root/rgo
diff options
context:
space:
mode:
Diffstat (limited to 'rgo')
-rw-r--r--rgo/Makefile33
-rw-r--r--rgo/include/rgo.h55
-rw-r--r--rgo/src/fndchr.S40
-rw-r--r--rgo/src/memcpy.S61
-rw-r--r--rgo/src/memdup.c22
-rw-r--r--rgo/src/memfill.S38
-rw-r--r--rgo/src/strdup.c17
-rw-r--r--rgo/src/strfill.c17
-rw-r--r--rgo/src/strlen.S35
9 files changed, 318 insertions, 0 deletions
diff --git a/rgo/Makefile b/rgo/Makefile
new file mode 100644
index 0000000..4a4e29e
--- /dev/null
+++ b/rgo/Makefile
@@ -0,0 +1,33 @@
+SRCS_ASM = \
+ src/fndchr.S \
+ src/memcpy.S \
+ src/memfill.S \
+ src/strlen.S
+SRCS_C = \
+ src/memdup.c \
+ src/strdup.c \
+ src/strfill.c
+
+OBJS_ASM := $(SRCS_ASM:.S=.o)
+OBJS_C := $(SRCS_C:.c=.o)
+OBJS := $(OBJS_ASM) $(OBJS_C)
+LIB := librgo.a
+
+ASFLAGS = \
+ -Iinclude \
+ -g
+
+CFLAGS = \
+ -Iinclude \
+ -g
+
+.PHONY: clean
+
+$(LIB): $(OBJS)
+ ar r $@ $^
+
+clean:
+ rm -fr $(OBJS)
+
+purge:
+ rm -fr $(LIB) $(OBJS)
diff --git a/rgo/include/rgo.h b/rgo/include/rgo.h
new file mode 100644
index 0000000..1d76281
--- /dev/null
+++ b/rgo/include/rgo.h
@@ -0,0 +1,55 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#if !defined(rgo_hdr)
+#define rgo_hdr
+
+#if defined(__ASSEMBLER__)
+.extern rgo_memcmp
+.extern rgo_memcpy
+.extern rgo_memeq
+.extern rgo_memfill
+.extern rgo_strcpy
+.extern rgo_streq
+.extern rgo_strfill
+.extern rgo_strlen
+#else
+#include <stddef.h>
+#include <stdint.h>
+
+#if defined(__cplusplus)
+#define rgo_priv_externc extern "C"
+#else
+#define rgo_priv_externc
+#endif
+
+rgo_priv_externc __attribute__((alloc_size(0x2),hot,malloc,nothrow)) void * __rgo_memdup(void const * __restrict__ ptr, size_t num);
+rgo_priv_externc __attribute__((hot,nothrow)) size_t rgo_fndchr( char const * __restrict__ str, char chr);
+rgo_priv_externc __attribute__((hot,nothrow)) size_t rgo_fndbyte( void const * __restrict__ ptr, size_t num,uint8_t byte);
+rgo_priv_externc __attribute__((hot,nothrow)) int8_t rgo_memcmp( void const * __restrict__ lptr,size_t num,void const * __restrict__ rptr);
+rgo_priv_externc __attribute__((hot,nothrow)) void rgo_memcpy( void const * __restrict__ in, size_t num,void * __restrict__ out);
+rgo_priv_externc __attribute__((hot,nothrow)) uint8_t rgo_memeq( void const * __restrict__ lptr,size_t num,void const * __restrict__ rptr);
+rgo_priv_externc __attribute__((hot,nothrow)) void rgo_memfill( void const * __restrict__ ptr, size_t num,uint8_t val);
+rgo_priv_externc __attribute__((hot,nothrow)) size_t rgo_strcpy( char const * __restrict__ str);
+rgo_priv_externc __attribute__((hot,malloc,nothrow)) char * rgo_strdup( char const * __restrict__ str);
+rgo_priv_externc __attribute__((hot,nothrow)) size_t rgo_streq( char const * __restrict__ lstr,char const * __restrict__ rstr);
+rgo_priv_externc __attribute__((hot,nothrow)) void rgo_strfill( char const * __restrict__ lstr,char chr);
+rgo_priv_externc __attribute__((hot,nothrow)) size_t rgo_strlen( char const * __restrict__ str);
+
+#if defined(__cplusplus)
+template<typename T> [[gnu::alloc_size(0x2),gnu::hot,gnu::malloc]] auto rgo_memdup(T const * __restrict__ const _ptr,::size_t const _num) noexcept -> T * {return static_cast<T *>(::__rgo_memdup(_ptr,_num));}
+#else
+#define rgo_memdup __rgo_memdup
+#endif
+#endif
+
+#endif \ No newline at end of file
diff --git a/rgo/src/fndchr.S b/rgo/src/fndchr.S
new file mode 100644
index 0000000..97c3768
--- /dev/null
+++ b/rgo/src/fndchr.S
@@ -0,0 +1,40 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+.global rgo_fndchr
+
+rgo_fndchr:
+#if defined(__x86_64__)
+ /*
+ rdi: char const * str
+ sil: char chr
+ */
+ /* rax: Address of the current character. */
+ movq %rdi,%rax
+ /* dl: Current character. */
+.loop:
+ movb (%rax),%dl
+ cmpb %dl,%sil
+ je .done /* Exit loop if we have found the character. */
+ testb %dl,%dl
+ je .err /* We encounted the null-terminator but not the specified character. */
+ incq %rax
+ jmp .loop
+.done:
+ subq %rdi,%rax
+ ret
+.err:
+ movq $0xFFFFFFFFFFFFFFFF,%rax
+ ret
+#endif
diff --git a/rgo/src/memcpy.S b/rgo/src/memcpy.S
new file mode 100644
index 0000000..d20e9d6
--- /dev/null
+++ b/rgo/src/memcpy.S
@@ -0,0 +1,61 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+.global rgo_memcpy
+
+rgo_memcpy:
+#if defined(__x86_64__)
+ /*
+ rdi: void const * in
+ rsi: size_t num
+ rdx: void * out
+ */
+ /* rcx: Address of the current input element. */
+ movq %rdi,%rcx
+ /* r8: Address of the current output element. */
+ movq %rdx,%r8
+ /* r9: Number of remaining elements. */
+ movq %rsi,%r9
+ /* r10: Temporary. */
+ /* xmm0: Temporary. */
+.big128cpy: /* SSE2 is a part of AMD64. */
+ cmpq $0x10,%r9
+ jl .wrdcpy
+ movdqu (%rcx),%xmm0
+ movdqu %xmm0,(%r8)
+ addq $0x10,%rcx
+ addq $0x10,%r8
+ subq $0x10,%r9
+ jmp .big128cpy
+.wrdcpy:
+ cmpq $0x8,%r9
+ jl .bytecpy
+ movq (%rcx),%r10
+ movq %r10,(%r8)
+ addq $0x8,%rcx
+ addq $0x8,%r8
+ subq $0x8,%r9
+ jmp .wrdcpy
+.bytecpy:
+ testq %r9,%r9
+ je .done
+ movb (%rcx),%r10b
+ movb %r10b,(%r8)
+ incq %rcx
+ incq %r8
+ decq %r9
+ jmp .bytecpy
+.done:
+ ret
+#endif
diff --git a/rgo/src/memdup.c b/rgo/src/memdup.c
new file mode 100644
index 0000000..0d166f0
--- /dev/null
+++ b/rgo/src/memdup.c
@@ -0,0 +1,22 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+#include <stdlib.h>
+
+void * __rgo_memdup(void const * const __restrict__ _ptr,size_t const _num) {
+ void * const __restrict__ dup = malloc(_num);
+ if (__builtin_expect (dup == NULL,0x0l)) {return NULL;}
+ rgo_memcpy(_ptr,_num,dup);
+ return dup;
+}
diff --git a/rgo/src/memfill.S b/rgo/src/memfill.S
new file mode 100644
index 0000000..3c4fb1f
--- /dev/null
+++ b/rgo/src/memfill.S
@@ -0,0 +1,38 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+.global rgo_memfill
+
+rgo_memfill:
+#if defined(__x86_64__)
+ /*
+ rdi: void const * ptr
+ rsi: size_t num
+ dl: int_least8_t val
+ */
+ /* We don't need to preserve any of the registers we use according to the ABI. */
+ /* rcx: Address of the current element. */
+ movq %rdi,%rcx
+ /* r8: Address of the last element. */
+ movq %rdi,%r8
+ addq %rsi,%r8
+.loop:
+ cmpq %r8,%rcx
+ je .done /* Exit loop if we have reached the final element. */
+ movb %dl,(%rcx)
+ incq %rcx /* Continue to next element. */
+ jmp .loop
+.done:
+ ret
+#endif
diff --git a/rgo/src/strdup.c b/rgo/src/strdup.c
new file mode 100644
index 0000000..f10ef84
--- /dev/null
+++ b/rgo/src/strdup.c
@@ -0,0 +1,17 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+#include <stdlib.h>
+
+char * rgo_strdup(char const * const __restrict__ _str) {return rgo_memdup(_str,rgo_strlen(_str) + (size_t)0x1);}
diff --git a/rgo/src/strfill.c b/rgo/src/strfill.c
new file mode 100644
index 0000000..4d73c11
--- /dev/null
+++ b/rgo/src/strfill.c
@@ -0,0 +1,17 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+#include <stdint.h>
+
+void rgo_strfill(char const * const __restrict__ _str,char const _chr) {rgo_memfill(_str,rgo_strlen(_str),(uint8_t)_chr);}
diff --git a/rgo/src/strlen.S b/rgo/src/strlen.S
new file mode 100644
index 0000000..ff6ed70
--- /dev/null
+++ b/rgo/src/strlen.S
@@ -0,0 +1,35 @@
+/*
+ Copyright 2022 Gabriel Jensen
+
+ This file is part of rgo.
+
+ rgo is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+
+ rgo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License along with rgo. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include <rgo.h>
+
+.global rgo_strlen
+
+rgo_strlen:
+#if defined(__x86_64__)
+ /*
+ rdi: char const * str
+ */
+ /* rsi: Address of the current character. */
+ movq %rdi,%rsi
+ /* dl: Current character. */
+.loop:
+ movb (%rsi),%dl
+ testb %dl,%dl
+ jz .done /* Exit loop if we have reached the null-terminator. */
+ incq %rsi /* Continue to the next character. */
+ jmp .loop
+.done:
+ movq %rsi,%rax
+ subq %rdi,%rax
+ ret
+#endif