Bug Summary

File:src/lib/libm/src/e_hypot.c
Warning:line 119, column 6
The result of the left shift is undefined because the left operand is negative

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name e_hypot.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/lib/libm/obj -resource-dir /usr/local/lib/clang/13.0.0 -include namespace.h -I /usr/src/lib/libm/arch/amd64 -I /usr/src/lib/libm/src -I /usr/src/lib/libm/src/ld80 -I /usr/src/lib/libm/hidden -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/lib/libm/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/lib/libm/src/e_hypot.c
1/* @(#)e_hypot.c 5.1 93/09/24 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
13/* hypot(x,y)
14 *
15 * Method :
16 * If (assume round-to-nearest) z=x*x+y*y
17 * has error less than sqrt(2)/2 ulp, than
18 * sqrt(z) has error less than 1 ulp (exercise).
19 *
20 * So, compute sqrt(x*x+y*y) with some care as
21 * follows to get the error below 1 ulp:
22 *
23 * Assume x>y>0;
24 * (if possible, set rounding to round-to-nearest)
25 * 1. if x > 2y use
26 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
27 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
28 * 2. if x <= 2y use
29 * t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y))
30 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
31 * yy1= y with lower 32 bits chopped, y2 = y-yy1.
32 *
33 * NOTE: scaling may be necessary if some argument is too
34 * large or too tiny
35 *
36 * Special cases:
37 * hypot(x,y) is INF if x or y is +INF or -INF; else
38 * hypot(x,y) is NAN if x or y is NAN.
39 *
40 * Accuracy:
41 * hypot(x,y) returns sqrt(x^2+y^2) with error less
42 * than 1 ulps (units in the last place)
43 */
44
45#include <float.h>
46#include <math.h>
47
48#include "math_private.h"
49
50double
51hypot(double x, double y)
52{
53 double a=x,b=y,t1,t2,yy1,y2,w;
54 int32_t j,k,ha,hb;
55
56 GET_HIGH_WORD(ha,x)do { ieee_double_shape_type gh_u; gh_u.value = (x); (ha) = gh_u
.parts.msw; } while (0)
;
1
Loop condition is false. Exiting loop
57 ha &= 0x7fffffff;
58 GET_HIGH_WORD(hb,y)do { ieee_double_shape_type gh_u; gh_u.value = (y); (hb) = gh_u
.parts.msw; } while (0)
;
2
Loop condition is false. Exiting loop
59 hb &= 0x7fffffff;
60 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
3
Assuming 'hb' is <= 'ha'
4
Taking false branch
61 SET_HIGH_WORD(a,ha)do { ieee_double_shape_type sh_u; sh_u.value = (a); sh_u.parts
.msw = (ha); (a) = sh_u.value; } while (0)
; /* a <- |a| */
5
Loop condition is false. Exiting loop
62 SET_HIGH_WORD(b,hb)do { ieee_double_shape_type sh_u; sh_u.value = (b); sh_u.parts
.msw = (hb); (b) = sh_u.value; } while (0)
; /* b <- |b| */
6
Loop condition is false. Exiting loop
63 if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
7
Assuming the condition is false
8
Taking false branch
64 k=0;
65 if(ha > 0x5f300000) { /* a>2**500 */
9
Assuming 'ha' is <= 1596981248
10
Taking false branch
66 if(ha >= 0x7ff00000) { /* Inf or NaN */
67 u_int32_t low;
68 w = a+b; /* for sNaN */
69 GET_LOW_WORD(low,a)do { ieee_double_shape_type gl_u; gl_u.value = (a); (low) = gl_u
.parts.lsw; } while (0)
;
70 if(((ha&0xfffff)|low)==0) w = a;
71 GET_LOW_WORD(low,b)do { ieee_double_shape_type gl_u; gl_u.value = (b); (low) = gl_u
.parts.lsw; } while (0)
;
72 if(((hb^0x7ff00000)|low)==0) w = b;
73 return w;
74 }
75 /* scale a and b by 2**-600 */
76 ha -= 0x25800000; hb -= 0x25800000; k += 600;
77 SET_HIGH_WORD(a,ha)do { ieee_double_shape_type sh_u; sh_u.value = (a); sh_u.parts
.msw = (ha); (a) = sh_u.value; } while (0)
;
78 SET_HIGH_WORD(b,hb)do { ieee_double_shape_type sh_u; sh_u.value = (b); sh_u.parts
.msw = (hb); (b) = sh_u.value; } while (0)
;
79 }
80 if(hb < 0x20b00000) { /* b < 2**-500 */
11
Assuming 'hb' is < 548405248
12
Taking true branch
81 if(hb <= 0x000fffff) { /* subnormal b or 0 */
13
Assuming 'hb' is > 1048575
14
Taking false branch
82 u_int32_t low;
83 GET_LOW_WORD(low,b)do { ieee_double_shape_type gl_u; gl_u.value = (b); (low) = gl_u
.parts.lsw; } while (0)
;
84 if((hb|low)==0) return a;
85 t1=0;
86 SET_HIGH_WORD(t1,0x7fd00000)do { ieee_double_shape_type sh_u; sh_u.value = (t1); sh_u.parts
.msw = (0x7fd00000); (t1) = sh_u.value; } while (0)
; /* t1=2^1022 */
87 b *= t1;
88 a *= t1;
89 k -= 1022;
90 } else { /* scale a and b by 2^600 */
91 ha += 0x25800000; /* a *= 2^600 */
92 hb += 0x25800000; /* b *= 2^600 */
93 k -= 600;
15
The value -600 is assigned to 'k'
94 SET_HIGH_WORD(a,ha)do { ieee_double_shape_type sh_u; sh_u.value = (a); sh_u.parts
.msw = (ha); (a) = sh_u.value; } while (0)
;
16
Loop condition is false. Exiting loop
95 SET_HIGH_WORD(b,hb)do { ieee_double_shape_type sh_u; sh_u.value = (b); sh_u.parts
.msw = (hb); (b) = sh_u.value; } while (0)
;
17
Loop condition is false. Exiting loop
96 }
97 }
98 /* medium size a and b */
99 w = a-b;
100 if (w>b) {
18
Assuming 'w' is > 'b'
19
Taking true branch
101 t1 = 0;
102 SET_HIGH_WORD(t1,ha)do { ieee_double_shape_type sh_u; sh_u.value = (t1); sh_u.parts
.msw = (ha); (t1) = sh_u.value; } while (0)
;
20
Loop condition is false. Exiting loop
103 t2 = a-t1;
104 w = sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
105 } else {
106 a = a+a;
107 yy1 = 0;
108 SET_HIGH_WORD(yy1,hb)do { ieee_double_shape_type sh_u; sh_u.value = (yy1); sh_u.parts
.msw = (hb); (yy1) = sh_u.value; } while (0)
;
109 y2 = b - yy1;
110 t1 = 0;
111 SET_HIGH_WORD(t1,ha+0x00100000)do { ieee_double_shape_type sh_u; sh_u.value = (t1); sh_u.parts
.msw = (ha+0x00100000); (t1) = sh_u.value; } while (0)
;
112 t2 = a - t1;
113 w = sqrt(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
114 }
115 if(k
20.1
'k' is not equal to 0
!=0) {
21
Taking true branch
116 u_int32_t high;
117 t1 = 1.0;
118 GET_HIGH_WORD(high,t1)do { ieee_double_shape_type gh_u; gh_u.value = (t1); (high) =
gh_u.parts.msw; } while (0)
;
22
Loop condition is false. Exiting loop
119 SET_HIGH_WORD(t1,high+(k<<20))do { ieee_double_shape_type sh_u; sh_u.value = (t1); sh_u.parts
.msw = (high+(k<<20)); (t1) = sh_u.value; } while (0)
;
23
The result of the left shift is undefined because the left operand is negative
120 return t1*w;
121 } else return w;
122}
123DEF_STD(hypot)__asm__(".global " "hypot" " ; " "hypot" " = " "_libm_hypot");
124LDBL_MAYBE_CLONE(hypot)__asm("");