Max 5 API Reference
00001 /* 00002 jit.functor.c 00003 00004 Copyright 2001-2005 - Cycling '74 00005 Joshua Kit Clayton jkc@cycling74.com 00006 00007 A functor is a function object with possible associated state. 00008 currently all of our functors take as input N values and output 00009 a single value. They may be evaulated using fixed point, 32 bit or 00010 64 bit floating point arithmetic. minimally a 64 bit floating point 00011 N-D evaluation function should be defined. Unless also defined 00012 by the functor class, 32 bit floating and 32 bit fixed point 00013 evaluation functions as well as specific 1D through 3D cases for 00014 each will be defined. 00015 00016 */ 00017 00018 #include "jit.common.h" 00019 #include "jit.functor.h" 00020 #include "ext_obex.h" 00021 00022 static t_hashtab *_jit_functor_hashtab=NULL; 00023 static t_symbol *ps_evalfixed,*ps_evalfloat32,*ps_evalfloat64,*ps_subfunctor; 00024 00025 long jit_functor_category_match(t_class *c, t_symbol *categoryname); 00026 long jit_functor_category_symcompare(t_symbol *categoryname, t_symbol *comparename); 00027 00028 long jit_functor_eval_fixed(t_jit_object *x, long dimcount, long *vals); 00029 float jit_functor_eval_float32(t_jit_object *x, long dimcount, float *vals); 00030 t_jit_err jit_functor_eval_typed(t_jit_object *x, t_symbol *s, long ac, t_atom *av, t_atom *rv); 00031 00032 // jitlib curve functors 00033 t_jit_err jit_functor_curve_linear_init(); 00034 t_jit_err jit_functor_curve_cubicspline_init(); 00035 00036 // jitlib filter functors 00037 t_jit_err jit_functor_filter_bessel_init(); 00038 t_jit_err jit_functor_filter_box_init(); 00039 t_jit_err jit_functor_filter_catmullrom_init(); 00040 t_jit_err jit_functor_filter_disk_init(); 00041 t_jit_err jit_functor_filter_gaussian_init(); 00042 t_jit_err jit_functor_filter_lanczossinc_init(); 00043 t_jit_err jit_functor_filter_mitchell_init(); 00044 t_jit_err jit_functor_filter_sinc_init(); 00045 t_jit_err jit_functor_filter_triangle_init(); 00046 00047 // jitlib noise functors 00048 t_jit_err jit_functor_noise_cell_init(); 00049 t_jit_err jit_functor_noise_checker_init(); 00050 t_jit_err jit_functor_noise_distorted_init(); 00051 t_jit_err jit_functor_noise_gradient_init(); 00052 t_jit_err jit_functor_noise_simplex_init(); 00053 t_jit_err jit_functor_noise_sparse_convolution_init(); 00054 t_jit_err jit_functor_noise_value_convolution_init(); 00055 t_jit_err jit_functor_noise_value_cubicspline_init(); 00056 t_jit_err jit_functor_noise_voronoi_init(); 00057 00058 // jitlib fractal functors 00059 t_jit_err jit_functor_fractal_mono_init(); 00060 t_jit_err jit_functor_fractal_multi_init(); 00061 t_jit_err jit_functor_fractal_hetero_init(); 00062 t_jit_err jit_functor_fractal_multi_hybrid_init(); 00063 t_jit_err jit_functor_fractal_multi_ridged_init(); 00064 t_jit_err jit_functor_fractal_turbulence_init(); 00065 00066 // jitlib distance functors 00067 t_jit_err jit_functor_distance_chebychev_init(); 00068 t_jit_err jit_functor_distance_euclidean_init(); 00069 t_jit_err jit_functor_distance_euclidean_squared_init(); 00070 t_jit_err jit_functor_distance_manhattan_init(); 00071 t_jit_err jit_functor_distance_manhattan_radial_init(); 00072 t_jit_err jit_functor_distance_minkovsky_init(); 00073 t_jit_err jit_functor_distance_superquadratic_init(); 00074 00075 // jitlib transfer functors 00076 t_jit_err jit_functor_transfer_bias_init(); 00077 t_jit_err jit_functor_transfer_cubic_init(); 00078 t_jit_err jit_functor_transfer_gain_init(); 00079 t_jit_err jit_functor_transfer_linear_init(); 00080 t_jit_err jit_functor_transfer_pulse_init(); 00081 t_jit_err jit_functor_transfer_quintic_init(); 00082 t_jit_err jit_functor_transfer_saw_init(); 00083 t_jit_err jit_functor_transfer_sine_init(); 00084 t_jit_err jit_functor_transfer_smoothstep_init(); 00085 t_jit_err jit_functor_transfer_smoothpulse_init(); 00086 t_jit_err jit_functor_transfer_step_init(); 00087 t_jit_err jit_functor_transfer_solarize_init(); 00088 00089 // jitlib functor evaluators 00090 t_jit_err jit_functor_evaluate_init(); 00091 00092 // jitlib sampler functors 00093 t_jit_err jit_functor_sampler_regular_init(); 00094 00095 00096 t_jit_err jit_functor_init(void) 00097 { 00098 _jit_functor_hashtab = hashtab_new(0); 00099 00100 // jitlib curve functors 00101 jit_functor_curve_linear_init(); 00102 jit_functor_curve_cubicspline_init(); 00103 00104 // jitlib filter functors 00105 jit_functor_filter_bessel_init(); 00106 jit_functor_filter_box_init(); 00107 jit_functor_filter_catmullrom_init(); 00108 jit_functor_filter_disk_init(); 00109 jit_functor_filter_gaussian_init(); 00110 jit_functor_filter_lanczossinc_init(); 00111 jit_functor_filter_mitchell_init(); 00112 jit_functor_filter_sinc_init(); 00113 jit_functor_filter_triangle_init(); 00114 00115 // jitlib noise functors 00116 jit_functor_noise_cell_init(); 00117 jit_functor_noise_checker_init(); 00118 jit_functor_noise_distorted_init(); 00119 jit_functor_noise_gradient_init(); 00120 jit_functor_noise_simplex_init(); 00121 jit_functor_noise_sparse_convolution_init(); 00122 jit_functor_noise_value_convolution_init(); 00123 jit_functor_noise_value_cubicspline_init(); 00124 jit_functor_noise_voronoi_init(); 00125 00126 // jitlib fractal functors 00127 jit_functor_fractal_mono_init(); 00128 jit_functor_fractal_multi_init(); 00129 jit_functor_fractal_hetero_init(); 00130 jit_functor_fractal_multi_hybrid_init(); 00131 jit_functor_fractal_multi_ridged_init(); 00132 jit_functor_fractal_turbulence_init(); 00133 00134 // jitlib distance functors 00135 jit_functor_distance_chebychev_init(); 00136 jit_functor_distance_euclidean_init(); 00137 jit_functor_distance_euclidean_squared_init(); 00138 jit_functor_distance_manhattan_init(); 00139 jit_functor_distance_manhattan_radial_init(); 00140 jit_functor_distance_minkovsky_init(); 00141 jit_functor_distance_superquadratic_init(); 00142 00143 // jitlib transfer functors 00144 jit_functor_transfer_bias_init(); 00145 jit_functor_transfer_cubic_init(); 00146 jit_functor_transfer_gain_init(); 00147 jit_functor_transfer_linear_init(); 00148 jit_functor_transfer_pulse_init(); 00149 jit_functor_transfer_quintic_init(); 00150 jit_functor_transfer_saw_init(); 00151 jit_functor_transfer_sine_init(); 00152 jit_functor_transfer_smoothstep_init(); 00153 jit_functor_transfer_smoothpulse_init(); 00154 jit_functor_transfer_step_init(); 00155 jit_functor_transfer_solarize_init(); 00156 00157 // jitlib functor evaluators 00158 jit_functor_evaluate_init(); 00159 00160 // jitlib sampler functors 00161 jit_functor_sampler_regular_init(); 00162 00163 // generate symbols 00164 ps_evalfixed = gensym("evalfixed"); 00165 ps_evalfloat32 = gensym("evalfloat32"); 00166 ps_evalfloat64 = gensym("evalfloat64"); 00167 ps_subfunctor = gensym("subfunctor"); 00168 00169 return JIT_ERR_NONE; 00170 } 00171 00172 t_jit_err jit_functor_setup_class(t_class *c, char *categoryname, char *classname) 00173 { 00174 method m; 00175 t_class *cc=NULL; 00176 t_jit_functor_interface *fi; 00177 char tmpstr[256]; 00178 00179 jit_functor_addcategory(c,gensym(categoryname)); 00180 00181 hashtab_lookup(_jit_functor_hashtab,gensym(classname),(t_jit_object **)&cc); 00182 if (cc) { 00183 // TODO: if we wish to support multiple categories for a given functor 00184 // just add obex extra category token, not methods (already added) 00185 return JIT_ERR_NONE; 00186 } 00187 00188 // add extra things to the class in the fashion of being the classes super class 00189 if (!jit_class_method(c,gensym("evalfixed"))) 00190 jit_class_addmethod(c, (method)jit_functor_eval_fixed, "evalfixed", A_CANT, 0L); 00191 if (!jit_class_method(c,gensym("evalfloat32"))) 00192 jit_class_addmethod(c, (method)jit_functor_eval_float32, "evalfloat32", A_CANT, 0L); 00193 00194 // for optimized dim cases 00195 m = jit_class_method(c,gensym("evalfixed")); 00196 if (!jit_class_method(c,gensym("evalfixed_1d"))) 00197 jit_class_addmethod(c, m, "evalfixed_1d", A_CANT, 0L); 00198 if (!jit_class_method(c,gensym("evalfixed_2d"))) 00199 jit_class_addmethod(c, m, "evalfixed_2d", A_CANT, 0L); 00200 if (!jit_class_method(c,gensym("evalfixed_3d"))) 00201 jit_class_addmethod(c, m, "evalfixed_3d", A_CANT, 0L); 00202 00203 m = jit_class_method(c,gensym("evalfloat32")); 00204 if (!jit_class_method(c,gensym("evalfloat32_1d"))) 00205 jit_class_addmethod(c, m, "evalfloat32_1d", A_CANT, 0L); 00206 if (!jit_class_method(c,gensym("evalfloat32_2d"))) 00207 jit_class_addmethod(c, m, "evalfloat32_2d", A_CANT, 0L); 00208 if (!jit_class_method(c,gensym("evalfloat32_3d"))) 00209 jit_class_addmethod(c, m, "evalfloat32_3d", A_CANT, 0L); 00210 00211 m = jit_class_method(c,gensym("evalfloat64")); 00212 if (!jit_class_method(c,gensym("evalfloat64_1d"))) 00213 jit_class_addmethod(c, m, "evalfloat64_1d", A_CANT, 0L); 00214 if (!jit_class_method(c,gensym("evalfloat64_2d"))) 00215 jit_class_addmethod(c, m, "evalfloat64_2d", A_CANT, 0L); 00216 if (!jit_class_method(c,gensym("evalfloat64_3d"))) 00217 jit_class_addmethod(c, m, "evalfloat64_3d", A_CANT, 0L); 00218 00219 //the typed variant 00220 jit_class_addmethod(c, (method)jit_functor_eval_typed, "eval", A_GIMMEBACK, 0L); 00221 // synonym since eval seems to be reserved in JS 00222 jit_class_addmethod(c, (method)jit_functor_eval_typed, "evaluate", A_GIMMEBACK, 0L); 00223 00224 // these utility functions will check to see if there is a method 00225 // defined for "subfunctor" (which is kind of like decorator implementation 00226 // where a symbol is passed in and an object pointer is returned and 00227 // on which the set/getattr method is to be called upon) 00228 jit_class_addmethod(c, (method)jit_functor_setattr, "setattr", A_GIMME, 0L); 00229 // save getattr for later. use attrstyle gimme or new gimmeback? not sure 00230 //jit_class_addmethod(c, jit_functor_getattr, "getattr", A_GIMMEBACK, 0L); 00231 00232 fi = (t_jit_functor_interface *) sysmem_newptrclear(sizeof(t_jit_functor_interface)); 00233 00234 fi->evalfixed = (t_jit_functor_fixed_sig) jit_class_method(c,gensym("evalfixed")); 00235 fi->evalfixed_1d = (t_jit_functor_fixed_sig) jit_class_method(c,gensym("evalfixed_1d")); 00236 fi->evalfixed_2d = (t_jit_functor_fixed_sig) jit_class_method(c,gensym("evalfixed_2d")); 00237 fi->evalfixed_3d = (t_jit_functor_fixed_sig) jit_class_method(c,gensym("evalfixed_3d")); 00238 00239 fi->evalfloat32 = (t_jit_functor_float32_sig) jit_class_method(c,gensym("evalfloat32")); 00240 fi->evalfloat32_1d = (t_jit_functor_float32_sig) jit_class_method(c,gensym("evalfloat32_1d")); 00241 fi->evalfloat32_2d = (t_jit_functor_float32_sig) jit_class_method(c,gensym("evalfloat32_2d")); 00242 fi->evalfloat32_3d = (t_jit_functor_float32_sig) jit_class_method(c,gensym("evalfloat32_3d")); 00243 00244 fi->evalfloat64 = (t_jit_functor_float64_sig) jit_class_method(c,gensym("evalfloat64")); 00245 fi->evalfloat64_1d = (t_jit_functor_float64_sig) jit_class_method(c,gensym("evalfloat64_1d")); 00246 fi->evalfloat64_2d = (t_jit_functor_float64_sig) jit_class_method(c,gensym("evalfloat64_2d")); 00247 fi->evalfloat64_3d = (t_jit_functor_float64_sig) jit_class_method(c,gensym("evalfloat64_3d")); 00248 00249 class_extra_store(c,gensym("functor_interface"),(t_object *)fi); 00250 00251 hashtab_store(_jit_functor_hashtab,gensym(classname),(t_object *)c); 00252 00253 // also store in hashtab as full category.classname string 00254 sprintf(tmpstr,"%s.%s",categoryname,classname); 00255 hashtab_store(_jit_functor_hashtab,gensym(tmpstr),(t_object *)c); 00256 00257 return JIT_ERR_NONE; 00258 } 00259 00260 t_jit_err jit_functor_addcategory(t_class *c, t_symbol *categoryname) 00261 { 00262 t_jit_linklist *catlist; 00263 00264 if (!(catlist=class_extra_lookup(c,gensym("functor_category")))) { 00265 catlist = jit_linklist_new(); 00266 class_extra_store(c,gensym("functor_category"),(t_object *)catlist); 00267 } 00268 if (catlist) { 00269 jit_linklist_append(catlist,categoryname); 00270 return JIT_ERR_NONE; 00271 } else 00272 return JIT_ERR_GENERIC; 00273 } 00274 00275 t_jit_object *jit_functor_getcategorylist(t_class *c) 00276 { 00277 return class_extra_lookup(c,gensym("functor_category")); 00278 } 00279 00280 long jit_functor_category_match(t_class *c, t_symbol *categoryname) 00281 { 00282 t_jit_linklist *catlist=(t_jit_linklist *)jit_functor_getcategorylist(c); 00283 t_jit_object *o=NULL; 00284 00285 if (catlist) { 00286 jit_linklist_findfirst(catlist,(void **)&o,(t_cmpfn)jit_functor_category_symcompare,categoryname); 00287 if (o) 00288 return 1; 00289 } 00290 return 0; 00291 } 00292 00293 long jit_functor_category_symcompare(t_symbol *categoryname, t_symbol *comparename) 00294 { 00295 return categoryname==comparename; 00296 } 00297 00298 t_jit_object *jit_functor_classlist_in_category(t_symbol *categoryname) 00299 { 00300 t_object *o=NULL; 00301 t_class *c; 00302 t_jit_linklist *classlist=NULL; 00303 t_symbol **ckv=NULL; 00304 long i,ckc=0; 00305 00306 if (_jit_functor_hashtab) { 00307 hashtab_getkeys(_jit_functor_hashtab,&ckc,&ckv); 00308 for (i=0;i<ckc;i++) { 00309 if (c=jit_functor_lookup(categoryname,ckv[i])) { 00310 if (!classlist) 00311 classlist = jit_linklist_new(); 00312 jit_linklist_append(classlist,(t_jit_object *)c); 00313 } 00314 } 00315 if (ckv) 00316 freebytes((char *)ckv,ckc*sizeof(t_symbol *)); 00317 } 00318 return (t_jit_object *)classlist; // callee should call jit_object_method(classlist,_jit_sym_chuck) instead of free 00319 } 00320 00321 t_class *jit_functor_lookup_relaxed(t_symbol *categoryname, t_symbol *classname) 00322 { 00323 t_class *c=NULL; 00324 long i,len; 00325 00326 // categoryname arg is placeholder for recommended category if 00327 // we need it later. for now, just look for classes. 00328 00329 hashtab_lookup(_jit_functor_hashtab,classname,(t_jit_object **)&c); 00330 if (!c) { 00331 // now check to see if the class has been specified in category.class syntax 00332 len = strlen(classname->s_name); 00333 for (i=0;i<len;i++) { 00334 if (classname->s_name[i]=='.') { 00335 hashtab_lookup(_jit_functor_hashtab,gensym(classname->s_name+i+1),(t_jit_object **)&c); 00336 if (c) 00337 return c; 00338 } 00339 } 00340 } 00341 return c; 00342 } 00343 00344 t_class *jit_functor_lookup(t_symbol *categoryname, t_symbol *classname) 00345 { 00346 t_class *c=NULL; 00347 00348 hashtab_lookup(_jit_functor_hashtab,classname,(t_jit_object **)&c); 00349 if (categoryname) { 00350 if (c&&jit_functor_category_match(c,categoryname)) 00351 return c; 00352 else 00353 return NULL; 00354 } 00355 return c; 00356 } 00357 00358 t_jit_err jit_functor_new_object_with_interface( 00359 t_jit_object **ob, t_jit_functor_interface **fi, 00360 t_symbol *category, t_symbol *classname) 00361 { 00362 t_class *c = NULL; 00363 00364 if(ob && fi && classname) 00365 { 00366 // lookup the object in the functor registry 00367 c = jit_functor_lookup_relaxed(category, classname); 00368 if(!c) return JIT_ERR_INVALID_PTR; 00369 00370 // create a new functor object 00371 (*ob) = NULL; 00372 (*ob) = jit_object_new(c->c_sym); 00373 if(!(*ob)) return JIT_ERR_INVALID_PTR; 00374 00375 // get the functor interface 00376 (*fi) = NULL; 00377 (*fi) = class_extra_lookup(c, gensym("functor_interface")); 00378 if(!(*fi)) return JIT_ERR_INVALID_PTR; 00379 00380 return JIT_ERR_NONE; 00381 } 00382 00383 return JIT_ERR_INVALID_PTR; 00384 } 00385 00386 t_jit_err jit_functor_wrapper_init( 00387 t_jit_functor_wrapper *e, t_symbol *category, t_symbol *classname) 00388 { 00389 if(e && classname) 00390 { 00391 if(e->ob) 00392 { 00393 jit_object_free(e->ob); 00394 e->ob = NULL; 00395 } 00396 00397 return jit_functor_new_object_with_interface(&e->ob, &e->fm, category, classname); 00398 } 00399 00400 return JIT_ERR_INVALID_PTR; 00401 } 00402 00403 // default functions for fixed and float32. strongly recommend defining the functions 00404 // to avoid message lookup overhead 00405 long jit_functor_eval_fixed(t_jit_object *x, long dimcount, long *vals) 00406 { 00407 t_jit_functor_float64_sig fp; 00408 fp = (t_jit_functor_float64_sig) object_getmethod(x,ps_evalfloat64); 00409 00410 return jit_functor_eval_fixed_with_float64(x,dimcount,vals,fp); 00411 } 00412 00413 float jit_functor_eval_float32(t_jit_object *x, long dimcount, float *vals) 00414 { 00415 t_jit_functor_float64_sig fp; 00416 fp = (t_jit_functor_float64_sig) object_getmethod(x,ps_evalfloat64); 00417 00418 return jit_functor_eval_float32_with_float64(x,dimcount,vals,fp); 00419 } 00420 00421 // to be called from javascript/java via atom/message based interface 00422 t_jit_err jit_functor_eval_typed(t_jit_object *x, t_symbol *s, long ac, t_atom *av, t_atom *rv) 00423 { 00424 long i; 00425 float v,f[256]; 00426 t_jit_functor_float32_sig fp; 00427 fp = (t_jit_functor_float32_sig) object_getmethod(x,ps_evalfloat32); 00428 00429 ac = MIN(ac,256); 00430 for (i=0;i<ac;i++) 00431 f[i] = atom_getfloat(av+i); 00432 00433 // later could parse typed argument list for attribute style arguments. 00434 v = (*fp)(x,ac,f); 00435 atom_setfloat(rv,v); 00436 return JIT_ERR_NONE; 00437 } 00438 00439 long jit_functor_eval_fixed_with_float32(t_jit_object *x, long dimcount, long *vals, t_jit_functor_float32_sig fp) 00440 { 00441 long i; 00442 float fvals[256]; 00443 00444 dimcount = MIN(dimcount,256); 00445 for (i=0;i<dimcount;i++) 00446 fvals[i] = FixedToFloat(vals[i]); 00447 return FloatToFixed((*fp)(x,dimcount,fvals)); 00448 } 00449 00450 long jit_functor_eval_fixed_with_float64(t_jit_object *x, long dimcount, long *vals, t_jit_functor_float64_sig fp) 00451 { 00452 long i; 00453 double dvals[256]; 00454 00455 dimcount = MIN(dimcount,256); 00456 for (i=0;i<dimcount;i++) 00457 dvals[i] = FixedToDouble(vals[i]); 00458 return DoubleToFixed((*fp)(x,dimcount,dvals)); 00459 } 00460 00461 float jit_functor_eval_float32_with_fixed(t_jit_object *x, long dimcount, float *vals, t_jit_functor_fixed_sig fp) 00462 { 00463 long i; 00464 long fixvals[256]; 00465 00466 dimcount = MIN(dimcount,256); 00467 for (i=0;i<dimcount;i++) 00468 fixvals[i] = FloatToFixed(vals[i]); 00469 return FixedToFloat((*fp)(x,dimcount,fixvals)); 00470 } 00471 00472 float jit_functor_eval_float32_with_float64(t_jit_object *x, long dimcount, float *vals, t_jit_functor_float64_sig fp) 00473 { 00474 long i; 00475 double dvals[256]; 00476 00477 dimcount = MIN(dimcount,256); 00478 for (i=0;i<dimcount;i++) 00479 dvals[i] = vals[i]; 00480 return (float) (*fp)(x,dimcount,dvals); 00481 } 00482 00483 double jit_functor_eval_float64_with_fixed(t_jit_object *x, long dimcount, double *vals, t_jit_functor_fixed_sig fp) 00484 { 00485 long i; 00486 long fixvals[256]; 00487 00488 dimcount = MIN(dimcount,256); 00489 for (i=0;i<dimcount;i++) 00490 fixvals[i] = DoubleToFixed(vals[i]); 00491 return FixedToDouble((*fp)(x,dimcount,fixvals)); 00492 } 00493 00494 double jit_functor_eval_float64_with_float32(t_jit_object *x, long dimcount, double *vals, t_jit_functor_float32_sig fp) 00495 { 00496 long i; 00497 float fvals[256]; 00498 00499 dimcount = MIN(dimcount,256); 00500 for (i=0;i<dimcount;i++) 00501 fvals[i] = vals[i]; 00502 return (double) (*fp)(x,dimcount,fvals); 00503 } 00504 00505 long jit_functor_eval_fixed_with_scalar_product(t_jit_object *x, long dimcount, long *vals, t_jit_functor_fixed_scalar_sig fp) 00506 { 00507 long i; 00508 long rval=(*fp)(x,vals[0]); 00509 00510 for (i=1;i<dimcount;i++) { 00511 //rval = FixMul(rval,(*fp)(x,vals[i])); 00512 // a little more precision than FixMul() since we assume values are in the range -fixed1 to fixed1 00513 rval = ((rval>>4L)*(((*fp)(x,vals[i]))>>4L))>>8L; 00514 } 00515 return rval; 00516 } 00517 00518 float jit_functor_eval_float32_with_scalar_product(t_jit_object *x, long dimcount, float *vals, t_jit_functor_float32_scalar_sig fp) 00519 { 00520 long i; 00521 float rval=(*fp)(x,vals[0]); 00522 00523 for (i=1;i<dimcount;i++) { 00524 rval *= (*fp)(x,vals[i]); 00525 } 00526 return rval; 00527 } 00528 00529 double jit_functor_eval_float64_with_scalar_product(t_jit_object *x, long dimcount, double *vals, t_jit_functor_float64_scalar_sig fp) 00530 { 00531 long i; 00532 double rval=(*fp)(x,vals[0]); 00533 00534 for (i=1;i<dimcount;i++) { 00535 rval *= (*fp)(x,vals[i]); 00536 } 00537 return rval; 00538 } 00539 00540 long jit_functor_eval_fixed_with_scalar_sum(t_jit_object *x, long dimcount, long *vals, t_jit_functor_fixed_scalar_sig fp) 00541 { 00542 long i; 00543 long rval=(*fp)(x,vals[0]); 00544 00545 for (i=1;i<dimcount;i++) { 00546 rval += (*fp)(x,vals[i]); 00547 } 00548 return rval; 00549 } 00550 00551 float jit_functor_eval_float32_with_scalar_sum(t_jit_object *x, long dimcount, float *vals, t_jit_functor_float32_scalar_sig fp) 00552 { 00553 long i; 00554 float rval=(*fp)(x,vals[0]); 00555 00556 for (i=1;i<dimcount;i++) { 00557 rval += (*fp)(x,vals[i]); 00558 } 00559 return rval; 00560 } 00561 00562 double jit_functor_eval_float64_with_scalar_sum(t_jit_object *x, long dimcount, double *vals, t_jit_functor_float64_scalar_sig fp) 00563 { 00564 long i; 00565 double rval=(*fp)(x,vals[0]); 00566 00567 for (i=1;i<dimcount;i++) { 00568 rval += (*fp)(x,vals[i]); 00569 } 00570 return rval; 00571 } 00572 00573 long jit_functor_eval_fixed_with_lut_wrap_product(t_jit_object *x, long dimcount, long *vals, long *fixlut) 00574 { 00575 long rval; 00576 long i,j; 00577 00578 j = vals[0]%fixed1; 00579 if (j<0) j += fixed1; 00580 rval = fixlut[j]; 00581 00582 for (i=1;i<dimcount;i++) { 00583 j = vals[i]%fixed1; 00584 if (j<0) j += fixed1; 00585 // a little more precision than FixMul() since we assume values are in the range -fixed1 to fixed1 00586 rval = ((rval>>4L)*(fixlut[j]>>4L))>>8L; 00587 } 00588 00589 return rval; 00590 } 00591 00592 long jit_functor_eval_fixed_with_lut_wrap_sum(t_jit_object *x, long dimcount, long *vals, long *fixlut) 00593 { 00594 long rval; 00595 long i,j; 00596 00597 j = vals[0]%fixed1; 00598 if (j<0) j += fixed1; 00599 rval = fixlut[j]; 00600 00601 for (i=1;i<dimcount;i++) { 00602 j = vals[i]%fixed1; 00603 if (j<0) j += fixed1; 00604 // a little more precision than FixMul() since we assume values are in the range -fixed1 to fixed1 00605 rval += fixlut[j]; 00606 } 00607 00608 return rval; 00609 } 00610 00611 long jit_functor_eval_fixed_with_lut_clip_product(t_jit_object *x, long dimcount, long *vals, long *fixlut) 00612 { 00613 long rval; 00614 long i,j; 00615 00616 j = CLAMP(vals[0],0,fixed1-1); 00617 rval = fixlut[j]; 00618 00619 for (i=1;i<dimcount;i++) { 00620 j = CLAMP(vals[i],0,fixed1-1); 00621 // a little more precision than FixMul() since we assume values are in the range -fixed1 to fixed1 00622 rval = ((rval>>4L)*(fixlut[j]>>4L))>>8L; 00623 } 00624 00625 return rval; 00626 } 00627 00628 long jit_functor_eval_fixed_with_lut_clip_sum(t_jit_object *x, long dimcount, long *vals, long *fixlut) 00629 { 00630 long rval; 00631 long i,j; 00632 00633 j = CLAMP(vals[0],0,fixed1-1); 00634 rval = fixlut[j]; 00635 00636 for (i=1;i<dimcount;i++) { 00637 j = CLAMP(vals[i],0,fixed1-1); 00638 // a little more precision than FixMul() since we assume values are in the range -fixed1 to fixed1 00639 rval += fixlut[j]; 00640 } 00641 00642 return rval; 00643 } 00644 00645 t_jit_err jit_functor_combined_dynarray_init( 00646 t_jit_functor_combined_dynarray *x, long count) 00647 { 00648 if(!x) 00649 return JIT_ERR_INVALID_PTR; 00650 00651 if(jit_functor_combined_dynarray_destroy(x)) 00652 return JIT_ERR_INVALID_PTR; 00653 00654 x->count = count; 00655 x->fixed = (long*)jit_newptr(x->count * sizeof(long)); 00656 if(!x->fixed) 00657 return JIT_ERR_INVALID_PTR; 00658 00659 x->float32 = (float*)jit_newptr(x->count * sizeof(float)); 00660 if(!x->float32) 00661 return JIT_ERR_INVALID_PTR; 00662 00663 x->float64 = (double*)jit_newptr(x->count * sizeof(double)); 00664 if(!x->float64) 00665 return JIT_ERR_INVALID_PTR; 00666 00667 return JIT_ERR_NONE; 00668 } 00669 00670 t_jit_err jit_functor_combined_dynarray_destroy( 00671 t_jit_functor_combined_dynarray *x) 00672 { 00673 if(!x) 00674 return JIT_ERR_INVALID_PTR; 00675 00676 // free any allocated mem 00677 if(x->fixed) 00678 jit_disposeptr((char *)x->fixed); 00679 x->fixed = NULL; 00680 00681 if(x->float32) 00682 jit_disposeptr((char *)x->float32); 00683 x->float32 = NULL; 00684 00685 if(x->float64) 00686 jit_disposeptr((char *)x->float64); 00687 x->float64 = NULL; 00688 00689 x->count = 0; 00690 00691 return JIT_ERR_NONE; 00692 } 00693 00694 t_jit_err jit_functor_setattr(t_jit_object *x, t_symbol *s, long ac, t_atom *av) 00695 { 00696 t_symbol *subfun; 00697 method m; 00698 t_jit_object *o; 00699 t_jit_err err = JIT_ERR_NONE; 00700 static method mf=NULL; 00701 00702 00703 if (ac && (subfun=jit_atom_getsym(av)) && (subfun!=_jit_sym_nothing)) { 00704 if (!mf) 00705 mf = jit_object_getmethod(NULL,NULL); 00706 m = jit_object_getmethod(x,ps_subfunctor); 00707 if (m!=mf) { 00708 o = (*m)(x,subfun); // get which object if any 00709 if (o&&ac>2) // ac = 1 == method name, ac > 2 == other arguments 00710 err = object_method_typed(o,s,ac-1,av+1,NULL); // call method on object 00711 else // ac = 1 == attr name, ac = 2 == attr value 00712 err = object_attr_setvalueof(x,subfun,ac-1,av+1); // subfun as attrname 00713 } else { 00714 err = object_attr_setvalueof(x,subfun,ac-1,av+1); // subfun as attrname 00715 } 00716 return err; 00717 } 00718 return JIT_ERR_GENERIC; 00719 } 00720 00721 /* 00722 t_jit_err jit_functor_getattr(t_jit_object *x, t_symbol *s, long ac, t_atom *av, t_atom *rv); 00723 deal with getattr later 00724 */
Copyright © 2008, Cycling '74