2012-01-06 22:47:49 +01:00
|
|
|
/* go-type-complex.c -- hash and equality complex functions.
|
|
|
|
|
|
|
|
Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
#include <complex.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2012-01-06 22:47:49 +01:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "go-type.h"
|
|
|
|
|
|
|
|
/* Hash function for float types. */
|
|
|
|
|
|
|
|
uintptr_t
|
|
|
|
__go_type_hash_complex (const void *vkey, uintptr_t key_size)
|
|
|
|
{
|
|
|
|
if (key_size == 8)
|
|
|
|
{
|
2014-05-29 01:10:47 +02:00
|
|
|
const complex float *cfp;
|
|
|
|
complex float cf;
|
2012-01-06 22:47:49 +01:00
|
|
|
float cfr;
|
|
|
|
float cfi;
|
2014-05-29 01:10:47 +02:00
|
|
|
uint64_t fi;
|
|
|
|
|
|
|
|
cfp = (const complex float *) vkey;
|
|
|
|
cf = *cfp;
|
|
|
|
|
|
|
|
cfr = crealf (cf);
|
|
|
|
cfi = cimagf (cf);
|
2012-01-06 22:47:49 +01:00
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
if (isinf (cfr) || isinf (cfi))
|
2012-01-06 22:47:49 +01:00
|
|
|
return 0;
|
|
|
|
|
2012-09-22 08:06:31 +02:00
|
|
|
/* NaN != NaN, so the hash code of a NaN is irrelevant. Make it
|
|
|
|
random so that not all NaNs wind up in the same place. */
|
2014-05-29 01:10:47 +02:00
|
|
|
if (isnan (cfr) || isnan (cfi))
|
2012-09-22 08:06:31 +02:00
|
|
|
return runtime_fastrand1 ();
|
|
|
|
|
2012-01-06 22:47:49 +01:00
|
|
|
/* Avoid negative zero. */
|
|
|
|
if (cfr == 0 && cfi == 0)
|
|
|
|
return 0;
|
|
|
|
else if (cfr == 0)
|
2014-05-29 01:10:47 +02:00
|
|
|
cf = cfi * I;
|
2012-01-06 22:47:49 +01:00
|
|
|
else if (cfi == 0)
|
2014-05-29 01:10:47 +02:00
|
|
|
cf = cfr;
|
2012-01-06 22:47:49 +01:00
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
memcpy (&fi, &cf, 8);
|
|
|
|
return (uintptr_t) cfi;
|
2012-01-06 22:47:49 +01:00
|
|
|
}
|
|
|
|
else if (key_size == 16)
|
|
|
|
{
|
2014-05-29 01:10:47 +02:00
|
|
|
const complex double *cdp;
|
|
|
|
complex double cd;
|
2012-01-06 22:47:49 +01:00
|
|
|
double cdr;
|
|
|
|
double cdi;
|
2014-05-29 01:10:47 +02:00
|
|
|
uint64_t di[2];
|
2012-01-06 22:47:49 +01:00
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
cdp = (const complex double *) vkey;
|
|
|
|
cd = *cdp;
|
|
|
|
|
|
|
|
cdr = creal (cd);
|
|
|
|
cdi = cimag (cd);
|
|
|
|
|
|
|
|
if (isinf (cdr) || isinf (cdi))
|
2012-01-06 22:47:49 +01:00
|
|
|
return 0;
|
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
if (isnan (cdr) || isnan (cdi))
|
2012-09-22 08:06:31 +02:00
|
|
|
return runtime_fastrand1 ();
|
|
|
|
|
2012-01-06 22:47:49 +01:00
|
|
|
/* Avoid negative zero. */
|
|
|
|
if (cdr == 0 && cdi == 0)
|
|
|
|
return 0;
|
|
|
|
else if (cdr == 0)
|
2014-05-29 01:10:47 +02:00
|
|
|
cd = cdi * I;
|
2012-01-06 22:47:49 +01:00
|
|
|
else if (cdi == 0)
|
2014-05-29 01:10:47 +02:00
|
|
|
cd = cdr;
|
2012-01-06 22:47:49 +01:00
|
|
|
|
2014-05-29 01:10:47 +02:00
|
|
|
memcpy (&di, &cd, 16);
|
|
|
|
return di[0] ^ di[1];
|
2012-01-06 22:47:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
runtime_throw ("__go_type_hash_complex: invalid complex size");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Equality function for complex types. */
|
|
|
|
|
|
|
|
_Bool
|
|
|
|
__go_type_equal_complex (const void *vk1, const void *vk2, uintptr_t key_size)
|
|
|
|
{
|
|
|
|
if (key_size == 8)
|
|
|
|
{
|
2014-05-29 01:10:47 +02:00
|
|
|
const complex float *cfp1;
|
|
|
|
const complex float *cfp2;
|
|
|
|
|
|
|
|
cfp1 = (const complex float *) vk1;
|
|
|
|
cfp2 = (const complex float *) vk2;
|
|
|
|
|
|
|
|
return *cfp1 == *cfp2;
|
2012-01-06 22:47:49 +01:00
|
|
|
}
|
|
|
|
else if (key_size == 16)
|
|
|
|
{
|
2014-05-29 01:10:47 +02:00
|
|
|
const complex double *cdp1;
|
|
|
|
const complex double *cdp2;
|
|
|
|
|
|
|
|
cdp1 = (const complex double *) vk1;
|
|
|
|
cdp2 = (const complex double *) vk2;
|
|
|
|
|
|
|
|
return *cdp1 == *cdp2;
|
2012-01-06 22:47:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
runtime_throw ("__go_type_equal_complex: invalid complex size");
|
|
|
|
}
|