From c109aff9130a53e7034d7a81d00c0db63f928d64 Mon Sep 17 00:00:00 2001 From: darkharvest Date: Wed, 14 Nov 2018 12:43:03 +0530 Subject: [PATCH] poisson test --- src/Common/Math.cpp | 4 ++- src/Common/int-util.h | 65 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/Common/Math.cpp b/src/Common/Math.cpp index eb0de72b..8a355bea 100644 --- a/src/Common/Math.cpp +++ b/src/Common/Math.cpp @@ -6,5 +6,7 @@ #include "Math.h" namespace { -char suppressMSVCWarningLNK4221; +#ifdef MSVC + char suppressMSVCWarningLNK4221; +#endif } diff --git a/src/Common/int-util.h b/src/Common/int-util.h index af4bcc34..7fe0307b 100644 --- a/src/Common/int-util.h +++ b/src/Common/int-util.h @@ -1,7 +1,20 @@ -// Copyright (c) 2011-2017 The Cryptonote developers -// Copyright (c) 2018 The Circle Foundation -// Distributed under the MIT/X11 software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. +// Copyright (c) 2012-2016, The CryptoNote developers, The Bytecoin developers +// Copyright (c) 2018 ryo-project +// +// This file is part of Bytecoin. +// +// Bytecoin is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Bytecoin is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with Bytecoin. If not, see . #pragma once @@ -10,6 +23,11 @@ #include #include #include +#include + +#if defined(__ANDROID__) +#include +#endif #if defined(_MSC_VER) #include @@ -114,15 +132,25 @@ static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uin static inline uint32_t ident32(uint32_t x) { return x; } static inline uint64_t ident64(uint64_t x) { return x; } +#ifndef __OpenBSD__ +# if defined(__ANDROID__) && defined(__swap32) && !defined(swap32) +# define swap32 __swap32 +# elif !defined(swap32) static inline uint32_t swap32(uint32_t x) { - x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8); - return (x << 16) | (x >> 16); + x = ((x & 0x00ff00ff) << 8) | ((x & 0xff00ff00) >> 8); + return (x << 16) | (x >> 16); } +# endif +# if defined(__ANDROID__) && defined(__swap64) && !defined(swap64) +# define swap64 __swap64 +# elif !defined(swap64) static inline uint64_t swap64(uint64_t x) { - x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8); - x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16); - return (x << 32) | (x >> 32); + x = ((x & 0x00ff00ff00ff00ff) << 8) | ((x & 0xff00ff00ff00ff00) >> 8); + x = ((x & 0x0000ffff0000ffff) << 16) | ((x & 0xffff0000ffff0000) >> 16); + return (x << 32) | (x >> 32); } +# endif +#endif /* __OpenBSD__ */ #if defined(__GNUC__) #define UNUSED __attribute__((unused)) @@ -165,7 +193,24 @@ static inline void memcpy_swap64(void *dst, const void *src, size_t n) { } } - +// Calculate ln(p) of Poisson distribution +// https://github.com/ryo-currency/ryo-writeups/blob/master/poisson-writeup.md +// Original idea : https://stackoverflow.com/questions/30156803/implementing-poisson-distribution-in-c +// Using logarithms avoids dealing with very large (k!) and very small (p < 10^-44) numbers +// lam - lambda parameter - in our case, how many blocks, on average, you would expect to see in the interval +// k - k parameter - in our case, how many blocks we have actually seen +// !!! k must not be zero +// return - ln(p) + +static inline double calc_poisson_ln(double lam, uint64_t k) +{ + double logx = -lam + k * log(lam); + do + { + logx -= log(k); // This can be tabulated + } while (--k > 0); + return logx; +} #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) static_assert(false, "BYTE_ORDER is undefined. Perhaps, GNU extensions are not enabled");