nemequ/portable-snippets logo

nemequ/portable-snippets

Free

Collection of miscellaneous portable C snippets.

FreeFree tier
Type
Open Source

About nemequ/portable-snippets

Portable Snippets is a collection of public domain (CC0) code snippets written in C for performing common tasks that are typically OS, architecture, and/or compiler-dependent. The primary goal is to move those annoying preprocessor conditionals from user code into the snippets. It includes modules for using compiler built-ins/intrinsics (builtin), endianness detection and swapping (endian), atomic synchronization operations (atomic), overflow-safe integer functions (safe-math), exact-width integer types (exact-int), cross-platform clock, CPU time, and monotonic time (clock), fast unaligned loads/stores (unaligned), one-time initialization (once), random number generation (random), debug traps and assertions (debug-trap), and assertion checking (check). The project is continuously tested on multiple compilers (GCC, Clang, MSVC, PGI, Emscripten) and encourages community contributions.

Key Features

Use compiler built-ins/intrinsics with fallback (builtin)
Endianness detection and swapping (endian)
Atomic synchronization operations (atomic)
Overflow-safe integer functions (safe-math)
Exact-width integer types (exact-int)
Cross-platform clock, CPU time, and monotonic time (clock)
Fast unaligned loads/stores (unaligned)
One-time initialization (once)
Random number generation (cryptographic, reproducible, fast) (random)
Debug traps and assertions (debug-trap)

Pros & Cons

Pros
  • Public domain (CC0) license for unrestricted use
  • Continuously tested on GCC, Clang, MSVC, PGI, and Emscripten
  • Modules are independent, can be used in isolation
  • Active repository with community contributions
  • Covers many common portability issues
Cons
  • Requires manual inclusion of snippets (not a library)
  • Some modules are experimental (e.g., unaligned, once, random)
  • Limited to C (no C++ or other languages directly)

Best For

Portable cross-platform C developmentReplacing preprocessor conditionals with clean codeWriting safe integer arithmeticEnsuring correct endian handlingCross-platform clock and timingCompiler intrinsic abstraction

FAQ

What license does this project use?
All code is released under CC0 (public domain).
Which compilers are tested?
The project is continuously tested on GCC, Clang, MSVC, PGI, and Emscripten.
Can I use just one module?
Yes, modules can be used in isolation, but keeping the whole repository together is recommended.
How can I contribute or report issues?
File an issue or pull request on the GitHub repository.