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#
Go
Haskell
HLSL
Hook
Hylo
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
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Zig
Javascript
GIMPLE
ispc 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
ispc (templates_new)
ispc (trunk)
ispc 1.10.0
ispc 1.11.0
ispc 1.12.0
ispc 1.13.0
ispc 1.14.0
ispc 1.14.1
ispc 1.15.0
ispc 1.16.0
ispc 1.16.1
ispc 1.17.0
ispc 1.18.0
ispc 1.19.0
ispc 1.20.0
ispc 1.21.0
ispc 1.22.0
ispc 1.23.0
ispc 1.9.1
ispc 1.9.2
Options
Source code
// https://www.reddit.com/r/programming/comments/q5zmtt/comment/hg9jv0d typedef uint64<4> MyRNGState; typedef double<2> Point; uniform uint64 xoshiro256ss(uniform MyRNGState& s) { uniform uint64 x = s[1] * 5; uniform uint64 r = (x<<7 | x>>57) * 9; uniform uint64 t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = s[3]<<45 | s[3]>>19; return r; } uint64 xoshiro256ss(varying MyRNGState& s) { uint64 x = s[1] * 5; uint64 r = (x<<7 | x>>57) * 9; uint64 t = s[1] << 17; s[2] ^= s[0]; s[3] ^= s[1]; s[1] ^= s[2]; s[0] ^= s[3]; s[2] ^= t; s[3] = s[3]<<45 | s[3]>>19; return r; } MyRNGState makeRNG(int idx) { MyRNGState s = { 0x3243f6a8885a308d, 0x313198a2e0370734, 0x4a4093822299f31d, 0x0082efa98ec4e6c8, }; s[0] += idx; s[1] += idx; s[2] += idx; s[3] += idx; return s; } inline double randd(varying MyRNGState& s) { return (double)(xoshiro256ss(s)) * 5.421010862427522e-20; } inline uniform double randd(uniform MyRNGState& s) { return (double)(xoshiro256ss(s)) * 5.421010862427522e-20; } Point rejection(varying MyRNGState& s) { Point c; do { c[0] = randd(s)*2 - 1; c[1] = randd(s)*2 - 1; } while (c[0]*c[0] + c[1]*c[1] > 1); return c; } Point sqrt_dist(varying MyRNGState& s) { double t = 2.0 * 3.141592653589793 * randd(s); double r = sqrt(randd(s)); Point c; double cs,sn; sincos(t,&cs,&sn); c[0] = r * cs; c[1] = r * sn; return c; } Point sum_dist(varying MyRNGState& s) { double t = 2.0 * 3.141592653589793 * randd(s); double r = randd(s) + randd(s); if (r >= 1.0) { r = 2.0 - r; } Point c; double cs,sn; sincos(t,&cs,&sn); c[0] = r * cs; c[1] = r * sn; return c; } Point max_dist(varying MyRNGState& s) { double t = 2.0 * 3.141592653589793 * randd(s); double r = randd(s); double x = randd(s); if (x > r) { r = x; } Point c; double cs,sn; sincos(t,&cs,&sn); c[0] = r * cs; c[1] = r * sn; return c; } #define NUM_ITERATIONS (1L << 25) #define BENCH(FUNC) \ export uniform uint64 bench_##FUNC () {\ MyRNGState s = makeRNG(programIndex);\ double sum[2];\ sum[0] = sum[1] = 0.0; \ foreach (i = 0 ... NUM_ITERATIONS) { \ Point c = FUNC(s); \ sum[0] += c[0]; \ sum[1] += c[1]; \ } \ return (uniform uint64)reduce_add(sum[0] + sum[1]);\ } BENCH(rejection) BENCH(sqrt_dist) BENCH(sum_dist) BENCH(max_dist)
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