void Matrix_Read_Input(Matrix *Mat) { Value *p; int i,j,n; char *c, s[1024],str[1024]; p = Mat->p_Init; for (i=0;i<Mat->NbRows;i++) { do { c = fgets(s, 1024, stdin); while(isspace(*c) && *c!='\n') ++c; } while(c && (*c =='#' || *c== '\n')); if (!c) { errormsg1( "Matrix_Read", "baddim", "not enough rows" ); break; } for (j=0;j<Mat->NbColumns;j++) { if(!c || *c=='\n' || *c=='#') { errormsg1("Matrix_Read", "baddim", "not enough columns"); break; } if (sscanf(c,"%s%n",str,&n) == 0) { errormsg1( "Matrix_Read", "baddim", "not enough columns" ); break; } value_read(*(p++),str); c += n; } } } /* Matrix_Read_Input */
int datapoint_read(datapoint_t* p, int fd) { if ((timestamp_read(&p->t, fd) != 0) || (value_read(&p->v, fd) != 0)) return -1; return 0; }
/** * ib_pack - Pack a structure into a buffer * @desc:Array of structure field descriptions * @desc_len:Number of entries in @desc * @structure:Structure to pack from * @buf:Buffer to pack into * * ib_pack() packs a list of structure fields into a buffer, * controlled by the array of fields in @desc. */ void ib_pack(const struct ib_field *desc, int desc_len, void *structure, void *buf) { int i; for (i = 0; i < desc_len; ++i) { if (desc[i].size_bits <= 32) { int shift; u32 val; __be32 mask; __be32 *addr; shift = 32 - desc[i].offset_bits - desc[i].size_bits; if (desc[i].struct_size_bytes) val = value_read(desc[i].struct_offset_bytes, desc[i].struct_size_bytes, structure) << shift; else val = 0; mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift); addr = (__be32 *) buf + desc[i].offset_words; *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask); } else if (desc[i].size_bits <= 64) { int shift; u64 val; __be64 mask; __be64 *addr; shift = 64 - desc[i].offset_bits - desc[i].size_bits; if (desc[i].struct_size_bytes) val = value_read(desc[i].struct_offset_bytes, desc[i].struct_size_bytes, structure) << shift; else val = 0; mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift); addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words); *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask); } else { if (desc[i].offset_bits % 8 ||
/* * Read the contents of a Vector */ Vector *Vector_Read() { Vector *vector; unsigned length; int i; char str[1024]; Value *p; scanf("%d", &length); vector = Vector_Alloc(length); if (!vector) { errormsg1("Vector_Read", "outofmem", "out of memory space"); return 0; } p = vector->p; for (i=0;i<length;i++) { scanf("%s",str); value_read(*(p++),str); } return vector; } /* Vector_Read */
void Matrix_Init(Matrix* M, int** constraints){ Value *p; int i,j; int NbRows; int NbColumns; char val[5]; unsigned rows,cols; rows=M->NbRows; cols=M->NbColumns; p = M->p_Init; //printf("testing matrix init: %u x %u \n", rows, cols); for (i=0;i<rows;i++){ for (j=0;j<cols;j++){ sprintf(val,"%2d",constraints[i][j]); //printf("%d %d %d \n",i,j,constraints[i][j]); value_read(*(p++),val); } } }
// LONG EVENT ACTION. u8 value_key_long_event_action(void * key_t) { u8 smoke_state; key_state* key = (key_state *) key_t; if (key->type != KEY_INCREASE && key->type != KEY_REDUCE) { return ERROR; } if (get_system_state() == STATE_CHARGING) return ERROR; if (key->state == KEY_ON) { // Do not respond when we are smoking. smoke_state = get_system_smoke(); if (smoke_state == SYS_SMOKE || smoke_state == SYS_SMOKE_DONE) return ERROR; // deal with press / long press. if (key->press_report_tic >= key->report_tic_interval || key->press_report_tic == 0) { u8 value_type; key->press_report_tic = 0; value_type = (value_read(CURRENT_MODE) == POWER_MODE) ? POWER_VALUE : PRESET_VOLTAGE_VALUE; if (key->press_keep_tic > key->long_press_interval) { value_update_step(value_type, VALUE_10_STEP, key->type); } else { value_update_step(value_type, VALUE_1_STEP, key->type); } draw_current_value(value_type); } } else if (key->state == KEY_OFF) { if (key->release_keep_tic > key->repeat_tic_interval) { key->repeat_event_action(key_t); } } return SUCCESS; }
int main(int argc, char **argv) { int i; char str[1024]; Matrix *C1, *P1; Polyhedron *C, *P; Enumeration *en; const char **param_name; int c, ind = 0; int hom = 0; #ifdef EP_EVALUATION Value *p, *tmp; int k; #endif while ((c = getopt_long(argc, argv, "h", options, &ind)) != -1) { switch (c) { case 'h': hom = 1; break; } } P1 = Matrix_Read(); C1 = Matrix_Read(); if(C1->NbColumns < 2) { fprintf( stderr, "Not enough parameters !\n" ); exit(0); } if (hom) { Matrix *C2, *P2; P2 = AddANullColumn(P1); Matrix_Free(P1); P1 = P2; C2 = AddANullColumn(C1); Matrix_Free(C1); C1 = C2; } P = Constraints2Polyhedron(P1,WS); C = Constraints2Polyhedron(C1,WS); Matrix_Free(P1); Matrix_Free(C1); /* Read the name of the parameters */ param_name = Read_ParamNames(stdin,C->Dimension - hom); if (hom) { const char **param_name2; param_name2 = (const char**)malloc(sizeof(char*) * (C->Dimension)); for (i = 0; i < C->Dimension - 1; i++) param_name2[i] = param_name[i]; param_name2[C->Dimension-1] = "_H"; free(param_name); param_name=param_name2; } en = Polyhedron_Enumerate(P,C,WS,param_name); if (hom) { Enumeration *en2; printf("inhomogeneous form:\n"); dehomogenize_enumeration(en, C->Dimension, WS); for (en2 = en; en2; en2 = en2->next) { Print_Domain(stdout, en2->ValidityDomain, param_name); print_evalue(stdout, &en2->EP, param_name); } } #ifdef EP_EVALUATION if( isatty(0) && C->Dimension != 0) { /* no tty input or no polyhedron -> no evaluation. */ printf("Evaluation of the Ehrhart polynomial :\n"); p = (Value *)malloc(sizeof(Value) * (C->Dimension)); for(i=0;i<C->Dimension;i++) value_init(p[i]); FOREVER { fflush(stdin); printf("Enter %d parameters : ",C->Dimension); for(k=0;k<C->Dimension;++k) { scanf("%s",str); value_read(p[k],str); } fprintf(stdout,"EP( "); value_print(stdout,VALUE_FMT,p[0]); for(k=1;k<C->Dimension;++k) { fprintf(stdout,","); value_print(stdout,VALUE_FMT,p[k]); } fprintf(stdout," ) = "); value_print(stdout,VALUE_FMT,*(tmp=compute_poly(en,p))); free(tmp); fprintf(stdout,"\n"); } }
/** * ib_pack - Pack a structure into a buffer * @desc:Array of structure field descriptions * @desc_len:Number of entries in @desc * @structure:Structure to pack from * @buf:Buffer to pack into * * ib_pack() packs a list of structure fields into a buffer, * controlled by the array of fields in @desc. */ void ib_pack(const struct ib_field *desc, int desc_len, void *structure, void *buf) { int i; for (i = 0; i < desc_len; ++i) { if (desc[i].size_bits <= 32) { int shift; u32 val; __be32 mask; __be32 *addr; shift = 32 - desc[i].offset_bits - desc[i].size_bits; if (desc[i].struct_size_bytes) val = value_read(desc[i].struct_offset_bytes, desc[i].struct_size_bytes, structure) << shift; else val = 0; mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift); addr = (__be32 *) buf + desc[i].offset_words; *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask); } else if (desc[i].size_bits <= 64) { int shift; u64 val; __be64 mask; __be64 *addr; shift = 64 - desc[i].offset_bits - desc[i].size_bits; if (desc[i].struct_size_bytes) val = value_read(desc[i].struct_offset_bytes, desc[i].struct_size_bytes, structure) << shift; else val = 0; mask = cpu_to_be64(((1ull << desc[i].size_bits) - 1) << shift); addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words); *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask); } else { if (desc[i].offset_bits % 8 || desc[i].size_bits % 8) { printk(KERN_WARNING "Structure field %s of size %d " "bits is not byte-aligned\n", desc[i].field_name, desc[i].size_bits); } if (desc[i].struct_size_bytes) memcpy(buf + desc[i].offset_words * 4 + desc[i].offset_bits / 8, structure + desc[i].struct_offset_bytes, desc[i].size_bits / 8); else memset(buf + desc[i].offset_words * 4 + desc[i].offset_bits / 8, 0, desc[i].size_bits / 8); } } }
int main( int argc, char **argv) { int i; const char **param_name; Matrix *C1, *P1; Polyhedron *P, *C; Enumeration *e, *en; Matrix * Validity_Lattice; int nb_parms; #ifdef EP_EVALUATION Value *p, *tmp; int k; #endif P1 = Matrix_Read(); C1 = Matrix_Read(); nb_parms = C1->NbColumns-2; if(nb_parms < 0) { fprintf( stderr, "Not enough parameters !\n" ); exit(0); } /* Read the name of the parameters */ param_name = Read_ParamNames(stdin,nb_parms); /* inflate the polyhedron, so that the inflated EP approximation will be an upper bound for the EP's polyhedron. */ mpolyhedron_deflate(P1,nb_parms); /* compute a polynomial approximation of the Ehrhart polynomial */ e = Ehrhart_Quick_Apx(P1, C1, &Validity_Lattice, 1024); Matrix_Free(C1); Matrix_Free(P1); printf("============ Ehrhart polynomial quick polynomial lower bound ============\n"); show_matrix(Validity_Lattice); for( en=e ; en ; en=en->next ) { Print_Domain(stdout,en->ValidityDomain, param_name); print_evalue(stdout,&en->EP, param_name); printf( "\n-----------------------------------\n" ); } #ifdef EP_EVALUATION if( isatty(0) && nb_parms != 0) { /* no tty input or no polyhedron -> no evaluation. */ printf("Evaluation of the Ehrhart polynomial :\n"); p = (Value *)malloc(sizeof(Value) * (nb_parms)); for(i=0;i<nb_parms;i++) value_init(p[i]); FOREVER { fflush(stdin); printf("Enter %d parameters : ",nb_parms); for(k=0;k<nb_parms;++k) { scanf("%s",str); value_read(p[k],str); } fprintf(stdout,"EP( "); value_print(stdout,VALUE_FMT,p[0]); for(k=1;k<nb_parms;++k) { fprintf(stdout,","); value_print(stdout,VALUE_FMT,p[k]); } fprintf(stdout," ) = "); value_print(stdout,VALUE_FMT,*(tmp=compute_poly(en,p))); free(tmp); fprintf(stdout,"\n"); } }
int main(int argc,char *argv[]) { Matrix *C1, *P1; Polyhedron *C, *P, *S; Polyhedron *CC, *PP; Enumeration *en; Value *p; int i,j,k; int m,M; char str[1024]; Value c; /******* Read the input *********/ P1 = Matrix_Read(); C1 = Matrix_Read(); if(C1->NbColumns < 2) { fprintf(stderr,"Not enough parameters !\n"); exit(0); } P = Constraints2Polyhedron(P1, MAXRAYS); C = Constraints2Polyhedron(C1, MAXRAYS); Matrix_Free(C1); Matrix_Free(P1); /******* Compute the true context *******/ CC = align_context(C,P->Dimension,MAXRAYS); PP = DomainIntersection(P,CC,MAXRAYS); Domain_Free(CC); C1 = Matrix_Alloc(C->Dimension+1,P->Dimension+1); for(i=0;i<C1->NbRows;i++) for(j=0;j<C1->NbColumns;j++) if(i==j-P->Dimension+C->Dimension) value_set_si(C1->p[i][j],1); else value_set_si(C1->p[i][j],0); CC = Polyhedron_Image(PP,C1,MAXRAYS); Domain_Free(C); Domain_Free(PP); Matrix_Free(C1); C = CC; /******* Initialize parameters *********/ p = (Value *)malloc(sizeof(Value) * (P->Dimension+2)); for(i=0;i<=P->Dimension;i++) { value_init(p[i]); value_set_si(p[i],0); } value_init(p[i]); value_set_si(p[i],1); /*** S = scanning list of polyhedra ***/ S = Polyhedron_Scan(P,C,MAXRAYS); value_init(c); /******* Count now *********/ FOREVER { fflush(stdin); printf("Enter %d parameters : ",C->Dimension); for(k=S->Dimension-C->Dimension+1;k<=S->Dimension;++k) { scanf(" %s", str); value_read(p[k],str); } printf("EP( "); value_print(stdout,VALUE_FMT,p[S->Dimension-C->Dimension+1]); for(k=S->Dimension-C->Dimension+2;k<=S->Dimension;++k) { printf(", "); value_print(stdout,VALUE_FMT,p[k]); } printf(" ) = "); count_points(1,S,p,&c); value_print(stdout,VALUE_FMT,c); printf("\n"); } for(i=0;i<=(P->Dimension+1);i++) value_clear(p[i]); value_clear(c); return(0); } /* main */
int main(int argc, char **argv) { isl_ctx *ctx; int i, nbPol, nbVec, nbMat, func, j, n; Polyhedron *A, *B, *C, *D, *E, *F, *G; char s[128]; struct barvinok_options *options = barvinok_options_new_with_defaults(); argc = barvinok_options_parse(options, argc, argv, ISL_ARG_ALL); ctx = isl_ctx_alloc_with_options(&barvinok_options_args, options); nbPol = nbVec = nbMat = 0; fgets(s, 128, stdin); while ((*s=='#') || ((sscanf(s, "D %d", &nbPol) < 1) && (sscanf(s, "V %d", &nbVec) < 1) && (sscanf(s, "M %d", &nbMat) < 1))) fgets(s, 128, stdin); for (i = 0; i < nbPol; ++i) { Matrix *M = Matrix_Read(); A = Constraints2Polyhedron(M, options->MaxRays); Matrix_Free(M); fgets(s, 128, stdin); while ((*s=='#') || (sscanf(s, "F %d", &func)<1)) fgets(s, 128, stdin); switch(func) { case 0: { Value cb, ck; value_init(cb); value_init(ck); fgets(s, 128, stdin); /* workaround for apparent bug in older gmps */ *strchr(s, '\n') = '\0'; while ((*s=='#') || (value_read(ck, s) != 0)) { fgets(s, 128, stdin); /* workaround for apparent bug in older gmps */ *strchr(s, '\n') = '\0'; } barvinok_count_with_options(A, &cb, options); if (value_ne(cb, ck)) return -1; value_clear(cb); value_clear(ck); break; } case 1: Polyhedron_Print(stdout, P_VALUE_FMT, A); B = Polyhedron_Polar(A, options->MaxRays); Polyhedron_Print(stdout, P_VALUE_FMT, B); C = Polyhedron_Polar(B, options->MaxRays); Polyhedron_Print(stdout, P_VALUE_FMT, C); Polyhedron_Free(C); Polyhedron_Free(B); break; case 2: Polyhedron_Print(stdout, P_VALUE_FMT, A); for (j = 0; j < A->NbRays; ++j) { B = supporting_cone(A, j); Polyhedron_Print(stdout, P_VALUE_FMT, B); Polyhedron_Free(B); } break; case 3: Polyhedron_Print(stdout, P_VALUE_FMT, A); C = B = NULL; barvinok_decompose(A,&B,&C); puts("Pos:"); Polyhedron_Print(stdout, P_VALUE_FMT, B); puts("Neg:"); Polyhedron_Print(stdout, P_VALUE_FMT, C); Domain_Free(B); Domain_Free(C); break; case 4: { Value cm, cb; struct tms tms_before, tms_between, tms_after; value_init(cm); value_init(cb); Polyhedron_Print(stdout, P_VALUE_FMT, A); times(&tms_before); manual_count(A, &cm); times(&tms_between); barvinok_count(A, &cb, 100); times(&tms_after); printf("manual: "); value_print(stdout, P_VALUE_FMT, cm); puts(""); time_diff(&tms_before, &tms_between); printf("Barvinok: "); value_print(stdout, P_VALUE_FMT, cb); puts(""); time_diff(&tms_between, &tms_after); value_clear(cm); value_clear(cb); break; } case 5: Polyhedron_Print(stdout, P_VALUE_FMT, A); B = triangulate_cone(A, 100); Polyhedron_Print(stdout, P_VALUE_FMT, B); check_triangulization(A, B); Domain_Free(B); break; case 6: Polyhedron_Print(stdout, P_VALUE_FMT, A); B = remove_equalities(A, options->MaxRays); Polyhedron_Print(stdout, P_VALUE_FMT, B); Polyhedron_Free(B); break; case 8: { evalue *EP; Matrix *M = Matrix_Read(); const char **param_name; C = Constraints2Polyhedron(M, options->MaxRays); Matrix_Free(M); Polyhedron_Print(stdout, P_VALUE_FMT, A); Polyhedron_Print(stdout, P_VALUE_FMT, C); EP = barvinok_enumerate_with_options(A, C, options); param_name = Read_ParamNames(stdin, C->Dimension); print_evalue(stdout, EP, (const char**)param_name); evalue_free(EP); Polyhedron_Free(C); } case 9: Polyhedron_Print(stdout, P_VALUE_FMT, A); Polyhedron_Polarize(A); C = B = NULL; barvinok_decompose(A,&B,&C); for (D = B; D; D = D->next) Polyhedron_Polarize(D); for (D = C; D; D = D->next) Polyhedron_Polarize(D); puts("Pos:"); Polyhedron_Print(stdout, P_VALUE_FMT, B); puts("Neg:"); Polyhedron_Print(stdout, P_VALUE_FMT, C); Domain_Free(B); Domain_Free(C); break; case 10: { evalue *EP; Value cb, ck; value_init(cb); value_init(ck); fgets(s, 128, stdin); sscanf(s, "%d", &n); for (j = 0; j < n; ++j) { Polyhedron *P; M = Matrix_Read(); P = Constraints2Polyhedron(M, options->MaxRays); Matrix_Free(M); A = DomainConcat(P, A); } fgets(s, 128, stdin); /* workaround for apparent bug in older gmps */ *strchr(s, '\n') = '\0'; while ((*s=='#') || (value_read(ck, s) != 0)) { fgets(s, 128, stdin); /* workaround for apparent bug in older gmps */ *strchr(s, '\n') = '\0'; } C = Universe_Polyhedron(0); EP = barvinok_enumerate_union(A, C, options->MaxRays); value_set_double(cb, compute_evalue(EP, &ck)+.25); if (value_ne(cb, ck)) return -1; Domain_Free(C); value_clear(cb); value_clear(ck); evalue_free(EP); break; } case 11: { isl_space *dim; isl_basic_set *bset; isl_pw_qpolynomial *expected, *computed; unsigned nparam; expected = isl_pw_qpolynomial_read_from_file(ctx, stdin); nparam = isl_pw_qpolynomial_dim(expected, isl_dim_param); dim = isl_space_set_alloc(ctx, nparam, A->Dimension - nparam); bset = isl_basic_set_new_from_polylib(A, dim); computed = isl_basic_set_lattice_width(bset); computed = isl_pw_qpolynomial_sub(computed, expected); if (!isl_pw_qpolynomial_is_zero(computed)) return -1; isl_pw_qpolynomial_free(computed); break; } case 12: { Vector *sample; int has_sample; fgets(s, 128, stdin); sscanf(s, "%d", &has_sample); sample = Polyhedron_Sample(A, options); if (!sample && has_sample) return -1; if (sample && !has_sample) return -1; if (sample && !in_domain(A, sample->p)) return -1; Vector_Free(sample); } } Domain_Free(A); } for (i = 0; i < nbVec; ++i) { int ok; Vector *V = Vector_Read(); Matrix *M = Matrix_Alloc(V->Size, V->Size); Vector_Copy(V->p, M->p[0], V->Size); ok = unimodular_complete(M, 1); assert(ok); Matrix_Print(stdout, P_VALUE_FMT, M); Matrix_Free(M); Vector_Free(V); } for (i = 0; i < nbMat; ++i) { Matrix *U, *V, *S; Matrix *M = Matrix_Read(); Smith(M, &U, &V, &S); Matrix_Print(stdout, P_VALUE_FMT, U); Matrix_Print(stdout, P_VALUE_FMT, V); Matrix_Print(stdout, P_VALUE_FMT, S); Matrix_Free(M); Matrix_Free(U); Matrix_Free(V); Matrix_Free(S); } isl_ctx_free(ctx); return 0; }