Skip to content

Always sys/random.h, I don't like Apple nor Google equally now #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cbits-apple/init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdint.h>
#include <Security/SecRandom.h>

uint64_t splitmix_init() {
uint64_t result;
int r = SecRandomCopyBytes(kSecRandomDefault, sizeof(uint64_t), &result);
return r == errSecSuccess ? result : 0xfeed1000;
}
4 changes: 0 additions & 4 deletions cbits-unix/init.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <stdint.h>
#include <unistd.h>

/* for macos */
#ifdef __APPLE__
#include <sys/random.h>
#endif

uint64_t splitmix_init() {
uint64_t result;
Expand Down
27 changes: 4 additions & 23 deletions cbits-win/init.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
#include <stdint.h>

#include <windows.h>
#include <ntsecapi.h>

uint64_t splitmix_init() {
/* Handy list at https://stackoverflow.com/a/3487338/1308058 */

uint64_t a = GetCurrentProcessId(); /* DWORD */
uint64_t b = GetCurrentThreadId(); /* DWORD */
uint64_t c = GetTickCount(); /* DWORD */

SYSTEMTIME t = {0,0,0,0,0,0,0,0};
GetSystemTime(&t);

LARGE_INTEGER i;
QueryPerformanceCounter(&i);

return a ^ (b << 32) ^ (c << 16)
^ ((uint64_t) t.wYear << 56)
^ ((uint64_t) t.wMonth << 48)
^ ((uint64_t) t.wDayOfWeek << 40)
^ ((uint64_t) t.wDay << 32)
^ ((uint64_t) t.wHour << 24)
^ ((uint64_t) t.wMinute << 16)
^ ((uint64_t) t.wSecond << 8)
^ ((uint64_t) t.wMilliseconds << 0)
^ ((uint64_t) i.QuadPart);
uint64_t result;
int r = RtlGenRandom(&result, sizeof(uint64_t));
return r ? result : 0xfeed1000;
}
83 changes: 44 additions & 39 deletions splitmix.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: >=1.10
cabal-version: 2.4
name: splitmix
version: 0.1.2
version: 0.1.3
synopsis: Fast Splittable PRNG
description:
Pure Haskell implementation of SplitMix described in
Expand All @@ -26,28 +26,30 @@ description:
(the mixing functions are easily inverted, and two successive outputs
suffice to reconstruct the internal state).

license: BSD3
license: BSD-3-Clause
license-file: LICENSE
maintainer: Oleg Grenrus <oleg.grenrus@iki.fi>
bug-reports: https://github.com/haskellari/splitmix/issues
category: System, Random
build-type: Simple
tested-with:
GHC ==8.6.5
|| ==8.8.4
|| ==8.10.4
|| ==9.0.2
|| ==9.2.8
|| ==9.4.8
|| ==9.6.7
|| ==9.8.4
|| ==9.10.2
|| ==9.12.2
GHC ==8.6.5
|| ==8.8.4
|| ==8.10.4
|| ==9.0.2
|| ==9.2.8
|| ==9.4.8
|| ==9.6.7
|| ==9.8.4
|| ==9.10.2
|| ==9.12.2

extra-doc-files:
Changelog.md
README.md

extra-source-files:
Changelog.md
make-hugs.sh
README.md
test-hugs.sh

flag optimised-mixer
Expand All @@ -72,7 +74,7 @@ library
-- ghc-options: -fplugin=DumpCore -fplugin-opt DumpCore:core-html

build-depends:
base >=4.12.0.0 && <4.22
, base >=4.12.0.0 && <4.22
, deepseq >=1.4.4.0 && <1.6

if flag(optimised-mixer)
Expand All @@ -85,19 +87,22 @@ library
if impl(ghcjs)
cpp-options: -DSPLITMIX_INIT_GHCJS=1

else
if impl(ghc)
cpp-options: -DSPLITMIX_INIT_C=1
elif impl(ghc)
cpp-options: -DSPLITMIX_INIT_C=1

if os(windows)
c-sources: cbits-win/init.c
if os(windows)
c-sources: cbits-win/init.c

else
c-sources: cbits-unix/init.c
elif (os(osx) || os(ios))
c-sources: cbits-apple/init.c
ld-options: -framework Security

else
cpp-options: -DSPLITMIX_INIT_COMPAT=1
build-depends: time >=1.2.0.3 && <1.15
c-sources: cbits-unix/init.c

else
cpp-options: -DSPLITMIX_INIT_COMPAT=1
build-depends: time >=1.2.0.3 && <1.15

source-repository head
type: git
Expand All @@ -110,7 +115,7 @@ benchmark comparison
hs-source-dirs: bench
main-is: Bench.hs
build-depends:
base
, base
, containers >=0.6.0.1 && <0.8
, criterion >=1.6.0.0 && <1.7
, random
Expand All @@ -124,7 +129,7 @@ benchmark simple-sum
hs-source-dirs: bench
main-is: SimpleSum.hs
build-depends:
base
, base
, random
, splitmix

Expand All @@ -136,7 +141,7 @@ benchmark range
main-is: Range.hs
other-modules: Data.Bits.Compat
build-depends:
base
, base
, random
, splitmix

Expand All @@ -147,7 +152,7 @@ test-suite examples
hs-source-dirs: tests
main-is: Examples.hs
build-depends:
base
, base
, HUnit >=1.6.0.0 && <1.7
, splitmix

Expand All @@ -162,7 +167,7 @@ test-suite splitmix-tests
Uniformity

build-depends:
base
, base
, containers >=0.4.0.0 && <0.8
, HUnit >=1.6.0.0 && <1.7
, math-functions >=0.3.3.0 && <0.4
Expand All @@ -177,7 +182,7 @@ test-suite montecarlo-pi
hs-source-dirs: tests
main-is: SplitMixPi.hs
build-depends:
base
, base
, splitmix

test-suite montecarlo-pi-32
Expand All @@ -187,7 +192,7 @@ test-suite montecarlo-pi-32
hs-source-dirs: tests
main-is: SplitMixPi32.hs
build-depends:
base
, base
, splitmix

test-suite splitmix-dieharder
Expand All @@ -197,15 +202,15 @@ test-suite splitmix-dieharder
hs-source-dirs: tests
main-is: Dieharder.hs
build-depends:
async >=2.2.1 && <2.3
, async >=2.2.1 && <2.3
, base
, bytestring >=0.10.8.2 && <0.13
, bytestring >=0.10.8.2 && <0.13
, deepseq
, process >=1.6.0.0 && <1.7
, process >=1.6.0.0 && <1.7
, random
, splitmix
, tf-random >=0.5 && <0.6
, vector >=0.13.0.0 && <0.14
, tf-random >=0.5 && <0.6
, vector >=0.13.0.0 && <0.14

test-suite splitmix-testu01
if !os(linux)
Expand All @@ -219,7 +224,7 @@ test-suite splitmix-testu01
c-sources: tests/cbits/testu01.c
extra-libraries: testu01
build-depends:
base
, base
, base-compat-batteries >=0.10.5 && <0.15
, splitmix

Expand All @@ -230,6 +235,6 @@ test-suite initialization
hs-source-dirs: tests
main-is: Initialization.hs
build-depends:
base
, base
, HUnit >=1.6.0.0 && <1.7
, splitmix
Loading