rm my old hash, use awesome floating point hash from Python, include license

This commit is contained in:
Don Bright 2015-09-23 19:30:46 -05:00
parent 600a96f3f4
commit a675ef6e4e
4 changed files with 354 additions and 63 deletions

259
doc/Python-LICENSE.txt Normal file
View file

@ -0,0 +1,259 @@
Part of OpenSCAD is copied from Python (linalg.cc/hash_floating_point()),
so the python license is included below.
----
A. HISTORY OF THE SOFTWARE
==========================
Python was created in the early 1990s by Guido van Rossum at Stichting
Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
as a successor of a language called ABC. Guido remains Python's
principal author, although it includes many contributions from others.
In 1995, Guido continued his work on Python at the Corporation for
National Research Initiatives (CNRI, see http://www.cnri.reston.va.us)
in Reston, Virginia where he released several versions of the
software.
In May 2000, Guido and the Python core development team moved to
BeOpen.com to form the BeOpen PythonLabs team. In October of the same
year, the PythonLabs team moved to Digital Creations (now Zope
Corporation, see http://www.zope.com). In 2001, the Python Software
Foundation (PSF, see http://www.python.org/psf/) was formed, a
non-profit organization created specifically to own Python-related
Intellectual Property. Zope Corporation is a sponsoring member of
the PSF.
All Python releases are Open Source (see http://www.opensource.org for
the Open Source Definition). Historically, most, but not all, Python
releases have also been GPL-compatible; the table below summarizes
the various releases.
Release Derived Year Owner GPL-
from compatible? (1)
0.9.0 thru 1.2 1991-1995 CWI yes
1.3 thru 1.5.2 1.2 1995-1999 CNRI yes
1.6 1.5.2 2000 CNRI no
2.0 1.6 2000 BeOpen.com no
1.6.1 1.6 2001 CNRI yes (2)
2.1 2.0+1.6.1 2001 PSF no
2.0.1 2.0+1.6.1 2001 PSF yes
2.1.1 2.1+2.0.1 2001 PSF yes
2.1.2 2.1.1 2002 PSF yes
2.1.3 2.1.2 2002 PSF yes
2.2 and above 2.1.1 2001-now PSF yes
Footnotes:
(1) GPL-compatible doesn't mean that we're distributing Python under
the GPL. All Python licenses, unlike the GPL, let you distribute
a modified version without making your changes open source. The
GPL-compatible licenses make it possible to combine Python with
other software that is released under the GPL; the others don't.
(2) According to Richard Stallman, 1.6.1 is not GPL-compatible,
because its license has a choice of law clause. According to
CNRI, however, Stallman's lawyer has told CNRI's lawyer that 1.6.1
is "not incompatible" with the GPL.
Thanks to the many outside volunteers who have worked under Guido's
direction to make these releases possible.
B. TERMS AND CONDITIONS FOR ACCESSING OR OTHERWISE USING PYTHON
===============================================================
PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
--------------------------------------------
1. This LICENSE AGREEMENT is between the Python Software Foundation
("PSF"), and the Individual or Organization ("Licensee") accessing and
otherwise using this software ("Python") in source or binary form and
its associated documentation.
2. Subject to the terms and conditions of this License Agreement, PSF hereby
grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
analyze, test, perform and/or display publicly, prepare derivative works,
distribute, and otherwise use Python alone or in any derivative version,
provided, however, that PSF's License Agreement and PSF's notice of copyright,
i.e., "Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015 Python Software Foundation; All Rights Reserved"
are retained in Python alone or in any derivative version prepared by Licensee.
3. In the event Licensee prepares a derivative work that is based on
or incorporates Python or any part thereof, and wants to make
the derivative work available to others as provided herein, then
Licensee hereby agrees to include in any such work a brief summary of
the changes made to Python.
4. PSF is making Python available to Licensee on an "AS IS"
basis. PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON WILL NOT
INFRINGE ANY THIRD PARTY RIGHTS.
5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON,
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
6. This License Agreement will automatically terminate upon a material
breach of its terms and conditions.
7. Nothing in this License Agreement shall be deemed to create any
relationship of agency, partnership, or joint venture between PSF and
Licensee. This License Agreement does not grant permission to use PSF
trademarks or trade name in a trademark sense to endorse or promote
products or services of Licensee, or any third party.
8. By copying, installing or otherwise using Python, Licensee
agrees to be bound by the terms and conditions of this License
Agreement.
BEOPEN.COM LICENSE AGREEMENT FOR PYTHON 2.0
-------------------------------------------
BEOPEN PYTHON OPEN SOURCE LICENSE AGREEMENT VERSION 1
1. This LICENSE AGREEMENT is between BeOpen.com ("BeOpen"), having an
office at 160 Saratoga Avenue, Santa Clara, CA 95051, and the
Individual or Organization ("Licensee") accessing and otherwise using
this software in source or binary form and its associated
documentation ("the Software").
2. Subject to the terms and conditions of this BeOpen Python License
Agreement, BeOpen hereby grants Licensee a non-exclusive,
royalty-free, world-wide license to reproduce, analyze, test, perform
and/or display publicly, prepare derivative works, distribute, and
otherwise use the Software alone or in any derivative version,
provided, however, that the BeOpen Python License is retained in the
Software, alone or in any derivative version prepared by Licensee.
3. BeOpen is making the Software available to Licensee on an "AS IS"
basis. BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE WILL NOT
INFRINGE ANY THIRD PARTY RIGHTS.
4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE
SOFTWARE FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS
AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY
DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
5. This License Agreement will automatically terminate upon a material
breach of its terms and conditions.
6. This License Agreement shall be governed by and interpreted in all
respects by the law of the State of California, excluding conflict of
law provisions. Nothing in this License Agreement shall be deemed to
create any relationship of agency, partnership, or joint venture
between BeOpen and Licensee. This License Agreement does not grant
permission to use BeOpen trademarks or trade names in a trademark
sense to endorse or promote products or services of Licensee, or any
third party. As an exception, the "BeOpen Python" logos available at
http://www.pythonlabs.com/logos.html may be used according to the
permissions granted on that web page.
7. By copying, installing or otherwise using the software, Licensee
agrees to be bound by the terms and conditions of this License
Agreement.
CNRI LICENSE AGREEMENT FOR PYTHON 1.6.1
---------------------------------------
1. This LICENSE AGREEMENT is between the Corporation for National
Research Initiatives, having an office at 1895 Preston White Drive,
Reston, VA 20191 ("CNRI"), and the Individual or Organization
("Licensee") accessing and otherwise using Python 1.6.1 software in
source or binary form and its associated documentation.
2. Subject to the terms and conditions of this License Agreement, CNRI
hereby grants Licensee a nonexclusive, royalty-free, world-wide
license to reproduce, analyze, test, perform and/or display publicly,
prepare derivative works, distribute, and otherwise use Python 1.6.1
alone or in any derivative version, provided, however, that CNRI's
License Agreement and CNRI's notice of copyright, i.e., "Copyright (c)
1995-2001 Corporation for National Research Initiatives; All Rights
Reserved" are retained in Python 1.6.1 alone or in any derivative
version prepared by Licensee. Alternately, in lieu of CNRI's License
Agreement, Licensee may substitute the following text (omitting the
quotes): "Python 1.6.1 is made available subject to the terms and
conditions in CNRI's License Agreement. This Agreement together with
Python 1.6.1 may be located on the Internet using the following
unique, persistent identifier (known as a handle): 1895.22/1013. This
Agreement may also be obtained from a proxy server on the Internet
using the following URL: http://hdl.handle.net/1895.22/1013".
3. In the event Licensee prepares a derivative work that is based on
or incorporates Python 1.6.1 or any part thereof, and wants to make
the derivative work available to others as provided herein, then
Licensee hereby agrees to include in any such work a brief summary of
the changes made to Python 1.6.1.
4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS"
basis. CNRI MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, CNRI MAKES NO AND
DISCLAIMS ANY REPRESENTATION OR WARRANTY OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF PYTHON 1.6.1 WILL NOT
INFRINGE ANY THIRD PARTY RIGHTS.
5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON
1.6.1 FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS
A RESULT OF MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1,
OR ANY DERIVATIVE THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.
6. This License Agreement will automatically terminate upon a material
breach of its terms and conditions.
7. This License Agreement shall be governed by the federal
intellectual property law of the United States, including without
limitation the federal copyright law, and, to the extent such
U.S. federal law does not apply, by the law of the Commonwealth of
Virginia, excluding Virginia's conflict of law provisions.
Notwithstanding the foregoing, with regard to derivative works based
on Python 1.6.1 that incorporate non-separable material that was
previously distributed under the GNU General Public License (GPL), the
law of the Commonwealth of Virginia shall govern this License
Agreement only as to issues arising under or with respect to
Paragraphs 4, 5, and 7 of this License Agreement. Nothing in this
License Agreement shall be deemed to create any relationship of
agency, partnership, or joint venture between CNRI and Licensee. This
License Agreement does not grant permission to use CNRI trademarks or
trade name in a trademark sense to endorse or promote products or
services of Licensee, or any third party.
8. By clicking on the "ACCEPT" button where indicated, or by copying,
installing or otherwise using Python 1.6.1, Licensee agrees to be
bound by the terms and conditions of this License Agreement.
ACCEPT
CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2
--------------------------------------------------
Copyright (c) 1991 - 1995, Stichting Mathematisch Centrum Amsterdam,
The Netherlands. All rights reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

View file

@ -55,6 +55,8 @@ using boost::math::isinf;
#include <boost/random/uniform_real.hpp>
/*Unicode support for string lengths and array accesses*/
#include <glib.h>
// hash double
#include "linalg.h"
#ifdef __WIN32__
#include <process.h>
@ -231,63 +233,6 @@ ValuePtr builtin_sign(const Context *, const EvalContext *evalctx)
return ValuePtr::undefined;
}
/*
This function creates an uint32 seed for boost's mt19937 pseudorandom
number generator given a C++ double input. Basically it's a hash function.
The goals are as follows:
1. produce different integer seeds for doubles that are very small or close
together (0.0001, 0.2, 3.5, 3.6 should all give different outputs)
2. keep backwards compatability with existing .scad files on the internet.
pass any 32-bit positive integer inputs through unchanged.
3. behave exactly the same across all platforms.
avoid using bit representation or undefined behaviors.
See also github issue 452.
*/
uint32_t make_uint32_seed ( double seed ) {
uint32_t result = 0;
if (round(seed)==seed && seed>0 && seed<UINT32_MAX) {
// preserve positive integer seeds for backwards compatability
result = boost_numeric_cast<uint32_t,double>(seed);
} else {
// split the mantissa into two 32 bit chunks, and along with
// the exponent, form 3 inputs for a sdbm-style hash.
// note c++ rules about undefined behavior for signed overflow.
uint32_t hash = 0;
double mantissa = 0;
int exp = 0;
mantissa = frexp( seed, &exp );
PRINTD("1");
while(round(mantissa)!=mantissa) mantissa *= 2;
int64_t tmp = boost_numeric_cast<int64_t,double>(mantissa);
uint64_t tmp0 = 0;
PRINTD("2");
if (tmp<0) {
PRINTD("2.1");
tmp = tmp + UINT64_MAX/2+1;
tmp0 = boost_numeric_cast<uint64_t,int64_t>(tmp);
} else {
PRINTD("2.2");
tmp0 = boost_numeric_cast<uint64_t,int64_t>(tmp);
tmp0 = tmp0 + UINT64_MAX/2+1;
}
PRINTD("3");
uint64_t tmp1 = tmp0 >> 32;
uint64_t tmp2 = tmp0 & 0x00000000FFFFFFFF;
uint32_t c1 = boost_numeric_cast<uint32_t,uint64_t>(tmp1);
uint32_t c2 = boost_numeric_cast<uint32_t,uint64_t>(tmp2);
uint32_t c3 = boost_numeric_cast<uint32_t,uint64_t>(exp);
PRINTD("4");
hash = c1 + 65599 * hash;
hash = c2 + 65599 * hash;
hash = c3 + 65599 * hash;
result = hash;
}
PRINTDB("seed input: %f, output: %u",seed % result);
return result;
}
ValuePtr builtin_rands(const Context *, const EvalContext *evalctx)
{
size_t n = evalctx->numArgs();
@ -296,16 +241,16 @@ ValuePtr builtin_rands(const Context *, const EvalContext *evalctx)
if (v0->type() != Value::NUMBER) goto quit;
double min = v0->toDouble();
if (boost::math::isinf(min)) {
PRINT("WARNING: rands() range cannot be -infinity");
min = std::numeric_limits<uint64_t>::min();
PRINT("WARNING: rands() range min cannot be infinite");
min = -std::numeric_limits<double>::max()/2;
PRINTB("WARNING: resetting to %f",min);
}
ValuePtr v1 = evalctx->getArgValue(1);
if (v1->type() != Value::NUMBER) goto quit;
double max = v1->toDouble();
if (boost::math::isinf(max)) {
PRINT("WARNING: rands() max cannot be infinity");
max = std::numeric_limits<uint64_t>::max();
PRINT("WARNING: rands() range max cannot be infinite");
max = std::numeric_limits<double>::max()/2;
PRINTB("WARNING: resetting to %f",max);
}
if (max < min) {
@ -326,8 +271,8 @@ ValuePtr builtin_rands(const Context *, const EvalContext *evalctx)
if (n > 3) {
ValuePtr v3 = evalctx->getArgValue(3);
if (v3->type() != Value::NUMBER) goto quit;
uint32_t seedui = make_uint32_seed ( v3->toDouble() );
deterministic_rng.seed( seedui );
uint32_t seed = static_cast<uint32_t>(hash_floating_point( v3->toDouble() ));
deterministic_rng.seed( seed );
deterministic = true;
}
Value::VectorType vec;

View file

@ -46,3 +46,89 @@ bool matrix_contains_nan( const Transform3d &m )
}
return false;
}
/* Hash a floating point number, copied almost line by line from
Python's pyhash.c, originally by the python team, Mark Dickinson, etc.
License is under ../doc/Python-LICENSE.TXT. Changes include
de-scattering typedefs, and removing the -1 special return case.
Why use this?
srand(): we need a hash for doubles that won't round them to ints first.
Backwards compatability: hash(x)==x if x is an signed integer with absolute
value less than _PyHASH_MODULUS.
Portability: It should behave the same across all platforms, independent
of internal bit representations &c. This code can be easily extended for
additional types in input and output if needed by modifying the bit
values and typedefs.
Speed: The loop only executes a couple of times at most.
How does it work?
It calculates the Remainder of the input divided by 2^31. Aka it finds
(input % 2^31), aka 'reduction modulo 2^31' where input can be a huge
floating point number like 3*2^90. It uses modular arithmetic and clever
programming. For example: (x*2^n)%z can be rewritten ( x%z * (2^n)%z ) % z
See also:
http://bob.ippoli.to/archives/2010/03/23/py3k-unified-numeric-hash/
https://github.com/python/cpython/blob/master/Python/pyhash.c
https://github.com/python/cpython/blob/master/Include/pyhash.h
http://stackoverflow.com/questions/4238122/hash-function-for-floats
http://betterexplained.com/articles/fun-with-modular-arithmetic/
*/
typedef int32_t Py_hash_t;
typedef uint32_t Py_uhash_t;
typedef double Float_t;
Py_hash_t hash_floating_point(Float_t v)
{
int _PyHASH_BITS = 31;
//if (sizeof(Py_uhash_t)==8) _PyHASH_BITS=61;
Py_uhash_t _PyHASH_MODULUS = (((Py_uhash_t)1 << _PyHASH_BITS) - 1);
Py_uhash_t _PyHASH_INF = 314159;
Py_uhash_t _PyHASH_NAN = 0;
int e, sign;
Float_t m;
Py_uhash_t x, y;
if (!std::isfinite(v)) {
if (std::isinf(v))
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
else
return _PyHASH_NAN;
}
m = frexp(v, &e);
sign = 1;
if (m < 0) {
sign = -1;
m = -m;
}
/* process 28 bits at a time; this should work well both for binary
and hexadecimal floating point. */
x = 0;
while (m) {
x = ((x << 28) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - 28);
m *= 268435456.0; // 2**28
e -= 28;
y = (Py_uhash_t)m; /* pull out integer part */
m -= y;
x += y;
if (x >= _PyHASH_MODULUS)
x -= _PyHASH_MODULUS;
}
/* adjust for the exponent; first reduce it modulo _PyHASH_BITS */
e = e >= 0 ? e % _PyHASH_BITS : _PyHASH_BITS-1-((-1-e) % _PyHASH_BITS);
x = ((x << e) & _PyHASH_MODULUS) | x >> (_PyHASH_BITS - e);
x = x * sign;
return (Py_hash_t)x;
}

View file

@ -23,6 +23,7 @@ using Eigen::Matrix4d;
bool matrix_contains_infinity( const Transform3d &m );
bool matrix_contains_nan( const Transform3d &m );
int32_t hash_floating_point( double v );
template<typename Derived> bool is_finite(const Eigen::MatrixBase<Derived>& x) {
return ( (x - x).array() == (x - x).array()).all();