css: set PRE’s max-width so it doesn’t stretch the viewport
[mina86.com.git] / posts / urls-in-cpp.en.html
blob9718b0302c0fe6040f2b27cfac6db3bab7089a62
1 <!-- subject: Pro tip: You can put {URLs} in {C} & {C++} code -->
2 <!-- date: 2022-04-01 01:17:43 -->
3 <!-- tags: c, c++ -->
4 <!-- categories: Misc, Techblog -->
6 <p>Documenting source code is important part of software engineering. Code is
7 read more often than it’s written making it crucial to provide enough context
8 for reader to understand what the implementation is doing. This can come in
9 the form of links to external resources providing description of an algorithm,
10 reference for an API or historic context justifying the code.
12 <p>As it turns out, C and C++ languages offer a little-known feature which
13 allows URLs to be included directly in the function source code. For example:
15 <pre>
16 static float rsqrt(float x) {
17 <a href=https://en.wikipedia.org/wiki/Fast_inverse_square_root>https://en.wikipedia.org/wiki/Fast_inverse_square_root</a>
18 static_assert(std::numeric_limits&lt;float>::is_iec559);
19 auto i = std::bit_cast&lt;uint32_t>(x) >> 1;
20 auto y = std::bit_cast&lt;float>(UINT32_C(0x5F375A86) - i);
21 y *= 1.5f - x * 0.5F * y * y;
22 return y;
24 </pre>