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: lrint.S,v 1.1 2007/12/01 04:39:05 dmix Exp $ */
34
/* long lrint (double A);
35
The lrint() function rounds A to the nearest integer, rounding the
36
halfway cases to the even integer direction. (That is both 1.5 and
37
2.5 values are rounded to 2). This function is similar to rint()
38
function, but it differs in type of return value and in that an
41
The rounded long integer value. If A is not a finite number or an
42
overflow was, this realization returns the LONG_MIN value (0x80000000).
46
- shift mantissa according to exponent
47
- round (if shift was to right)
55
subi rA3, 126 ; exponent field of 0.5
56
brlo .L_zr ; A is too small
57
; fabs(A) >= 0x0.800000p+00
59
brlo .L_right ; shtft to right and round
60
breq .L_sign ; no shift
61
; fabs(A) >= 0x0.800000p+25
63
brsh .L_err ; fabs(A) is too big
65
; 0x0.800000p+25 <= fabs(A) <= 0x0.ffffffp+31 --> shift to left by 1..7
66
mov r0, rA3 ; shift counter
77
; 0x0.800000p+00 <= fabs(A) <= 0x0.ffffffp+23
78
; Shift A to right by 1 (rA3 == -1) .. 24 (rA3 == -24) positions and
81
clr rAE ; accumulator for lower bits
84
; Quick shift to right by 8. The trick with rAE is needed to save info
85
; about the lowerest bits. This will be used to compare fraction with
96
; shift to right by 1..7 (slow)
102
ori rAE, 1 ; save flag that lowerst bits are not all 0
108
brcc .L_sign ; fraction < 0.5
109
brne 7f ; fraction > 0.5
111
rjmp .L_sign ; fraction == 0.5 and value is even
114
sbci rA2, -1 ; rA2 was <= 0x7f, so rA3 will not changed
116
; restore the sign and return
128
.L_err: set ; force return 0x80000000
131
.L_zr: rjmp _U(__fp_zero) ; return 0x00000000