/*Returns a uniform random float. The expected value is within FLT_MIN (e.g., 1E-37) of 0.5. _bits: An initial set of random bits. _base: This should be -(the number of bits in _bits), up to -32. Return: A float uniformly distributed between 0 (inclusive) and 1 (exclusive). The average value was measured over 2**32 samples to be 0.50000037448772916.*/ static float isaac_float_bits(isaac_ctx *_ctx,uint32_t _bits,int _base){ float ret; int nbits_needed; while(!_bits){ if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0; _base-=32; _bits=isaac_next_uint32(_ctx); } /*Note: This could also be determined with frexp(), for a slightly more portable solution, but that takes twice as long, and one has to worry about rounding effects, which can over-estimate the exponent when given FLT_MANT_DIG+1 consecutive one bits. Even the fallback C implementation of ILOGNZ_32() yields an implementation 25% faster than the frexp() method.*/ nbits_needed=FLT_MANT_DIG-ilog32_nz(_bits); #if FLT_MANT_DIG>32 ret=ldexpf((float)_bits,_base); # if FLT_MANT_DIG>65 while(32-nbits_needed<0){ # else if(32-nbits_needed<0){ # endif _base-=32; nbits_needed-=32; ret+=ldexpf((float)isaac_next_uint32(_ctx),_base); } _bits=isaac_next_uint32(_ctx)>>32-nbits_needed; ret+=ldexpf((float)_bits,_base-nbits_needed); #else if(nbits_needed>0){ _bits=_bits<<nbits_needed|isaac_next_uint32(_ctx)>>(32-nbits_needed); } # if FLT_MANT_DIG<32 else _bits>>=-nbits_needed;
/*Returns a uniform random float. The expected value is within FLT_MIN (e.g., 1E-37) of 0.5. _bits: An initial set of random bits. _base: This should be -(the number of bits in _bits), up to -64. Return: A float uniformly distributed between 0 (inclusive) and 1 (exclusive). The average value was measured over 2**32 samples to be 0.499991407275206357.*/ static float isaac64_float_bits(isaac64_ctx *_ctx,uint64_t _bits,int _base){ float ret; int nbits_needed; while(!_bits){ if(_base+FLT_MANT_DIG<FLT_MIN_EXP)return 0; _base-=64; _bits=isaac64_next_uint64(_ctx); } nbits_needed=FLT_MANT_DIG-ilog_64_nz(_bits); #if FLT_MANT_DIG>64 ret=ldexpf((float)_bits,_base); # if FLT_MANT_DIG>129 while(64-nbits_needed<0){ # else if(64-nbits_needed<0){ # endif _base-=64; nbits_needed-=64; ret+=ldexpf((float)isaac64_next_uint64(_ctx),_base); } _bits=isaac64_next_uint64(_ctx)>>(64-nbits_needed); ret+=ldexpf((float)_bits,_base-nbits_needed); #else if(nbits_needed>0){ _bits=_bits<<nbits_needed|isaac64_next_uint64(_ctx)>>(64-nbits_needed); } # if FLT_MANT_DIG<64 else _bits>>=-nbits_needed;
static inline void jxr_unpack_sample(fz_context *ctx, struct info *info, jxr_image_t image, int *sp, unsigned char *dp) { int k, bpc, comps, alpha; float v; if (info->format == JXRC_FMT_32bppRGBE) { dp[0] = sRGB_from_scRGB(ldexpf(sp[0], sp[3] - 128 - 8)) * 255 + 0.5f; dp[1] = sRGB_from_scRGB(ldexpf(sp[1], sp[3] - 128 - 8)) * 255 + 0.5f; dp[2] = sRGB_from_scRGB(ldexpf(sp[2], sp[3] - 128 - 8)) * 255 + 0.5f; return; } if (info->format == JXRC_FMT_16bppBGR565) { dp[0] = sp[0] << 3; dp[1] = sp[1] << 2; dp[2] = sp[2] << 3; return; } comps = fz_mini(fz_colorspace_n(ctx, info->cspace), jxr_get_IMAGE_CHANNELS(image)); alpha = jxr_get_ALPHACHANNEL_FLAG(image); bpc = jxr_get_CONTAINER_BPC(image); for (k = 0; k < comps + alpha; k++) { switch (bpc) { default: fz_throw(ctx, FZ_ERROR_GENERIC, "unknown sample type: %d", bpc); case JXR_BD1WHITE1: dp[k] = sp[k] ? 255 : 0; break; case JXR_BD1BLACK1: dp[k] = sp[k] ? 0 : 255; break; case JXR_BD5: dp[k] = sp[k] << 3; break; case JXR_BD8: dp[k] = sp[k]; break; case JXR_BD10: dp[k] = sp[k] >> 2; break; case JXR_BD16: dp[k] = sp[k] >> 8; break; case JXR_BD16S: v = sp[k] * (1.0f / (1 << 13)); goto decode_float32; case JXR_BD32S: v = sp[k] * (1.0f / (1 << 24)); goto decode_float32; case JXR_BD16F: v = float32_from_float16(sp[k]); goto decode_float32; case JXR_BD32F: v = float32_from_int32_bits(sp[k]); goto decode_float32; decode_float32: if (k < comps) dp[k] = sRGB_from_scRGB(fz_clamp(v, 0, 1)) * 255 + 0.5f; else dp[k] = fz_clamp(v, 0, 1) * 255 + 0.5f; break; } } }
Region RegionFactory:: operator()(const Reference& ref) const { Position a, b; // For integers 'i': "2 to the power of 'i'" == powf(2.f, i) == ldexpf(1.f, i) Position blockSize( block_size_.texels_per_row () * ldexpf(1.f,ref.log2_samples_size[0]), block_size_.texels_per_column () * ldexpf(1.f,ref.log2_samples_size[1])); a.time = blockSize.time * ref.block_index[0]; a.scale = blockSize.scale * ref.block_index[1]; b.time = a.time + blockSize.time; b.scale = a.scale + blockSize.scale; return Region(a,b); }
cl_object cl_scale_float(cl_object x, cl_object y) { const cl_env_ptr the_env = ecl_process_env(); cl_fixnum k; if (ECL_FIXNUMP(y)) { k = ecl_fixnum(y); } else { FEwrong_type_nth_arg(ecl_make_fixnum(/*SCALE-FLOAT*/737),2,y,ecl_make_fixnum(/*FIXNUM*/372)); } switch (ecl_t_of(x)) { case t_singlefloat: x = ecl_make_single_float(ldexpf(ecl_single_float(x), k)); break; case t_doublefloat: x = ecl_make_double_float(ldexp(ecl_double_float(x), k)); break; #ifdef ECL_LONG_FLOAT case t_longfloat: x = ecl_make_long_float(ldexpl(ecl_long_float(x), k)); break; #endif default: FEwrong_type_nth_arg(ecl_make_fixnum(/*SCALE-FLOAT*/737),1,x,ecl_make_fixnum(/*FLOAT*/374)); } ecl_return1(the_env, x); }
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. } }
/** * @internal * @brief Get a reproducible single precision scale * * For any given X, return a reproducible scaling factor Y of the form * * 2^(#SBWIDTH * z) where z is an integer * * such that * * Y * 2^(-@c FLT_MANT_DIG - #SBWIDTH - 1) < X < Y * 2\^(#SBWIDTH + 2) * * @param X single precision number to be scaled * @return reproducible scaling factor * * @author Peter Ahrens * @date 19 Jun 2015 */ float binned_sscale(const float X){ int e = EXPF(X); e = e < SBWIDTH ? SBWIDTH : e; e -= (e - EXPF_BIAS - 1) % SBWIDTH; e -= EXPF_BIAS; return ldexpf(0.5, e); }
float myexp(float x) { int sign = +1; if (x < 0) sign = -1; x = abs(x); const float z = x * M_LOG2E; const int m = floorf(z); const float w = z - m; float r = 0.0; const float u = w * M_LN2; float r1 = ldexpf(1, m); float r2 = 1.0/5040;; //r2 = fmaf(r2, x, 1.0/5040); r2 = fmaf(r2, x, 1.0/720); r2 = fmaf(r2, x, 1.0/120); r2 = fmaf(r2, x, 1.0/24); r2 = fmaf(r2, x, 1.0/6); r2 = fmaf(r2, x, 0.5); r2 = fmaf(r2, x, 1); r2 = fmaf(r2, x, 1); r = fmaf(r1, r2, 0); //r = r1 * r2; return (sign < 0)? 1.0/r : r; }
int64_t buzzmsg_deserialize_float(float* data, buzzdarray_t buf, uint32_t pos) { /* Make sure enough bytes are left to read */ if(pos + 2*sizeof(uint32_t) > buzzdarray_size(buf)) return -1; /* Read the mantissa and the exponent */ int32_t mant; int32_t exp; pos = buzzmsg_deserialize_u32((uint32_t*)(&mant), buf, pos); pos = buzzmsg_deserialize_u32((uint32_t*)(&exp), buf, pos); /* A zero mantissa corresponds to a zero float */ if(mant == 0) { *data = 0; } else { /* We must calculate the float * * Idea: use the function ldexpf(), which is the opposite of frexpf() * For this, we need to transform the mantissa into a normalized * fraction in the range [0.5,1) * * 1. Take the absolute value of the mantissa, which is in [1, MAX_MANTISSA+1] * 2. Subtract 1, so it's in [0, MAX_MANTISSA] * 3. divide by MAX_MANTISSA, so it's in [0,1) * 4. divide by 2, so it's in [0,0.5) * 5. Add 0.5, so it's in [0.5,1) */ *data = (float)(abs(mant) - 1) / (2.0f * MAX_MANTISSA) + 0.5f; /* Now use ldexpf() to calculate the absolute value */ *data = ldexpf(*data, exp); /* Finally take care of the sign */ if(mant < 0) *data = -*data; } return pos; }
void run_log2_tests(void) { int i; /* * We should insist that log2() return exactly the correct * result and not raise an inexact exception for powers of 2. */ feclearexcept(FE_ALL_EXCEPT); for (i = FLT_MIN_EXP - FLT_MANT_DIG; i < FLT_MAX_EXP; i++) { assert(log2f(ldexpf(1.0, i)) == i); assert(fetestexcept(ALL_STD_EXCEPT) == 0); } for (i = DBL_MIN_EXP - DBL_MANT_DIG; i < DBL_MAX_EXP; i++) { assert(log2(ldexp(1.0, i)) == i); assert(fetestexcept(ALL_STD_EXCEPT) == 0); } for (i = LDBL_MIN_EXP - LDBL_MANT_DIG; i < LDBL_MAX_EXP; i++) { assert(log2l(ldexpl(1.0, i)) == i); #if 0 /* XXX This test does not pass yet. */ assert(fetestexcept(ALL_STD_EXCEPT) == 0); #endif } }
int main() { float a; std::bitset<32> x; a = 1; x = std::bitset<32>(*reinterpret_cast<unsigned long*>(&a)); std::cout << a << " " << x << std::endl; a = ldexpf(a, 1); x = std::bitset<32>(*reinterpret_cast<unsigned long*>(&a)); std::cout << a << " " << x << std::endl; a = ldexpf(a, -2); x = std::bitset<32>(*reinterpret_cast<unsigned long*>(&a)); std::cout << a << " " << x << std::endl; return 0; }
float float_from_gain(gain_minifloat_t a) { int mantissa = a & MANTISSA_MAX; int exponent = (a >> MANTISSA_BITS) & EXPONENT_MAX; return ldexpf((exponent > 0 ? HIDDEN_BIT | mantissa : mantissa << 1) / ONE_FLOAT, exponent - EXCESS); }
void test_ldexp() { int ip = 1; static_assert((std::is_same<decltype(ldexp((double)0, ip)), double>::value), ""); static_assert((std::is_same<decltype(ldexpf(0, ip)), float>::value), ""); static_assert((std::is_same<decltype(ldexpl(0, ip)), long double>::value), ""); assert(ldexp(1, ip) == 2); }
static float get_float(BitstreamContext *bc) { int power = bitstream_read(bc, 5); float f = ldexpf(bitstream_read(bc, 23), power - 23); if (bitstream_read_bit(bc)) f = -f; return f; }
static float get_0x1p120f(void) { #ifdef _MSC_VER return ldexpf(1.0, 120); #else return 0x1p120f; #endif }
ATF_TC_BODY(ldexpf_inf_pos, tc) { const float x = 1.0L / 0.0L; size_t i; for (i = 0; i < __arraycount(exps); i++) ATF_CHECK(ldexpf(x, exps[i]) == x); }
static float get_float(GetBitContext *gb) { int power = get_bits(gb, 5); float f = ldexpf(get_bits_long(gb, 23), power - 23); if (get_bits1(gb)) f = -f; return f; }
static float float32_unpack(BitstreamReader *bs) { int mantissa = bs->read(bs, 21); int exponent = bs->read(bs, 10); int sign = bs->read(bs, 1); return ldexpf(sign ? -mantissa : mantissa, exponent - 788); }
void Rankwave::gen_waves (Addsynth *D, float fsamp, float fbase, float *scale) { Pipewave::initstatic (fsamp); fbase *= D->_fn / (D->_fd * scale [9]); for (int i = _n0; i <= _n1; i++) { _pipes [i - _n0].genwave (D, i - _n0, fsamp, ldexpf (fbase * scale [i % 12], i / 12 - 5)); } _modif = true; }
float mb_read_float(const unsigned char *buf) { if(!buf) return(nanf("")); // Convert MODBus float (see 2.11.1) into native single-precision float /* You are not expected to understand this */ bool s=buf[2]&0x80; int8_t e=(((buf[2]&0x7f)<<1)|((buf[3]>>7)&1))-127; uint32_t m=(1<<23)|((buf[3]&0x7f)<<16)|(buf[0]<<8)|buf[1]; float f=ldexpf(m, e-23); return(copysignf(f, s?-1:1)); }
/** @brief Process mode with auto navigation guidance */ void NavigationManager::executeAutoNavMode() { /* Execute guidance */ _guidMgr.execute(); /* Compute the command */ _ctrl.execute(); /* Move the force into body frame */ math::Vector3f ctrlFrcDemB = system::system.dataPool.estAtt_IB.rotateQconjVQ( system::system.dataPool.ctrlFrcDemI); /* Scale the force and convert is to integer */ system::system.dataPool.ctrlFrcDemB( ldexpf(ctrlFrcDemB.x, SCALE_TORSOR), ldexpf(ctrlFrcDemB.y, SCALE_TORSOR), ldexpf(ctrlFrcDemB.z, SCALE_TORSOR)); }
static float_approx * setup(cl_object number, float_approx *approx) { cl_object f = cl_integer_decode_float(number); cl_fixnum e = ecl_fixnum(VALUES(1)), min_e; bool limit_f = 0; switch (ecl_t_of(number)) { case t_singlefloat: min_e = FLT_MIN_EXP; limit_f = (number->SF.SFVAL == ldexpf(FLT_RADIX, FLT_MANT_DIG-1)); break; case t_doublefloat: min_e = DBL_MIN_EXP; limit_f = (number->DF.DFVAL == ldexp(FLT_RADIX, DBL_MANT_DIG-1)); break; #ifdef ECL_LONG_FLOAT case t_longfloat: min_e = LDBL_MIN_EXP; limit_f = (number->longfloat.value == ldexpl(FLT_RADIX, LDBL_MANT_DIG-1)); #endif } approx->low_ok = approx->high_ok = ecl_evenp(f); if (e > 0) { cl_object be = EXPT_RADIX(e); if (limit_f) { cl_object be1 = ecl_times(be, ecl_make_fixnum(FLT_RADIX)); approx->r = times2(ecl_times(f, be1)); approx->s = ecl_make_fixnum(FLT_RADIX*2); approx->mm = be; approx->mp = be1; } else { approx->r = times2(ecl_times(f, be)); approx->s = ecl_make_fixnum(2); approx->mm = be; approx->mp = be; } } else if (!limit_f || (e == min_e)) { approx->r = times2(f); approx->s = times2(EXPT_RADIX(-e)); approx->mp = ecl_make_fixnum(1); approx->mm = ecl_make_fixnum(1); } else { approx->r = times2(ecl_make_fixnum(FLT_RADIX)); approx->s = times2(EXPT_RADIX(1-e)); approx->mp = ecl_make_fixnum(FLT_RADIX); approx->mm = ecl_make_fixnum(1); } return approx; }
/* Scale down the denominator, eliminating the zeros * so that we have smaller operands. */ cl_fixnum scale = remove_zeros(&den); cl_fixnum num_size = ecl_integer_length(num); cl_fixnum delta = ecl_integer_length(den) - num_size; scale -= delta; { cl_fixnum adjust = digits + delta + 1; if (adjust > 0) { num = ecl_ash(num, adjust); } else if (adjust < 0) { den = ecl_ash(den, -adjust); } } do { const cl_env_ptr the_env = ecl_process_env(); cl_object fraction = ecl_truncate2(num, den); cl_object rem = ecl_nth_value(the_env, 1); cl_fixnum len = ecl_integer_length(fraction); if ((len - digits) == 1) { if (ecl_oddp(fraction)) { cl_object one = ecl_minusp(num)? ecl_make_fixnum(-1) : ecl_make_fixnum(1); if (rem == ecl_make_fixnum(0)) { if (cl_logbitp(ecl_make_fixnum(1), fraction) != ECL_NIL) fraction = ecl_plus(fraction, one); } else { fraction = ecl_plus(fraction, one); } } *scaleout = scale - (digits + 1); return fraction; } den = ecl_ash(den, 1); scale++; } while (1); } #if 0 /* Unused, we do not have ecl_to_float() */ static float ratio_to_float(cl_object num, cl_object den) { cl_fixnum scale; cl_object bits = prepare_ratio_to_float(num, den, FLT_MANT_DIG, &scale); #if (FIXNUM_BITS-ECL_TAG_BITS) >= FLT_MANT_DIG /* The output of prepare_ratio_to_float will always fit an integer */ float output = ecl_fixnum(bits); #else float output = ECL_FIXNUMP(bits)? ecl_fixnum(bits) : _ecl_big_to_double(bits); #endif return ldexpf(output, scale); }
ATF_TC_BODY(ldexpf_nan, tc) { const float x = 0.0L / 0.0L; float y; size_t i; ATF_REQUIRE(isnan(x) != 0); for (i = 0; i < __arraycount(exps); i++) { y = ldexpf(x, exps[i]); ATF_CHECK(isnan(y) != 0); } }
float expf(float x) { int n; float xn, g, r, z, y; char sign; if(x>=0.0) { y=x; sign=0; } else { y=-x; sign=1; } if(y<EXPEPS) return 1.0; if(y>BIGX) { if(sign) { errno=ERANGE; return XMAX; } else { return 0.0; } } z=y*K1; n=z; if(n<0) --n; if(z-n>=0.5) ++n; xn=n; g=((y-xn*C1))-xn*C2; z=g*g; r=P(z)*g; r=0.5+(r/(Q(z)-r)); n++; z=ldexpf(r, n); if(sign) return 1.0/z; else return z; }
ATF_TC_BODY(ldexpf_zero_pos, tc) { const float x = 0.0L; float y; size_t i; ATF_REQUIRE(signbit(x) == 0); for (i = 0; i < __arraycount(exps); i++) { y = ldexpf(x, exps[i]); ATF_CHECK(x == y); ATF_CHECK(signbit(y) == 0); } }
void rgbe_to_rgb(RGB& rgb, RGBE const rgbe, float const gamma) { if (rgbe.e) { int const exponent = rgbe.e - 128; float const scale = (1.f/256.f) * ldexpf(1.f, exponent); rgb.r = powf(scale * rgbe.r, gamma); rgb.g = powf(scale * rgbe.g, gamma); rgb.b = powf(scale * rgbe.b, gamma); } else { rgb = RGB(); } }
float asincosf(const float x, const int isacos) { float y, g, r; int i; static myconst float a[2]={ 0.0, QUART_PI }; static myconst float b[2]={ HALF_PI, QUART_PI }; y=fabsf(x); i=isacos; if (y < EPS) r=y; else { if (y > 0.5) { i=1-i; if (y > 1.0) { errno=EDOM; return 0.0; } g=(0.5-y)+0.5; g=ldexpf(g,-1); y=sqrtf(g); y=-(y+y); } else { g=y*y; } r=y+y*((P(g)*g)/Q(g)); } if (isacos) { if (x < 0.0) r=(b[i]+r)+b[i]; else r=(a[i]-r)+a[i]; } else { r=(a[i]+r)+a[i]; if (x<0.0) r=-r; } return r; }
float cephes_expf(float xx) { float x, z; int n; x = xx; if( x > MAXLOGF) { //mtherr( "expf", OVERFLOW ); return( MAXNUMF ); } if( x < MINLOGF ) { //mtherr( "expf", UNDERFLOW ); return(0.0); } /* Express e**x = e**g 2**n * = e**g e**( n loge(2) ) * = e**( g + n loge(2) ) */ z = floorf( LOG2EF * x + 0.5 ); /* floor() truncates toward -infinity. */ x -= z * C1; x -= z * C2; n = z; z = x * x; /* Theoretical peak relative error in [-0.5, +0.5] is 4.2e-9. */ z = ((((( 1.9875691500E-4f * x + 1.3981999507E-3f) * x + 8.3334519073E-3f) * x + 4.1665795894E-2f) * x + 1.6666665459E-1f) * x + 5.0000001201E-1f) * z + x + 1.0; /* multiply by power of 2 */ x = ldexpf( z, n ); return( x ); }
ATF_TC_BODY(ldexpf_exp2f, tc) { const float n[] = { 1, 2, 3, 10, 50, 100 }; const float eps = 1.0e-9; const float x = 12.0; float y; size_t i; for (i = 0; i < __arraycount(n); i++) { y = ldexpf(x, n[i]); if (fabsf(y - (x * exp2f(n[i]))) > eps) { atf_tc_fail_nonfatal("ldexpf(%0.01f, %0.01f) " "!= %0.01f * exp2f(%0.01f)", x, n[i], x, n[i]); } } }