Zero-overhead HAL Templates

Hard C++ Architecture
Solve this problem

Design a zero-overhead C++ Hardware Abstraction Layer using compile-time templates. The HAL should map GPIO ports and pins using template parameters, with all operations resolved at compile-time (no runtime cost). Use constexpr and inline for optimization.

Constraints

  • Template parameters: PORT address, PIN number.
  • Read/write operations directly dereference volatile register.
  • set() sets bit, clear() clears bit, toggle() toggles.
  • No virtual functions.
  • Compile-time evaluation enforced.
  • Must produce no non-inline functions in disassembly.
Starter codeC++
1#include <stdint.h>2template<uint32_t PORT, int PIN>3class GPIO {4public:5    static void set();6    static void clear();7    static void toggle();8};