1/*
2 * PROJECT:     ReactOS CRT
3 * LICENSE:     MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE:     Floating point conversion routines
5 * COPYRIGHT:   Copyright 2024 Timo Kreuzer <timo.kreuzer@reactos.org>
6 */
7
8#include <asm.inc>
9
10EXTERN __ftol2:PROC
11
12/* FUNCTIONS ***************************************************************/
13.code
14
15__real@43e0000000000000:
16    .quad HEX(43e0000000000000)
17
18PUBLIC __ftoul2_legacy
19__ftoul2_legacy:
20
21    /* Compare the fp number, passed in st(0), against (LLONG_MAX + 1)
22       aka 9223372036854775808.0 (which is 0x43e0000000000000 in double format).
23       If it is smaller, it fits into an __int64, so we can pass it to _ftol2.
24       After this the original fp value has moved to st(1) */
25    fld qword ptr [__real@43e0000000000000]
26    fcom
27
28    /* Put the comparison result bits into ax */
29    fnstsw ax
30
31    /* Here we test the bits for c0 (0x01) and c3 (0x40).
32       We check the parity bit after the test. If it is set,
33       an even number of bits were set.
34       If both are 0, st(1) < st(0), i.e. our value is ok.
35       If both are 1, the value is NaN/Inf and we let _ftol2 handle it. */
36    test ah, HEX(41)
37    jnp __ftoul2_legacy2
38
39    /* Clean up the fp stack and forward to _ftol2 */
40    fstp st(0)
41    jmp __ftol2
42
43__ftoul2_legacy2:
44
45    /* Subtract (LLONG_MAX + 1) from the given fp value and put the result in st(1).
46       st(0) = 9223372036854775808.0
47       st(1) = original fp value - 9223372036854775808.0 */
48    fsub st(1), st(0)
49
50    /* Compare the result to (LLONG_MAX + 1) again and pop the fp stack.
51       Here we check, whether c0 and c3 are both 0, indicating that st(0) > st(1),
52       i.e. fp - (LLONG_MAX + 1) < (LLONG_MAX + 1) */
53    fcomp
54    fnstsw ax
55    test ah, HEX(41)
56    jnz __ftoul2_legacy3
57
58    /* We have established that fp - (LLONG_MAX + 1) fits into an __int64,
59       so pass that to _ftol2 and manually add the difference to the result */
60    call __ftol2
61    add edx, HEX(80000000)
62    ret
63
64__ftoul2_legacy3:
65
66    /* The value is too large, just return the error value */
67    xor eax, eax
68    mov edx, HEX(80000000)
69    ret
70
71END
72