6

In numerical computation, it is often needed to scale numbers to be in safe range.

For example, computing Euclidean distance: sqrt(a^2+b^2). Here, if magnitude of a or b is too small/large, then underflow/overflow can happen.

A common approach to solve this is to divide numbers by the largest magnitude number. However, this solution is:

  • slow (division is slow)
  • causes a little extra inaccuracy

So I thought that instead of dividing by the largest magnitude number, let's multiply it with a close power-of-2 reciprocal number. This seems a better solution, as:

  • multiplication is much faster than division
  • better accuracy, as multiplying with a power-of-2 number is exact

So, I'd like to create a small utility function, which has a logic like this (by ^, I mean exponentiation):

void getScaler(double value, double &scaler, double &scalerReciprocal) {
    int e = <exponent of value>;
    if (e<-1022) { scaler=2^-1022; scalerReciprocal = 2^1022; }
    } else if (e>1022) { scaler=2^1022; scalerReciprocal = 2^-1022; }
    } else { scaler=2^e; scalerReciprocal = 2^(2046-e); }
}

This function should return a normalized scaler & scalerReciprocal, both are power-of-2 numbers, where scaler is near to value, and scalerReciprocal is the reciprocal of scaler.

The maximum allowed exponents for scaler/scaleReciprocal are -1022..1022 (I don't want to work with subnormal scaler, as subnormal numbers can be slow).

What would be a fast way to do this? Can this be done with pure floating-point operations? Or should I extract the exponent from value, and use simple ifs to do the logic? Is there some kind of trick to do the comparison with (-)1022 fast (as the range is symmetric)?

Note: scaler doesn't need to be the closest possible power-of-2. If some logic needs it, scaler can be some small power-of-2 away from the closest value.

12
  • I don't think it will answer your question but if you are looking for increasing performances the fastest way of multiplying and dividing by 2 is bit shifting. In signal processing you subtract the offset of the signal and as you said scale it so that range being [0,1] and you design the filter to have max magnitude 1. Plus I don't understand: you are scaling only if the exponent is smaller than -1022 or bigger than 1022: why is that? what range should it be?
    – roschach
    Jan 21, 2019 at 21:27
  • @FrancescoBoi: No, scaling would always happen. But scaler should only have exponents between -1022..1022 (which is almost the whole range. Only the problematic border values are eliminated).
    – geza
    Jan 21, 2019 at 21:38
  • Are you interested in portable pure C that will compile efficiently for x86, or are you also interested in C with intrinsics for SIMD instructions like AVX512 _mm512_getexp_pd (extract exponent as a double) and _mm512_scalef_pd which does dst[63:0] := tmp_src1[63:0] * POW(2, FLOOR(tmp_src2[63:0])) (i.e. adds the integer part of a double to the exponent field of another.) Jan 21, 2019 at 21:49
  • @PeterCordes: I'm mostly interested in pure C, or maybe extensions, which are widely available. AVX is still not that widespread. But thanks for mentioning it, I didn't know about _mm512_getexp_pd/_mm512_scalef_pd. They seem similar to the old FXTRACT/FSCALE.
    – geza
    Jan 21, 2019 at 21:55
  • 1
    @PeterCordes With respect to the ternary operator and minsd, minpd, maxsd, maxpd, somehow clang does a better job than gcc. Godbolt link
    – wim
    Jan 22, 2019 at 13:30

3 Answers 3

7

Function s = get_scale(z) computes the "close power of 2". Since the fraction bits of s are zero, the inverse of s is just an (inexpensive) integer subtraction: see function inv_of_scale.

On x86 get_scale and inv_of_scale compile to quite efficient assembly with clang. Compiler clang translates the ternary operators to minsd and maxsd, see also Peter Cordes' comment. With gcc, it is slightly more efficient to translate these functions to x86 intrinsics code (get_scale_x86 and inv_of_scale_x86), see Godbolt.

Note that C explicitly permits type-punning through a union, whereas C++ (c++11) has no such permission Although gcc 8.2 and clang 7.0 do not complain about the union, you can improve the C++ portabily by using the memcpy trick instead of the union trick. Such a modification of the code should be trivial. The code should handle subnormals correctly.

#include<stdio.h>
#include<stdint.h>
#include<immintrin.h>
/* gcc -Wall -m64 -O3 -march=sandybridge dbl_scale.c */

union dbl_int64{
    double d;
    uint64_t i;
};

double get_scale(double t){
    union dbl_int64 x;
    union dbl_int64 x_min;
    union dbl_int64 x_max;
    uint64_t mask_i;
           /* 0xFEDCBA9876543210 */
    x_min.i = 0x0010000000000000ull;
    x_max.i = 0x7FD0000000000000ull;
    mask_i =  0x7FF0000000000000ull;
    x.d = t;
    x.i = x.i & mask_i;                    /* Set fraction bits to zero, take absolute value */
    x.d = (x.d < x_min.d) ? x_min.d : x.d; /* If subnormal: set exponent to 1                */
    x.d = (x.d > x_max.d) ? x_max.d : x.d; /* If exponent is very large: set exponent to 7FD, otherwise the inverse is a subnormal */
    return x.d;
}

double get_scale_x86(double t){
    __m128d x = _mm_set_sd(t);
    __m128d x_min = _mm_castsi128_pd(_mm_set1_epi64x(0x0010000000000000ull));
    __m128d x_max = _mm_castsi128_pd(_mm_set1_epi64x(0x7FD0000000000000ull));
    __m128d mask  = _mm_castsi128_pd(_mm_set1_epi64x(0x7FF0000000000000ull));
            x     = _mm_and_pd(x, mask);
            x     = _mm_max_sd(x, x_min);
            x     = _mm_min_sd(x, x_max);
    return _mm_cvtsd_f64(x);
}

/* Compute the inverse 1/t of a double t with all zero fraction bits     */
/* and exponent between the limits of function get_scale                 */
/* A single integer subtraction is much less expensive than a            */
/* floating point division.                                               */
double inv_of_scale(double t){
    union dbl_int64 x;
                     /* 0xFEDCBA9876543210 */
    uint64_t inv_mask = 0x7FE0000000000000ull;
    x.d = t;
    x.i = inv_mask - x.i;
    return x.d;
}

double inv_of_scale_x86(double t){
    __m128i inv_mask = _mm_set1_epi64x(0x7FE0000000000000ull);
    __m128d x        = _mm_set_sd(t);
    __m128i x_i      = _mm_sub_epi64(inv_mask, _mm_castpd_si128(x));
    return _mm_cvtsd_f64(_mm_castsi128_pd(x_i));
}


int main(){
    int n = 14;
    int i;
    /* Several example values, 4.94e-324 is the smallest subnormal */
    double y[14] = { 4.94e-324, 1.1e-320,  1.1e-300,  1.1e-5,  0.7,  1.7,  123.1, 1.1e300,  
                     1.79e308, -1.1e-320,    -0.7, -1.7, -123.1,  -1.1e307};
    double z, s, u;

    printf("Portable code:\n");
    printf("             x       pow_of_2        inverse       pow2*inv      x*inverse \n");
    for (i = 0; i < n; i++){  
        z = y[i];
        s = get_scale(z);
        u = inv_of_scale(s);
        printf("%14e %14e %14e %14e %14e\n", z, s, u, s*u, z*u);
    }

    printf("\nx86 specific SSE code:\n");
    printf("             x       pow_of_2        inverse       pow2*inv      x*inverse \n");
    for (i = 0; i < n; i++){  
        z = y[i];
        s = get_scale_x86(z);
        u = inv_of_scale_x86(s);
        printf("%14e %14e %14e %14e %14e\n", z, s, u, s*u, z*u);
    }

    return 0;
}

The output looks fine:

Portable code:
             x       pow_of_2        inverse       pow2*inv      x*inverse 
 4.940656e-324  2.225074e-308  4.494233e+307   1.000000e+00   2.220446e-16
 1.099790e-320  2.225074e-308  4.494233e+307   1.000000e+00   4.942713e-13
 1.100000e-300  7.466109e-301  1.339386e+300   1.000000e+00   1.473324e+00
  1.100000e-05   7.629395e-06   1.310720e+05   1.000000e+00   1.441792e+00
  7.000000e-01   5.000000e-01   2.000000e+00   1.000000e+00   1.400000e+00
  1.700000e+00   1.000000e+00   1.000000e+00   1.000000e+00   1.700000e+00
  1.231000e+02   6.400000e+01   1.562500e-02   1.000000e+00   1.923437e+00
 1.100000e+300  6.696929e+299  1.493222e-300   1.000000e+00   1.642544e+00
 1.790000e+308  4.494233e+307  2.225074e-308   1.000000e+00   3.982882e+00
-1.099790e-320  2.225074e-308  4.494233e+307   1.000000e+00  -4.942713e-13
 -7.000000e-01   5.000000e-01   2.000000e+00   1.000000e+00  -1.400000e+00
 -1.700000e+00   1.000000e+00   1.000000e+00   1.000000e+00  -1.700000e+00
 -1.231000e+02   6.400000e+01   1.562500e-02   1.000000e+00  -1.923437e+00
-1.100000e+307  5.617791e+306  1.780059e-307   1.000000e+00  -1.958065e+00

x86 specific SSE code:
             x       pow_of_2        inverse       pow2*inv      x*inverse 
 4.940656e-324  2.225074e-308  4.494233e+307   1.000000e+00   2.220446e-16
 1.099790e-320  2.225074e-308  4.494233e+307   1.000000e+00   4.942713e-13
 1.100000e-300  7.466109e-301  1.339386e+300   1.000000e+00   1.473324e+00
  1.100000e-05   7.629395e-06   1.310720e+05   1.000000e+00   1.441792e+00
  7.000000e-01   5.000000e-01   2.000000e+00   1.000000e+00   1.400000e+00
  1.700000e+00   1.000000e+00   1.000000e+00   1.000000e+00   1.700000e+00
  1.231000e+02   6.400000e+01   1.562500e-02   1.000000e+00   1.923437e+00
 1.100000e+300  6.696929e+299  1.493222e-300   1.000000e+00   1.642544e+00
 1.790000e+308  4.494233e+307  2.225074e-308   1.000000e+00   3.982882e+00
-1.099790e-320  2.225074e-308  4.494233e+307   1.000000e+00  -4.942713e-13
 -7.000000e-01   5.000000e-01   2.000000e+00   1.000000e+00  -1.400000e+00
 -1.700000e+00   1.000000e+00   1.000000e+00   1.000000e+00  -1.700000e+00
 -1.231000e+02   6.400000e+01   1.562500e-02   1.000000e+00  -1.923437e+00
-1.100000e+307  5.617791e+306  1.780059e-307   1.000000e+00  -1.958065e+00

Vectorization

Function get_scale should vectorize with compilers that support auto-vectorization. The following piece of code vectorizes very well with clang (no need to write SSE/AVX intrinsics code).

/* Test how well get_scale vectorizes: */
void get_scale_vec(double * __restrict__ t, double * __restrict__ x){
    int n = 1024;
    int i;
    for (i = 0; i < n; i++){
        x[i] = get_scale(t[i]);
    }
}

Unfortunately gcc doesn't find the vmaxpd and vminpd instructions.

7
  • Thanks for the answer! Based on your solution, I've found a (perhaps) faster one.
    – geza
    Jan 22, 2019 at 14:47
  • 1
    Re: union type-punning: GNU C++ explicitly supports it as an extension to ISO C++. See gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Type-punning and gcc.gnu.org/onlinedocs/gcc/…. I think MSVC also supports it, but IDK if it's documented. Still, no downside to using memcpy for good compilers, as long as it's the full width of the type. Jan 23, 2019 at 19:57
  • @PeterCordes: as far as I know, MSVC doesn't utilize optimization based on strict aliasing rules. For example, this code reloads *a twice unnecessarily.
    – geza
    Jan 23, 2019 at 21:03
  • @geza: strict aliasing is related but separate from union type-punning. *(int*)&my_float is still UB in GNU C, although it may often work in practice for simple cases like this where there aren't also FP operations mixed in. Jan 23, 2019 at 21:07
  • 1
    @PeterCordes: sure :) I meant, that with MSVC, all kind of type-punning works, not just union based, like with gcc/clang. I've never seen documented this anywhere though, it's just based on my experience.
    – geza
    Jan 23, 2019 at 21:10
3

Based on wim's answer, here's another solution, which can be faster, as it has one less instruction. The output is a little bit different, but still fulfills the requirements.

The idea is to use bit operations to fix border cases: put a 01 to the lsb of the exponent, no matter of its value. So, exponent:

  • 0 becomes 1 (-1023 becomes -1022)
  • 2046 becomes 2045 (1023 becomes 1022)
  • other exponents modified as well, but just slightly: the number can become two times larger compared to wim's solution (when exponent lsb changes from 00 to 01), or halved (when 10->01) or 1/4 (when 11->01)

So, this modified routine works (and I think that it's pretty cool that the problem can be solved with only 2 fast asm instructions):

#include<stdio.h>
#include<stdint.h>
#include<immintrin.h>
/* gcc -Wall -m64 -O3 -march=sandybridge dbl_scale.c */

union dbl_int64{
    double d;
    uint64_t i;
};

double get_scale(double t){
    union dbl_int64 x;
    uint64_t and_i;
    uint64_t or_i;
         /* 0xFEDCBA9876543210 */
    and_i = 0x7FD0000000000000ull;
    or_i =  0x0010000000000000ull;
    x.d = t;
    x.i = (x.i & and_i)|or_i;                     /* Set fraction bits to zero, take absolute value */
    return x.d;
}

double get_scale_x86(double t){
    __m128d x = _mm_set_sd(t);
    __m128d x_and = _mm_castsi128_pd(_mm_set1_epi64x(0x7FD0000000000000ull));
    __m128d x_or  = _mm_castsi128_pd(_mm_set1_epi64x(0x0010000000000000ull));
            x     = _mm_and_pd(x, x_and);
            x     = _mm_or_pd(x, x_or);
    return _mm_cvtsd_f64(x);
}

/* Compute the inverse 1/t of a double t with all zero fraction bits     */
/* and exponent between the limits of function get_scale                 */
/* A single integer subtraction is much less expensive than a            */
/* floating point division.                                               */
double inv_of_scale(double t){
    union dbl_int64 x;
                     /* 0xFEDCBA9876543210 */
    uint64_t inv_mask = 0x7FE0000000000000ull;
    x.d = t;
    x.i = inv_mask - x.i;
    return x.d;
}

double inv_of_scale_x86(double t){
    __m128i inv_mask = _mm_set1_epi64x(0x7FE0000000000000ull);
    __m128d x        = _mm_set_sd(t);
    __m128i x_i      = _mm_sub_epi64(inv_mask, _mm_castpd_si128(x));
    return _mm_cvtsd_f64(_mm_castsi128_pd(x_i));
}


int main(){
    int n = 14;
    int i;
    /* Several example values, 4.94e-324 is the smallest subnormal */
    double y[14] = { 4.94e-324, 1.1e-320,  1.1e-300,  1.1e-5,  0.7,  1.7,  123.1, 1.1e300,  
                     1.79e308, -1.1e-320,    -0.7, -1.7, -123.1,  -1.1e307};
    double z, s, u;

    printf("Portable code:\n");
    printf("             x       pow_of_2        inverse       pow2*inv      x*inverse \n");
    for (i = 0; i < n; i++){  
        z = y[i];
        s = get_scale(z);
        u = inv_of_scale(s);
        printf("%14e %14e %14e %14e %14e\n", z, s, u, s*u, z*u);
    }

    printf("\nx86 specific SSE code:\n");
    printf("             x       pow_of_2        inverse       pow2*inv      x*inverse \n");
    for (i = 0; i < n; i++){  
        z = y[i];
        s = get_scale_x86(z);
        u = inv_of_scale_x86(s);
        printf("%14e %14e %14e %14e %14e\n", z, s, u, s*u, z*u);
    }

    return 0;
}
8
  • Which 3 instructions are you counting? Yours reduces it to 2, just ANDPS / ORPS instead of ANDPS/MINPD/MAXPD. Or if you're counting actually scaling 2 values according to the max magnitude, then you'd need AND + AND + (select the highest exponent with MAXPD or AVX512 VPMAXUQ) + OR + PSUBQ, and then apply it to both inputs with 2x MULPD or VFMADD... if you or the compiler can contract the first step after normalization into an FMA. Jan 22, 2019 at 14:59
  • And BTW, avoid intrinsics for scalar; they suck because there's no way to tell the compiler you want a vector with an undefined upper element, i.e. no scalar->128 equivalent of __m256 _mm256_castps128_ps256 (__m128 a). Unfortunately, other than clang most compilers do actually waste an instruction zero-extending for _mm_set_sd(t). How to merge a scalar into a vector without the compiler wasting an instruction zeroing upper elements? Design limitation in Intel's intrinsics?. Just use the union type-pun version, I think all the major x86 compilers su Jan 22, 2019 at 15:03
  • Oh, I just looked at your Godbolt link. That's weird that for scalar compilers fail to use ANDPS / ORPS, even in a loop, and actually extract to GP regs with movq. There are no call-preserved XMM registers in x86-64 System V, so they couldn't hoist the constants, but using them from memory would still be a win. Hopefully compilers will auto-vectorize the pure C version, though. Jan 22, 2019 at 15:09
  • 1
    You might want to add a version of your function that takes 2 inputs and returns a scale factor to use for both, because like I said you can combine that max-magnitude finding with zeroing the significand. Jan 22, 2019 at 19:07
  • 1
    @PeterCordes: With the AND/OR idea, normal values are scaled to the [0.5...8.0) range, which is perfect for reliably computing a hypotenuse, indeed.
    – wim
    Jan 23, 2019 at 8:37
2

You can use

double frexp (double x, int* exp); 

Returned value is the fractional part of of x and exp is the exponent (minus the offset).

Alternatively, the following code gets the exponent part of a double.

int get_exp(double *d) {
  long long *l = (long long *) d;
  return ((*l & (0x7ffLL << 52) )>> 52)-1023 ;
}
3
  • 1
    frexp does more than needed here. And does less, as I need to clamp exp, and then I need to convert back to get a double. I don't think that frexp really usable in my case, as I need speed. I'd rather extract the exponent by hand, if that's the way to go (it's just a memcpy, shift and a mask).
    – geza
    Jan 21, 2019 at 21:15
  • @geza: frexp is just a shift and a mask, standardized and documented to make it portable. If you want to adjust the exponent, pair it with ldexp (note that ldexp adds to the exponent rather than replacing it)
    – Ben Voigt
    Jan 21, 2019 at 21:57
  • 1
    @BenVoigt: unfortunately, no. Check out the source code. It handles nan/inf, and subnormal numbers. And it does some logic, which could be part of my if (exp<-1022) .. logic. So with frexp, I'd have redundant code. I'm not saying that it is very slow. But still, it is better (for me) to extract exponent manually, if that's the way to go.
    – geza
    Jan 21, 2019 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.