File: fixunssfsi.S

package info (click to toggle)
avr-libc 1%3A1.8.0-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,444 kB
  • sloc: ansic: 164,744; asm: 8,048; makefile: 7,456; sh: 3,907; pascal: 443; python: 45
file content (130 lines) | stat: -rw-r--r-- 4,128 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* Copyright (c) 2002  Michael Stumpf  <mistumpf@de.pepperl-fuchs.com>
   Copyright (c) 2007  Dmitry Xmelkov
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE. */

/* $Id: fixunssfsi.S 1487 2007-12-01 02:12:55Z dmix $ */

#include "fp32def.h"
#include "asmdef.h"

/* unsigned long __fixunssfsi (float A);
     The __fixunssfsi() function converts A to the integer value. No
     rounding, the fractional is losted. The compiler calls this function
     to perform a cast operation from float (i.e. double) to unsigned long.
     No saturation. Negative input is permissable (like GCC/x86). Besides
     a normal 32-bits value __fixunssfsi() returns the extra error flag
     in a call-scratched register. This is used when a caller is the
     __fixsfsi() function.

   Return:
     rA3.rA2.rA1.rA0	- 32-bits integer
     rAE		- flag of error (NaN, overflow or A is too big for
    			signed conversion)

   if (!isnan(A) && fabs(A) <= 0x0.ffffffp+31)
      return [A < 0 ? -(long)(-A) : (long)A, OK_FLAG]
   if (!isnan(A) && fabs(A) <= 0x0.ffffffp+32)
      return [A < 0 ? -(unsigned long)(-A) : (unsigned long)A, ERR_FLAG]
   if (isnan(A) || fabs(A) >= 0x0.800000p+33)
      return [0, ERR_FLAG]

   Why no saturation?
     - GCC on Intel's x86 does not do it
     - with saturation it will be more difficult to check result
     - if cast is to 'int' (16 bits for AVR) then saturation is unusefull
     - in practice saturation is needed with user's desirable limits

   Algorithm roughly:
     - split
     - shift mantissa according to exponent
     - restore the sign
 */

ENTRY __fixunssfsi
	rcall	_U(__fp_splitA)
	brcs	.L_err
  ; A is finite
	subi	rA3, 127	; exponent field of 1.0
	brlo	.L_zr
  ; fabs(A) >= 1.0
	mov	rAE, rA3
	clr	rA3
	subi	rAE, 23
	brlo	4f		; shift to right
	breq	.L_sign		; no shift
  ; fabs(A) >= 0x0.800000p+25  To reduce code size we will not check
  ; number of shifts. Instead we will check a MSB of result.
1:	lsl	rA0
	rol	rA1
	rol	rA2
	rol	rA3
	brmi	2f		; next shift is impossible: data lost
	dec	rAE
	brne	1b
	rjmp	.L_sign

2:	cpi	rAE, 1
	breq	.L_sign		; rAE: overflow for 'signed long' usage

.L_err:	rcall	_U(__fp_zero)
	ldi	rAE, 1		; error flag
	ret

.L_zr:	rjmp	_U(__fp_zero)	; return 0x00000000, clear rAE

  ; fabs(A) <= 0x0.ffffffp+23
  ; Shift A to right by 1 (rA3==-1) .. 23 (rA3==-23) positions.
	
3:	mov	rA0, rA1
	mov	rA1, rA2
	clr	rA2
	subi	rAE, -8
	breq	.L_sign

4:	cpi	rAE, -7
	brlt	3b		; quick shift is possible
	
5:	lsr	rA2
	ror	rA1
	ror	rA0
	inc	rAE
	brne	5b

  ; restore the sign and return
.L_sign:
	brtc	6f
	com	rA3
	com	rA2
	com	rA1
	neg	rA0
	sbci	rA1, -1
	sbci	rA2, -1
	sbci	rA3, -1
6:	ret

ENDFUNC