Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xLARFGP unnecessarily overflows #934

Closed
2 tasks done
christoph-conrads opened this issue Nov 10, 2023 · 0 comments
Closed
2 tasks done

xLARFGP unnecessarily overflows #934

christoph-conrads opened this issue Nov 10, 2023 · 0 comments

Comments

@christoph-conrads
Copy link
Contributor

christoph-conrads commented Nov 10, 2023

Description

xLARFGP may cause an overflow in line 195. It is my expectation that no overflow takes place.

The operations executed are (ALPHA is assigned to several times, signs are ignored):

ALPHA ≔ X(1)
XNORM ≔ ∥X(1:)∥₂
BETA ≔ ∥X∥₂
ALPHA' ≔ ALPHA + BETA
ALPHA''  ≔ XNORM * (XNORM / ALPHA')

The reciprocal of ALPHA'' is computed in line 223 and this computation may overflow when 0 < ALPHA ≪ 1 and 0 < XNORM ≪ ALPHA. For example, ALPHA = ε² and XNORM = ε⁴.

The C code demonstrates the problem (compile it with gcc -Wextra -Wall -std=c99 -pedantic slarfgp.c -llapack -lm):

#include <math.h>
#include <stddef.h>
#include <stdio.h>

typedef int lapack_int;

void slarfgp_(
    lapack_int* n,
    float* alpha,
    float* x,
    lapack_int* incx,
    float* tau);

#define N 2

int main()
{
    float nan = NAN;
    float eps = ldexpf(1.0f, -23);
    lapack_int n = N;
    float x[N] = { powf(eps, 2.0f), powf(eps, 4.0f) };
    lapack_int incx = 1;
    float tau = nan;

    printf("input=[%.2e; %.2e]\n", x[0], x[1]);

    slarfgp_(&n, x + 0, x + 1, &incx, &tau);

    printf("output=[%.2e; %.2e]\n", x[0], x[1]);
    printf("tau=%.2e\n", tau);

	if(isinf(x[1])) {
		fprintf(stderr, "x[1] is infinite!\n");
	}
}

Checklist

  • I've included a minimal example to reproduce the issue
  • I'd be willing to make a PR to solve this issue
@langou langou closed this as completed in 64897c4 Nov 11, 2023
christoph-conrads added a commit to christoph-conrads/lapack that referenced this issue Nov 12, 2023
Avoid overflows when XNORM << ABS(ALPHA) << 1.

fixes Reference-LAPACK#934
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant