% Created 2024-04-02 Tue 18:00
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\author{Getz Mikalsen}
\date{2024-04-01}
\title{FreeBSD GSOC 2024 Proposal}
\hypersetup{
 pdfauthor={Getz Mikalsen},
 pdftitle={FreeBSD GSOC 2024 Proposal},
 pdfkeywords={},
 pdfsubject={Port of libc SIMD enhancements to aarch64},
 pdfcreator={Emacs 30.0.50 (Org mode 9.6.15)}, 
 pdflang={English}}
\begin{document}

\maketitle
\tableofcontents

\textbf{\textbf{Note:}} This proposal is also available in the following formats:

\textbf{\textbf{ASCII Text:}}  \url{https://dflund.se/\~getz/GSOC/FreeBSDproposal.txt}

\textbf{\textbf{HTML:}} \url{https://dflund.se/\~getz/GSOC/FreeBSDproposal.html}

\textbf{\textbf{PDF:}} \url{https://dflund.se/\~getz/GSOC/FreeBSDproposal.pdf}

\section{General Information}
\label{sec:orgb3ab4f5}

\begin{verbatim}
Name Getz Mikalsen
Email getz@sdf.org
Phone <MASKED>
IRC getz at libera.chat
Matrix @getz:envs.net
Timezone UTC+1
\end{verbatim}

I can dedicate a significant amount of my time during the summer. With no other
major obligations, I plan to work around 40 hours per week on the proposed tasks
and objectives.

As for the timeline, I will align my schedule with the suggested Google Summer
of Code program dates. The project will aim to commence on the official coding
start date.

I am committed to maintaining open communication with my mentor and the FreeBSD
community throughout the project's duration. Through the mailing lists, IRC and
a weekly blog post. This will ensure that I stay on track, address any
challenges promptly, and incorporate feedback effectively. Regular progress
updates and discussions will be a priority to facilitate a smooth and successful
collaboration as suggested by GSOC.

\subsection{Biography}
\label{sec:org949d615}

For the past four years, I have been an active FreeBSD user, running it on my
personal laptop. Currently, I serve as a System Administrator at our
University's Computer Society, Datorföreningen vid LU/LTH. In this role, I
manage a diverse range of computer systems, including legacy machines like VAX,
Sun, and SGI, as well as modern servers running FreeBSD, Debian, and OpenBSD. My
responsibilities encompass system upgrades, security patch installation,
troubleshooting, and backup and recovery procedures.

As an Embedded Electronics Engineering student pursuing a Master's Degree at
Lund University (LTH) in Sweden [1], I have gained extensive knowledge in processor
design, computer architecture, and I am currently implementing the RISC-V base
ISA as a soft core on an FPGA. Furthermore, I have an interest in reverse
engineering and actively participate at crackmes.one (previously crackmes.de).
Reading and writing assembly code is fun and at times tedious, -O2 is a mess.

Mentor Robert Clausecker <fuz@FreeBSD.org> volunteers to mentor the project.

We have discussed the project on \#freebsd.soc on libera.chat

\section{Porting amd64 libc SIMD enhancements to arm64 (aarch64)}
\label{sec:org06b4fb1}

\subsection{Project Description}
\label{sec:orge2b1b1e}

The proposed project aims to enhance the performance of the FreeBSD libc library
by expanding its performance enhancements to the aarch64 architecture. This will
be done by introducing NEON and groundwork for SVE support for optimized string
manipulation functions.

This will involve addressing the lack of the pmovmskb instruction in NEON
through clever techniques. Furthermore, the project will lay the groundwork for
future SVE support by introducing an \texttt{"aarch64\_archlevel.h"} header file to
handle different architectural levels. The scope of the project will be limited
to porting to NEON due to time constraints and lack of SVE capable hardware.

I currently have access to NEON capable hardware in the form of an M1 Macbook
and two Raspberry Pi 5.

\subsubsection{libc Porting and Optimization}
\label{sec:orgf0371d5}
The initial phase of the project (before midterm evaluation) is suggested to
concentrate on porting and optimizing smaller libc functions from the
\emph{src/lib/libc/amd64/string} directory, such as strlen. These functions will
serve as a foundation for optimizing other string manipulation routines,
including strchrnul, strcat, and related functions.

\subsection{Technical Approach}
\label{sec:org6224263}

As an initial step, I have begun porting the strlen function to the aarch64
architecture with NEON optimizations. The amd64 implementation leverages the
PCMPEQB, PMOVMSKB, and BSF instructions to efficiently scan the string bytes.
For aarch64, I discovered the equivalent optimization technique using the SHRN,
FMOV, RBIT, CLZ, and bit shifting instructions, as used in the ARM "Optimized
Routines" codebase [2]. Important to keep in mind is to avoid the costly moves
back and forth to GPRs.

\subsection{Significance for FreeBSD}
\label{sec:orgacf3d7a}

Enhancing the libc library's support for aarch64 architectures is crucial for
ensuring FreeBSD's continued relevance and performance on modern ARM-based
systems. As the adoption of ARM processors in server and embedded environments
continues to grow, optimizing the libc library for these architectures will
enable FreeBSD to deliver optimal performance and efficiency on a broader range
of hardware platforms.

I am committed to becoming a long-term FreeBSD contributor, actively maintaining
and further improving the optimized code and hope that this GSOC project will be
a good starting point.

\subsection{Applicant Background}
\label{sec:org521a6da}

I am currently pursuing a Master's Degree in Embedded Electronics Engineering at
Lund University (LTH) in Sweden, I am passionate about performance optimization
and low-power computing. Single Instruction Multiple Data (SIMD) technologies
like NEON and SVE offer significant potential for improving the efficiency of
software components, such as the libc library, on ARM-based systems.

I'm currently involved in a research project with a group at LTH creating an
in-memory computing platform using memristor arrays. This project ends before
the summer.

I previously maintained of a branch of GerdaOS [3], for the JioPhone1, a
featurephone released in India. Until I broke the device while debugging. :D I
have completed relevant courses in computer architecture, digital design,
operating systems and effective C programming. I am confident in my ability to
contribute effectively to the proposed project.

I have a strong background in programming languages such as C, C++, Rust, and
shell scripting (z\{sh\}, sed, awk). Additionally, I have hands-on experience with
patching FreeBSD and am familiar with the workflow, "git pull \&\& make buildworld
\&\& make buildkernel".

Im familiar with x86-64 and MIPS assembly which positions me well to grasp
ARM/NEON concepts quickly.

\section{Deliverables}
\label{sec:orgfcada5d}

Introduce a new ARCHLEVEL flag for the aarch64 architecture, allowing selection
between base, NEON, and SVE instruction set extensions. This will involve
creating an \texttt{aarch64\_archlevel.h} header file to manage the different levels of
architectural support.

Update the \texttt{simd(7)} manual page to document the new SIMD-accelerated string
functions for aarch64, including details on the ARCHLEVEL flag and its usage.

And port the functions with already existing SIMD implementations for
amd64 to aarch64.

All development and contributions will be hosted in a publicly accessible fork
of the FreeBSD source code repository (freebsd-src git repository). The primary
deliverable functions for this project are the following:

\begin{verbatim}
Functions to be Ported
------------------------

HEADER    FUNCTION  NOTES
string.h  stpcpy    String copy functions
          stpncpy
          strcat
          strncat
          strcpy
          strncpy
          strlcpy
          strlcat
          strchrnul
          strrchr
          strcspn
          strspn
          strpbrk
          strsep    String tokenisation functions
          strcmp    String comparison functions
          strncmp
          memcpy    Memory copy functions
          memccpy
          memset    Memory initialisation functions
          memchr    Memory search functions
          memrchr
          memcmp    Memory comparison function
          strlen    String length
          timingsafe_bcmp Constant time functions
          timingsafe_memcmp

Additional functions that will benefit from these enhancements:

strings.h index           *legacy, through strchr
          rindex  	      *legacy, through strrchr
          bcmp            *legacy, through memcmp
          bcopy           *legacy, through memmove
          bzero           *legacy, through memset
          explicit_bzero  through memset
          memset_s        through memset
          mempcpy         through memmove
          memcpy          through memmove
          strdup          through memcpy, strlen
          strtok          through strtok_r

\end{verbatim}

\subsection{Testing}
\label{sec:org78331e6}

Performance benchmarking will be carried out similarly to how fuz did when he
implemented the libc SIMD enhancements for amd64 using his strfperf [4] utility.
Functionality tests are located in \emph{src/lib/libc/tests/string}, If further tests
are needed they will be written.

For example \texttt{/src/lib/libc/tests/string/memcmp\_test.c} has support for custom
memcmp function. Helping development tremendously.

\section{Project Schedule}
\label{sec:org55d454d}

The submitter is based in Copenhagen, Denmark.

I intend to continue studying ARM intrinsics ahead of the start date and provide
a proof of concept SIMD port for strlen(3) for the first week. Then gather
feedback and investigate the need for additional unit tests for the ported
functions.

This work will build from the same algorithmic ideas developed in the SIMD
enhancened functions provided by fuz last year [5].

\subsection{Timeline}
\label{sec:org203cc37}

350 hours are allocated for the project as per the GSOC guidelines for a large
project.

\emph{I request the coding start date to be moved up from May 27 to May 31st due to
collisions with an University exam on the 30th}

May 1-26
Community bonding

May 31
Coding start

July 8
Midterm goals reached

July 12
Midterm eval deadline

August 26 - September 2
Final GSoC contributor evaluations

[1] \url{https://www.lunduniversity.lu.se/lubas/i-uoh-lu-TAEEE}

[2] \url{https://github.com/ARM-software/optimized-routines}

[3] \url{https://archive.luxferre.top/gerdaos}

[4] \url{https://github.com/clausecker/strperf}

[5] \url{https://lists.freebsd.org/archives/freebsd-current/2023-December/005195.html}
\end{document}
