Table of Contents
Fetching ...

Euclidean Affine Functions and Applications to Calendar Algorithms

Cassio Neri, Lorenz Schneider

TL;DR

The paper develops a systematic framework for Euclidean affine functions $f(r)=\frac{\alpha r+\beta}{\delta}$ and residuals $\mathring{f}(r)=(\alpha r+\beta)\%\delta$, deriving algebraic relations and CPU-friendly approximations that accelerate common integer-divide-and-remainder tasks. It then builds a modular calendar framework based on rata die functions, introducing a computational calendar and a series of efficient mappings that translate Gregorian dates into fast, unsigned arithmetic and back. The main contribution is a set of fast EAF-based evaluation strategies (including fast division and fast residuals) and a generic, provably correct approach to calendar arithmetic that yields speedups over widely used libraries such as glibc, Boost, libc++, OpenJDK, and .NET. Practically, these methods enable substantially faster Gregorian calendar computations and date conversions on modern CPUs, with broad implications for date/time handling in software systems. The work also provides a unified, mathematically grounded pathway to derive fast, low-latency date transformations across epochs and calendar representations.

Abstract

We study properties of Euclidean affine functions (EAFs), namely those of the form $f(r) = (α\cdot r + β)/δ$, and their closely related expression $\mathring{f}(r) = (α\cdot r + β)\%δ$, where $r$, $α$, $β$ and $δ$ are integers, and where $/$ and $\%$ respectively denote the quotient and remainder of Euclidean division. We derive algebraic relations and numerical approximations that are important for the efficient evaluation of these expressions in modern CPUs. Since simple division and remainder are particular cases of EAFs (when $α= 1$ and $β= 0$), the optimisations proposed in this paper can also be appplied to them. Such expressions appear in some of the most common tasks in any computer system, such as printing numbers, times and dates. We use calendar calculations as the main application example because it is richer with respect to the number of EAFs employed. Specifically, the main application presented in this article relates to Gregorian calendar algorithms. We will show how they can be implemented substantially more efficiently than is currently the case in widely used C, C++, C# and Java open source libraries. Gains in speed of a factor of two or more are common.

Euclidean Affine Functions and Applications to Calendar Algorithms

TL;DR

The paper develops a systematic framework for Euclidean affine functions and residuals , deriving algebraic relations and CPU-friendly approximations that accelerate common integer-divide-and-remainder tasks. It then builds a modular calendar framework based on rata die functions, introducing a computational calendar and a series of efficient mappings that translate Gregorian dates into fast, unsigned arithmetic and back. The main contribution is a set of fast EAF-based evaluation strategies (including fast division and fast residuals) and a generic, provably correct approach to calendar arithmetic that yields speedups over widely used libraries such as glibc, Boost, libc++, OpenJDK, and .NET. Practically, these methods enable substantially faster Gregorian calendar computations and date conversions on modern CPUs, with broad implications for date/time handling in software systems. The work also provides a unified, mathematically grounded pathway to derive fast, low-latency date transformations across epochs and calendar representations.

Abstract

We study properties of Euclidean affine functions (EAFs), namely those of the form , and their closely related expression , where , , and are integers, and where and respectively denote the quotient and remainder of Euclidean division. We derive algebraic relations and numerical approximations that are important for the efficient evaluation of these expressions in modern CPUs. Since simple division and remainder are particular cases of EAFs (when and ), the optimisations proposed in this paper can also be appplied to them. Such expressions appear in some of the most common tasks in any computer system, such as printing numbers, times and dates. We use calendar calculations as the main application example because it is richer with respect to the number of EAFs employed. Specifically, the main application presented in this article relates to Gregorian calendar algorithms. We will show how they can be implemented substantially more efficiently than is currently the case in widely used C, C++, C# and Java open source libraries. Gains in speed of a factor of two or more are common.

Paper Structure

This paper contains 15 sections, 20 theorems, 78 equations, 4 figures, 4 tables.

Key Result

Lemma 2.4

Let $f$ be the EAF $f(r) = (\alpha\cdot r + \beta)/\delta$, with $\delta > 0$, and $g:\mathbb{Z}\rightarrow\mathbb{Z}$. For any $q\in\mathbb{Z}$ such that $f(g(q) - 1) < f(g(q))$, we have:

Figures (4)

  • Figure 1: The geometry of replacing $n/\delta$ with $\alpha'\cdot n/2^k$, where $\delta = 5$ and $k = 8$, $\alpha' = 2^k/\delta + 1 = 52$.
  • Figure 2: Years $0$ and $1$ in the Gregorian and computational calendars.
  • Figure 3: Relative and absolute timings of rata die evaluations.
  • Figure 4: Relative and absolute timings of rata die inverse evaluations.

Theorems & Definitions (62)

  • Definition 2.1
  • Definition 2.2
  • Definition 2.3
  • Lemma 2.4
  • proof
  • Theorem 2.5
  • proof
  • Example 2.6
  • Example 2.7
  • Example 2.8
  • ...and 52 more