Skip to content

Commit b7a9051

Browse files
committed
Remove sqrt from the volatile list
The LLVM IR spec now explicitly says that this intrinsic is required to be rounded correctly. That means that without fasthmath flag (which we do not set here), this intrinsic must have the bitwise correctly rounded answer and should thus not differ between compile and runtime. If there is still a case where it does differ, that is likely some other underlying bug that we should fix instead. Before: ``` julia> f() = sqrt(2) f (generic function with 1 method) julia> @code_typed f() CodeInfo( 1 ─ %1 = Base.Math.sqrt_llvm(2.0)::Float64 └── return %1 ) => Float64 ``` After: ``` julia> @code_typed f() CodeInfo( 1 ─ return 1.4142135623730951 ) => Float64 ```
1 parent 75a1d0f commit b7a9051

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

base/compiler/optimize.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@ function is_pure_intrinsic_infer(f::IntrinsicFunction)
618618
f === Intrinsics.pointerset || # this one is never effect-free
619619
f === Intrinsics.llvmcall || # this one is never effect-free
620620
f === Intrinsics.arraylen || # this one is volatile
621-
f === Intrinsics.sqrt_llvm || # this one may differ at runtime (by a few ulps)
622621
f === Intrinsics.sqrt_llvm_fast || # this one may differ at runtime (by a few ulps)
623622
f === Intrinsics.have_fma || # this one depends on the runtime environment
624623
f === Intrinsics.cglobal) # cglobal lookup answer changes at runtime

test/compiler/inline.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,7 @@ let
891891
ftest(a) = (fcond(a, nothing); a)
892892
@test fully_eliminated(ftest, Tuple{Bool})
893893
end
894+
895+
# sqrt not considered volatile
896+
f_sqrt() = sqrt(2)
897+
@test fully_eliminated(f_sqrt, Tuple{})

test/math.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,3 +1317,72 @@ end
13171317
@test func(-1.9369631f13, 2.1513551f-7, -1.7354427f-24) == -4.1670958f6
13181318
end
13191319
end
1320+
1321+
# Test that sqrt behaves correctly and doesn't exhibit fp80 double rounding.
1322+
# This happened on old glibc versions.
1323+
# Test case from https://sourceware.org/bugzilla/show_bug.cgi?id=14032.
1324+
@testset "sqrt double rounding" begin
1325+
testdata = [
1326+
(0x1.fffffffffffffp+1023, 0x1.fffffffffffffp+511),
1327+
(0x1.ffffffffffffbp+1023, 0x1.ffffffffffffdp+511),
1328+
(0x1.ffffffffffff7p+1023, 0x1.ffffffffffffbp+511),
1329+
(0x1.ffffffffffff3p+1023, 0x1.ffffffffffff9p+511),
1330+
(0x1.fffffffffffefp+1023, 0x1.ffffffffffff7p+511),
1331+
(0x1.fffffffffffebp+1023, 0x1.ffffffffffff5p+511),
1332+
(0x1.fffffffffffe7p+1023, 0x1.ffffffffffff3p+511),
1333+
(0x1.fffffffffffe3p+1023, 0x1.ffffffffffff1p+511),
1334+
(0x1.fffffffffffdfp+1023, 0x1.fffffffffffefp+511),
1335+
(0x1.fffffffffffdbp+1023, 0x1.fffffffffffedp+511),
1336+
(0x1.fffffffffffd7p+1023, 0x1.fffffffffffebp+511),
1337+
(0x1.0000000000003p-1022, 0x1.0000000000001p-511),
1338+
(0x1.0000000000007p-1022, 0x1.0000000000003p-511),
1339+
(0x1.000000000000bp-1022, 0x1.0000000000005p-511),
1340+
(0x1.000000000000fp-1022, 0x1.0000000000007p-511),
1341+
(0x1.0000000000013p-1022, 0x1.0000000000009p-511),
1342+
(0x1.0000000000017p-1022, 0x1.000000000000bp-511),
1343+
(0x1.000000000001bp-1022, 0x1.000000000000dp-511),
1344+
(0x1.000000000001fp-1022, 0x1.000000000000fp-511),
1345+
(0x1.0000000000023p-1022, 0x1.0000000000011p-511),
1346+
(0x1.0000000000027p-1022, 0x1.0000000000013p-511),
1347+
(0x1.000000000002bp-1022, 0x1.0000000000015p-511),
1348+
(0x1.000000000002fp-1022, 0x1.0000000000017p-511),
1349+
(0x1.0000000000033p-1022, 0x1.0000000000019p-511),
1350+
(0x1.0000000000037p-1022, 0x1.000000000001bp-511),
1351+
(0x1.7167bc36eaa3bp+6, 0x1.3384c7db650cdp+3),
1352+
(0x1.7570994273ad7p+6, 0x1.353186e89b8ffp+3),
1353+
(0x1.7dae969442fe6p+6, 0x1.389640fb18b75p+3),
1354+
(0x1.7f8444fcf67e5p+6, 0x1.395659e94669fp+3),
1355+
(0x1.8364650e63a54p+6, 0x1.3aea9efe1a3d7p+3),
1356+
(0x1.85bedd274edd8p+6, 0x1.3bdf20c867057p+3),
1357+
(0x1.8609cf496ab77p+6, 0x1.3bfd7e14b5eabp+3),
1358+
(0x1.873849c70a375p+6, 0x1.3c77ed341d27fp+3),
1359+
(0x1.8919c962cbaaep+6, 0x1.3d3a7113ee82fp+3),
1360+
(0x1.8de4493e22dc6p+6, 0x1.3f27d448220c3p+3),
1361+
(0x1.924829a17a288p+6, 0x1.40e9552eec28fp+3),
1362+
(0x1.92702cd992f12p+6, 0x1.40f94a6fdfddfp+3),
1363+
(0x1.92b763a8311fdp+6, 0x1.4115af614695fp+3),
1364+
(0x1.947da013c7293p+6, 0x1.41ca91102940fp+3),
1365+
(0x1.9536091c494d2p+6, 0x1.4213e334c77adp+3),
1366+
(0x1.61b04c6p-1019, 0x1.a98b88f18b46dp-510),
1367+
(0x1.93789f1p-1018, 0x1.4162ae43d5821p-509),
1368+
(0x1.a1989b4p-1018, 0x1.46f6736eb44bbp-509),
1369+
(0x1.f93bc9p-1018, 0x1.67a36ec403bafp-509),
1370+
(0x1.2f675e3p-1017, 0x1.8a22ab6dcfee1p-509),
1371+
(0x1.a158508p-1017, 0x1.ce418a96cf589p-509),
1372+
(0x1.cd31f078p-1017, 0x1.e5ef1c65dccebp-509),
1373+
(0x1.33b43b08p-1016, 0x1.18a9f607e1701p-508),
1374+
(0x1.6e66a858p-1016, 0x1.324402a00b45fp-508),
1375+
(0x1.8661cbf8p-1016, 0x1.3c212046bfdffp-508),
1376+
(0x1.bbb221b4p-1016, 0x1.510681b939931p-508),
1377+
(0x1.c4942f3cp-1016, 0x1.5461e59227ab5p-508),
1378+
(0x1.dbb258c8p-1016, 0x1.5cf7b0f78d3afp-508),
1379+
(0x1.57103ea4p-1015, 0x1.a31ab946d340bp-508),
1380+
(0x1.9b294f88p-1015, 0x1.cad197e28e85bp-508),
1381+
(0x1.0000000000001p+0, 0x1p+0),
1382+
(0x1.fffffffffffffp-1, 0x1.fffffffffffffp-1),
1383+
]
1384+
for (x,y) in testdata
1385+
@test sqrt(x) === y
1386+
@test Base.invokelatest((@eval ()->sqrt($x))) == y
1387+
end
1388+
end

0 commit comments

Comments
 (0)