static GLUSvoid convertRGB(GLUSubyte* rgbe, const GLUSfloat* rgb) { GLUSfloat significant[3]; GLUSint exponent[3]; GLUSint maxExponent; significant[0] = frexpf(rgb[0], &exponent[0]); significant[1] = frexpf(rgb[1], &exponent[1]); significant[2] = frexpf(rgb[2], &exponent[2]); maxExponent = exponent[0]; if (exponent[1] > maxExponent) { maxExponent = exponent[1]; } if (exponent[2] > maxExponent) { maxExponent = exponent[2]; } rgbe[0] = (GLUSubyte)(significant[0] * 256.0f * powf(2.0f, (GLUSfloat)(exponent[0] - maxExponent))); rgbe[1] = (GLUSubyte)(significant[1] * 256.0f * powf(2.0f, (GLUSfloat)(exponent[1] - maxExponent))); rgbe[2] = (GLUSubyte)(significant[2] * 256.0f * powf(2.0f, (GLUSfloat)(exponent[2] - maxExponent))); rgbe[3] = (GLUSubyte)(maxExponent + 128); }
void runAddingQuantizer(LADSPA_Handle instance, unsigned long sampleCount) { Quantizer *q_instance = (Quantizer *) instance; // Get the input and output ports. The convention in all of the plugins I've read has been to assign these to local variables, instead of // accessing the instance struct members each time. LADSPA_Data *input = q_instance->inputPort, *output = q_instance->outputPort; // Get the gain for run_adding. This is used by the macro! LADSPA_Data runAddingGain = q_instance->runAddingGain; // Calculate the step size from the input value. float stepSize = (*(q_instance->reductionFactor) >= Q_FACTOR_LOWER && *(q_instance->reductionFactor) <= Q_FACTOR_UPPER) ? *(q_instance->reductionFactor) * FLOAT_STEP : Q_FACTOR_LOWER; // Calculation intermediate storage. int exponentContainer; float significand; // Performing the processing. for(int i = 0; i < sampleCount; i++) { significand = frexpf(input[i], &exponentContainer); // Extract the significand of the sample for quantization. significand = signum(significand) * floorf(fabs(significand)/stepSize + 0.5f) * stepSize; // Apply the quantization! buffer_write(output[i], ldexpf(significand, exponentContainer)); // Reapply the exponent and write the quantized value. } }
/* Convert a normal single-precision float into the 7.16 format * used by the R300 fragment shader. */ uint32_t pack_float24(float f) { union { float fl; uint32_t u; } u; float mantissa; int exponent; uint32_t float24 = 0; if (f == 0.0) return 0; u.fl = f; mantissa = frexpf(f, &exponent); /* Handle -ve */ if (mantissa < 0) { float24 |= (1 << 23); mantissa = mantissa * -1.0; } /* Handle exponent, bias of 63 */ exponent += 62; float24 |= (exponent << 16); /* Kill 7 LSB of mantissa */ float24 |= (u.u & 0x7FFFFF) >> 7; return float24; }
static inline int log2f_round(float x) { int exp; float norm = frexpf(x, &exp); if (norm >= M_SQRT1_2) return exp; return exp-1; }
int main(int argc, char *argv[]) { float x; int i; x = frexpf((float) argc, &i); return 0; }
void test_frexp() { int ip; static_assert((std::is_same<decltype(frexp((double)0, &ip)), double>::value), ""); static_assert((std::is_same<decltype(frexpf(0, &ip)), float>::value), ""); static_assert((std::is_same<decltype(frexpl(0, &ip)), long double>::value), ""); assert(frexp(0, &ip) == 0); }
static inline int log2f_ceil(float x) { int exp; float norm = frexpf(x, &exp); if (norm > 0.5f) return exp; return exp-1; }
cl_object cl_float_precision(cl_object x) { const cl_env_ptr the_env = ecl_process_env(); int precision; switch (ecl_t_of(x)) { case t_singlefloat: { float f = ecl_single_float(x); if (f == 0.0) { precision = 0; } else { int exp; frexpf(f, &exp); if (exp >= FLT_MIN_EXP) { precision = FLT_MANT_DIG; } else { precision = FLT_MANT_DIG - (FLT_MIN_EXP - exp); } } break; } case t_doublefloat: { double f = ecl_double_float(x); if (f == 0.0) { precision = 0; } else { int exp; frexp(f, &exp); if (exp >= DBL_MIN_EXP) { precision = DBL_MANT_DIG; } else { precision = DBL_MANT_DIG - (DBL_MIN_EXP - exp); } } break; } #ifdef ECL_LONG_FLOAT case t_longfloat: { long double f = ecl_long_float(x); if (f == 0.0) { precision = 0; } else { int exp; frexp(f, &exp); if (exp >= LDBL_MIN_EXP) { precision = LDBL_MANT_DIG; } else { precision = LDBL_MANT_DIG - (LDBL_MIN_EXP - exp); } } break; } #endif default: FEwrong_type_nth_arg(ecl_make_fixnum(/*FLOAT-PRECISION*/376),1,x,ecl_make_fixnum(/*FLOAT*/374)); } ecl_return1(the_env, ecl_make_fixnum(precision)); }
/* This is a portable implementation of nextafterf that is intended to be independent of the floating point format or its in memory representation. This implementation works correctly with denormalized values. */ float nextafterf(float x, float y) { /* This variable is marked volatile to avoid excess precision problems on some platforms, including IA-32. */ volatile float delta; float absx, denorm_min; if (isnan(x) || isnan(y)) return x + y; if (x == y) return x; if (!isfinite (x)) return x > 0 ? __FLT_MAX__ : - __FLT_MAX__; /* absx = fabsf (x); */ absx = (x < 0.0) ? -x : x; /* __FLT_DENORM_MIN__ is non-zero iff the target supports denormals. */ if (__FLT_DENORM_MIN__ == 0.0f) denorm_min = __FLT_MIN__; else denorm_min = __FLT_DENORM_MIN__; if (absx < __FLT_MIN__) delta = denorm_min; else { float frac; int exp; /* Discard the fraction from x. */ frac = frexpf (absx, &exp); delta = scalbnf (0.5f, exp); /* Scale x by the epsilon of the representation. By rights we should have been able to combine this with scalbnf, but some targets don't get that correct with denormals. */ delta *= __FLT_EPSILON__; /* If we're going to be reducing the absolute value of X, and doing so would reduce the exponent of X, then the delta to be applied is one exponent smaller. */ if (frac == 0.5f && (y < x) == (x > 0)) delta *= 0.5f; /* If that underflows to zero, then we're back to the minimum. */ if (delta == 0.0f) delta = denorm_min; } if (y < x) delta = -delta; return x + delta; }
float nextafterf(const float x, const float y) { int origexp, newexp; #ifdef isnan if (isnan(x) || isnan(y)) return x + y; #endif if (x == y) return x; if (x == 0.0f) return y > 0.0 ? FLT_MIN : -FLT_MIN; frexpf(x, &origexp); if (x >= 0.0f) { if (y > x) { if (x < FLT_MIN) return FLT_MIN; return x + scalbnf(FLT_EPSILON, origexp - 1); } else if (x > FLT_MIN) { float temp = x - scalbnf(FLT_EPSILON, origexp - 1); frexpf(temp, &newexp); if (newexp == origexp) return temp; return x - scalbnf(FLT_EPSILON, origexp - 2); } else return 0.0f; } else { if (y < x) { if (x > -FLT_MIN) return -FLT_MIN; return x - scalbnf(FLT_EPSILON, origexp - 1); } else if (x < -FLT_MIN) { float temp = x + scalbnf(FLT_EPSILON, origexp - 1); frexpf(temp, &newexp); if (newexp == origexp) return temp; return x + scalbnf(FLT_EPSILON, origexp - 2); } else return 0.0f; } }
cl_object cl_decode_float(cl_object x) { const cl_env_ptr the_env = ecl_process_env(); int e, s; cl_type tx = ecl_t_of(x); float f; switch (tx) { case t_singlefloat: { f = ecl_single_float(x); if (f >= 0.0) { s = 1; } else { f = -f; s = 0; } f = frexpf(f, &e); x = ecl_make_single_float(f); break; } case t_doublefloat: { double d = ecl_double_float(x); if (d >= 0.0) { s = 1; } else { d = -d; s = 0; } d = frexp(d, &e); x = ecl_make_double_float(d); break; } #ifdef ECL_LONG_FLOAT case t_longfloat: { long double d = ecl_long_float(x); if (d >= 0.0) s = 1; else { d = -d; s = 0; } d = frexpl(d, &e); x = ecl_make_long_float(d); break; } #endif default: FEwrong_type_nth_arg(ecl_make_fixnum(/*DECODE-FLOAT*/275),1,x,ecl_make_fixnum(/*FLOAT*/374)); } ecl_return3(the_env, x, ecl_make_fixnum(e), ecl_make_single_float(s)); }
float logarithmf(float x, int ten) { int N; float f, w, z; /* Check for domain/range errors here. */ if (x == 0.0) { errno = ERANGE; return (-z_infinity_f.f); } else if (x < 0.0) { errno = EDOM; return (z_notanum_f.f); } else if (!isfinite(x)) { if (isnanf(x)) return (z_notanum_f.f); else return (z_infinity_f.f); } /* Get the exponent and mantissa where x = f * 2^N. */ f = frexpf(x, &N); z = f - 0.5; if (f > __SQRT_HALF) z = (z - 0.5) / (f * 0.5 + 0.5); else { N--; z /= (z * 0.5 + 0.5); } w = z * z; /* Use Newton's method with 4 terms. */ z += z * w * (a[0]) / ((w + 1.0) * w + b[0]); if (N != 0) z = (N * C2 + z) + N * C1; if (ten) z *= C3; return (z); }
/* side effect: changes *lsp to cosines of lsp */ void vorbis_lsp_to_curve(float *curve,int *map,int n,int ln,float *lsp,int m, float amp,float ampoffset){ int i; float wdel=M_PI/ln; vorbis_fpu_control fpu; vorbis_fpu_setround(&fpu); for(i=0;i<m;i++)lsp[i]=vorbis_coslook(lsp[i]); i=0; while(i<n){ int k=map[i]; int qexp; float p=.7071067812f; float q=.7071067812f; float w=vorbis_coslook(wdel*k); float *ftmp=lsp; int c=m>>1; do{ q*=ftmp[0]-w; p*=ftmp[1]-w; ftmp+=2; }while(--c); if(m&1){ /* odd order filter; slightly assymetric */ /* the last coefficient */ q*=ftmp[0]-w; q*=q; p*=p*(1.f-w*w); }else{ /* even order filter; still symmetric */ q*=q*(1.f+w); p*=p*(1.f-w); } q=frexpf(p+q,&qexp); q=vorbis_fromdBlook(amp* vorbis_invsqlook(q)* vorbis_invsq2explook(qexp+m)- ampoffset); do{ curve[i++]*=q; }while(map[i]==k); } vorbis_fpu_restore(fpu); }
ATF_TC_BODY(fpclassify_float, tc) { float d0, d1, d2, f, ip; int e, i; d0 = FLT_MIN; ATF_REQUIRE_EQ(fpclassify(d0), FP_NORMAL); f = frexpf(d0, &e); ATF_REQUIRE_EQ(e, FLT_MIN_EXP); ATF_REQUIRE_EQ(f, 0.5); d1 = d0; /* shift a "1" bit through the mantissa (skip the implicit bit) */ for (i = 1; i < FLT_MANT_DIG; i++) { d1 /= 2; ATF_REQUIRE_EQ(fpclassify(d1), FP_SUBNORMAL); ATF_REQUIRE(d1 > 0 && d1 < d0); d2 = ldexpf(d0, -i); ATF_REQUIRE_EQ(d2, d1); d2 = modff(d1, &ip); ATF_REQUIRE_EQ(d2, d1); ATF_REQUIRE_EQ(ip, 0); f = frexpf(d1, &e); ATF_REQUIRE_EQ(e, FLT_MIN_EXP - i); ATF_REQUIRE_EQ(f, 0.5); } d1 /= 2; ATF_REQUIRE_EQ(fpclassify(d1), FP_ZERO); f = frexpf(d1, &e); ATF_REQUIRE_EQ(e, 0); ATF_REQUIRE_EQ(f, 0); }
// http://www.graphics.cornell.edu/online/formats/rgbe/ bool write_rgbe(char const* const path, Image const& image) { FILE* const out = fopen(path, "wb"); if (!out) { return false; } fprintf(out, "#?RADIANCE\n"); fprintf(out, "GAMMA=%g\n", 1.0); fprintf(out, "EXPOSURE=%g\n", 1.0); fprintf(out, "FORMAT=32-bit_rle_rgbe\n"); fprintf(out, "\n"); int const width = image.width; int const height = image.height; fprintf(out, "-Y %d +X %d\n", height, width); for (int y = 0; y < height; ++y) { RGB const* scanline = image.pixels + y * width; for (int x = 0; x < width; ++x) { RGB const rgb = scanline[x]; float const dominant = fmaxf(rgb.r, fmaxf(rgb.g, rgb.b)); RGBE rgbe; if (dominant < 1e-32) { rgbe.r = rgbe.g = rgbe.b = rgbe.e = 0; } else { int exponent = INT_MIN; float const significand = frexpf(dominant, &exponent); float const scale = significand * 256.f / dominant; rgbe.r = static_cast<uint8_t>(scale * rgb.r); rgbe.g = static_cast<uint8_t>(scale * rgb.g); rgbe.b = static_cast<uint8_t>(scale * rgb.b); rgbe.e = static_cast<uint8_t>(exponent + 128); } fwrite(&rgbe, sizeof(rgbe), 1, out); } } fclose(out); return true; }
// Value encoders/decoders int mb_write_float(unsigned char *buf, float val) { if(!buf) return(MB_ENOBUF); // Convert native single-precision float into MODBus format (see 2.11.1) bool s=signbit(val); int e; float fm=frexpf(val, &e); // get mantissa and exponent e+=126; // bias the exponent uint32_t m=floor(fm*(1<<24)); // get mantissa bits // write the four (shuffled) bytes buf[0]=(m>>8)&0xff; buf[1]=m&0xff; buf[2]=(s?0x80:0)|((e>>1)&0x7f); buf[3]=((e&1)<<7)|((m>>16)&0x7f); return(MB_EOK); }
packed_colour::packed_colour(const V3 &col) { float d; d = std::max(std::max(col.r, col.g), col.b); r = g = b = e = 0; if (d > 1e-30f) { int exp; d = frexpf(d, &exp) * 255.0f / d; if (col.r > 0.0) r = (uint8_t)(col.r * d + 0.5f); if (col.g > 0.0) g = (uint8_t)(col.g * d + 0.5f); if (col.b > 0.0) b = (uint8_t)(col.b * d + 0.5f); e = (uint8_t)exp + excess; } }
void buzzmsg_serialize_float(buzzdarray_t buf, float data) { /* The mantissa */ int32_t mant; /* The exponent */ int32_t exp; /* * Split the data into a normalized fraction (nf) and an integral power of 2 * when data == 0, nf = 0 and exp = 0 * otherwise data = nf * 2^exp and nf is in +-[0.5,1) */ float nf = frexpf(data, &exp); /* Take the magnitude of nf and translate it by -0.5 */ nf = fabsf(nf) - 0.5f; /* Now we can safely test for zero */ if(nf < 0.0f) { /* The mantissa is zero */ mant = 0; } else { /* Let's calculate the mantissa * * Idea: * nf now is in [0,0.5), we want to map it to the entire range of uint32_t * * 1. multiply nf by 2 so it's in [0,1) * 2. multiply the result by the maximum value for the mantissa * 3. problem: 0 maps to 0 and it would indistinguishable from nf == 0 above * solution: simply sum 1 to the mantissa */ mant = (int32_t)(nf * 2.0f * MAX_MANTISSA) + 1; /* Now take care of the sign */ if(data < 0.0f) mant = -mant; } /* Serialize the data */ buzzmsg_serialize_u32(buf, mant); buzzmsg_serialize_u32(buf, exp); }
/// F2H: Convert a floating point number to half-float inline HALF F2H(FLOAT in) { bool negative = (in < 0); int exponent; int mantissa = 2048.0 * frexpf((negative)?(-in):(in),&exponent); if (mantissa == 0) { // Zero and -Zero return (negative)?(0x8000):(0x0000); } /* ** this would be the fixup code. mantissa <<= 1; // shift into explicit one-bit exponent--; // normalize the exponent correspondingly. */ exponent += 14; // Exponent bias is 15. If this is too large to represent, generate INFs if (exponent >= 31) { return (negative)?(0xfc00):(0x7c00); } else if (exponent < 1) { // Denormalize the number. while(exponent < 1) { exponent++; mantissa >>= 1; } // This is represented with the exponent zero. exponent = 0; } // // Now combine the results into one value. Remove the implicit one, // and shift the exponent in place mantissa = (mantissa & 0x03ff) | (exponent << 10); if (negative) mantissa |= 0x8000; return mantissa; }
gain_minifloat_t gain_from_float(float v) { if (std::isnan(v) || v <= 0.0f) { return 0; } if (v >= 2.0f) { return MINIFLOAT_MAX; } int exp; float r = frexpf(v, &exp); if ((exp += EXCESS) > EXPONENT_MAX) { return MINIFLOAT_MAX; } if (-exp >= MANTISSA_BITS) { return 0; } int mantissa = (int) (r * ONE_FLOAT); return exp > 0 ? (exp << MANTISSA_BITS) | (mantissa & ~HIDDEN_BIT) : (mantissa >> (1 - exp)) & MANTISSA_MAX; }
float cbrtf (float x) { int e, rem, sign; float z; if (!isfinite (x) || x == 0.0F) return x; if (x > 0) sign = 1; else { sign = -1; x = -x; } z = x; /* extract power of 2, leaving * mantissa between 0.5 and 1 */ x = frexpf(x, &e); /* Approximate cube root of number between .5 and 1, * peak relative error = 9.2e-6 */ x = (((-0.13466110473359520655053 * x + 0.54664601366395524503440 ) * x - 0.95438224771509446525043 ) * x + 1.1399983354717293273738 ) * x + 0.40238979564544752126924; /* exponent divided by 3 */ if (e >= 0) { rem = e; e /= 3; rem -= 3*e; if (rem == 1) x *= CBRT2; else if (rem == 2) x *= CBRT4; } /* argument less than 1 */ else { e = -e; rem = e; e /= 3; rem -= 3*e; if (rem == 1) x /= CBRT2; else if (rem == 2) x /= CBRT4; e = -e; } /* multiply by power of 2 */ x = ldexpf(x, e); /* Newton iteration */ x -= ( x - (z/(x*x)) ) * 0.333333333333; if (sign < 0) x = -x; return (x); }
float cephes_logf( float xx ) { register float y; float x, z, fe; int e; x = xx; fe = 0.0; /* Test for domain */ if( x <= 0.0 ) { // ERROR return( MINLOGF ); } x = frexpf( x, &e ); // printf("\nmy_logf: frexp -> e = %d x = %g\n", e, x); if( x < SQRTHF ) { e -= 1; x = x + x - 1.0; /* 2x - 1 */ } else { x = x - 1.0; } z = x * x; /* 3.4e-9 */ /* p = logfcof; y = *p++ * x; for( i=0; i<8; i++ ) { y += *p++; y *= x; } y *= z; */ y = (((((((( 7.0376836292E-2f * x - 1.1514610310E-1f) * x + 1.1676998740E-1f) * x - 1.2420140846E-1f) * x + 1.4249322787E-1f) * x - 1.6668057665E-1f) * x + 2.0000714765E-1f) * x - 2.4999993993E-1f) * x + 3.3333331174E-1f) * x * z; // printf("my_logf: poly = %g\n", y); if( e ) { fe = e; y += -2.12194440e-4f * fe; } y += -0.5 * z; /* y - 0.5 x^2 */ // printf("my_logf: x = %g y = %g\n", x, y); z = x + y; /* ... + x */ if( e ) z += 0.693359375f * fe; return( z ); }
static int32_t Add_vstate_render_voice( Voice_state* vstate, Proc_state* proc_state, const Device_thread_state* proc_ts, const Au_state* au_state, const Work_buffers* wbs, int32_t buf_start, int32_t buf_stop, double tempo) { rassert(vstate != NULL); rassert(proc_state != NULL); rassert(proc_ts != NULL); rassert(au_state != NULL); rassert(wbs != NULL); rassert(tempo > 0); const Device_state* dstate = &proc_state->parent; const Proc_add* add = (Proc_add*)proc_state->parent.device->dimpl; Add_vstate* add_state = (Add_vstate*)vstate; rassert(is_p2(ADD_BASE_FUNC_SIZE)); // Get frequencies Work_buffer* freqs_wb = Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PITCH); Work_buffer* pitches_wb = freqs_wb; if (freqs_wb == NULL) freqs_wb = Work_buffers_get_buffer_mut(wbs, ADD_WORK_BUFFER_FIXED_PITCH); Proc_fill_freq_buffer(freqs_wb, pitches_wb, buf_start, buf_stop); const float* freqs = Work_buffer_get_contents(freqs_wb); // Get volume scales Work_buffer* scales_wb = Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_FORCE); Work_buffer* dBs_wb = scales_wb; if (scales_wb == NULL) scales_wb = Work_buffers_get_buffer_mut(wbs, ADD_WORK_BUFFER_FIXED_FORCE); Proc_fill_scale_buffer(scales_wb, dBs_wb, buf_start, buf_stop); const float* scales = Work_buffer_get_contents(scales_wb); // Get output buffer for writing float* out_bufs[2] = { NULL }; Proc_state_get_voice_audio_out_buffers( proc_ts, PORT_OUT_AUDIO_L, PORT_OUT_COUNT, out_bufs); // Get phase modulation signal const Work_buffer* mod_wbs[] = { Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PHASE_MOD_L), Device_thread_state_get_voice_buffer( proc_ts, DEVICE_PORT_TYPE_RECV, PORT_IN_PHASE_MOD_R), }; for (int ch = 0; ch < 2; ++ch) { if (mod_wbs[ch] == NULL) { Work_buffer* zero_buf = Work_buffers_get_buffer_mut( wbs, (Work_buffer_type)(ADD_WORK_BUFFER_MOD_L + ch)); Work_buffer_clear(zero_buf, buf_start, buf_stop); mod_wbs[ch] = zero_buf; } } // Add base waveform tones const double inv_audio_rate = 1.0 / dstate->audio_rate; const float* base = Sample_get_buffer(add->base, 0); for (int h = 0; h < add_state->tone_limit; ++h) { const Add_tone* tone = &add->tones[h]; const double pitch_factor = tone->pitch_factor; const double volume_factor = tone->volume_factor; if ((pitch_factor <= 0) || (volume_factor <= 0)) continue; const double pannings[] = { -tone->panning, tone->panning, }; const double pitch_factor_inv_audio_rate = pitch_factor * inv_audio_rate; Add_tone_state* tone_state = &add_state->tones[h]; for (int32_t ch = 0; ch < 2; ++ch) { float* out_buf_ch = out_bufs[ch]; if (out_buf_ch == NULL) continue; const double panning_factor = 1 + pannings[ch]; const float* mod_values_ch = Work_buffer_get_contents(mod_wbs[ch]); double phase = tone_state->phase[ch]; int32_t res_slice_start = buf_start; while (res_slice_start < buf_stop) { int32_t res_slice_stop = buf_stop; // Get current pitch range const float first_mod_shift = mod_values_ch[res_slice_start] - add_state->prev_mod[ch]; const float first_phase_shift_abs = (float)fabs( first_mod_shift + (freqs[res_slice_start] * pitch_factor_inv_audio_rate)); int shift_exp = 0; const float shift_norm = frexpf(first_phase_shift_abs, &shift_exp); const float min_phase_shift_abs = ldexpf(0.5f, shift_exp); const float max_phase_shift_abs = min_phase_shift_abs * 2.0f; // Choose appropriate waveform resolution for current pitch range int32_t cur_size = ADD_BASE_FUNC_SIZE; if (isfinite(shift_norm) && (shift_norm > 0.0f)) { cur_size = (int32_t)ipowi(2, max(-shift_exp + 1, 3)); cur_size = min(cur_size, ADD_BASE_FUNC_SIZE * 2); rassert(is_p2(cur_size)); } const uint32_t cur_size_mask = (uint32_t)cur_size - 1; const int base_offset = (ADD_BASE_FUNC_SIZE * 4 - cur_size * 2); rassert(base_offset >= 0); rassert(base_offset < (ADD_BASE_FUNC_SIZE * 4) - 1); const float* cur_base = base + base_offset; // Get length of input compatible with current waveform resolution const int32_t res_check_stop = min(res_slice_stop, max(Work_buffer_get_const_start(freqs_wb), Work_buffer_get_const_start(mod_wbs[ch])) + 1); for (int32_t i = res_slice_start + 1; i < res_check_stop; ++i) { const float cur_mod_shift = mod_values_ch[i] - mod_values_ch[i - 1]; const float cur_phase_shift_abs = (float)fabs( cur_mod_shift + (freqs[i] * pitch_factor_inv_audio_rate)); if (cur_phase_shift_abs < min_phase_shift_abs || cur_phase_shift_abs > max_phase_shift_abs) { res_slice_stop = i; break; } } for (int32_t i = res_slice_start; i < res_slice_stop; ++i) { const float freq = freqs[i]; const float vol_scale = scales[i]; const float mod_val = mod_values_ch[i]; // Note: + mod_val is specific to phase modulation const double actual_phase = phase + mod_val; const double pos = actual_phase * cur_size; // Note: direct cast of negative doubles to uint32_t is undefined const uint32_t pos1 = (uint32_t)(int32_t)floor(pos) & cur_size_mask; const uint32_t pos2 = (pos1 + 1) & cur_size_mask; const float item1 = cur_base[pos1]; const float item_diff = cur_base[pos2] - item1; const double lerp_val = pos - floor(pos); const double value = (item1 + (lerp_val * item_diff)) * volume_factor * panning_factor; out_buf_ch[i] += (float)value * vol_scale; phase += freq * pitch_factor_inv_audio_rate; // Normalise to range [0, 1) if (phase >= 1) { phase -= 1; // Don't bother updating the phase if our frequency is too high if (phase >= 1) phase = tone_state->phase[ch]; } } rassert(res_slice_start < res_slice_stop); add_state->prev_mod[ch] = mod_values_ch[res_slice_stop - 1]; res_slice_start = res_slice_stop; } tone_state->phase[ch] = phase; } } if (add->is_ramp_attack_enabled) Proc_ramp_attack(vstate, 2, out_bufs, buf_start, buf_stop, dstate->audio_rate); return buf_stop; }
sl_def(do_test, void) { #define x values[p].d #define xf values[p].f #define y values[p+1].d #define yf values[p+1].f #define n values[p+2].i #define nl values[p+2].l #define call1(F) \ values[p].desc = #F; \ values[p].d = F(x); \ ++p; \ values[p].desc = #F "f"; \ values[p].f = F ## f (xf); \ ++p #define call1i(F) \ values[p].desc = #F; \ values[p].i = F(x); \ ++p #define call2(F) \ values[p].desc = #F; \ values[p].d = F(x, y); \ p += 2; \ values[p].desc = #F "f"; \ values[p].f = F ## f (xf, yf); \ p += 2 #define call2i(F) \ values[p].desc = #F; \ values[p].i = F(x, y); \ p += 2; \ values[p].desc = #F "f"; \ values[p].i = F(xf, yf); \ p += 2 /* classify */ call1i(fpclassify); call1i(signbit); call1i(isfinite); call1i(isnormal); call1i(isnan); call1i(isinf); /* trig */ call1(acos); call1(asin); call1(atan); call2(atan2); call1(cos); call1(sin); call1(tan); /* hyperbolic */ call1(acosh); call1(asinh); call1(atanh); call1(cosh); call1(sinh); call1(tanh); /* exp/log */ call1(exp); call1(exp2); call1(expm1); values[p].desc = "frexp"; values[p].d = frexp(x, &values[p+1].i); p += 2; values[p].desc = "frexpf"; values[p].f = frexpf(xf, &values[p+1].i); p += 2; values[p].desc = "ilogb"; values[p].i = ilogb(x); p++; values[p].desc = "ilogbf"; values[p].i = ilogbf(xf); p++; values[p].desc = "ldexp"; values[p].d = ldexp(x, n); p+=3; values[p].desc = "ldexpf"; values[p].f = ldexpf(xf, n); p+=3; call1(log); call1(log10); call1(log1p); call1(log2); call1(logb); values[p].desc = "modf"; values[p].d = modf(x, &y); p += 2; values[p].desc = "modff"; values[p].f = modff(xf, &yf); p += 2; values[p].desc = "scalbn"; values[p].d = scalbn(x, n); p+=3; values[p].desc = "scalbnf"; values[p].f = scalbnf(xf, n); p+=3; values[p].desc = "scalbln"; values[p].d = scalbln(x, nl); p+=3; values[p].desc = "scalblnf"; values[p].f = scalblnf(xf, nl); p+=3; /* power/abs */ call1(cbrt); call1(fabs); call2(hypot); call2(pow); call1(sqrt); /* error/gamma */ call1(erf); call1(erfc); call1(lgamma); call1(tgamma); call1(ceil); call1(floor); call1(nearbyint); call1(rint); call1(lrint); values[p].desc = "lrint"; values[p].l = lrint(x); p++; values[p].desc = "lrintf"; values[p].l = lrintf(xf); p++; values[p].desc = "llrint"; values[p].ll = llrint(x); p++; values[p].desc = "llrintf"; values[p].ll = llrintf(xf); p++; call1(round); values[p].desc = "lround"; values[p].l = lround(x); p++; values[p].desc = "lroundf"; values[p].l = lroundf(xf); p++; values[p].desc = "llround"; values[p].ll = llround(x); p++; values[p].desc = "llroundf"; values[p].ll = llroundf(xf); p++; call1(trunc); /* rem/mod */ call2(fmod); call2(remainder); values[p].desc = "remquo"; values[p].d = remquo(x, y, &values[p+1].i); p+=2; values[p].desc = "remquof"; values[p].f = remquof(xf, yf, &values[p+1].i); p+=2; call2(copysign); /* nan */ values[p].desc = "nan"; values[p].d = nan(""); ++p; values[p].desc = "nanf"; values[p].f = nanf(""); ++p; call2(nextafter); /* min/max/dim */ call2(fdim); call2(fmax); call2(fmin); values[p].desc = "fma"; values[p].d = fma(x, y, values[p+2].d); p+=3; values[p].desc = "fmaf"; values[p].d = fmaf(xf, yf, values[p+2].f); p+=3; /* comp */ call2i(isgreater); call2i(isgreaterequal); call2i(isless); call2i(islessequal); call2i(islessgreater); call2i(isunordered); #undef x #undef xf #undef y #undef n }
static long next_pow2(long x) { int p; float v = frexpf((float)x, &p); return (long)ldexpf(ceilf(2.0*v),p-1); }
static long ilog2(unsigned long x) { int p; float v = frexpf((float)x, &p); return (v > 0.5 ? p : p-1); }
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 }
static void callback(reg_id_t reg, int displacement, reg_id_t destReg, int opcode, app_pc addr){ int r, s; const char * destRegName = get_register_name(destReg); int regId = atoi(destRegName + 3 * sizeof(char)); dr_mcontext_t mcontext; memset(&mcontext, 0, sizeof(dr_mcontext_t)); mcontext.flags = DR_MC_ALL; mcontext.size = sizeof(dr_mcontext_t); bool result = dr_get_mcontext(dr_get_current_drcontext(), &mcontext); reg_t mem_reg; if(reg == DR_REG_RAX) mem_reg = mcontext.rax; else if(reg == DR_REG_RBP) mem_reg = mcontext.rbp; else if(reg == DR_REG_RBX) mem_reg = mcontext.rbx; else if(reg == DR_REG_RCX) mem_reg = mcontext.rcx; else if(reg == DR_REG_RDI) mem_reg = mcontext.rdi; else if(reg == DR_REG_RDX) mem_reg = mcontext.rdx; else if(reg == DR_REG_RSI) mem_reg = mcontext.rsi; else if(reg == DR_REG_RSP) mem_reg = mcontext.rsp; else mem_reg = NULL; //deal with a null case, rip enum doesn't exist int bits = 0; double loss = 0; double lossD = 0; if(is_single_precision_instr(opcode)){ float op1, op2; // printf("Mem reg contents: %f\n", *(float*)(mem_reg + displacement)); op2 = *(float*)(mem_reg + displacement); // for(r=0; r<16; ++r) // for(s=0; s<4; ++s) // printf("reg %i.%i: %f\n", r, s, // *((float*) &mcontext.ymm[r].u32[s])); op1 = *((float*) &mcontext.ymm[regId].u32[0]); // dr_fprintf(logF, "%d: %f %f\n",opcode, op1, op2); int exp1, exp2; float mant1, mant2; mant1 = frexpf(op1, &exp1); mant2 = frexpf(op2, &exp2); bits = abs(exp1-exp2); double dop1 = op1; double dop2 = op2; if(opcode == OP_addss){ double dadd = dop1 + dop2; float fadd = op1 + op2; lossD = dadd - fadd; //printf("double %.13lf float %.13f\n", dadd, fadd); } else{ double dsub = dop1 - dop2; float fsub = op1 - op2; lossD = dsub - fsub; } // printf("diff of double and float is %.13lf\n", lossD); } else{ double op1, op2; printf("Mem reg contents: %.13lf\n", *(double*)(mem_reg + displacement)); op2 = *(double*)(mem_reg + displacement); // for(r=0; r<16; ++r) // for(s=0; s<2; ++s) // printf("reg %i.%i: %lf\n", r, s, // *((double*) &mcontext.ymm[r].u64[s])); op1 = *((double*) &mcontext.ymm[regId].u64[0]); // dr_fprintf(logF, "%d: %.13lf %.13lf\n",opcode, op1, op2); int exp1, exp2; double mant1, mant2; mant1 = frexp(op1, &exp1); mant2 = frexp(op2, &exp2); bits = abs(exp1-exp2); printf("op1 %.13lf mantissa %.13lf exp %d\n", op1, mant1, exp1); printf("op2 %.13lf mantissa %.13lf exp %d\n", op2, mant2, exp2); } print_address(addr, bits, loss, lossD); }
static void getRegReg(reg_id_t r1, reg_id_t r2, int opcode, app_pc addr){ const char * r1Name = get_register_name(r1); const char * r2Name = get_register_name(r2); int s1 = atoi(r1Name + 3 * sizeof(char)); int s2 = atoi(r2Name + 3 * sizeof(char)); dr_mcontext_t mcontext; memset(&mcontext, 0, sizeof(dr_mcontext_t)); mcontext.flags = DR_MC_MULTIMEDIA; mcontext.size = sizeof(dr_mcontext_t); bool result = dr_get_mcontext(dr_get_current_drcontext(), &mcontext); int r, s; int bits = 0; double loss = 0; double lossD = 0; if(is_single_precision_instr(opcode)){ float op1, op2; // for(r=0; r<16; ++r) // for(s=0; s<4; ++s) // printf("reg %i.%i: %f\n", r, s, // *((float*) &mcontext.ymm[r].u32[s])); op1 = *((float*) &mcontext.ymm[s1].u32[0]); op2 = *((float*) &mcontext.ymm[s2].u32[0]); // dr_fprintf(logF, "%d: %f %f\n",opcode, op1, op2); int exp1, exp2; float mant1, mant2; mant1 = frexpf(op1, &exp1); mant2 = frexpf(op2, &exp2); bits = abs(exp1-exp2); double dop1 = op1; double dop2 = op2; if(opcode == OP_addss){ double dadd = dop1 + dop2; float fadd = op1 + op2; lossD = dadd - fadd; // printf("double %.13lf float %.13f\n", dadd, fadd); } else{ double dsub = dop1 - dop2; float fsub = op1 - op2; lossD = dsub - fsub; } // printf("diff of double and float is %.13lf\n", lossD); } else{ double op1, op2; // for(r=0; r<16; ++r) // for(s=0; s<2; ++s) // printf("reg %i.%i: %f\n", r, s, // *((double*) &mcontext.ymm[r].u64[s])); op1 = *((double*) &mcontext.ymm[s1].u64[0]); op2 = *((double*) &mcontext.ymm[s2].u64[0]); // dr_fprintf(logF, "%d: %.13lf %.13lf\n",opcode, op1, op2); int exp1, exp2; double mant1, mant2; mant1 = frexp(op1, &exp1); mant2 = frexp(op2, &exp2); bits = abs(exp1-exp2); printf("op1 %.13lf mantissa %.13lf exp %d\n", op1, mant1, exp1); printf("op2 %.13lf mantissa %.13lf exp %d\n", op2, mant2, exp2); } print_address(addr, bits, loss, lossD); }
cl_object cl_integer_decode_float(cl_object x) { const cl_env_ptr the_env = ecl_process_env(); int e, s = 1; switch (ecl_t_of(x)) { #ifdef ECL_LONG_FLOAT case t_longfloat: { long double d = ecl_long_float(x); if (signbit(d)) { s = -1; d = -d; } if (d == 0.0) { e = 0; x = ecl_make_fixnum(0); } else { d = frexpl(d, &e); x = _ecl_long_double_to_integer(ldexpl(d, LDBL_MANT_DIG)); e -= LDBL_MANT_DIG; } break; } #endif case t_doublefloat: { double d = ecl_double_float(x); if (signbit(d)) { s = -1; d = -d; } if (d == 0.0) { e = 0; x = ecl_make_fixnum(0); } else { d = frexp(d, &e); x = _ecl_double_to_integer(ldexp(d, DBL_MANT_DIG)); e -= DBL_MANT_DIG; } break; } case t_singlefloat: { float d = ecl_single_float(x); if (signbit(d)) { s = -1; d = -d; } if (d == 0.0) { e = 0; x = ecl_make_fixnum(0); } else { d = frexpf(d, &e); x = _ecl_double_to_integer(ldexp(d, FLT_MANT_DIG)); e -= FLT_MANT_DIG; } break; } default: FEwrong_type_nth_arg(ecl_make_fixnum(/*INTEGER-DECODE-FLOAT*/438),1,x,ecl_make_fixnum(/*FLOAT*/374)); } ecl_return3(the_env, x, ecl_make_fixnum(e), ecl_make_fixnum(s)); }