Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #3
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const BITS_TO_PRESERVE = @as(comptime_int, (@typeInfo(usize).Int.bits - 1) - @clz(@as(usize, 20))); // Kinda an approximation of 16 log2(x). Will be divided by 8 to approximate 2 log2(x). fn log_approx_helper(x: usize) usize { const fls = @intCast(std.math.Log2Int(usize), (@typeInfo(usize).Int.bits - 1) - @clz(x)); // [4, 63] const x_with_msb_unset = x ^ @as(usize, 1) << fls; const pack_bits_under_old_msb = x_with_msb_unset >> fls - BITS_TO_PRESERVE; return @as(usize, fls) << BITS_TO_PRESERVE | pack_bits_under_old_msb; // [16, 1023] on 64-bit } /// Modify the array so that it can hold at least `new_capacity` items. /// Invalidates pointers if additional memory is needed. export fn ensureTotalCapacity(capacity: usize, new_capacity: usize) usize { const power = 1 + (log_approx_helper(new_capacity +| 20) -| log_approx_helper(capacity +| 20)) / 8; const shift = @intCast(std.math.Log2Int(usize), power >> 1); const approx_sqrt_2_power = (@as(usize, 1) << shift) | (@as(usize, power & 1) << (shift -| 1)); return @max(capacity +| (capacity / 2 + 8), (capacity +| 20) *| approx_sqrt_2_power - 20); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree