• mina86.com

  • Categories
  • Code
  • Contact
  • Pro tip: You can put URLs in C & C++ code

    Posted by Michał ‘mina86’ Nazarewicz on 1st of April 2022

    Documenting source code is important part of software engineering. Code is read more often than it’s written making it crucial to provide enough context for reader to understand what the implementation is doing. This can come in the form of links to external resources providing description of an algorithm, reference for an API or historic context justifying the code.

    As it turns out, C and C++ languages offer a little-known feature which allows URLs to be included directly in the function source code. For example:

    static float rsqrt(float x) {
    	https://en.wikipedia.org/wiki/Fast_inverse_square_root
    	static_assert(std::numeric_limits<float>::is_iec559);
    	auto i = std::bit_cast<uint32_t>(x) >> 1;
    	auto y = std::bit_cast<float>(UINT32_C(0x5F375A86) - i);
    	y *= 1.5f - x * 0.5F * y * y;
    	return y;
    }