File: fix_float_precision

package info (click to toggle)
htslib 1.21%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 10,940 kB
  • sloc: ansic: 68,108; sh: 3,580; perl: 2,021; makefile: 887; cpp: 40
file content (34 lines) | stat: -rw-r--r-- 1,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
From: Gustavo Romero <gromero@linux.vnet.ibm.com>
Subject: ppc64el float handling fix

I dug a bit further and it looks like that sam_parse1() is actually
generating a different value for the floats in question, so when they are
loaded back for the comparison they are already screwed up

because strtod() is used in float_to_le() and so also in u32_to_le(), which are
inlined and float_to_le() takes a float and not a double as the first argument
the use strtof() instead of strtod() in there looks the best, avoiding a
truncation from double to float, which might cause precision issues, specially
between different archs (like casts) plus optimization. So the following
change fixed the issue for me.

--- htslib.orig/sam.c
+++ htslib/sam.c
@@ -2561,7 +2561,7 @@
             if (grow_B_array(b, nalloc, 4) < 0)
                 return NULL;
         }
-        float_to_le(strtod(q + 1, &q), b->data + b->l_data);
+        float_to_le(strtof(q + 1, &q), b->data + b->l_data);
         b->l_data += 4;
     }
     return q;
@@ -2826,7 +2826,7 @@
             }
         } else if (type == 'f') {
             b->data[b->l_data++] = 'f';
-            float_to_le(strtod(q, &q), b->data + b->l_data);
+            float_to_le(strtof(q, &q), b->data + b->l_data);
             b->l_data += sizeof(float);
         } else if (type == 'd') {
             b->data[b->l_data++] = 'd';