#define PY_SSIZE_T_CLEAN #include #include #include #include /* All arrays originate inside our Python wrapper. Enforce contiguous native buffers, dtype, rank, extents and index ranges before native indexing. */ static int view(PyObject *obj, Py_buffer *v, int rank, char type, int write) { if (PyObject_GetBuffer(obj, v, PyBUF_C_CONTIGUOUS|PyBUF_FORMAT|(write?PyBUF_WRITABLE:0)) < 0) return 0; int integer = type=='q'; if (v->ndim!=rank || v->itemsize!=8 || !v->format || (integer ? strcmp(v->format,"q") && strcmp(v->format,"l") : strcmp(v->format,"d")) || (uintptr_t)v->buf % 8) { PyErr_SetString(PyExc_ValueError,"invalid contiguous buffer type or rank");return 0; } return 1; } static int separate_output(const Py_buffer *views,int output){ uintptr_t dst=(uintptr_t)views[output].buf; for(int i=0;i=src ? dst-src<(uintptr_t)views[i].len : src-dst<(uintptr_t)views[output].len)){ PyErr_SetString(PyExc_ValueError,"output overlaps an input"); return 0; } } return 1; } /* ord is the sorted list of time positions. Select interval membership in rank order; one packed value per rank, no per-interval indirect gathers. */ #if defined(__x86_64__) && (defined(__GNUC__) || defined(__clang__)) && !defined(_WIN32) #include __attribute__((target("avx512f,popcnt"))) static Py_ssize_t filter_wide(const int64_t *ord,const double *rankval,double *out, Py_ssize_t n,Py_ssize_t a,Py_ssize_t b) { Py_ssize_t j=0,count=0; __m512i lo=_mm512_set1_epi64(a),hi=_mm512_set1_epi64(b); for(;j+8<=n;j+=8){ __m512i ix=_mm512_loadu_si512(ord+j); __mmask8 mask=_mm512_cmpge_epi64_mask(ix,lo)&_mm512_cmplt_epi64_mask(ix,hi); _mm512_mask_compressstoreu_pd(out+count,mask,_mm512_loadu_pd(rankval+j)); count+=__builtin_popcount((unsigned)mask); } for(;j=a && ord[j]=cumulative[w+1])w++; uint64_t flag=_pdep_u64((uint64_t)1<<(lo-cumulative[w]),bits[w]); double x=rankval[64*w+__builtin_ctzll(flag)],y=x; if(lo+1=.5?y-(y-x)*(1.-f):x+(y-x)*f); } return 1; } /* Across-window SIMD leaves every within-interval sum strictly serial. A small [time][8 windows] tile avoids a gather per scalar visit. */ __attribute__((target("avx512f"))) static void numeric_eight(const double *x,const int64_t *bounds,double *out,double *tile, Py_ssize_t t,Py_ssize_t c,Py_ssize_t m,int width,int turbo){ for(Py_ssize_t ch=0;chth[l]; hist[a]++;if(j)hist[k+prev*k+a]++;prev=a; } } #define CAN_FILTER (__builtin_cpu_supports("avx512f") && __builtin_cpu_supports("popcnt")) #endif /* Bucket by value then insertion-sort locally. Equal finite values may be permuted, but ordered value statistics do not depend on tied time positions. Defer signed zero / extreme exponents to the NumPy ordering implementation. */ static PyObject *rank_short(PyObject *self,PyObject *args) { PyObject *xo,*oo;Py_buffer v[2]={{0}};int ok=1; if(!PyArg_ParseTuple(args,"OO",&xo,&oo))return NULL; if(!view(xo,v,3,'d',0)||!view(oo,v+1,3,'q',1)||!separate_output(v,1))goto done; Py_ssize_t n=v[0].shape[0],t=v[0].shape[1],c=v[0].shape[2]; if(t<1||t>256||c<1||v[1].shape[0]!=n||v[1].shape[1]!=c||v[1].shape[2]!=t){ PyErr_SetString(PyExc_ValueError,"invalid short-rank geometry");goto done; } const double *x=v[0].buf;int64_t *out=v[1].buf; Py_BEGIN_ALLOW_THREADS for(Py_ssize_t i=0;ok && ihi)hi=z;vals[j]=z; } if(!ok)break; if(hi==lo){for(int j=0;j=t)k=(int)t-1; key[j]=(uint8_t)k;edges[k+1]++; } for(int j=1;j<=t;++j)edges[j]+=edges[j-1]; for(int j=0;jbegin && vals[idx[h-1]]>vals[id]){idx[h]=idx[h-1];--h;}idx[h]=id; } begin=end; } } Py_END_ALLOW_THREADS done: for(int j=0;j<2;++j)if(v[j].obj)PyBuffer_Release(v+j); if(PyErr_Occurred())return NULL; return PyBool_FromLong(ok); } #include "interval_levels.h" static PyObject *intervals(PyObject *self,PyObject *args) { PyObject *obj[5]; int turbo; Py_buffer v[5]={{0}}; if(!PyArg_ParseTuple(args,"OOOOOi",&obj[0],&obj[1],&obj[2],&obj[3],&obj[4],&turbo)) return NULL; if(!view(obj[0],v+0,3,'d',0)||!view(obj[1],v+1,2,'q',0)|| !view(obj[2],v+2,3,'q',0)||!view(obj[3],v+3,1,'d',0)||!view(obj[4],v+4,3,'d',1)) goto finish; if(!separate_output(v,4))goto finish; 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[3].shape[0]; const int width=(turbo?3:5)+(int)nq; if((turbo && (nq!=1 || ((const double*)v[3].buf)[0]!=0.5)) || t<1 || t>1048576 || c<2 || c>256 || nq<1 || nq>16 || v[1].shape[1]!=2 || v[2].shape[0]!=n || v[2].shape[1]!=c || v[2].shape[2]!=t || v[4].shape[0]!=n || v[4].shape[1]!=m || v[4].shape[2]!=width*c) { PyErr_SetString(PyExc_ValueError,"incompatible interval arguments");goto finish; } const double *x=v[0].buf,*q=v[3].buf; double *out=v[4].buf; const int64_t *bounds=v[1].buf,*order=v[2].buf; for(Py_ssize_t j=0;j1 || (j && q[j]t || bounds[2*j]>=bounds[2*j+1]){ PyErr_SetString(PyExc_ValueError,"empty or invalid interval");goto finish; } for(Py_ssize_t j=0;j=t){ PyErr_SetString(PyExc_ValueError,"invalid ordering");goto finish; } double *sorted=PyMem_Malloc(3*(size_t)t*sizeof(double)); if(!sorted){PyErr_NoMemory();goto finish;} uint64_t *prefix=NULL; #ifdef CAN_FILTER int words=(int)((t+63)/64); if(t<=256 && m>=8 && CAN_FILTER && __builtin_cpu_supports("bmi2")){ prefix=PyMem_Malloc((t+1)*words*sizeof(uint64_t)); if(!prefix){PyMem_Free(sorted);PyErr_NoMemory();goto finish;} } #endif double *tile=NULL;Py_ssize_t packed=0; #ifdef CAN_FILTER if(n>=8 && t<=1024 && m>=4 && CAN_FILTER){ tile=PyMem_Malloc(8*t*sizeof(double)); if(tile)packed=n/8*8; } #endif Py_BEGIN_ALLOW_THREADS #ifdef CAN_FILTER for(Py_ssize_t i=0;i=rankval[j-1]); } #ifdef CAN_FILTER if(monotonic && prefix){ memset(prefix,0,(t+1)*words*sizeof(uint64_t)); for(Py_ssize_t rank=0;rank=packed){ double sum=0,power=0; for(Py_ssize_t j=0;j=16)count=filter_wide(ord,rankval,sorted,t,a,b);else #endif for(Py_ssize_t j=0;j=a && ix=0.5?high-delta*(1.0-f):low+delta*f); } } } Py_END_ALLOW_THREADS PyMem_Free(tile);PyMem_Free(prefix);PyMem_Free(sorted); finish: for(int i=0;i<5;i++)if(v[i].obj)PyBuffer_Release(&v[i]); if(PyErr_Occurred())return NULL; Py_RETURN_NONE; } static PyObject *state_counts(PyObject *self,PyObject *args){ PyObject *xx,*qq,*oo;Py_buffer v[3]={{0}}; if(!PyArg_ParseTuple(args,"OOO",&xx,&qq,&oo))return NULL; if(!view(xx,&v[0],3,'d',0)||!view(qq,&v[1],3,'d',0)||!view(oo,&v[2],3,'d',1))goto done; if(!separate_output(v,2))goto done; Py_ssize_t n=v[0].shape[0],c=v[0].shape[1],t=v[0].shape[2],k=v[1].shape[2]+1; if(t<1||k<2||k>32||v[1].shape[0]!=n||v[1].shape[1]!=c||v[2].shape[0]!=n|| v[2].shape[1]!=c||v[2].shape[2]!=k*(1+k)){ PyErr_SetString(PyExc_ValueError,"incompatible count buffers");goto done; } const double *x=v[0].buf,*q=v[1].buf;double *out=v[2].buf; Py_BEGIN_ALLOW_THREADS memset(out,0,v[2].len); for(Py_ssize_t z=0;z=16 && t<=8192 && CAN_FILTER){state_fused(u,th,hist,(int)t,(int)k);continue;} #endif int prev=0; for(Py_ssize_t j=0;jth[l]; hist[state]++; if(j)hist[k+prev*k+state]++; prev=state; } } Py_END_ALLOW_THREADS done: for(int i=0;i<3;i++)if(v[i].obj)PyBuffer_Release(&v[i]); if(PyErr_Occurred())return NULL; Py_RETURN_NONE; } /* Pool normalized histogram, transition probabilities and Shannon terms in one pass. A small table prepared by NumPy retains the original log and division for all attainable (numerator, denominator) pairs. */ static double symbolic_sum(const double *a,int m){ if(m<8){ double s=-0.;for(int i=0;i128){int cut=m/16*8;return symbolic_sum(a,cut)+symbolic_sum(a+cut,m-cut);} double sums[8];memcpy(sums,a,sizeof sums); int j=8; for(;j+8<=m;j+=8)for(int i=0;i<8;i++)sums[i]+=a[j+i]; double res=((sums[0]+sums[1])+(sums[2]+sums[3]))+((sums[4]+sums[5])+(sums[6]+sums[7])); for(;j512 || k<2 || k>16 || v[1].shape[0]!=n || v[1].shape[1]!=c || v[2].shape[0]!=t+1 || v[2].shape[1]!=t+1 || v[2].shape[2]!=2 || v[3].shape[0]!=n || v[3].shape[1]!=c || v[3].shape[2]!=k+k*k+1){ PyErr_SetString(PyExc_ValueError,"invalid bounded symbolic pooling dimensions");goto done; } const double *x=v[0].buf,*b=v[1].buf,*tab=v[2].buf;double *out=v[3].buf; Py_BEGIN_ALLOW_THREADS for(Py_ssize_t row=0;row=16 && CAN_FILTER)state_fused(src,th,counts,t,k); else #endif { int prev=0; for(int j=0;jth[l]; counts[a]++;if(j)counts[k+prev*k+a]++;prev=a; } } for(int j=0;j