Пример #1
0
void test_cbrt()
{
    static_assert((std::is_same<decltype(cbrt((double)0)), double>::value), "");
    static_assert((std::is_same<decltype(cbrtf(0)), float>::value), "");
    static_assert((std::is_same<decltype(cbrtl(0)), long double>::value), "");
    assert(cbrt(1) == 1);
}
Пример #2
0
/*
 * Compute the target congestion window for the next RTT according to 
 * cubic equation when an ack is received.
 *
 * W(t) = C(t-K)^3 + W(last_max)
 */
static uint32_t
tcp_cubic_update(struct tcpcb *tp, u_int32_t rtt)
{
	float K, var;
	u_int32_t elapsed_time, win;

	win = min(tp->snd_cwnd, tp->snd_wnd);
	if (tp->t_ccstate->cub_last_max == 0)
		tp->t_ccstate->cub_last_max = tp->snd_ssthresh;

	if (tp->t_ccstate->cub_epoch_start == 0) {
		/*
		 * This is the beginning of a new epoch, initialize some of
		 * the variables that we need to use for computing the 
		 * congestion window later.
		 */
		tp->t_ccstate->cub_epoch_start = tcp_now;
		if (tp->t_ccstate->cub_epoch_start == 0)
			tp->t_ccstate->cub_epoch_start = 1;
		if (win < tp->t_ccstate->cub_last_max) {

			VERIFY(current_task() == kernel_task);

			/*
			 * Compute cubic epoch period, this is the time
			 * period that the window will take to increase to
			 * last_max again after backoff due to loss.
			 */
			K = (tp->t_ccstate->cub_last_max - win)
			    / tp->t_maxseg / tcp_cubic_coeff;
			K = cbrtf(K);
			tp->t_ccstate->cub_epoch_period = K * TCP_RETRANSHZ;
			/* Origin point */
			tp->t_ccstate->cub_origin_point = 
				tp->t_ccstate->cub_last_max;
		} else {
			tp->t_ccstate->cub_epoch_period = 0;
			tp->t_ccstate->cub_origin_point = win;
		}
		tp->t_ccstate->cub_target_win = 0;
	}
	
	VERIFY(tp->t_ccstate->cub_origin_point > 0);	
	/*
	 * Compute the target window for the next RTT using smoothed RTT
	 * as an estimate for next RTT.
	 */
	elapsed_time = timer_diff(tcp_now, 0, 
		tp->t_ccstate->cub_epoch_start, 0);

	if (tcp_cubic_use_minrtt)
		elapsed_time += max(tcp_cubic_use_minrtt, rtt);
	else
		elapsed_time += rtt;
	var = (elapsed_time  - tp->t_ccstate->cub_epoch_period) / TCP_RETRANSHZ;
	var = var * var * var * (tcp_cubic_coeff * tp->t_maxseg);

	tp->t_ccstate->cub_target_win = tp->t_ccstate->cub_origin_point + var;
	return (tp->t_ccstate->cub_target_win);
}
Пример #3
0
/**
 * @brief Restores the state of the last simulation or places the particles in
 *the shape of a cube if no state is found.
 *
 * @param[out] buffer        Points to the particles buffer to be filled
 * @param[in] parameters    Contains the simulation parameters
 *
 */
void sph_simulation::init_particles(particle* buffer,
                                    const simulation_parameters& parameters) {
  int particles_per_cube_side = ceil(cbrtf(parameters.particles_count));
  float side_length = cbrtf(initial_volume);
  float spacing = side_length / (float)particles_per_cube_side;

  std::cout << "volume: " << initial_volume << " side_length: " << side_length
            << " spacing: " << spacing << std::endl;

  // Last simualtion serialized its last frame
  // Lets load that and pick up where it let off
  std::filebuf fb;
  if (fb.open("last_frame.bin", std::ios::in)) {
    std::istream file_in(&fb);

    cereal::BinaryInputArchive archive(file_in);
    archive.loadBinary(buffer, sizeof(particle) * parameters.particles_count);

    fb.close();
  }
  // No serialized particle array was found
  // Initialize the particles in their default position
  else {
    for (unsigned int i = 0; i < parameters.particles_count; ++i) {
      // Arrange the particles in the form of a cube
      buffer[i].position.s[0] =
          (float)(i % particles_per_cube_side) * spacing - side_length / 2.f;
      buffer[i].position.s[1] =
          ((float)((i / particles_per_cube_side) % particles_per_cube_side) *
           spacing);
      buffer[i].position.s[2] =
          (float)(i / (particles_per_cube_side * particles_per_cube_side)) *
              spacing -
          side_length / 2.f;

      buffer[i].velocity.s[0] = 0.f;
      buffer[i].velocity.s[1] = 0.f;
      buffer[i].velocity.s[2] = 0.f;
      buffer[i].intermediate_velocity.s[0] = 0.f;
      buffer[i].intermediate_velocity.s[1] = 0.f;
      buffer[i].intermediate_velocity.s[2] = 0.f;

      buffer[i].density = 0.f;
      buffer[i].pressure = 0.f;
    }
  }
}
Пример #4
0
  inline float MathTrait<float>::cbrt( const float val )
  {
#ifdef _MSC_VER
    return cbrt( val ) ;
#else
    return cbrtf( val ) ;
#endif
  }
Пример #5
0
static float
husl_f(float t)
{
    if(t > lab_e)
        return cbrtf(t);
    else
        return 7.787f * t + 16.0f / 116.0f;
}
Пример #6
0
static float cubic_db_to_def(const float db)
{
	if (db == 0.0f)
		return 1.0f;
	else if (db == -INFINITY)
		return 0.0f;

	return cbrtf(db_to_mul(db));
}
Пример #7
0
/**
 * Calculate the "cubic deadband" system parameters.
 * @param[in] The width of the deadband
 * @param[in] Slope of deadband at in=0, sane values between 0 and 1.
 * @param[out] m cubic weighting of function
 * @param[out] integrated response at in=w
 */
static void vtol_deadband_setup(float w, float b, float *m, float *r)
{
	/* So basically.. we want the function to be tangent to the
	** linear sections-- have a slope of 1-- at -w and w.  In the
	** middle we want a slope of b.   So the cube here does all the
	** work b isn't doing. */
	*m = cbrtf((1-b)/(3*powf(w,2)));

	*r = powf(*m*w, 3)+b*w;
}
Пример #8
0
/* Assumes mvir in Msun and dt in Myr. */
inline float halo_tidal_range(float mvir, float av_a) {
  /* TIDAL_FORCE_LIMIT = Gc*m/r^3 (km/s/Myr per comoving Mpc) */
  /* r^3 = Gc*m/TIDAL_FORCE_LIMIT / a^2 */
  /* Mutiply by 2 to be safe.*/
  float tforce_limit = TIDAL_FORCE_LIMIT;
  if (tidal_extra_range && TIDAL_FORCE_LIMIT > 0.001) {
    tforce_limit = 0.01 * TIDAL_FORCE_LIMIT;
    if (tforce_limit < 0.001) tforce_limit = 0.001;
  }
  return (2*cbrtf(fabs(mvir*Gc/(tforce_limit*av_a*av_a))));
}
Пример #9
0
// Possible cube root optimization.
// (borrowed from metamerist.com)
static inline float cbrtf2 ( float x )
{
#ifdef CONFIG_FLOAT32
	// Avoid strict-aliasing optimization (gcc -O2).
	union { float f; int i; } u;
	u.f = x;
	u.i = (u.i / 3) + 710235478;
	return u.f;
#else
	return cbrtf(x);
#endif
}
Пример #10
0
/* } */
void setup_system(atom a[]){
  // initialize energy array
  //for () {
  //}
    float width = ceil(cbrtf((float) NA));
    first_L = cbrtf((float) NA/(DENSITY*100*100*100*6.022E23/39.948));
    L = first_L;
    float spacing =  L/width;
    float offset = spacing/2.0;
    size_t count = 0;
    for(float xpos = offset; xpos <= spacing*width; xpos +=spacing){
        for(float ypos = offset; ypos <= spacing*width; ypos +=spacing){
            for(float zpos = offset; zpos <= spacing*width; zpos +=spacing){
                a[count].atomic_number = 18;
                a[count].x = xpos;
                a[count].y = ypos;
                a[count].z = zpos;
                count++;
            }
        }
    }
}
Пример #11
0
static long
rgbaf_to_Labaf (float *src,
                float *dst,
                long   samples)
{
  long n = samples;

  while (n--)
    {
      float r = src[0];
      float g = src[1];
      float b = src[2];
      float a = src[3];

      float xr = 0.43603516f / D50_WHITE_REF_X * r + 0.38511658f / D50_WHITE_REF_X * g + 0.14305115f / D50_WHITE_REF_X * b;
      float yr = 0.22248840f / D50_WHITE_REF_Y * r + 0.71690369f / D50_WHITE_REF_Y * g + 0.06060791f / D50_WHITE_REF_Y * b;
      float zr = 0.01391602f / D50_WHITE_REF_Z * r + 0.09706116f / D50_WHITE_REF_Z * g + 0.71392822f / D50_WHITE_REF_Z * b;

      float fx = xr > LAB_EPSILON ? cbrtf (xr) : (LAB_KAPPA * xr + 16.0f) / 116.0f;
      float fy = yr > LAB_EPSILON ? cbrtf (yr) : (LAB_KAPPA * yr + 16.0f) / 116.0f;
      float fz = zr > LAB_EPSILON ? cbrtf (zr) : (LAB_KAPPA * zr + 16.0f) / 116.0f;

      float L = 116.0f * fy - 16.0f;
      float A = 500.0f * (fx - fy);
      float B = 200.0f * (fy - fz);

      dst[0] = L;
      dst[1] = A;
      dst[2] = B;
      dst[3] = a;

      src += 4;
      dst += 4;
    }

  return samples;
}
Пример #12
0
void Player::HitBall()
	{
		for (size_t i = 0; i < _ballManager->GetNumberOfBalls(); i++)
		{
			Ball& ball = *_ballManager->GetBallAtIndex(i);

			// don't test if ball belongs to other player
			// not enough balls to justify it.
			if (!ball.IsContained())
			{
				Vec2 ppos = this->convertToWorldSpace(Vec2());
				Vec2 bpos = ball.getParent()->convertToWorldSpace(ball.getPosition());
				Vec2 toBall = bpos - ppos;
				float r = 100;
				if (toBall.length() < r)
				{
					if (ball.getType() == BombBall::type)
					{	
						Vec2 emptySpace; //Basically a placeholder because this will only be used in case of bomb balls, but hit requires a vec2 even if one is not used.
						ball.Hit(emptySpace);
						PlayerHitByBall(nullptr, &ball);
						((Game_Scene*)(this->getParent()->getParent()))->SeeSaw(this, -4);
					}
					else if (ball.getType() == WalletBall::type)
					{
						Vec2 emptySpace; //Basically a placeholder because this will only be used in case of bomb balls, but hit requires a vec2 even if one is not used.
						ball.Hit(emptySpace);
						((Game_Scene*)(this->getParent()->getParent()))->SeeSaw(this, 4);
						AudioHelper::PlayRandom("Moneyball", 2);
					}
					else
					{ 
						float difficulty = 0.1f; // 0=easy, 1=hard
						float dy = toBall.y / r; // -1 -> 1
					
						dy = (dy > 0 ? 1 : -1) * pow(abs(cbrtf(dy)), (1.0f - difficulty)); // cubic curve, harder to get y just right
						dy = (dy + 1) / 2.0f; // 0 -> 1 for lerp
						Vec2 hitDir = ccpLerp(Vec2(Settings::horizontalSpeed, -250), Vec2(Settings::horizontalSpeed, 550), dy); // lerp between mim/max hit strength
						ball.Hit(hitDir);
					}
					AudioHelper::PlayRandom("bathitting", 3, 0.75 + rand_0_1() * 0.5);
				}
			}
		}
	}
Пример #13
0
void volume_move(){
    total_vol_moves+=1.0;
    float old_e = total_energy(old_argons);
    float old_vol = powf(L,3);
    float new_vol = expf(logf(old_vol) + (random() - 0.5)*VOL_MOVE);
    float new_L = cbrtf(new_vol);
    for(size_t i = NA; i>0;i--){
        new_argons[i].x*=(new_L/L);
        new_argons[i].y*=(new_L/L);
        new_argons[i].z*=(new_L/L);
    }
    float new_e = total_energy(new_argons);
    if (random()<expf(-(1.0/(KB *TEMPERATURE))*((new_e - old_e) + (PRESSURE*(new_vol- old_vol)) - ((NA+1)*(KB*TEMPERATURE)*logf(new_vol/old_vol))))){
        accepted_vol_moves +=1.0;
        memcpy(old_argons, new_argons, sizeof(new_argons));
        L=new_L;
    } else{
        memcpy(new_argons,old_argons,sizeof(new_argons));
    }
}
Пример #14
0
static long
Yaf_to_Laf (float *src,
            float *dst,
            long   samples)
{
  long n = samples;

  while (n--)
    {
      float yr = src[0];
      float a  = src[1];
      float L  = yr > LAB_EPSILON ? 116.0 * cbrtf (yr) - 16 : LAB_KAPPA * yr;

      dst[0] = L;
      dst[1] = a;

      src += 2;
      dst += 2;
    }

  return samples;
}
Пример #15
0
static TACommandVerdict cbrtf_cmd(TAThread thread,TAInputStream stream)
{
    float x, res;

    // Prepare

    x = readFloat(&stream);
    
    START_TARGET_OPERATION(thread);
    
    // Execute

    res = cbrtf(x);
    
    END_TARGET_OPERATION(thread);
    
    // Response
    
    writeFloat(thread, res);

    sendResponse(thread);
    return taDefaultVerdict;
}
int PolynomialSolver::solveCubic(float c[4], float s[3])
{
  int	i, num;
  float	sub,
    A, B, C,
    sq_A, p, q,
    cb_p, D;

  // normalize the equation:x ^ 3 + Ax ^ 2 + Bx  + C = 0
  A = c[2] / c[3];
  B = c[1] / c[3];
  C = c[0] / c[3];

  // substitute x = y - A / 3 to eliminate the quadric term: x^3 + px + q = 0
  sq_A = A * A;
  p = 1.0f/3.0f * (-1.0f/3.0f * sq_A + B);
  q = 1.0f/2.0f * (2.0f/27.0f * A *sq_A - 1.0f/3.0f * A * B + C);

  // use Cardano's formula
  cb_p = p * p * p;
  D = q * q + cb_p;

  if(isZero(D))
  {
    if(isZero(q))
    {
      // one triple solution
      s[0] = 0.0f;
      num = 1;
    }
    else
    {
      // one single and one float solution
      float u = cbrtf(-q);
      s[0] = 2.0f * u;
      s[1] = - u;
      num = 2;
    }
  }
  else if(D < 0.0)
	{
    // casus irreductibilis: three real solutions
    float phi = 1.0f/3.0f * std::acos(-q / std::sqrt(-cb_p));
    float t = 2.0f * std::sqrt(-p);
    s[0] = t * std::cos(phi);
    s[1] = -t * std::cos(phi + pi / 3.0f);
    s[2] = -t * std::cos(phi - pi / 3.0f);
    num = 3;
	}
  else
	{
    // one real solution
    float sqrt_D = std::sqrt(D);
    float u = cbrtf(sqrt_D + std::abs(q));
    if(q > 0.0f)
      s[0] = - u + p / u;
    else
      s[0] = u - p / u;
    num = 1;
	}

  // resubstitute
  sub = 1.0f / 3.0f * A;
  for(i = 0; i < num; i++)
    s[i] -= sub;
  return num;
}
Пример #17
0
inline float BB_TABLE_UNMAP(float T) {
    // return powf ((T - BB_DRAPER) / BB_TABLE_SPACING, 1.0f/BB_TABLE_XPOWER);
    float t  = (T - BB_DRAPER) / BB_TABLE_SPACING;
    float ic = cbrtf(t);
    return ic * ic; // ^2/3
}
Пример #18
0
__device__ inline float  occaCuda_fastCbrt(const float x){  return cbrtf(x); }
Пример #19
0
/**
 * Calculate rate distortion cost for quantizing with given codebook
 *
 * @return quantization distortion
 */
static av_always_inline float quantize_and_encode_band_cost_template(
                                struct AACEncContext *s,
                                PutBitContext *pb, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
                                int *bits, int BT_ZERO, int BT_UNSIGNED,
                                int BT_PAIR, int BT_ESC)
{
    const float IQ = ff_aac_pow2sf_tab[POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float  Q = ff_aac_pow2sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
    int i, j;
    float cost = 0;
    const int dim = BT_PAIR ? 2 : 4;
    int resbits = 0;
    const float  Q34 = sqrtf(Q * sqrtf(Q));
    const int range  = aac_cb_range[cb];
    const int maxval = aac_cb_maxval[cb];
    int off;

    if (BT_ZERO) {
        for (i = 0; i < size; i++)
            cost += in[i]*in[i];
        if (bits)
            *bits = 0;
        return cost * lambda;
    }
    if (!scaled) {
        abs_pow34_v(s->scoefs, in, size);
        scaled = s->scoefs;
    }
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !BT_UNSIGNED, maxval);
    if (BT_UNSIGNED) {
        off = 0;
    } else {
        off = maxval;
    }
    for (i = 0; i < size; i += dim) {
        const float *vec;
        int *quants = s->qcoefs + i;
        int curidx = 0;
        int curbits;
        float rd = 0.0f;
        for (j = 0; j < dim; j++) {
            curidx *= range;
            curidx += quants[j] + off;
        }
        curbits =  ff_aac_spectral_bits[cb-1][curidx];
        vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
        if (BT_UNSIGNED) {
            for (j = 0; j < dim; j++) {
                float t = fabsf(in[i+j]);
                float di;
                if (BT_ESC && vec[j] == 64.0f) { //FIXME: slow
                    if (t >= CLIPPED_ESCAPE) {
                        di = t - CLIPPED_ESCAPE;
                        curbits += 21;
                    } else {
                        int c = av_clip(quant(t, Q), 0, 8191);
                        di = t - c*cbrtf(c)*IQ;
                        curbits += av_log2(c)*2 - 4 + 1;
                    }
                } else {
                    di = t - vec[j]*IQ;
                }
                if (vec[j] != 0.0f)
                    curbits++;
                rd += di*di;
            }
        } else {
            for (j = 0; j < dim; j++) {
                float di = in[i+j] - vec[j]*IQ;
                rd += di*di;
            }
        }
        cost    += rd * lambda + curbits;
        resbits += curbits;
        if (cost >= uplim)
            return uplim;
        if (pb) {
            put_bits(pb, ff_aac_spectral_bits[cb-1][curidx], ff_aac_spectral_codes[cb-1][curidx]);
            if (BT_UNSIGNED)
                for (j = 0; j < dim; j++)
                    if (ff_aac_codebook_vectors[cb-1][curidx*dim+j] != 0.0f)
                        put_bits(pb, 1, in[i+j] < 0.0f);
            if (BT_ESC) {
                for (j = 0; j < 2; j++) {
                    if (ff_aac_codebook_vectors[cb-1][curidx*2+j] == 64.0f) {
                        int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
                        int len = av_log2(coef);

                        put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
                        put_bits(pb, len, coef & ((1 << len) - 1));
                    }
                }
            }
        }
    }

    if (bits)
        *bits = resbits;
    return cost;
}
Пример #20
0
/**
 * Calculate rate distortion cost for quantizing with given codebook
 *
 * @return quantization distortion
 */
static float quantize_band_cost(struct AACEncContext *s, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
                                int *bits)
{
    const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
    int i, j, k;
    float cost = 0;
    const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
    int resbits = 0;
#ifndef USE_REALLY_FULL_SEARCH
    const float  Q34 = sqrtf(Q * sqrtf(Q));
    const int range  = aac_cb_range[cb];
    const int maxval = aac_cb_maxval[cb];
    int offs[4];
#endif /* USE_REALLY_FULL_SEARCH */

    if (!cb) {
        for (i = 0; i < size; i++)
            cost += in[i]*in[i];
        if (bits)
            *bits = 0;
        return cost * lambda;
    }
#ifndef USE_REALLY_FULL_SEARCH
    offs[0] = 1;
    for (i = 1; i < dim; i++)
        offs[i] = offs[i-1]*range;
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
#endif /* USE_REALLY_FULL_SEARCH */
    for (i = 0; i < size; i += dim) {
        float mincost;
        int minidx  = 0;
        int minbits = 0;
        const float *vec;
#ifndef USE_REALLY_FULL_SEARCH
        int (*quants)[2] = &s->qcoefs[i];
        mincost = 0.0f;
        for (j = 0; j < dim; j++)
            mincost += in[i+j]*in[i+j];
        minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
        minbits = ff_aac_spectral_bits[cb-1][minidx];
        mincost = mincost * lambda + minbits;
        for (j = 0; j < (1<<dim); j++) {
            float rd = 0.0f;
            int curbits;
            int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
            int same   = 0;
            for (k = 0; k < dim; k++) {
                if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
                    same = 1;
                    break;
                }
            }
            if (same)
                continue;
            for (k = 0; k < dim; k++)
                curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
#else
        mincost = INFINITY;
        vec = ff_aac_codebook_vectors[cb-1];
        for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
            float rd = 0.0f;
            int curbits = ff_aac_spectral_bits[cb-1][j];
#endif /* USE_REALLY_FULL_SEARCH */
            if (IS_CODEBOOK_UNSIGNED(cb)) {
                for (k = 0; k < dim; k++) {
                    float t = fabsf(in[i+k]);
                    float di;
                    if (vec[k] == 64.0f) { //FIXME: slow
                        //do not code with escape sequence small values
                        if (t < 39.0f*IQ) {
                            rd = INFINITY;
                            break;
                        }
                        if (t >= CLIPPED_ESCAPE) {
                            di = t - CLIPPED_ESCAPE;
                            curbits += 21;
                        } else {
                            int c = av_clip(quant(t, Q), 0, 8191);
                            di = t - c*cbrtf(c)*IQ;
                            curbits += av_log2(c)*2 - 4 + 1;
                        }
                    } else {
                        di = t - vec[k]*IQ;
                    }
                    if (vec[k] != 0.0f)
                        curbits++;
                    rd += di*di;
                }
            } else {
                for (k = 0; k < dim; k++) {
                    float di = in[i+k] - vec[k]*IQ;
                    rd += di*di;
                }
            }
            rd = rd * lambda + curbits;
            if (rd < mincost) {
                mincost = rd;
                minidx  = j;
                minbits = curbits;
            }
        }
        cost    += mincost;
        resbits += minbits;
        if (cost >= uplim)
            return uplim;
    }

    if (bits)
        *bits = resbits;
    return cost;
}

static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
                                     const float *in, int size, int scale_idx,
                                     int cb, const float lambda)
{
    const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
    const int dim = (cb < FIRST_PAIR_BT) ? 4 : 2;
    int i, j, k;
#ifndef USE_REALLY_FULL_SEARCH
    const float  Q34 = sqrtf(Q * sqrtf(Q));
    const int range  = aac_cb_range[cb];
    const int maxval = aac_cb_maxval[cb];
    int offs[4];
    float *scaled = s->scoefs;
#endif /* USE_REALLY_FULL_SEARCH */

//START_TIMER
    if (!cb)
        return;

#ifndef USE_REALLY_FULL_SEARCH
    offs[0] = 1;
    for (i = 1; i < dim; i++)
        offs[i] = offs[i-1]*range;
    abs_pow34_v(scaled, in, size);
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
#endif /* USE_REALLY_FULL_SEARCH */
    for (i = 0; i < size; i += dim) {
        float mincost;
        int minidx  = 0;
        int minbits = 0;
        const float *vec;
#ifndef USE_REALLY_FULL_SEARCH
        int (*quants)[2] = &s->qcoefs[i];
        mincost = 0.0f;
        for (j = 0; j < dim; j++)
            mincost += in[i+j]*in[i+j];
        minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
        minbits = ff_aac_spectral_bits[cb-1][minidx];
        mincost = mincost * lambda + minbits;
        for (j = 0; j < (1<<dim); j++) {
            float rd = 0.0f;
            int curbits;
            int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
            int same   = 0;
            for (k = 0; k < dim; k++) {
                if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
                    same = 1;
                    break;
                }
            }
            if (same)
                continue;
            for (k = 0; k < dim; k++)
                curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
#else
        vec = ff_aac_codebook_vectors[cb-1];
        mincost = INFINITY;
        for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
            float rd = 0.0f;
            int curbits = ff_aac_spectral_bits[cb-1][j];
            int curidx  = j;
#endif /* USE_REALLY_FULL_SEARCH */
            if (IS_CODEBOOK_UNSIGNED(cb)) {
                for (k = 0; k < dim; k++) {
                    float t = fabsf(in[i+k]);
                    float di;
                    if (vec[k] == 64.0f) { //FIXME: slow
                        //do not code with escape sequence small values
                        if (t < 39.0f*IQ) {
                            rd = INFINITY;
                            break;
                        }
                        if (t >= CLIPPED_ESCAPE) {
                            di = t - CLIPPED_ESCAPE;
                            curbits += 21;
                        } else {
                            int c = av_clip(quant(t, Q), 0, 8191);
                            di = t - c*cbrtf(c)*IQ;
                            curbits += av_log2(c)*2 - 4 + 1;
                        }
                    } else {
                        di = t - vec[k]*IQ;
                    }
                    if (vec[k] != 0.0f)
                        curbits++;
                    rd += di*di;
                }
            } else {
                for (k = 0; k < dim; k++) {
                    float di = in[i+k] - vec[k]*IQ;
                    rd += di*di;
                }
            }
            rd = rd * lambda + curbits;
            if (rd < mincost) {
                mincost = rd;
                minidx  = curidx;
                minbits = curbits;
            }
        }
        put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
        if (IS_CODEBOOK_UNSIGNED(cb))
            for (j = 0; j < dim; j++)
                if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
                    put_bits(pb, 1, in[i+j] < 0.0f);
        if (cb == ESC_BT) {
            for (j = 0; j < 2; j++) {
                if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
                    int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
                    int len = av_log2(coef);

                    put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
                    put_bits(pb, len, coef & ((1 << len) - 1));
                }
            }
        }
    }
//STOP_TIMER("quantize_and_encode")
}

/**
 * structure used in optimal codebook search
 */
typedef struct BandCodingPath {
    int prev_idx; ///< pointer to the previous path point
    float cost;   ///< path cost
    int run;
} BandCodingPath;

/**
 * Encode band info for single window group bands.
 */
static void encode_window_bands_info(AACEncContext *s, SingleChannelElement *sce,
                                     int win, int group_len, const float lambda)
{
    BandCodingPath path[120][12];
    int w, swb, cb, start, start2, size;
    int i, j;
    const int max_sfb  = sce->ics.max_sfb;
    const int run_bits = sce->ics.num_windows == 1 ? 5 : 3;
    const int run_esc  = (1 << run_bits) - 1;
    int idx, ppos, count;
    int stackrun[120], stackcb[120], stack_len;
    float next_minrd = INFINITY;
    int next_mincb = 0;

    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
    start = win*128;
    for (cb = 0; cb < 12; cb++) {
        path[0][cb].cost     = 0.0f;
        path[0][cb].prev_idx = -1;
        path[0][cb].run      = 0;
    }
    for (swb = 0; swb < max_sfb; swb++) {
        start2 = start;
        size = sce->ics.swb_sizes[swb];
        if (sce->zeroes[win*16 + swb]) {
            for (cb = 0; cb < 12; cb++) {
                path[swb+1][cb].prev_idx = cb;
                path[swb+1][cb].cost     = path[swb][cb].cost;
                path[swb+1][cb].run      = path[swb][cb].run + 1;
            }
        } else {
            float minrd = next_minrd;
            int mincb = next_mincb;
            next_minrd = INFINITY;
            next_mincb = 0;
            for (cb = 0; cb < 12; cb++) {
                float cost_stay_here, cost_get_here;
                float rd = 0.0f;
                for (w = 0; w < group_len; w++) {
                    FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(win+w)*16+swb];
                    rd += quantize_band_cost(s, sce->coeffs + start + w*128,
                                             s->scoefs + start + w*128, size,
                                             sce->sf_idx[(win+w)*16+swb], cb,
                                             lambda / band->threshold, INFINITY, NULL);
                }
                cost_stay_here = path[swb][cb].cost + rd;
                cost_get_here  = minrd              + rd + run_bits + 4;
                if (   run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run]
                    != run_value_bits[sce->ics.num_windows == 8][path[swb][cb].run+1])
                    cost_stay_here += run_bits;
                if (cost_get_here < cost_stay_here) {
                    path[swb+1][cb].prev_idx = mincb;
                    path[swb+1][cb].cost     = cost_get_here;
                    path[swb+1][cb].run      = 1;
                } else {
                    path[swb+1][cb].prev_idx = cb;
                    path[swb+1][cb].cost     = cost_stay_here;
                    path[swb+1][cb].run      = path[swb][cb].run + 1;
                }
                if (path[swb+1][cb].cost < next_minrd) {
                    next_minrd = path[swb+1][cb].cost;
                    next_mincb = cb;
                }
            }
        }
        start += sce->ics.swb_sizes[swb];
    }

    //convert resulting path from backward-linked list
    stack_len = 0;
    idx       = 0;
    for (cb = 1; cb < 12; cb++)
        if (path[max_sfb][cb].cost < path[max_sfb][idx].cost)
            idx = cb;
    ppos = max_sfb;
    while (ppos > 0) {
        cb = idx;
        stackrun[stack_len] = path[ppos][cb].run;
        stackcb [stack_len] = cb;
        idx = path[ppos-path[ppos][cb].run+1][cb].prev_idx;
        ppos -= path[ppos][cb].run;
        stack_len++;
    }
    //perform actual band info encoding
    start = 0;
    for (i = stack_len - 1; i >= 0; i--) {
        put_bits(&s->pb, 4, stackcb[i]);
        count = stackrun[i];
        memset(sce->zeroes + win*16 + start, !stackcb[i], count);
        //XXX: memset when band_type is also uint8_t
        for (j = 0; j < count; j++) {
            sce->band_type[win*16 + start] =  stackcb[i];
            start++;
        }
        while (count >= run_esc) {
            put_bits(&s->pb, run_bits, run_esc);
            count -= run_esc;
        }
        put_bits(&s->pb, run_bits, count);
    }
}

typedef struct TrellisPath {
    float cost;
    int prev;
    int min_val;
    int max_val;
} TrellisPath;

#define TRELLIS_STAGES 121
#define TRELLIS_STATES 256

static void search_for_quantizers_anmr(AVCodecContext *avctx, AACEncContext *s,
                                       SingleChannelElement *sce,
                                       const float lambda)
{
    int q, w, w2, g, start = 0;
    int i, j;
    int idx;
    TrellisPath paths[TRELLIS_STAGES][TRELLIS_STATES];
    int bandaddr[TRELLIS_STAGES];
    int minq;
    float mincost;

    for (i = 0; i < TRELLIS_STATES; i++) {
        paths[0][i].cost    = 0.0f;
        paths[0][i].prev    = -1;
        paths[0][i].min_val = i;
        paths[0][i].max_val = i;
    }
    for (j = 1; j < TRELLIS_STAGES; j++) {
        for (i = 0; i < TRELLIS_STATES; i++) {
            paths[j][i].cost    = INFINITY;
            paths[j][i].prev    = -2;
            paths[j][i].min_val = INT_MAX;
            paths[j][i].max_val = 0;
        }
    }
    idx = 1;
    abs_pow34_v(s->scoefs, sce->coeffs, 1024);
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
        start = w*128;
        for (g = 0; g < sce->ics.num_swb; g++) {
            const float *coefs = sce->coeffs + start;
            float qmin, qmax;
            int nz = 0;

            bandaddr[idx] = w * 16 + g;
            qmin = INT_MAX;
            qmax = 0.0f;
            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
                if (band->energy <= band->threshold || band->threshold == 0.0f) {
                    sce->zeroes[(w+w2)*16+g] = 1;
                    continue;
                }
                sce->zeroes[(w+w2)*16+g] = 0;
                nz = 1;
                for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
                    float t = fabsf(coefs[w2*128+i]);
                    if (t > 0.0f)
                        qmin = FFMIN(qmin, t);
                    qmax = FFMAX(qmax, t);
                }
            }
            if (nz) {
                int minscale, maxscale;
                float minrd = INFINITY;
                //minimum scalefactor index is when minimum nonzero coefficient after quantizing is not clipped
                minscale = av_clip_uint8(log2(qmin)*4 - 69 + SCALE_ONE_POS - SCALE_DIV_512);
                //maximum scalefactor index is when maximum coefficient after quantizing is still not zero
                maxscale = av_clip_uint8(log2(qmax)*4 +  6 + SCALE_ONE_POS - SCALE_DIV_512);
                for (q = minscale; q < maxscale; q++) {
                    float dists[12], dist;
                    memset(dists, 0, sizeof(dists));
                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
                        FFPsyBand *band = &s->psy.psy_bands[s->cur_channel*PSY_MAX_BANDS+(w+w2)*16+g];
                        int cb;
                        for (cb = 0; cb <= ESC_BT; cb++)
                            dists[cb] += quantize_band_cost(s, coefs + w2*128, s->scoefs + start + w2*128, sce->ics.swb_sizes[g],
                                                            q, cb, lambda / band->threshold, INFINITY, NULL);
                    }
                    dist = dists[0];
                    for (i = 1; i <= ESC_BT; i++)
                        dist = FFMIN(dist, dists[i]);
                    minrd = FFMIN(minrd, dist);

                    for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
                        float cost;
                        int minv, maxv;
                        if (isinf(paths[idx - 1][i].cost))
                            continue;
                        cost = paths[idx - 1][i].cost + dist
                               + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
                        minv = FFMIN(paths[idx - 1][i].min_val, q);
                        maxv = FFMAX(paths[idx - 1][i].max_val, q);
                        if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
                            paths[idx][q].cost    = cost;
                            paths[idx][q].prev    = i;
                            paths[idx][q].min_val = minv;
                            paths[idx][q].max_val = maxv;
                        }
                    }
                }
            } else {
                for (q = 0; q < TRELLIS_STATES; q++) {
                    if (!isinf(paths[idx - 1][q].cost)) {
                        paths[idx][q].cost = paths[idx - 1][q].cost + 1;
                        paths[idx][q].prev = q;
                        paths[idx][q].min_val = FFMIN(paths[idx - 1][q].min_val, q);
                        paths[idx][q].max_val = FFMAX(paths[idx - 1][q].max_val, q);
                        continue;
                    }
                    for (i = FFMAX(q - SCALE_MAX_DIFF, 0); i < FFMIN(q + SCALE_MAX_DIFF, TRELLIS_STATES); i++) {
                        float cost;
                        int minv, maxv;
                        if (isinf(paths[idx - 1][i].cost))
                            continue;
                        cost = paths[idx - 1][i].cost + ff_aac_scalefactor_bits[q - i + SCALE_DIFF_ZERO];
                        minv = FFMIN(paths[idx - 1][i].min_val, q);
                        maxv = FFMAX(paths[idx - 1][i].max_val, q);
                        if (cost < paths[idx][q].cost && maxv-minv < SCALE_MAX_DIFF) {
                            paths[idx][q].cost    = cost;
                            paths[idx][q].prev    = i;
                            paths[idx][q].min_val = minv;
                            paths[idx][q].max_val = maxv;
                        }
                    }
                }
            }
            sce->zeroes[w*16+g] = !nz;
            start += sce->ics.swb_sizes[g];
            idx++;
        }
    }
    idx--;
    mincost = paths[idx][0].cost;
    minq    = 0;
    for (i = 1; i < TRELLIS_STATES; i++) {
        if (paths[idx][i].cost < mincost) {
            mincost = paths[idx][i].cost;
            minq = i;
        }
    }
    while (idx) {
        sce->sf_idx[bandaddr[idx]] = minq;
        minq = paths[idx][minq].prev;
        idx--;
    }
    //set the same quantizers inside window groups
    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
        for (g = 0;  g < sce->ics.num_swb; g++)
            for (w2 = 1; w2 < sce->ics.group_len[w]; w2++)
                sce->sf_idx[(w+w2)*16+g] = sce->sf_idx[w*16+g];
}
Пример #21
0
/**
 * Calculate rate distortion cost for quantizing with given codebook
 *
 * @return quantization distortion
 */
static float quantize_and_encode_band_cost(struct AACEncContext *s,
                                PutBitContext *pb, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
                                int *bits)
{
    const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float  Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f*IQ;
    int i, j, k;
    float cost = 0;
    const int dim = cb < FIRST_PAIR_BT ? 4 : 2;
    int resbits = 0;
#ifndef USE_REALLY_FULL_SEARCH
    const float  Q34 = sqrtf(Q * sqrtf(Q));
    const int range  = aac_cb_range[cb];
    const int maxval = aac_cb_maxval[cb];
    int offs[4];
#endif /* USE_REALLY_FULL_SEARCH */

    if (!cb) {
        for (i = 0; i < size; i++)
            cost += in[i]*in[i];
        if (bits)
            *bits = 0;
        return cost * lambda;
    }
#ifndef USE_REALLY_FULL_SEARCH
    offs[0] = 1;
    for (i = 1; i < dim; i++)
        offs[i] = offs[i-1]*range;
    if (!scaled) {
        abs_pow34_v(s->scoefs, in, size);
        scaled = s->scoefs;
    }
    quantize_bands(s->qcoefs, in, scaled, size, Q34, !IS_CODEBOOK_UNSIGNED(cb), maxval);
#endif /* USE_REALLY_FULL_SEARCH */
    for (i = 0; i < size; i += dim) {
        float mincost;
        int minidx  = 0;
        int minbits = 0;
        const float *vec;
#ifndef USE_REALLY_FULL_SEARCH
        int (*quants)[2] = &s->qcoefs[i];
        mincost = 0.0f;
        for (j = 0; j < dim; j++)
            mincost += in[i+j]*in[i+j];
        minidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
        minbits = ff_aac_spectral_bits[cb-1][minidx];
        mincost = mincost * lambda + minbits;
        for (j = 0; j < (1<<dim); j++) {
            float rd = 0.0f;
            int curbits;
            int curidx = IS_CODEBOOK_UNSIGNED(cb) ? 0 : 40;
            int same   = 0;
            for (k = 0; k < dim; k++) {
                if ((j & (1 << k)) && quants[k][0] == quants[k][1]) {
                    same = 1;
                    break;
                }
            }
            if (same)
                continue;
            for (k = 0; k < dim; k++)
                curidx += quants[k][!!(j & (1 << k))] * offs[dim - 1 - k];
            curbits =  ff_aac_spectral_bits[cb-1][curidx];
            vec     = &ff_aac_codebook_vectors[cb-1][curidx*dim];
#else
        mincost = INFINITY;
        vec = ff_aac_codebook_vectors[cb-1];
        for (j = 0; j < ff_aac_spectral_sizes[cb-1]; j++, vec += dim) {
            float rd = 0.0f;
            int curbits = ff_aac_spectral_bits[cb-1][j];
            int curidx = j;
#endif /* USE_REALLY_FULL_SEARCH */
            if (IS_CODEBOOK_UNSIGNED(cb)) {
                for (k = 0; k < dim; k++) {
                    float t = fabsf(in[i+k]);
                    float di;
                    if (vec[k] == 64.0f) { //FIXME: slow
                        //do not code with escape sequence small values
                        if (t < 39.0f*IQ) {
                            rd = INFINITY;
                            break;
                        }
                        if (t >= CLIPPED_ESCAPE) {
                            di = t - CLIPPED_ESCAPE;
                            curbits += 21;
                        } else {
                            int c = av_clip(quant(t, Q), 0, 8191);
                            di = t - c*cbrtf(c)*IQ;
                            curbits += av_log2(c)*2 - 4 + 1;
                        }
                    } else {
                        di = t - vec[k]*IQ;
                    }
                    if (vec[k] != 0.0f)
                        curbits++;
                    rd += di*di;
                }
            } else {
                for (k = 0; k < dim; k++) {
                    float di = in[i+k] - vec[k]*IQ;
                    rd += di*di;
                }
            }
            rd = rd * lambda + curbits;
            if (rd < mincost) {
                mincost = rd;
                minidx  = curidx;
                minbits = curbits;
            }
        }
        cost    += mincost;
        resbits += minbits;
        if (cost >= uplim)
            return uplim;
        if (pb) {
        put_bits(pb, ff_aac_spectral_bits[cb-1][minidx], ff_aac_spectral_codes[cb-1][minidx]);
        if (IS_CODEBOOK_UNSIGNED(cb))
            for (j = 0; j < dim; j++)
                if (ff_aac_codebook_vectors[cb-1][minidx*dim+j] != 0.0f)
                    put_bits(pb, 1, in[i+j] < 0.0f);
        if (cb == ESC_BT) {
            for (j = 0; j < 2; j++) {
                if (ff_aac_codebook_vectors[cb-1][minidx*2+j] == 64.0f) {
                    int coef = av_clip(quant(fabsf(in[i+j]), Q), 0, 8191);
                    int len = av_log2(coef);

                    put_bits(pb, len - 4 + 1, (1 << (len - 4 + 1)) - 2);
                    put_bits(pb, len, coef & ((1 << len) - 1));
                }
            }
        }
        }
    }

    if (bits)
        *bits = resbits;
    return cost;
}
static float quantize_band_cost(struct AACEncContext *s, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
                                int *bits)
{
    return quantize_and_encode_band_cost(s, NULL, in, scaled, size, scale_idx,
                                         cb, lambda, uplim, bits);
}

static void quantize_and_encode_band(struct AACEncContext *s, PutBitContext *pb,
                                     const float *in, int size, int scale_idx,
                                     int cb, const float lambda)
{
    quantize_and_encode_band_cost(s, pb, in, NULL, size, scale_idx, cb, lambda,
                                  INFINITY, NULL);
}
Пример #22
0
static int testf(float float_x, long double long_double_x, /*float complex float_complex_x,*/ int int_x, long long_x)
{
int r = 0;
r += acosf(float_x);
r += acoshf(float_x);
r += asinf(float_x);
r += asinhf(float_x);
r += atan2f(float_x, float_x);
r += atanf(float_x);
r += atanhf(float_x);
/*r += cargf(float_complex_x); - will fight with complex numbers later */
r += cbrtf(float_x);
r += ceilf(float_x);
r += copysignf(float_x, float_x);
r += cosf(float_x);
r += coshf(float_x);
r += erfcf(float_x);
r += erff(float_x);
r += exp2f(float_x);
r += expf(float_x);
r += expm1f(float_x);
r += fabsf(float_x);
r += fdimf(float_x, float_x);
r += floorf(float_x);
r += fmaf(float_x, float_x, float_x);
r += fmaxf(float_x, float_x);
r += fminf(float_x, float_x);
r += fmodf(float_x, float_x);
r += frexpf(float_x, &int_x);
r += gammaf(float_x);
r += hypotf(float_x, float_x);
r += ilogbf(float_x);
r += ldexpf(float_x, int_x);
r += lgammaf(float_x);
r += llrintf(float_x);
r += llroundf(float_x);
r += log10f(float_x);
r += log1pf(float_x);
r += log2f(float_x);
r += logbf(float_x);
r += logf(float_x);
r += lrintf(float_x);
r += lroundf(float_x);
r += modff(float_x, &float_x);
r += nearbyintf(float_x);
r += nexttowardf(float_x, long_double_x);
r += powf(float_x, float_x);
r += remainderf(float_x, float_x);
r += remquof(float_x, float_x, &int_x);
r += rintf(float_x);
r += roundf(float_x);
#ifdef __UCLIBC_SUSV3_LEGACY__
r += scalbf(float_x, float_x);
#endif
r += scalblnf(float_x, long_x);
r += scalbnf(float_x, int_x);
r += significandf(float_x);
r += sinf(float_x);
r += sinhf(float_x);
r += sqrtf(float_x);
r += tanf(float_x);
r += tanhf(float_x);
r += tgammaf(float_x);
r += truncf(float_x);
return r;
}
Пример #23
0
	//------------------------------------------------------------------------------
	double Cmath::cbrt( double x )
	{
		return (double) cbrtf( (float) x );
	}
Пример #24
0
void sph_simulation::load_settings(std::string fluid_file_name,
                                   std::string parameters_file_name) {
  int particles_inside_influence_radius = 0;

  {
    picojson::value fluid_params;
    std::ifstream fluid_stream(fluid_file_name);
    fluid_stream >> fluid_params;

    parameters.fluid_density =
        (float)(fluid_params.get<picojson::object>()["fluid_density"]
                    .get<double>());
    parameters.dynamic_viscosity =
        (float)(fluid_params.get<picojson::object>()["dynamic_viscosity"]
                    .get<double>());
    parameters.restitution =
        (float)(fluid_params.get<picojson::object>()["restitution"]
                    .get<double>());
    if (parameters.restitution < 0 || parameters.restitution > 1) {
      throw std::runtime_error("Restitution has an invalid value!");
    }

    parameters.K =
        (float)(fluid_params.get<picojson::object>()["k"].get<double>());
    parameters.surface_tension_threshold =
        (float)(fluid_params
                    .get<picojson::object>()["surface_tension_threshold"]
                    .get<double>());
    parameters.surface_tension =
        (float)(fluid_params.get<picojson::object>()["surface_tension"]
                    .get<double>());
    particles_inside_influence_radius =
        (int)(fluid_params
                  .get<picojson::object>()["particles_inside_influence_radius"]
                  .get<double>());
  }

  {
    picojson::value sim_params;
    std::ifstream sim_stream(parameters_file_name);
    sim_stream >> sim_params;

    parameters.particles_count =
        (unsigned int)(sim_params.get<picojson::object>()["particles_count"]
                           .get<double>());

    if (parameters.particles_count % kPreferredWorkGroupSizeMultiple != 0) {
      std::cout << std::endl
                << "\033[1;31m You should choose a number of particles that is "
                   "divisble by the preferred work group size.\033[0m";
      std::cout << std::endl
                << "\033[1;31m Performances will be sub-optimal.\033[0m"
                << std::endl;
    }

    parameters.particle_mass =
        (float)(sim_params.get<picojson::object>()["particle_mass"]
                    .get<double>());
    parameters.simulation_time =
        (float)(sim_params.get<picojson::object>()["simulation_time"]
                    .get<double>());
    parameters.target_fps =
        (float)(sim_params.get<picojson::object>()["target_fps"].get<double>());
    parameters.simulation_scale =
        (float)(sim_params.get<picojson::object>()["simulation_scale"]
                    .get<double>());

    parameters.constant_acceleration.s[0] =
        (float)(sim_params.get<picojson::object>()["constant_acceleration"]
                    .get<picojson::object>()["x"]
                    .get<double>());
    parameters.constant_acceleration.s[1] =
        (float)(sim_params.get<picojson::object>()["constant_acceleration"]
                    .get<picojson::object>()["y"]
                    .get<double>());
    parameters.constant_acceleration.s[2] =
        (float)(sim_params.get<picojson::object>()["constant_acceleration"]
                    .get<picojson::object>()["z"]
                    .get<double>());

    write_intermediate_frames =
        sim_params.get<picojson::object>()["write_all_frames"].get<bool>();
    serialize = sim_params.get<picojson::object>()["serialize"].get<bool>();
  }

  parameters.total_mass = parameters.particles_count * parameters.particle_mass;
  initial_volume = parameters.total_mass / parameters.fluid_density;
  parameters.h = cbrtf(3.f * (particles_inside_influence_radius *
                              (initial_volume / parameters.particles_count)) /
                       (4.f * M_PI));
  parameters.time_delta = 1.f / parameters.target_fps;

  parameters.max_velocity = 0.8f * parameters.h / parameters.time_delta;

  precomputed_terms.poly_6 = 315.f / (64.f * M_PI * pow(parameters.h, 9.f));
  precomputed_terms.poly_6_gradient =
      -945.f / (32.f * M_PI * pow(parameters.h, 9.f));
  precomputed_terms.poly_6_laplacian =
      -945.f / (32.f * M_PI * pow(parameters.h, 9.f));
  precomputed_terms.spiky = -45.f / (M_PI * pow(parameters.h, 6.f));
  precomputed_terms.viscosity = 45.f / (M_PI * pow(parameters.h, 6.f));
}
Пример #25
0
void ParticleSystem::update(){
  int repielValue = 2;
  float averfft=1.0f;
  float fftMax = 0.0f;
  float trigger;


  // fft is uninitialized
  if(!fftSize || !fft)
   return ;

  // compute fft average value and maximum
  for(int i=0; i< fftSize; i++){  averfft+=fft[i];
    if(fft[i]>fftMax)
      fftMax = fft[i];
  }
  averfft/=fftSize;

  int x, y;// -----
  if (particles.size() > maxParticles & fftFlowFactor > 0){
    fftFlowFactor-=0.1;
  }
  if (particles.size() < maxParticles ) {
    fftFlowFactor+=0.1; 
  } 
  for(int k = fftSize; k >0 ; --k){
      int i = ofRandom(0, fftSize);
      float factor = fftFlowFactor * cbrtf(fft[i]*(float)(i+1)/20.0); 
      //factor = sqrtf(factor);
      int numPart = factor * fft[i];

      for(int j=0; j< numPart; j++){
        float angle;
        if (rotatingAngle){
          angle = ofGetFrameNum()%100;
        } else{
          angle = 0;
        }
        if(particleRandomPos){
          x = ofRandom(0, width);
          y = ofRandom(0, height);
        } else {
          x = width/2;
          y = height/2;
        }
        Particle p=Particle(x, y, particleSizeMin, particleSizeMax);
        p.alpha = 0.0f;
        p.id = i;
        p.velocityMultiplier = particleVelocityMult;
        p.angle = angle  +  i/(fftSize-1)*TWO_PI;
        p.update();
        particles.push_back(p);
      }
  }

  // FIXME: rewrite this
  // number of particles to add
  /*int count = fftSize; // averfft * fftMax * 2000;
  while ( count >= 0){
    int i = ofRandom(0, fftSize-1);
    // apply correct threshold depending on frequency
    if ( i < fftSize/3 && ! averageTrigger ){
      trigger = lowThresh;
    } else if(i >= fftSize/3 && i < 2*(fftSize/3) && ! averageTrigger){
      trigger = midThresh;
    } else if( ! averageTrigger ){
      trigger = highThresh;
    } else { // then averfft trigger enabled
      trigger = averfft;
    }
    count--;
    // if fft value and not reaching max paticles number
    if(fft[i] > trigger && particles.size() < maxParticles){
      //cout<<fft[i]*10<<endl;
      for(float cnt = 0.0f; cnt < fft[i] * 10.0f ; cnt+=0.1f){
      //cout<<count<<endl;
      //count--;
      // add new particle
      int x = width/2;
      int y = height/2;
      float angle;
      if (rotatingAngle){
        angle = ofGetFrameNum()%100;
      } else{
        angle = 0;
      }
      if(particleRandomPos){
        x = ofRandom(0, width);
        y = ofRandom(0, height);
      } else {

      }
      Particle p=Particle(x, y, particleSizeMin, particleSizeMax);
      p.alpha = 0.0f;
      p.id = i;
      p.velocityMultiplier = particleVelocityMult;
      p.angle = angle  +  i/(fftSize-1)*TWO_PI;
      p.update();
      particles.push_back(p);
      }
    }
  }*/

  // for each particles
  for(int j=0; j < particles.size(); j++){
    particles[j].accel *= accelDamp;
    particles[j].velocity *= velocityDamp;
    //particles[j].addDamping();

    if(! particleDeserveToLive(j)){
      //cout<<"ERASING"<<endl;
      particles.erase(particles.begin()+j);
      continue;
      //addParticle();
    }

    //---------------------------------------------------- Move Particles with a perlin noise
    if(enableNoise){
      float noiseAngle = noise->Get(particles[j].position.x, particles[j].position.y) * TWO_PI;
      float noiseAngle2  = noise->Get(particles[j].position.x, particles[j].position.y) * TWO_PI;
      ofxVec3f noiseForce = ofxVec3f(cos(noiseAngle), -sin(noiseAngle2), 0);
      particles[j].accel += noiseMult * noiseForce;
    }

    int fftid=particles[j].id;
    float sign=(ofRandom(0,1))?-1.0f:1.0f;
    float fftAngle = sign * ((float )(fft[fftid]/fftMax)*PI + particles[j].angle);

    // apply force
    ofxVec2f fftForce;

    if( averFFTSpeed)
      fftForce = ofxVec3f(averfft * fftMult * cos(fftAngle), averfft * fftMult * sin(fftAngle), 0);
    else
      fftForce = ofxVec3f(sqrt(fft[fftid]) * averfft * fftMult * cos(fftAngle), averfft * sqrt(fft[fftid]) * fftMult * sin(fftAngle), 0);

    particles[j].accel += fftForce;

    // set particle color
    msaColor color;
    int h = (lroundf(particles[j].id * 360.0f / (float)fftSize));
    color.setHSV(h,  MIN(2.0f*fft[fftid], 1.0), MIN(2.0f* averfft, 1.0) );
    particles[j].r = color.r;
    particles[j].g = color.g;
    particles[j].b = color.b;
    particles[j].alpha = 0.5 + 0.5 * fft[fftid];
    particles[j].update();
  }

}
Пример #26
0
__host__ void single_precision_math_functions()
{
    int iX;
    float fX, fY;

    acosf(1.0f);
    acoshf(1.0f);
    asinf(0.0f);
    asinhf(0.0f);
    atan2f(0.0f, 1.0f);
    atanf(0.0f);
    atanhf(0.0f);
    cbrtf(0.0f);
    ceilf(0.0f);
    copysignf(1.0f, -2.0f);
    cosf(0.0f);
    coshf(0.0f);
    //cospif(0.0f);
    //cyl_bessel_i0f(0.0f);
    //cyl_bessel_i1f(0.0f);
    erfcf(0.0f);
    //erfcinvf(2.0f);
    //erfcxf(0.0f);
    erff(0.0f);
    //erfinvf(1.0f);
    exp10f(0.0f);
    exp2f(0.0f);
    expf(0.0f);
    expm1f(0.0f);
    fabsf(1.0f);
    fdimf(1.0f, 0.0f);
    //fdividef(0.0f, 1.0f);
    floorf(0.0f);
    fmaf(1.0f, 2.0f, 3.0f);
    fmaxf(0.0f, 0.0f);
    fminf(0.0f, 0.0f);
    fmodf(0.0f, 1.0f);
    frexpf(0.0f, &iX);
    hypotf(1.0f, 0.0f);
    ilogbf(1.0f);
    isfinite(0.0f);
    isinf(0.0f);
    isnan(0.0f);
    ///j0f(0.0f);
    ///j1f(0.0f);
    ///jnf(-1.0f, 1.0f);
    ldexpf(0.0f, 0);
    ///lgammaf(1.0f);
    ///llrintf(0.0f);
    ///llroundf(0.0f);
    log10f(1.0f);
    log1pf(-1.0f);
    log2f(1.0f);
    logbf(1.0f);
    logf(1.0f);
    ///lrintf(0.0f);
    ///lroundf(0.0f);
    modff(0.0f, &fX);
    ///nanf("1");
    nearbyintf(0.0f);
    //nextafterf(0.0f);
    //norm3df(1.0f, 0.0f, 0.0f);
    //norm4df(1.0f, 0.0f, 0.0f, 0.0f);
    //normcdff(0.0f);
    //normcdfinvf(1.0f);
    //fX = 1.0f; normf(1, &fX);
    powf(1.0f, 0.0f);
    //rcbrtf(1.0f);
    remainderf(2.0f, 1.0f);
    remquof(1.0f, 2.0f, &iX);
    //rhypotf(0.0f, 1.0f);
    ///rintf(1.0f);
    //rnorm3df(0.0f, 0.0f, 1.0f);
    //rnorm4df(0.0f, 0.0f, 0.0f, 1.0f);
    //fX = 1.0f; rnormf(1, &fX);
    roundf(0.0f);
    //rsqrtf(1.0f);
    ///scalblnf(0.0f, 1);
    scalbnf(0.0f, 1);
    signbit(1.0f);
    sincosf(0.0f, &fX, &fY);
    //sincospif(0.0f, &fX, &fY);
    sinf(0.0f);
    sinhf(0.0f);
    //sinpif(0.0f);
    sqrtf(0.0f);
    tanf(0.0f);
    tanhf(0.0f);
    tgammaf(2.0f);
    truncf(0.0f);
    ///y0f(1.0f);
    ///y1f(1.0f);
    ///ynf(1, 1.0f);
}
Пример #27
0
void
domathf (void)
{
#ifndef NO_FLOAT
  float f1;
  float f2;

  int i1;

  f1 = acosf(0.0);
  fprintf( stdout, "acosf          : %f\n", f1);

  f1 = acoshf(0.0);
  fprintf( stdout, "acoshf         : %f\n", f1);

  f1 = asinf(1.0);
  fprintf( stdout, "asinf          : %f\n", f1);

  f1 = asinhf(1.0);
  fprintf( stdout, "asinhf         : %f\n", f1);

  f1 = atanf(M_PI_4);
  fprintf( stdout, "atanf          : %f\n", f1);

  f1 = atan2f(2.3, 2.3);
  fprintf( stdout, "atan2f         : %f\n", f1);

  f1 = atanhf(1.0);
  fprintf( stdout, "atanhf         : %f\n", f1);

  f1 = cbrtf(27.0);
  fprintf( stdout, "cbrtf          : %f\n", f1);

  f1 = ceilf(3.5);
  fprintf( stdout, "ceilf          : %f\n", f1);

  f1 = copysignf(3.5, -2.5);
  fprintf( stdout, "copysignf      : %f\n", f1);

  f1 = cosf(M_PI_2);
  fprintf( stdout, "cosf           : %f\n", f1);

  f1 = coshf(M_PI_2);
  fprintf( stdout, "coshf          : %f\n", f1);

  f1 = erff(42.0);
  fprintf( stdout, "erff           : %f\n", f1);

  f1 = erfcf(42.0);
  fprintf( stdout, "erfcf          : %f\n", f1);

  f1 = expf(0.42);
  fprintf( stdout, "expf           : %f\n", f1);

  f1 = exp2f(0.42);
  fprintf( stdout, "exp2f          : %f\n", f1);

  f1 = expm1f(0.00042);
  fprintf( stdout, "expm1f         : %f\n", f1);

  f1 = fabsf(-1.123);
  fprintf( stdout, "fabsf          : %f\n", f1);

  f1 = fdimf(1.123, 2.123);
  fprintf( stdout, "fdimf          : %f\n", f1);

  f1 = floorf(0.5);
  fprintf( stdout, "floorf         : %f\n", f1);
  f1 = floorf(-0.5);
  fprintf( stdout, "floorf         : %f\n", f1);

  f1 = fmaf(2.1, 2.2, 3.01);
  fprintf( stdout, "fmaf           : %f\n", f1);

  f1 = fmaxf(-0.42, 0.42);
  fprintf( stdout, "fmaxf          : %f\n", f1);

  f1 = fminf(-0.42, 0.42);
  fprintf( stdout, "fminf          : %f\n", f1);

  f1 = fmodf(42.0, 3.0);
  fprintf( stdout, "fmodf          : %f\n", f1);

  /* no type-specific variant */
  i1 = fpclassify(1.0);
  fprintf( stdout, "fpclassify     : %d\n", i1);

  f1 = frexpf(42.0, &i1);
  fprintf( stdout, "frexpf         : %f\n", f1);

  f1 = hypotf(42.0, 42.0);
  fprintf( stdout, "hypotf         : %f\n", f1);

  i1 = ilogbf(42.0);
  fprintf( stdout, "ilogbf         : %d\n", i1);

  /* no type-specific variant */
  i1 = isfinite(3.0);
  fprintf( stdout, "isfinite       : %d\n", i1);

  /* no type-specific variant */
  i1 = isgreater(3.0, 3.1);
  fprintf( stdout, "isgreater      : %d\n", i1);

  /* no type-specific variant */
  i1 = isgreaterequal(3.0, 3.1);
  fprintf( stdout, "isgreaterequal : %d\n", i1);

  /* no type-specific variant */
  i1 = isinf(3.0);
  fprintf( stdout, "isinf          : %d\n", i1);

  /* no type-specific variant */
  i1 = isless(3.0, 3.1);
  fprintf( stdout, "isless         : %d\n", i1);

  /* no type-specific variant */
  i1 = islessequal(3.0, 3.1);
  fprintf( stdout, "islessequal    : %d\n", i1);

  /* no type-specific variant */
  i1 = islessgreater(3.0, 3.1);
  fprintf( stdout, "islessgreater  : %d\n", i1);

  /* no type-specific variant */
  i1 = isnan(0.0);
  fprintf( stdout, "isnan          : %d\n", i1);

  /* no type-specific variant */
  i1 = isnormal(3.0);
  fprintf( stdout, "isnormal       : %d\n", i1);

  /* no type-specific variant */
  f1 = isunordered(1.0, 2.0);
  fprintf( stdout, "isunordered    : %d\n", i1);

  f1 = j0f(1.2);
  fprintf( stdout, "j0f            : %f\n", f1);

  f1 = j1f(1.2);
  fprintf( stdout, "j1f            : %f\n", f1);

  f1 = jnf(2,1.2);
  fprintf( stdout, "jnf            : %f\n", f1);

  f1 = ldexpf(1.2,3);
  fprintf( stdout, "ldexpf         : %f\n", f1);

  f1 = lgammaf(42.0);
  fprintf( stdout, "lgammaf        : %f\n", f1);

  f1 = llrintf(-0.5);
  fprintf( stdout, "llrintf        : %f\n", f1);
  f1 = llrintf(0.5);
  fprintf( stdout, "llrintf        : %f\n", f1);

  f1 = llroundf(-0.5);
  fprintf( stdout, "lroundf        : %f\n", f1);
  f1 = llroundf(0.5);
  fprintf( stdout, "lroundf        : %f\n", f1);

  f1 = logf(42.0);
  fprintf( stdout, "logf           : %f\n", f1);

  f1 = log10f(42.0);
  fprintf( stdout, "log10f         : %f\n", f1);

  f1 = log1pf(42.0);
  fprintf( stdout, "log1pf         : %f\n", f1);

  f1 = log2f(42.0);
  fprintf( stdout, "log2f          : %f\n", f1);

  f1 = logbf(42.0);
  fprintf( stdout, "logbf          : %f\n", f1);

  f1 = lrintf(-0.5);
  fprintf( stdout, "lrintf         : %f\n", f1);
  f1 = lrintf(0.5);
  fprintf( stdout, "lrintf         : %f\n", f1);

  f1 = lroundf(-0.5);
  fprintf( stdout, "lroundf        : %f\n", f1);
  f1 = lroundf(0.5);
  fprintf( stdout, "lroundf        : %f\n", f1);

  f1 = modff(42.0,&f2);
  fprintf( stdout, "lmodff         : %f\n", f1);

  f1 = nanf("");
  fprintf( stdout, "nanf           : %f\n", f1);

  f1 = nearbyintf(1.5);
  fprintf( stdout, "nearbyintf     : %f\n", f1);

  f1 = nextafterf(1.5,2.0);
  fprintf( stdout, "nextafterf     : %f\n", f1);

  f1 = powf(3.01, 2.0);
  fprintf( stdout, "powf           : %f\n", f1);

  f1 = remainderf(3.01,2.0);
  fprintf( stdout, "remainderf     : %f\n", f1);

  f1 = remquof(29.0,3.0,&i1);
  fprintf( stdout, "remquof        : %f\n", f1);

  f1 = rintf(0.5);
  fprintf( stdout, "rintf          : %f\n", f1);
  f1 = rintf(-0.5);
  fprintf( stdout, "rintf          : %f\n", f1);

  f1 = roundf(0.5);
  fprintf( stdout, "roundf         : %f\n", f1);
  f1 = roundf(-0.5);
  fprintf( stdout, "roundf         : %f\n", f1);

  f1 = scalblnf(1.2,3);
  fprintf( stdout, "scalblnf       : %f\n", f1);

  f1 = scalbnf(1.2,3);
  fprintf( stdout, "scalbnf        : %f\n", f1);

  /* no type-specific variant */
  i1 = signbit(1.0);
  fprintf( stdout, "signbit        : %i\n", i1);

  f1 = sinf(M_PI_4);
  fprintf( stdout, "sinf           : %f\n", f1);

  f1 = sinhf(M_PI_4);
  fprintf( stdout, "sinhf          : %f\n", f1);

  f1 = sqrtf(9.0);
  fprintf( stdout, "sqrtf          : %f\n", f1);

  f1 = tanf(M_PI_4);
  fprintf( stdout, "tanf           : %f\n", f1);

  f1 = tanhf(M_PI_4);
  fprintf( stdout, "tanhf          : %f\n", f1);

  f1 = tgammaf(2.1);
  fprintf( stdout, "tgammaf        : %f\n", f1);

  f1 = truncf(3.5);
  fprintf( stdout, "truncf         : %f\n", f1);

  f1 = y0f(1.2);
  fprintf( stdout, "y0f            : %f\n", f1);

  f1 = y1f(1.2);
  fprintf( stdout, "y1f            : %f\n", f1);

  f1 = ynf(3,1.2);
  fprintf( stdout, "ynf            : %f\n", f1);
#endif
}
Пример #28
0
void test (float f, double d, long double ld)
{
  if (sqrt (0.0) != 0.0)
    link_error ();

  if (sqrt (1.0) != 1.0)
    link_error ();

  if (cbrt (0.0) != 0.0)
    link_error ();

  if (cbrt (1.0) != 1.0)
    link_error ();

  if (cbrt (-1.0) != -1.0)
    link_error ();

  if (exp (0.0) != 1.0)
    link_error ();

  if (exp (1.0) <= 2.71 || exp (1.0) >= 2.72)
    link_error ();

  if (log (1.0) != 0.0)
    link_error ();

  if (sin (0.0) != 0.0)
    link_error ();

  if (cos (0.0) != 1.0)
    link_error ();

  if (tan (0.0) != 0.0)
    link_error ();

  if (atan (0.0) != 0.0)
    link_error ();

  if (4.0*atan (1.0) <= 3.14 || 4.0*atan (1.0) >= 3.15)
    link_error ();

  if (pow (d, 0.0) != 1.0)
    link_error ();

  if (pow (1.0, d) != 1.0)
    link_error ();


  if (sqrtf (0.0F) != 0.0F)
    link_error ();

  if (sqrtf (1.0F) != 1.0F)
    link_error ();

  if (cbrtf (0.0F) != 0.0F)
    link_error ();

  if (cbrtf (1.0F) != 1.0F)
    link_error ();

  if (cbrtf (-1.0F) != -1.0F)
    link_error ();

  if (expf (0.0F) != 1.0F)
    link_error ();

  if (expf (1.0F) <= 2.71F || expf (1.0F) >= 2.72F)
    link_error ();

  if (logf (1.0F) != 0.0F)
    link_error ();

  if (sinf (0.0F) != 0.0F)
    link_error ();

  if (cosf (0.0F) != 1.0F)
    link_error ();

  if (tanf (0.0F) != 0.0F)
    link_error ();

  if (atanf (0.0F) != 0.0F)
    link_error ();

  if (4.0F*atanf (1.0F) <= 3.14F || 4.0F*atanf (1.0F) >= 3.15F)
    link_error ();

  if (powf (f, 0.0F) != 1.0F)
    link_error ();

  if (powf (1.0F, f) != 1.0F)
    link_error ();


  if (sqrtl (0.0L) != 0.0L)
    link_error ();

  if (sqrtl (1.0L) != 1.0L)
    link_error ();

  if (cbrtl (0.0L) != 0.0L)
    link_error ();

  if (cbrtl (1.0L) != 1.0L)
    link_error ();

  if (cbrtl (-1.0L) != -1.0L)
    link_error ();

  if (expl (0.0L) != 1.0L)
    link_error ();

  if (expl (1.0L) <= 2.71L || expl (1.0L) >= 2.72L)
    link_error ();

  if (logl (1.0L) != 0.0L)
    link_error ();

  if (sinl (0.0L) != 0.0L)
    link_error ();

  if (cosl (0.0L) != 1.0L)
    link_error ();

  if (tanl (0.0L) != 0.0L)
    link_error ();

  if (atanl (0.0) != 0.0L)
    link_error ();

  if (4.0L*atanl (1.0L) <= 3.14L || 4.0L*atanl (1.0L) >= 3.15L)
    link_error ();

  if (powl (ld, 0.0L) != 1.0L)
    link_error ();

  if (powl (1.0L, ld) != 1.0L)
    link_error ();
}
Пример #29
0
void mri_metrics_pp( MRI_IMAGE *imp , MRI_IMAGE *imq , float *met , byte *mmm )
{
   int nvox , nmmm=0 ;
   int *rst , *pst , *qst ;
   byte *par, *qar ;
   float qj,rij , rat,tmp,lrr,rm1,rp1 , fac ;
   float esum,tsum,hsum , jsum,dsum,xsum , qsum,asum ;
   register int ii,jj,kk ;
   MRI_IMAGE *imqq, *impp ;

   if( imp == NULL || imq == NULL            ) return ;
   if( met == NULL || imp->nvox != imq->nvox ) return ;

   nvox = imp->nvox ;

   impp = (imp->kind==MRI_byte) ? imp : mri_to_byte(imp) ;
   imqq = (imq->kind==MRI_byte) ? imq : mri_to_byte(imq) ;
   par  = MRI_BYTE_PTR(impp) ;
   qar  = MRI_BYTE_PTR(imqq) ;

   pst = (int *)calloc(256    ,sizeof(int)) ;
   qst = (int *)calloc(256    ,sizeof(int)) ;
   rst = (int *)calloc(256*256,sizeof(int)) ;

   if( mmm != NULL ){
     for( kk=0 ; kk < nvox ; kk++ ) if( mmm[kk] ) nmmm++ ;
     fac = 1.0f / nmmm ;
     for( kk=0 ; kk < nvox ; kk++ ){
       if( mmm[kk] == 0 ) continue ;
       ii = par[kk] ; jj = qar[kk] ;
       pst[ii]++ ; qst[jj]++ ; rst[ii+256*jj]++ ;
     }
   } else {
     fac = 1.0f / nvox ;
     for( kk=0 ; kk < nvox ; kk++ ){
       ii = par[kk] ; jj = qar[kk] ;
       pst[ii]++ ; qst[jj]++ ; rst[ii+256*jj]++ ;
     }
   }

   esum = tsum = hsum = 0.0f ;
   jsum = dsum = xsum = 0.0f ;
   qsum = asum =        0.0f ;
   for( jj=0 ; jj < 256 ; jj++ ){
     qj = (float)qst[jj] ;
     if( qj > 0.0f ){
       kk = 256*jj ; qj *= fac ;
       for( ii=0 ; ii < 256 ; ii++ ){
         rij = (float)rst[ii+kk] ;
         if( rij > 0.0f ){
           rat = (qj *(float)pst[ii]) / rij ;
           lrr = logf(rat) ;

           esum += rij * lrr ;
           tmp   = 1.0f/sqrtf(rat)-1.0f ; hsum += rij * tmp ;
           tmp   = 1.0f/cbrtf(rat)-1.0f ; qsum += rij * tmp ;
           tmp   = lrr*lrr*lrr          ; asum += rij * tmp ;
         }
       }
     }
   }

   free((void*)rst); free((void*)qst); free((void*)pst);
   if( impp != imp ) mri_free(impp);
   if( imqq != imq ) mri_free(imqq);

   met[0] = -fac * esum * 0.5f ;
   met[1] =  fac * hsum * 0.5f ;
   met[2] =  fac * qsum ;
   return ;
}
Пример #30
0
TEST(math, cbrtf) {
  ASSERT_FLOAT_EQ(3.0f, cbrtf(27.0f));
}