Latest posts
Fixed point sqrt and rsqrt approximations
05 Mar 2017
Binary fixed-point real number representation is a popular alternative to floating point. It is useful on embedded systems without native float support, or in situations where uniform precision across the whole representable range is preferred.
The main idea is to store an approximation of a real number as some integer value equal to the real number scaled by 2N, there N is a number of fractional bits. For example 9.34375 with 8 fractional bits will be stored as 9.34375*256 = 2392 = 0x958. Alternatively, this can be written as I·2E
, where I
is an integer and E
is an exponent (usually negative): 2392*2-8 = 9.34375.
Basic arithmetic operations can be expressed with integer arithmetic: addition and subtraction work as is, multiplication require additional division of the product by 2-E and division require scaling the dividend by 2-E. And all those additional operations with 2-E can be performed as a combination of bit shifts, additions, and subtractions.
So basic operations are simple and fast. But how can we calculate more complex functions? For example sqrt(x) and 1/sqrt(x)?
Here is my single-header C++ fixed-point math library with sqrt and rsqrt approximations. Next, I will describe how this calculation is performed.
GLSL graph
25 Feb 2017
Recently, I've made simple WebGL 2 based graph plotting tool: GLSL grapher.
There are a lot of different online graph plotting tools. But rarely you can find anything which supports writing functions as GLSL code. So here it is.
{{substag}} tag substitution engine
27 Sep 2016
This blog is built by custom static site generator written in C++. It combines some HTML templates, some text snippets and files with blog posts contents into a couple of plain HTML files. An important part of this process is actual template substitution.
Before writing my own template substitution engine I've searched for existing solutions. {{mustache}}
looks like an almost ideal engine for my case.
But existing C++ libs for it turned out to be too complex for such simple task. They require using some variant-like tree structures to pass data into the engine. And some aspects of {{mustache}}
are not great for me:
- It HTML-escapes everything by default. While I need to stitch actual HTML code.
- The different behavior of regular tag expansion and partial expansion. A lot of my snippets need recursive expansion.
And it was actually faster to write my own tag substitution engine than to try to work around existing libs.
So here it is: {{substag}}
Start of this blog
23 Sep 2016
Hi, my name is Ryhor Spivak, and this blog is a collection of my programming related code snippets, algorithm ideas and some random thoughts.