| 1 | ## esl_mem : str*() like functions for char arrays |
| 2 | |
| 3 | |
| 4 | Many useful C library functions for parsing char data assume that the |
| 5 | input is a NUL-terminated string. Easel also needs to deal with |
| 6 | non-terminated char arrays. Working with nonterminated char arrays is |
| 7 | especially important in data input with `ESL_BUFFER`, where an input |
| 8 | might be a memory-mapped file on disk. We want to avoid making copies |
| 9 | of data just to add `\0` string terminators. `esl_mem` provides a set |
| 10 | of string function substitutes that take a pointer and a length in |
| 11 | bytes `(char *s, esl_pos_t n)` as input. |
| 12 | |
| 13 | |
| 14 | ### esl_mem_strtof() versus esl_memtof() |
| 15 | |
| 16 | It is shockingly difficult to produce a correct implementation of |
| 17 | `strtod()` or related C library functions that convert a string |
| 18 | decimal representation to a floating-point number, such as `strtof()` |
| 19 | and `atof()`. Correct conversion includes a guarantee that the |
| 20 | resulting floating point representation will be within one ulp of the |
| 21 | decimal string representation, correctly rounded. One canonical |
| 22 | implementation by David Gay [1] is over 5500 lines of C code. |
| 23 | Unfortunately the C library provides no alternative for nonterminated |
| 24 | char arrays. |
| 25 | |
| 26 | When the input is a non-terminated char array, Easel provides two |
| 27 | choices: |
| 28 | |
| 29 | * `esl_mem_strtof()` is a fast and compact reimplementation of |
| 30 | `strtof()` that works on char arrays, but sacrifices some accuracy. |
| 31 | |
| 32 | * `esl_memtof()` is slower but accurate. It copies the data to a |
| 33 | NUL-terminated string buffer and passes it to `strtof()` for |
| 34 | correct conversion. |
| 35 | |
| 36 | In benchmarking during development of the JSON-based HMMER4 profile |
| 37 | file parser, I measured the speed difference between the two routines |
| 38 | at about three-fold. In HMMER, I was more concerned with speed than |
| 39 | guaranteeing absolute accuracy of the conversion, so HMMER4 uses |
| 40 | `esl_json_ReadFloat()`, which mirrors the `esl_mem_strtof()` |
| 41 | implementation. |
| 42 | |
| 43 | The accuracy loss in `esl_mem_strtof()` is caused by a small roundoff |
| 44 | accumulation error that seems difficult to avoid (and hence the |
| 45 | difference between the Gay implementation [1] and mine). Still, it is |
| 46 | almost always within +/-1 ulp of the correct `strtof()` result. The |
| 47 | `utest_mem_strtof_error()` unit test verifies that in 100K different |
| 48 | string representation conversions, no `esl_mem_strtof()` conversion |
| 49 | deviates by more than +/-4 ulp (a relative error of about 5e-7) from |
| 50 | `strtof()`. Applications that demand smaller errors than this need to |
| 51 | use `esl_memtof()`. |
| 52 | |
| 53 | `esl_mem_strtof()` is also unable to deal with a pathological case |
| 54 | where the significand by itself would over/underflow, but when |
| 55 | combined with the exponent, the result is within the valid range of a |
| 56 | float. For example, it parses |
| 57 | 9999999999999999999999999999999999999999e-10 as +infinity, not as |
| 58 | 1e30. Full `strtof()` implementations get this right, as does |
| 59 | `esl_memtof()`. |
| 60 | |
| 61 | [1] David M. Gay (1990) |
| 62 | ["Correctly rounded binary-decimal and decimal-binary conversions"](https://www.ampl.com/REFS/rounding.pdf), |
| 63 | AT&T manuscript 90-10. Implementation: [NETLIB dtoa.c code](https://www.ampl.com/netlib/fp/dtoa.c). |