9th June: Floating point myths, Decent Concepts, Memory pools, Game loops, and more C++ Links

Hello in another C++ Stories Newsletter!

Thank you for being part of our community!

Here's the plan for today's email:

  • Top C++ News
  • Quick Links
  • Reminders

Let's start with reminders :)

Reminders

1. Floating-point myths

Three Myths About Floating-Point Numbers - C++ Stories

Floating-point numbers hide a lot of exciting encoding techniques to represent a wide range of values.  In a new article, Adam, a fellow programmer, and blogger, explains several myths on that data type. Highlights:

  • Know which values in your program are input data, constants, or results of some calculations.
  • Know the capabilities and limitations of floating-point types -  their maximum range, minimum possible number, precision in binary or decimal places, maximum integer represented precisely.
  • Learn about how floating-point numbers are stored, bit by bit.
  • Learn about special values - INF, NaN, positive and negative zero, denormals. Understand how they behave in computations.
  • Take a look at assembly generated by the compiler to see how CPU or GPU operates on your numbers.

2. A Perfect Game Loop

Getting The Game Loop Right - YouTube - by Vittorio Romeo

Just 8 minutes, but explains very well how to move from a simple update loop: while() { update() render(); } - which is dependent on the CPU speed, to a numerically stable and time-based technique.

For example, with an update function taking the delta time update(delta), we get consistent game speed. However, it might get too low precision for simulations, and instabilities (when delta time changes rapidly).

Vittorio proposes a mechanism that calls:

<code><code>while(accumulator > slice) { update(fixed_step); accumulator-=slice;}</code></code>

Additional discussion at the end with some links to further research, like this one: Fix Your Timestep! | Gaffer On Games.

A Quick Question

What's SBO? (in C++)

Think carefully, find the answer at the end of this email :)

3.  Computing the number of digits

Computing the number of digits of an integer even faster by Daniel Lemire

A simple problem: given the integer 999, you want 3, but given the integer 1000, you want 4. What's the fastest way to compute it?

The main idea is to compute the logarithm and then use a lookup table to find the final solution. It appears that we might do even better and reduce the number of multiplications. What's more, the author shows code how to compute the lookup tables at compile time with C++ constexpr.

See the discussion at Reddit/C++, you can find the benchmark of three techniques here: https://quick-bench.com/q/IWGBAZ0YZOCFWi0T98gV-N7PCSE - it shows the fast method to be 1.9 faster than the initial one.

4. Memory Pool

How to implement a memory pool | Practical Modern C++ - Martin Vorbrodt

In his new article and a one-hour video - Martin implements a simple memory pool of fixed size memory chunk allocations. The pool preallocates a memory block on demand and then returns a requested memory block when you call its alloc function. Code from the lesson also included.

5. Decent Concepts

Decent concepts - by Andrzej Krzemieński

In his new blog post, Andrzej discusses a more coherent approach for writing C++20 Concepts. In addition, he goes into details about semantic requirements (can we check it at compile-time, or only at runtime?).

6. Quick Links

Bonus

See slides from my recent presentation on C++20 in this Patreon post (5$+ level required)

See you next week!

Best Regards 

Bartek

PS: The answer to the question: What's SBO? It's an acronym for small Buffer Optimization - a popular technique to reserve some space within the type footprint to avoid additional dynamic memory allocation. It's used in many std::string implementations and few other data types in STL.

Become a patron to

156
Unlock 156 exclusive posts
Be part of the community
Get discounts to an online store
Connect via private message