All Blog Entries

All blog entries, ordered from most recent. Entry count: 1066.

Pages: > 1 2 3 4 ... 134 >

# How to check if an integer number is a power of 10?

Oct 2017

I was recently asked a test question to write a function that would check whether a given integer number is a power of ten. I didn't know the answer immediately because I never solved this puzzle before, so I needed to think about a solution. I quickly came to a conclusion that there are many possible solutions, so I later decided to write them down and share with you.

Number is a power of 10 if it's equal to 10, 100, 1000 etc. 1 is also 0-th power of 10. Other numbers like 2, 3, 11, 12 etc. are not powers of 10. 0 and all negative numbers should also be rejected.

Below you can find 5 different solutions - all of them correct, but some of them more efficient than others. Example code is in C, but you can easily implement the same algorithms in Java or any other programming language.

First thing that came to my mind was to convert the number to a string and check whether it contains '1' followed by '0'-s.

int IsLog10_v1(int32_t x)
// Convert x to string.
char buf[12];
itoa(x, buf, 10);
const size_t bufLen = strlen(buf);

// Check if string contains '1' followed by '0'-s.
if(buf[0] != '1')
return 0;
for(size_t i = 1; i < bufLen; ++i)
if(buf[i] != '0')
return 0;
return 1;

Another option is to convert the number to floating-point format, use "real" log10 function, and check if the result is an integer. Note that double must be used here, because float has too small precision and would incorrectly return 1 for inputs like: 999999999, 1000000001.

int IsLog10_v2(int32_t x)
// Convert x to double. Calculate log10 of it.
double x_d = (double)x;
double x_log10 = log10(x_d);
// Check if result is integer number - has zero fractional part.
return x_log10 - floor(x_log10) == 0.0;

If we want to operate on integer numbers only, we may come up with a recursive algorithm that checks whether the number is greater than zero, divisible by 10 and then calls the same function for x/10.

int IsLog10_v3(int32_t x)
if(x <= 0)
return 0;
if(x == 1)
return 1;
if(x % 10 != 0)
return 0;
// Recursion.
return IsLog10_v3(x / 10);

Because it's a tail recursion, it's easy to convert it to iterative algorithm (use loop instead of recursive call).

int IsLog10_v4(int32_t x)
if(x <= 0)
return 0;
// Same algorithm as v3, but converted from recursion to loop.
if(x == 1)
return 1;
if(x % 10 != 0)
return 0;
x /= 10;

Finally, the most efficient solution is to notice that there are only few possible powers of 10 in the range of 32-bit integers, so we can just enumerate them all.

int IsLog10_v5(int32_t x)
// Just enumerate all possible results.
x == 1 ||
x == 10 ||
x == 100 ||
x == 1000 ||
x == 10000 ||
x == 100000 ||
x == 1000000 ||
x == 10000000 ||
x == 100000000 ||
x == 1000000000;

You can find full source code as a simple, complete C program together with tests on my GitHub, as file: IsLog10.c.

Comments | #c #algorithms Share

# Vulkan Memory Allocator 2.0.0-alpha.3

Sep 2017

I just published new version of Vulkan Memory Allocator 2.0.0-alpha.3. I'm quite happy with the quality of this code. Documentation is also updated, so if nothing else, please just go see User guide. I still marked it as "alpha" because I would like to ask for feedback and I may still change everything.

I would like to discuss proposed terminology. Naming things in code is a hard problem in general, and especially as English is not my native language, so please fill free to contact me and propose more elegant names to what I called: allocator, allocation, pool, block, stats, free range, used/unused bytes, own memory, persistently mapped memory, pointer to mapped data, lost allocation (becoming lost, making other lost), defragmentation, and used internally: suballocation, block vector.

Comments | #vulkan #productions #libraries #graphics Share

# What is Samsung phone doing to photos?! (sharpening a lot)

Sep 2017

I now use Samsung Galaxy S7 smartphone and I'm quite happy with it, except for the camera. I noticed that all the photos taken with it look bad. There is clearly something wrong with them. When I zoomed in, I noticed that the device applies insane amount of sharpening. Every photo looks like it was first filtered by bilateral filter (a kind of edge-preserving blur that is used for noise reduction) and then sharpening with intensity set to maximum, which causes annoying ringing artifacts around the edges.

I decided to make an experiment. I gathered all the devices I had access to that can take photos and I brought them to a place where I could photograph a building that has many sharp edges, plus some tram cables. It was the middle of a sunny day, so lighting brightness and contrast was high and devices didn't have a reason to apply too much processing to the photos taken. I configured all of them to fully automatic mode, maximum resolution and JPEG as output format (except Canon camera, where I forgot about it, so I actually made CR2 RAW that I later converted to JPEG). Devices I used for comparison were (click on each link to access original photo file):

When you zoom in to the building, you can clearly see that both Samsung phones applied very strong sharpening. It turns out this is a known problem. There is a discussion on Reddit, as well as YouTube video about it. Sony phone and DSLR don't have this effect.

Samsung Galaxy S6:

Samsung Galaxy S7:

Sony Xperia Z2:

Canon PowerShot G7X Mark II:

Nikon D90:

What's interesting is that the Canon camera also applied some sharpening, and did it even in RAW! (How can they call it RAW then?!) Fortunately in this camera it can be disabled: While in photo shooting mode, press MENU button, go to tab 6, select "Picture Style" and set it to "Neutral", so that the first parameter in the sequence of numbers (meaning "Sharpness Strength") is 0.

In Samsung phones this filter cannot be disabled :( The only way to take pictures without it is to use RAW, where it's not applied. To do it, while in photo shooting mode: swipe left, choose "Professional", enter configuration, select "Image size" and there enable "Save RAW and JPEG files". You need to enter "Professional" mode every time you want to take a photo. Then of course you need to process the image on your PC and convert it to JPEG, e.g. in Adobe Lightroom or other similar program, but there you can decide how much sharpening do you need (or none).

Comments | #photography #hardware Share

# Changes on My Website

Aug 2017

I've dedicated my free time last week to improving this website. I made it myself from scratch more than 10 years ago, using some old version of PHP, MySQL, and HTML 4.01, so it definitely needed some refresh. I introduced many changes to the code, most notably:

There are still things to do. The website, as well as database, still uses ISO-8859-2 codepage instead of UTF-8 (it was a standard for encoding Polish characters before Unicode became popular). The scripts still generate the page using lots of print()-s instead of some template system. But maybe I will fix that next time :)

Comments | #webdev Share

# How to Restrict Access to Apache Server to Local Machine?

Aug 2017

I wanted to do some web development locally, so I installed Apache 2.2, PHP, and MySQL on my Windows 10 machine. When configuring it, I wanted to restrict access to the Apache server to two machines only - local one and another one in my local network.

The way to do it is to enable and use mod_authz_host module. In file C:\Apache2\conf\httpd.conf I needed to make sure that following line is not commented:

LoadModule authz_host_module modules/

Then I could add appropriate directives to <Directory ...> section of this file, or alternatively use them in .htaccess file located next to files of my website.

To deny access from all addresses except my two computers, I started from this:

Order deny,allow
Deny from all
Allow from
Allow from

After restarting Apache (needed to apply any changes in configuration), I found out that I could access my website from the other computer, but not from the local one. I quickly recalled that connections to the same machine go through special loopback interface and use special address: localhost, which has IP So I changed my configuration to this:

Order deny,allow
Deny from all
Allow from
Allow from
Allow from

It didn't work either. That's when I started to search for address where the local connection comes from, using Process Hacker - Network tab, as well as Apache log in file C:\Apache2\logs\access.log. What I found out is that the loopback connection uses IPv6, where address of localhost is: "::1" - however strange it may seem. Explanation of this format can be found here: IPv6 at Wikipedia.

Apache accepts this form of address, so following configuration finally allowed me to connect from my local computer, as well as the other computer from my network:

Order deny,allow
Deny from all
Allow from
Allow from
Allow from
Allow from ::1

Comments | #webdev #networking Share

# How to Use Vulkan SDK with AppVeyor and Travis CI?

Aug 2017

AppVeyor and Travis CI are popular, free web services that provide continuous integration style build and testing environments for open source projects - for Windows and Linux, respectively. They are relatively easy to setup if your project is self-contained, but can be a little bit tricky if you need to install some additional dependencies. Today I successfully finished installing Vulkan SDK in these environments, to test Vulkan Memory Allocator project. Here is how to do it:

In AppVeyor I have everything configured on their website, so you can’t really see configuration file. To download Vulkan SDK, I used curl command which is available by default. It took me some time to find direct link to SDK file because Download page on points to addresses like “”, which is actually a web page full of JavaScript that eventually redirects to real file when opened in a web browser. Finally I found it. Then I entered following commands as Environment > Install script:

curl --silent --show-error --output VulkanSDK.exe
.\VulkanSDK.exe /S

First command downloads and second one installs Vulkan SDK in silent mode. The SDK is installed to C:\VulkanSDK\ directory.

Next I added an environmental variable in Environment > Environmental variables section:

value: c:\VulkanSDK\

In my Visual Studio project settings, I added include directory "$(VULKAN_SDK)/Include" and library directory "$(VULKAN_SDK)/Lib". Then I could successfully #include <vulkan/vulkan.h> and link with vulkan-1.lib.

For Travis CI you can see my current configuration file as: .travis.yml. What I did here is I added few commands to install section. First there are some apt-get commands that install some additional libraries, which I took from Getting Started with the Vulkan SDK page:

sudo apt-get -qq update
sudo apt-get install -y libassimp-dev libglm-dev graphviz libxcb-dri3-0 libxcb-present0 libpciaccess0 cmake libpng-dev libxcb-dri3-dev libx11-dev libx11-xcb-dev libmirclient-dev libwayland-dev libxrandr-dev

Then I download and install Vulkan SDK. URL to real file is in the same format as for Windows. I used wget command for downloading. The file is a self-extracting archive that unpacks SDK content to following subdirectory of current directory: VulkanSDK/

wget -O
chmod ugo+x

Finally I issue a command that sets environmental variable pointing to the SDK, to have it available in the code just like on Windows:


Then I needed to configure my project to search for include files in "$(VULKAN_SDK)/include" and library files in "$(VULKAN_SDK)/lib" (directory names are lowercase this time!). Finally I could #include <vulkan/vulkan.h> and link with

Comments | #graphics #vulkan Share

# Stencil Test Explained Using Code

Aug 2017

I must admit I never used stencil buffer in my personal code. I know it's there available in GPUs and all graphics APIs for years and it's useful for many things, but somehow I never had a need to use it. Recently I became aware that I don't fully understand it. There are many descriptions of stencil test on the Internet, but none of them definitely answered my questions in the way I would like them to be answered. I thought that a piece of pseudocode would explain it better than words, so here is my explanation of the stencil test.

Let's choose Direct3D 11 as our graphics API. Other APIs have similar sets of parameters. D3D11 offers following configuration parameters for stencil test:


BOOL StencilEnable
UINT8 StencilReadMask
UINT8 StencilWriteMask


D3D11_STENCIL_OP StencilFailOp
D3D11_STENCIL_OP StencilDepthFailOp
D3D11_STENCIL_OP StencilPassOp

Parameter passed to ID3D11DeviceContext::OMSetDepthStencilState method:

UINT StencilRef

How do they work? If you render pixel (x, y) and you have current value of stencil buffer available as:

UINT8 Stencil[x, y]

Then pseudocode for stencil test and write could look like below. First, one of two sets of parameters is selected depending on whether current primitive is front-facing or back-facing:

    if(primitive has no front and back face, e.g. points, lines)
      StencilOpDesc = FrontFace
       if(primitive is front facing)
          StencilOpDesc = FrontFace
          StencilOpDesc = BackFace

Then, a test is performed:

    StencilTestPassed =
       (StencilRef & StencilReadMask) StencilOpDesc.StencilFunc
       (Stencil[x, y] & StencilReadMask)

StencilOpDesc.StencilFunc is a comparison operator that can be one of possible enum values: D3D11_COMPARISON_NEVER, LESS, EQUAL, LESS_EQUAL, GREATER, NOT_EQUAL, GREATER_EQUAL, ALWAYS. I think this is quite self-explanatory.

Notice how StencilRef is on the left side of comparison operator and current stencil buffer value is on the right.

Both StencilRef and current stencil buffer value are ANDed with StencilReadMask before comparison.

Next, based on the result of this test, as well as result of depth-test aka Z-test (which is out of scope of this article), an operation is selected:

          Op = StencilOpDesc.SencilPassOp
          Op = StencilOpDesc.StencilDepthFailOp
        Op = StencilOpDesc.StencilFailOp

Op is another enum that controls a new value to be written to stencil buffer. It can be one of:

    case D3D11_STENCIL_OP_KEEP: NewValue = Stencil[x, y]
    case D3D11_STENCIL_OP_ZERO: NewValue = 0
    case D3D11_STENCIL_OP_REPLACE: NewValue = StencilRef
    case D3D11_STENCIL_OP_INCR_SAT: NewValue = min(Stencil[x, y] + 1, 0xFF)
    case D3D11_STENCIL_OP_DECR_SAT: NewValue = max(Stencil[x, y] - 1, 0)
    case D3D11_STENCIL_OP_INVERT: NewValue = ~Stencil[x, y]
    case D3D11_STENCIL_OP_INCR: NewValue = Stencil[x, y] + 1 // with 8-bit wrap-around
    case D3D11_STENCIL_OP_DECR: NewValue = Stencil[x, y] - 1 // with 8-bit wrap-around

Finally, the new value is written to the stencil buffer. Notice how only those bits are changed that are included in StencilWriteMask. Others remain unchanged.

    Stencil[x, y] =
       (Stencil[x, y] & ~StencilWriteMask) |
       (NewValue & StencilWriteMask)

Now as we have all this explained in a very strict way using code, let me answer doubts I had before understanding this, in form of a FAQ.

Q: Are there no separate flags to enable stencil test and stencil write?

A: No. There is only one flag StencilEnable to enable all this functionality.

Q: So how to use only one and not the other?

A: You can find specific set of settings to do that.

To perform only stencil test and not write, set StencilEnable to true, StencilFunc to the comparison function you need and set all *Op to KEEP or alternatively set StencilWriteMask to 0 to disable any modifications to stencil buffer.

To perform only stencil write and not stencil test, set StencilEnable to true, all *Op and StencilWriteMask to values you need and set StencilFunc to ALWAYS to make the stencil test always passing.

Q: Is the StencilRef value also masked by StencilReadMask?

A: Yes. As you can see in the code, it is also ANDed with StencilReadMask, just as the previous value from stencil buffer. You don't need to provide it "pre-masked". (Comparison to "premultipled alpha" comes to my mind...)

In other words, we could say that only bits indicated by StencilReadMask from both sides participate in comparison.

Q: What are stencil value bits replaced to in REPLACE Op mode?

A: They are replaced with StencilRef value - the same that was used in comparison.

Q: Why is it the same StencilRef value as used for comparison, not separate one?

A: I don't know. There is separate StencilReadMask and StencilWriteMask. They could have provided separate "StencilReadRef" and "StencilWriteRef" - but for some reason the didn't :P

Q: What value is incremented/decremented when Op in INCR*, DECR*?

A: It's the original value read from stencil buffer, not masked or shifted in relation to StencilReadMask or StencilWriteMask. Which means it doesn't make much sense to use these ops if your StencilWriteMask looks like e.g. 0xF0 - masks out least significant bits.

Q: Is depth buffer updated when stencil test fails?

A: No. Failing stencil test means that the pixel is discarded, so Z-buffer is not updated and color is not written or blended to render targets.

On the other hand, failing Z-test can cause stencil buffer to be updated when you use StencilDepthFailOp other than KEEP.

If I misunderstood something and some of the information in this article is wrong, please let me know by e-mail or comment below.

Comments | #directx #graphics Share

# Visual C++: IntelliSense Versus Macros

Aug 2017

When you code in C++ using Visual Studio, you may meet following problem: Your code uses preprocessor directives that depend on some macro that is defined elsewhere, e.g. in one of CPP files including the header file you write, and so IntelliSense gets lost and stops working, or even completely grays out that part of your code as inactive. For example:

// Some code...


// Some code where IntelliSense stops working...


I just found a solution to that. It turns out there is a special macro predefined when code is processed by Visual Studio IntelliSense. It's called just __INTELLISENSE__. By using it, you can change parts of your code as seen by IntelliSense parser, e.g. define some macros, without influencing logic seen by the compiler. For example:


// Some code...


// Some more code where IntelliSense is working again...


Comments | #c++ #visual studio Share

Pages: > 1 2 3 4 ... 134 >

[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror]
Copyright © 2004-2017