/* Each bin may represent exactly one observed double. Detect collisions and * let the original ranking routine take over. In each time-prefix vector, * byte b counts samples with value <= bin b (T<=255). */ #if defined(CAN_FILTER) typedef struct {unsigned char lo,hi;double frac;} LevelCut; __attribute__((target("avx512bw,avx512f"))) static int level_cdf(const double *src,int t,int c,const int64_t *bounds,int m, const LevelCut *cuts,int nq,int width,int turbo,double *out,int ch){ double min=src[0],max=src[0],values[64];uint64_t used=0; for(int j=0;jmax)max=v;} /* Accepted values also bound every numeric interval moment far below DBL_MAX. */ if(min < -1.e140 || max > 1.e140)return 0; double span=max-min,scale=span?63./span:0.; if(!isfinite(scale)||!isfinite(span))return 0; unsigned char key[256]; for(int j=0;j63)k=63; uint64_t bit=UINT64_C(1)<lo))); int hi=__builtin_ctzll(_mm512_cmpgt_epu8_mask(f,_mm512_set1_epi8((char)s->hi))); double x=values[lo],y=values[hi],q=s->frac; dst[(width-nq)*c+ch*nq+h]=turbo?((n&1)?x:(x+y)*.5):(q>=.5?y-(y-x)*(1.-q):x+(y-x)*q); } } return 1; } #endif static PyObject *interval_levels(PyObject *self,PyObject *args){ PyObject *obj[4];int turbo;Py_buffer v[4]={{0}};int ok=0; if(!PyArg_ParseTuple(args,"OOOOi",&obj[0],&obj[1],&obj[2],&obj[3],&turbo))return NULL; if(!view(obj[0],v,3,'d',0)||!view(obj[1],v+1,2,'q',0)||!view(obj[2],v+2,1,'d',0)|| !view(obj[3],v+3,3,'d',1)||!separate_output(v,3))goto done; Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2],m=v[1].shape[0],nq=v[2].shape[0]; int width=(turbo?3:5)+(int)nq;const double *q=v[2].buf;const int64_t *bounds=v[1].buf; if(n<8||t<1||t>255||c<2||c>256||m<1||m>65536||nq<1||nq>16||v[1].shape[1]!=2|| v[3].shape[0]!=n||v[3].shape[1]!=m||v[3].shape[2]!=c*width|| (turbo && (nq!=1||q[0]!=.5))){ PyErr_SetString(PyExc_ValueError,"unsupported bounded CDF geometry");goto done; } for(int j=0;j1){PyErr_SetString(PyExc_ValueError,"invalid quantile");goto done;} for(int j=0;jt||bounds[2*j]>=bounds[2*j+1]){ PyErr_SetString(PyExc_ValueError,"invalid interval");goto done;} #ifdef CAN_FILTER if(__builtin_cpu_supports("avx512bw") && CAN_FILTER){ LevelCut *cuts=PyMem_Malloc(m*nq*sizeof(*cuts)); double *tile=PyMem_Malloc(8*t*sizeof(double)),*out=v[3].buf;const double *x=v[0].buf; if(!cuts || !tile){PyMem_Free(cuts);PyMem_Free(tile);PyErr_NoMemory();goto done;} for(int j=0;j