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
d source #1
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
dmd 2.078.3
dmd 2.079.0
dmd 2.079.1
dmd 2.080.1
dmd 2.081.2
dmd 2.082.0
dmd 2.089.0
dmd 2.092.0
dmd 2.094.2
dmd nightly
gdc (trunk)
gdc 10.1
gdc 10.2
gdc 10.5
gdc 11.1
gdc 11.3
gdc 11.4
gdc 12.1
gdc 12.2
gdc 12.3
gdc 12.4
gdc 13.1
gdc 13.2
gdc 13.3
gdc 14.1
gdc 14.2
gdc 9.2
gdc 9.3
gdc 9.5
gdc arm 12.2.0
gdc arm 12.3.0
gdc arm 12.4.0
gdc arm 13.1.0
gdc arm 13.2.0
gdc arm 13.3.0
gdc arm 14.1.0
gdc arm 14.2.0
gdc arm64 12.2.0
gdc arm64 12.3.0
gdc arm64 12.4.0
gdc arm64 13.1.0
gdc arm64 13.2.0
gdc arm64 13.3.0
gdc arm64 14.1.0
gdc arm64 14.2.0
gdc hppa 14.2.0
gdc loongarch64 14.1.0
gdc loongarch64 14.2.0
gdc mips 12.1.0
gdc mips 12.2.0
gdc mips 12.3.0
gdc mips 12.4.0
gdc mips 13.1.0
gdc mips 13.2.0
gdc mips 13.3.0
gdc mips 14.1.0
gdc mips 14.2.0
gdc mips64 12.1.0
gdc mips64 12.2.0
gdc mips64 12.3.0
gdc mips64 12.4.0
gdc mips64 13.1.0
gdc mips64 13.2.0
gdc mips64 13.3.0
gdc mips64 14.1.0
gdc mips64 14.2.0
gdc mipsel 12.1.0
gdc mipsel 12.2.0
gdc mipsel 12.3.0
gdc mipsel 12.4.0
gdc mipsel 13.1.0
gdc mipsel 13.2.0
gdc mipsel 13.3.0
gdc mipsel 14.1.0
gdc mipsel 14.2.0
gdc powerpc 12.1.0
gdc powerpc 12.2.0
gdc powerpc 12.3.0
gdc powerpc 12.4.0
gdc powerpc 13.1.0
gdc powerpc 13.2.0
gdc powerpc 13.3.0
gdc powerpc 14.1.0
gdc powerpc 14.2.0
gdc powerpc64 12.1.0
gdc powerpc64 12.2.0
gdc powerpc64 12.3.0
gdc powerpc64 12.4.0
gdc powerpc64 13.1.0
gdc powerpc64 13.2.0
gdc powerpc64 13.3.0
gdc powerpc64 14.1.0
gdc powerpc64 14.2.0
gdc powerpc64 trunk
gdc powerpc64le 12.1.0
gdc powerpc64le 12.2.0
gdc powerpc64le 12.3.0
gdc powerpc64le 12.4.0
gdc powerpc64le 13.1.0
gdc powerpc64le 13.2.0
gdc powerpc64le 13.3.0
gdc powerpc64le 14.1.0
gdc powerpc64le 14.2.0
gdc powerpc64le trunk
gdc riscv32 12.2.0
gdc riscv32 12.3.0
gdc riscv32 12.4.0
gdc riscv32 13.1.0
gdc riscv32 13.2.0
gdc riscv32 13.3.0
gdc riscv32 14.1.0
gdc riscv32 14.2.0
gdc riscv32 trunk
gdc riscv64 12.2.0
gdc riscv64 12.3.0
gdc riscv64 12.4.0
gdc riscv64 13.1.0
gdc riscv64 13.2.0
gdc riscv64 13.3.0
gdc riscv64 14.1.0
gdc riscv64 14.2.0
gdc riscv64 trunk
gdc s390x 12.1.0
gdc s390x 12.2.0
gdc s390x 12.3.0
gdc s390x 12.4.0
gdc s390x 13.1.0
gdc s390x 13.2.0
gdc s390x 13.3.0
gdc s390x 14.1.0
gdc s390x 14.2.0
ldc 0.17.2
ldc 1.0.0
ldc 1.1.0
ldc 1.10.0
ldc 1.11.0
ldc 1.12.0
ldc 1.13.0
ldc 1.14.0
ldc 1.15.0
ldc 1.16.0
ldc 1.17.0
ldc 1.18.0
ldc 1.19.0
ldc 1.2.0
ldc 1.20.0
ldc 1.21.0
ldc 1.22.0
ldc 1.23.0
ldc 1.24.0
ldc 1.25.1
ldc 1.26.0
ldc 1.27.1
ldc 1.28.1
ldc 1.29.0
ldc 1.3.0
ldc 1.30.0
ldc 1.31.0
ldc 1.32.1
ldc 1.33.0
ldc 1.34.0
ldc 1.35.0
ldc 1.36.0
ldc 1.37.0
ldc 1.38.0
ldc 1.39.0
ldc 1.4.0
ldc 1.5.0
ldc 1.6.0
ldc 1.7.0
ldc 1.8.0
ldc 1.9.0
ldc beta
ldc latest CI
Options
Source code
import std.format; import std.math.exponential; import std.math.rounding; import std.range; import std.stdio; import std.traits; int bsearch1000(ref int[1001] xs, int x) { auto i = 0; if (xs[512] <= x) { i = 1000 - 512 + 1; } if (xs[i+256] <= x) { i += 256; } if (xs[i+128] <= x) { i += 128; } if (xs[i+64] <= x) { i += 64; } if (xs[i+32] <= x) { i += 32; } if (xs[i+16] <= x) { i += 16; } if (xs[i+8] <= x) { i += 8; } if (xs[i+4] <= x) { i += 4; } if (xs[i+2] <= x) { i += 2; } if (xs[i+1] <= x) { i += 1; } if (xs[i] == x) return i; else return 0; } auto iflog2(int n) => cast(int) floor(log2(n)); auto bsearch(T, size_t n)(ref T[n] xs, T x) { writeln("static array"); enum k = iflog2(n - 1); writeln("k = ", k); auto p = 0; if (xs[2 ^^ k] <= x) p = (n - 1) - (2 ^^ k) + 1; static foreach_reverse (int i; 0 .. k) { if (xs[p + 2 ^^ i] <= x) p += 2 ^^ i; } if (p > 0 && xs[p] == x) return p; return 0; } auto bsearch(T)(T[] xs, T x) { writeln("dynamic array"); auto n = cast(int) xs.length; auto k = iflog2(n - 1); auto p = 0; if (xs[2 ^^ k] <= x) p = (n - 1) - (2 ^^ k) + 1; foreach_reverse (int i; 0 .. k) { if (xs[p + 2 ^^ i] <= x) p += 2 ^^ i; } if (p > 0 && xs[p] == x) return p; return 0; } void main() { int[9] es = [0, 1, 2, 2, 2, 2, 2, 2, 2]; writeln(bsearch(es, 2)); int[1001] xs; int[] dxs; dxs.length = 1001; for (auto i = 1; i <= 1000; i++) { xs[i] = i * 2; dxs[i] = i * 2; } writeln(bsearch(xs, 1000)); writeln(bsearch(dxs, 1000)); }
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