1
/* Copyright (c) 2007 Dmitry Xmelkov
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in
11
the documentation and/or other materials provided with the
13
* Neither the name of the copyright holders nor the names of
14
contributors may be used to endorse or promote products derived
15
from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
POSSIBILITY OF SUCH DAMAGE. */
29
/* $Id: lround.S,v 1.2 2007/12/01 02:12:54 dmix Exp $ */
34
/* long lround (double A);
35
The lround() function rounds A to the nearest integer, but rounds
36
halfway cases away from zero (instead of to the nearest even integer).
37
This function is similar to round() function, but it differs in
38
type of return value and in that an overflow is possible.
40
The rounded long integer value. If A is infinite, NaN or an overflow
41
was, this realization returns the LONG_MIN value (0x80000000).
45
- shift mantissa according to exponent
49
Objections to saturation are listen in __fixunssfsi.S file.
56
subi rA3, 126 ; exponent field of 0.5
57
brlo .L_zr ; A is too small
58
; fabs(A) >= 0x0.800000p+00
60
brlo 2f ; shtft to right and round
61
breq .L_sign ; no shift
62
; fabs(A) >= 0x0.800000p+25
64
brsh .L_err ; A is too big
66
; 0x0.800000p+25 <= fabs(A) <= 0x0.ffffffp+31 --> shift to left by 1..7
67
mov r0, rA3 ; shift counter
78
; quick shift to right by 8
79
5: mov r0, rA0 ; save for possible round
87
; 0x0.800000p+00 <= fabs(A) <= 0x0.ffffffp+23
88
; Shift A to right by 1 (rA3 == -1) .. 24 (rA3 == -24) positions and
92
; shift to right by 1..7 (slow)
96
inc rA3 ; C is not changed
98
; Round. Now flag C is set if fractional is >= 0.5
101
adc rA2, r1 ; rA2 was <= 0x7f, so rA3 will not changed
103
; restore the sign and return
115
.L_err: set ; force return 0x80000000
118
.L_zr: rjmp _U(__fp_zero) ; return 0x00000000