/* 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; /* Assign each sample the count of strictly smaller samples. Equal values therefore share a code; the gaps in rank space are harmless for a CDF. */ __attribute__((target("avx512bw,avx512f"))) static void rank_codes64(const double *src,int t,int c,unsigned char *key,double *values){ double v[64];for(int j=0;j=8?255:(1u<<(t-j))-1; __m512d x=_mm512_maskz_loadu_pd(valid,v+j);__m512i ranks=_mm512_setzero_si512(),one=_mm512_set1_epi64(1); for(int h=0;hmax)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)<64)return 0; rank_codes64(src,t,c,key,values);break; } values[k]=v;used|=bit;key[j]=(unsigned char)k; } static const unsigned char index[64]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, 32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63}; __m512i prefix[256],cdf=_mm512_setzero_si512(),one=_mm512_set1_epi8(1),bins=_mm512_loadu_si512(index); prefix[0]=cdf; for(int j=0;jlo))); 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,partial=0;Py_buffer v[4]={{0}};int ok=0;PyObject *mask=NULL; if(!PyArg_ParseTuple(args,"OOOOi|p",&obj[0],&obj[1],&obj[2],&obj[3],&turbo,&partial))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<1||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;jn?n:lo+8; int good=1; for(Py_ssize_t i=lo;good&&i