void rb_ensure_space(resizable_buf b, int n) { if (b->size + n > b->allocated_size) { int new_size = b->allocated_size + max_i(rb_resize_step,b->size + n - b->allocated_size) ; b->data = (char*)realloc_or_die((void*)b->data,new_size) ; b->allocated_size = new_size ; } }
static void wait_for_client(struct server_state* ss) { msg(1, "Waiting for client to connect\n"); TRY( listen(ss->listen_sock, 1) ); TRY( ss->tcp_sock = accept(ss->listen_sock, NULL, NULL) ); msg(1, "Accepted client connection\n"); TRY( shutdown(ss->listen_sock, SHUT_RD) ); struct epoll_event e; e.events = EPOLLIN; e.data.fd = ss->tcp_sock; TRY( epoll_ctl(ss->epoll, EPOLL_CTL_ADD, ss->tcp_sock, &e) ); if( cfg_hw_ts ) { e.events = EPOLLIN | EPOLLHUP | EPOLLERR | EPOLLPRI; e.data.fd = ss->udp_sock_ts; TRY( epoll_ctl(ss->epoll, EPOLL_CTL_ADD, ss->udp_sock_ts, &e) ); } if( cfg_hw_ts ) { int tsm = SOF_TIMESTAMPING_RX_HARDWARE | SOF_TIMESTAMPING_RAW_HARDWARE; int rc = setsockopt(ss->tcp_sock, SOL_SOCKET, SO_TIMESTAMPING, &tsm, sizeof(tsm)); if( rc < 0 ) { fprintf(stderr, "ERROR: failed to enable h/w timestamping for TCP RX\n"); exit(5); } } int one = 1; TRY( setsockopt(ss->tcp_sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one)) ); ss->rx_msg_size = sock_get_int(ss->tcp_sock); ss->tx_msg_size = sock_get_int(ss->tcp_sock); int min_tx_msg = max_i(strlen(INTERESTING_MSG), strlen(BORING_MSG)); if( ss->tx_msg_size < min_tx_msg ) { fprintf(stderr, "ERROR: UDP message size %d less than minimum %d\n", ss->tx_msg_size, min_tx_msg); exit(6); } ss->tx_buf = malloc(ss->tx_msg_size); strncpy(ss->tx_buf, BORING_MSG, ss->tx_msg_size); ss->tx_buf_ts = malloc(ss->tx_msg_size); strncpy(ss->tx_buf_ts, INTERESTING_MSG, ss->tx_msg_size); ss->rx_buf = malloc(ss->rx_msg_size); if( cfg_iter == 0 ) cfg_iter = 5/*seconds*/ * cfg_send_rate / cfg_measure_nth; if( cfg_warm_n == 0 ) { cfg_warm_n = cfg_iter / 10; if( cfg_warm_n == 0 ) cfg_warm_n = 2; } ss->rtt_sum = 0; ss->rtt_min = -1; ss->rtt_max = 0; ss->rtt_n = -cfg_warm_n; }
int main(void) { double source[SIZE] = {4.3, 1.1, 4.3, 5.9, -20.5}; printf("Array: "); show_array(SIZE, source); printf("Index of max: %d\n", max_i(SIZE, source)); return 0; }
/** * @brief Dump a size in a human-readable format. * * @param stream output stream to print to * @param unum the size * @param width minimal field width */ static inline void dump_sz_human(FILE *stream, uintmax_t unum, int width) { char postfix = '\0'; // Make sure the width is at least 1 width = max_i(width, 1); // Determine postfix #define SET_POSTFIX_IFOV(c) if (unum > 1024) { unum /= 1024; postfix = c; } SET_POSTFIX_IFOV('K'); SET_POSTFIX_IFOV('M'); SET_POSTFIX_IFOV('G'); SET_POSTFIX_IFOV('T'); SET_POSTFIX_IFOV('P'); #undef SET_POSTFIX_IFOV // Print it out if (postfix) fprintf(stream, "%*" PRIuMAX "%c", width - 1, unum, postfix); else fprintf(stream, "%*" PRIuMAX, width, unum); }
char *add_big_int(char *num_a, char *num_b, char *result) { int i, max_len; int step; /* min_len = min_i(strlen(num_a), strlen(num_b)); */ max_len = max_i(strlen(num_a), strlen(num_b)); /* if(strlen(num_a) > strlen(num_b)) { */ /* long_num = num_a; */ /* short_num = num_b; */ /* } else { */ /* long_num = num_b; */ /* short_num = num_a; */ /* } */ reverse(num_a); reverse(num_b); fill_with_zero(num_a, num_b, max_len); step = 0; for(i = 0; i < max_len; ++i) { step = add_char(num_a[i], num_b[i], result + i, step); } if(step == 1) { result[i++] = step + '0'; } result[i] = 0; /* for each char a in num_a and b in num_b: */ /* c_i != 0 */ /* result[c_i] = a + b + result[c_i-1] - 10 */ /* result[c_i-1] -= 10 */ /* c_i++ */ return reverse(result); }
/** box-based near field correction */ static double _Complex SearchBox(double *y, fastsum_plan *ths) { double _Complex val = 0.0; int t, l; int y_multiind[ths->d]; int multiindex[ths->d]; int y_ind; for (t=0; t < ths->d; t++) { y_multiind[t] = ((y[t] + 0.25 - ths->eps_B/2.0) / ths->eps_I); } if (ths->d==1) { for (y_ind = max_i(0, y_multiind[0]-1); y_ind < ths->box_count_per_dim && y_ind <= y_multiind[0]+1; y_ind++){ val += calc_SearchBox(ths->d, y, ths->box_x, ths->box_alpha, ths->box_offset[y_ind], ths->box_offset[y_ind+1], ths->Add, ths->Ad, ths->p, ths->eps_I, ths->k, ths->kernel_param, ths->flags); } } else if (ths->d==2) { for (multiindex[0] = max_i(0, y_multiind[0]-1); multiindex[0] < ths->box_count_per_dim && multiindex[0] <= y_multiind[0]+1; multiindex[0]++) for (multiindex[1] = max_i(0, y_multiind[1]-1); multiindex[1] < ths->box_count_per_dim && multiindex[1] <= y_multiind[1]+1; multiindex[1]++) { y_ind = (ths->box_count_per_dim * multiindex[0]) + multiindex[1]; val += calc_SearchBox(ths->d, y, ths->box_x, ths->box_alpha, ths->box_offset[y_ind], ths->box_offset[y_ind+1], ths->Add, ths->Ad, ths->p, ths->eps_I, ths->k, ths->kernel_param, ths->flags); } } else if(ths->d==3) { for (multiindex[0] = max_i(0, y_multiind[0]-1); multiindex[0] < ths->box_count_per_dim && multiindex[0] <= y_multiind[0]+1; multiindex[0]++) for (multiindex[1] = max_i(0, y_multiind[1]-1); multiindex[1] < ths->box_count_per_dim && multiindex[1] <= y_multiind[1]+1; multiindex[1]++) for (multiindex[2] = max_i(0, y_multiind[2]-1); multiindex[2] < ths->box_count_per_dim && multiindex[2] <= y_multiind[2]+1; multiindex[2]++) { y_ind = ((ths->box_count_per_dim * multiindex[0]) + multiindex[1]) * ths->box_count_per_dim + multiindex[2]; val += calc_SearchBox(ths->d, y, ths->box_x, ths->box_alpha, ths->box_offset[y_ind], ths->box_offset[y_ind+1], ths->Add, ths->Ad, ths->p, ths->eps_I, ths->k, ths->kernel_param, ths->flags); } } else { exit(-1); } return val; }
int clamp_i(int val, int min, int max) { return min_i(max_i(val, min), max); }
/** * Process a opts_set D-Bus request. */ static bool cdbus_process_opts_set(session_t *ps, DBusMessage *msg) { const char *target = NULL; if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) return false; #define cdbus_m_opts_set_do(tgt, type, real_type) \ if (!strcmp(MSTR(tgt), target)) { \ real_type val; \ if (!cdbus_msg_get_arg(msg, 1, type, &val)) \ return false; \ ps->o.tgt = val; \ goto cdbus_process_opts_set_success; \ } // fade_delta if (!strcmp("fade_delta", target)) { int32_t val = 0.0; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_INT32, &val)) return false; ps->o.fade_delta = max_i(val, 1); goto cdbus_process_opts_set_success; } // fade_in_step if (!strcmp("fade_in_step", target)) { double val = 0.0; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_DOUBLE, &val)) return false; ps->o.fade_in_step = normalize_d(val) * OPAQUE; goto cdbus_process_opts_set_success; } // fade_out_step if (!strcmp("fade_out_step", target)) { double val = 0.0; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_DOUBLE, &val)) return false; ps->o.fade_out_step = normalize_d(val) * OPAQUE; goto cdbus_process_opts_set_success; } // no_fading_openclose if (!strcmp("no_fading_openclose", target)) { dbus_bool_t val = FALSE; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val)) return false; opts_set_no_fading_openclose(ps, val); goto cdbus_process_opts_set_success; } // unredir_if_possible if (!strcmp("unredir_if_possible", target)) { dbus_bool_t val = FALSE; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val)) return false; if (ps->o.unredir_if_possible != val) { ps->o.unredir_if_possible = val; ps->ev_received = true; } goto cdbus_process_opts_set_success; } // clear_shadow if (!strcmp("clear_shadow", target)) { dbus_bool_t val = FALSE; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val)) return false; if (ps->o.clear_shadow != val) { ps->o.clear_shadow = val; force_repaint(ps); } goto cdbus_process_opts_set_success; } // track_focus if (!strcmp("track_focus", target)) { dbus_bool_t val = FALSE; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val)) return false; // You could enable this option, but never turn if off if (val) { opts_init_track_focus(ps); } goto cdbus_process_opts_set_success; } // vsync if (!strcmp("vsync", target)) { const char * val = NULL; if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_STRING, &val)) return false; vsync_deinit(ps); if (!parse_vsync(ps, val)) { printf_errf("(): " CDBUS_ERROR_BADARG_S, 1, "Value invalid."); cdbus_reply_err(ps, msg, CDBUS_ERROR_BADARG, CDBUS_ERROR_BADARG_S, 1, "Value invalid."); } else if (!vsync_init(ps)) { printf_errf("(): " CDBUS_ERROR_CUSTOM_S, "Failed to initialize specified VSync method."); cdbus_reply_err(ps, msg, CDBUS_ERROR_CUSTOM, CDBUS_ERROR_CUSTOM_S, "Failed to initialize specified VSync method."); } else goto cdbus_process_opts_set_success; return true; } // redirected_force if (!strcmp("redirected_force", target)) { cdbus_enum_t val = UNSET; if (!cdbus_msg_get_arg(msg, 1, CDBUS_TYPE_ENUM, &val)) return false; ps->o.redirected_force = val; force_repaint(ps); goto cdbus_process_opts_set_success; } // stoppaint_force cdbus_m_opts_set_do(stoppaint_force, CDBUS_TYPE_ENUM, cdbus_enum_t); #undef cdbus_m_opts_set_do printf_errf("(): " CDBUS_ERROR_BADTGT_S, target); cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target); return true; cdbus_process_opts_set_success: if (!dbus_message_get_no_reply(msg)) cdbus_reply_bool(ps, msg, true); return true; }
/** initialization of fastsum plan */ void fastsum_init_guru(fastsum_plan *ths, int d, int N_total, int M_total, kernel k, double *param, unsigned flags, int nn, int m, int p, double eps_I, double eps_B) { int t; int N[d], n[d]; int n_total; int sort_flags_trafo = 0; int sort_flags_adjoint = 0; #ifdef _OPENMP int nthreads = nfft_get_omp_num_threads(); #endif if (d > 1) { sort_flags_trafo = NFFT_SORT_NODES; #ifdef _OPENMP sort_flags_adjoint = NFFT_SORT_NODES | NFFT_OMP_BLOCKWISE_ADJOINT; #else sort_flags_adjoint = NFFT_SORT_NODES; #endif } ths->d = d; ths->N_total = N_total; ths->M_total = M_total; ths->x = (double *)nfft_malloc(d*N_total*(sizeof(double))); ths->alpha = (double _Complex *)nfft_malloc(N_total*(sizeof(double _Complex))); ths->y = (double *)nfft_malloc(d*M_total*(sizeof(double))); ths->f = (double _Complex *)nfft_malloc(M_total*(sizeof(double _Complex))); ths->k = k; ths->kernel_param = param; ths->flags = flags; ths->p = p; ths->eps_I = eps_I; /* =(double)ths->p/(double)nn; */ /** inner boundary */ ths->eps_B = eps_B; /* =1.0/16.0; */ /** outer boundary */ /** init spline for near field computation */ if (!(ths->flags & EXACT_NEARFIELD)) { if (ths->d==1) { ths->Ad = 4*(ths->p)*(ths->p); ths->Add = (double _Complex *)nfft_malloc((ths->Ad+5)*(sizeof(double _Complex))); } else { if (ths->k == one_over_x) { double delta = 1e-8; switch(p) { case 2: delta = 1e-3; break; case 3: delta = 1e-4; break; case 4: delta = 1e-5; break; case 5: delta = 1e-6; break; case 6: delta = 1e-6; break; case 7: delta = 1e-7; break; default: delta = 1e-8; } #if defined(NF_KUB) ths->Ad = max_i(10, (int) ceil(1.4/pow(delta,1.0/4.0))); ths->Add = (double _Complex *)nfft_malloc((ths->Ad+3)*(sizeof(double _Complex))); #elif defined(NF_QUADR) ths->Ad = (int) ceil(2.2/pow(delta,1.0/3.0)); ths->Add = (double _Complex *)nfft_malloc((ths->Ad+3)*(sizeof(double _Complex))); #elif defined(NF_LIN) ths->Ad = (int) ceil(1.7/pow(delta,1.0/2.0)); ths->Add = (double _Complex *)nfft_malloc((ths->Ad+3)*(sizeof(double _Complex))); #else #error define NF_LIN or NF_QUADR or NF_KUB #endif } else { ths->Ad = 2*(ths->p)*(ths->p); ths->Add = (double _Complex *)nfft_malloc((ths->Ad+3)*(sizeof(double _Complex))); } } } /** init d-dimensional NFFT plan */ ths->n = nn; for (t=0; t<d; t++) { N[t] = nn; n[t] = 2*nn; } nfft_init_guru(&(ths->mv1), d, N, N_total, n, m, sort_flags_adjoint | PRE_PHI_HUT| PRE_PSI| MALLOC_X | MALLOC_F_HAT| MALLOC_F| FFTW_INIT | FFT_OUT_OF_PLACE, FFTW_MEASURE| FFTW_DESTROY_INPUT); nfft_init_guru(&(ths->mv2), d, N, M_total, n, m, sort_flags_trafo | PRE_PHI_HUT| PRE_PSI| MALLOC_X | MALLOC_F_HAT| MALLOC_F| FFTW_INIT | FFT_OUT_OF_PLACE, FFTW_MEASURE| FFTW_DESTROY_INPUT); /** init d-dimensional FFTW plan */ n_total = 1; for (t=0; t<d; t++) n_total *= nn; ths->b = (fftw_complex *)nfft_malloc(n_total*sizeof(fftw_complex)); #ifdef _OPENMP #pragma omp critical (nfft_omp_critical_fftw_plan) { fftw_plan_with_nthreads(nthreads); #endif ths->fft_plan = fftw_plan_dft(d,N,ths->b,ths->b,FFTW_FORWARD,FFTW_ESTIMATE); #ifdef _OPENMP } #endif if (ths->flags & NEARFIELD_BOXES) { ths->box_count_per_dim = floor((0.5 - ths->eps_B) / ths->eps_I) + 1; ths->box_count = 1; for (t=0; t<ths->d; t++) ths->box_count *= ths->box_count_per_dim; ths->box_offset = (int *) nfft_malloc((ths->box_count+1) * sizeof(int)); ths->box_alpha = (double _Complex *)nfft_malloc(ths->N_total*(sizeof(double _Complex))); ths->box_x = (double *) nfft_malloc(ths->d * ths->N_total * sizeof(double)); } }