Square Root of 42: C++ Implementation

Application for Software Developer Position at Headlands Tech

Hello, I'm Tuğrul KÖK, applying for the Software Developer position at Headlands Tech. Recognizing that Headlands Tech operates in High Frequency Trading, I've prepared this demonstration to showcase my C++ programming capabilities and understanding of performance-critical systems.

In response to your coding challenge, I've implemented a C++ program that computes the square root of 42 using multiple optimization strategies. Rather than submitting a simple text file, I've deployed this as a live demonstration running on a custom POSIX socket server built from scratch using standard <sys/socket.h>, demonstrating both algorithmic proficiency and system programming skills.

Full Source Code & Documentation: GitHub Repository

Design Philosophy: While I recognize that std::sqrt is the standard for production code (often mapping to hardware instructions), I implemented these custom solutions to demonstrate algorithmic proficiency. This project highlights my ability to optimize across three layers of abstraction: Mathematical (deriving Newton-Raphson and Fast Inverse Square Root approximations), System-Level (direct usage of x86 SIMD Intrinsics), and Compiler-Level (leveraging constexpr and std::bit_cast for compile-time evaluation and type safety).

Technical Stack: C++20 POSIX Sockets x86 SIMD Nginx Reverse Proxy HTTPS

Live Benchmark Results

All methods compute √42. Results are calculated in real-time on the server:

[★] Unified Smart Function (Best of Both Worlds)
Compile-time evaluation: Result is baked into the binary (zero CPU cost at runtime)
6.480741
[1] x86 SIMD Intrinsics (Hardware-Accelerated)
Direct hardware instruction using SSE registers for maximum performance
6.480741
[2] Constexpr (Compile-Time Evaluation)
Newton-Raphson method evaluated at compile time using C++20 constexpr (baked into binary)
6.480741
[3] Fast Inverse Square Root (Bit Manipulation)
Quake III-style approximation using std::bit_cast for type-safe bit operations
6.480965 (Error: 0.000225, 0.003466%)

Source Code Implementation

The main implementation uses a unified smart function, with individual methods shown below for demonstration:

[★] Unified Smart Function (Production Implementation)

How it works: This is the main production function that intelligently chooses the best approach based on context. It uses std::is_constant_evaluated() to detect compile-time evaluation and automatically routes to the optimal path.

Key features:
  • Path A (Compile-Time): When evaluated at compile time, uses Newton-Raphson method—the compiler does the math during compilation
  • Path B (Runtime x86): When executed at runtime on x86 architectures, uses hardware intrinsics for maximum performance
  • Path C (Runtime Fallback): On non-x86 architectures, uses std::sqrt which compiles to hardware instructions (e.g., FSQRT on ARM). This provides equivalent behavior and precision across all architectures, ensuring consistency regardless of CPU type
  • Single function interface with automatic optimization based on evaluation context
  • Demonstrates advanced C++20 metaprogramming and conditional compilation

// Unified "Best of Both Worlds" Function
constexpr float sqrt_smart(int x) {
    // Path A: Compile-Time (The compiler does the math)
    if (std::is_constant_evaluated()) {
        return sqrt_constexpr(static_cast(x));
    } 
    // Path B: Runtime (The Hardware does the math)
    else {
        #if HAS_INTRINSICS
            // We can safely use intrinsics here because this block 
            // is ONLY entered at runtime!
            return sqrt_intrinsics(x);
        #else
            // Path C: Standard library fallback
            // Provides equivalent behavior on non-x86 architectures
            // std::sqrt compiles to hardware instructions (e.g., FSQRT on ARM)
            return std::sqrt(x);
        #endif
    }
}

// Example usage demonstrating compile-time evaluation
// Compile-time evaluation example (baked into binary)
static constexpr int val = 42;
static constexpr float res_smart_compile = sqrt_smart(val);
// Result is baked into the binary as a raw number. Zero CPU cost at runtime.

[1] x86 SIMD Intrinsics (Hardware-Accelerated)

How it works: This method leverages x86 SSE (Streaming SIMD Extensions) instructions to compute the square root directly in hardware. The _mm_sqrt_ss instruction uses the CPU's floating-point unit, which is significantly faster than software implementations.

Key features:
  • Direct hardware instruction execution via SSE registers
  • Automatic fallback to std::sqrt on non-x86 architectures
  • Single-precision floating-point operation (_mm_sqrt_ss)
  • Zero overhead abstraction—compiles to a single CPU instruction

// 1. Intrinsics Version
float sqrt_intrinsics(int x) {
    if (x < 0) return -1.0f;
    
    // check if intrinsics are available
#if HAS_INTRINSICS
    // set the number
    __m128 num = _mm_set_ss(static_cast(x));
    // square root the number
    __m128 result_vector = _mm_sqrt_ss(num);
    // convert the result to a float
    return _mm_cvtss_f32(result_vector);
#else
    return std::sqrt(static_cast(x));
#endif
}

[2] Constexpr (Compile-Time Evaluation)

How it works: This implementation uses the Newton-Raphson method, an iterative algorithm for finding roots. The constexpr keyword allows the compiler to evaluate this at compile time when the input is known, eliminating runtime computation entirely.

Key features:
  • Newton-Raphson iterative method: x_{n+1} = 0.5 * (x_n + a/x_n)
  • C++20 constexpr enables compile-time evaluation
  • Converges to machine precision automatically
  • Can be used in template metaprogramming and constant expressions

// 2. Constexpr Version
constexpr float sqrt_constexpr(float x) {
    if (x < 0.0f) return -1.0f;

    // newton-raphson method
    float curr = x, prev = 0.0f;
    while (curr != prev) {
        prev = curr; // previous value
        curr = 0.5f * (curr + x / curr); // new value
    }
    return curr; // return the final value
}

[3] Fast Inverse Square Root (Bit Manipulation)

How it works: This is a derivative of the famous Quake III fast inverse square root algorithm. It uses bit manipulation to generate an initial approximation, then refines it with one iteration of Newton's method. The magic number 0x1fbd1df5 is derived from the IEEE 754 floating-point representation.

Accuracy vs. Latency Trade-off: This algorithm demonstrates the ability to make informed decisions about precision vs. performance. While std::sqrt is hardware-accelerated and precise, there are scenarios in low-latency systems (HFT, Monte Carlo simulations, graphics) where a small approximation error is acceptable for significant speed gains. The error margin is explicitly calculated and displayed above.

Key features:
  • Uses std::bit_cast (C++20) for type-safe bit manipulation—avoids undefined behavior
  • Magic number approximation: 0x1fbd1df5 + (i >> 1) exploits floating-point bit patterns
  • One Newton-Raphson iteration refines the initial guess
  • Historically used in game engines for real-time graphics (Quake III Arena)
  • Error margin is calculated and displayed to demonstrate analytical rigor
  • See detailed mathematical derivation in the PDF below

// 3. Fast Square Root Approximation
float sqrt_fast(float x) {
    if (x < 0.0f) return -1.0f;
    
    // 1. Bit-level Manipulation (Initial Guess)
    // Safely reinterpret float bits as int32 for manipulation
    int32_t i = std::bit_cast(x);
    
    // Apply the magic constant and bit-shift
    i = 0x1fbd1df5 + (i >> 1);

    // Reinterpret back to float
    float y = std::bit_cast(i);

    // 2. Newton-Raphson Refinement
    // f(y) = y^2 - x = 0  =>  y' = 0.5 * (y + x/y)
    y = 0.5f * (y + x / y);
    
    return y;
}

Additional Resources

Technical Highlights