File: modf.S

package info (click to toggle)
avr-libc 1%3A1.6.2.cvs20080610-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 14,848 kB
  • ctags: 55,619
  • sloc: ansic: 92,267; asm: 6,692; sh: 4,131; makefile: 2,481; python: 976; pascal: 426; perl: 116
file content (112 lines) | stat: -rw-r--r-- 3,267 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
/* Copyright (c) 2002  Michael Stumpf  <mistumpf@de.pepperl-fuchs.com>
   Copyright (c) 2006  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: modf.S,v 1.7 2007/01/14 15:11:25 dmix Exp $ */

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

/*  double modf (double x, double *iptr);

    The modf() function breaks the argument x into an integral part and a
    fractional part, each of which has the same sign as x. The integral part
    is stored in iptr.
	This implementation skips writing by zero pointer.
 */

#define	iptr_lo	r20

ENTRY modf
  ; save iptr
	X_movw	ZL, iptr_lo
  ; XH = exponent field
	X_movw	XL, rA2
	lsl	XL
	rol	XH
  ; B = A
	X_movw	rB0, rA0
	X_movw	rB2, rA2
  ; Is there an integral part?
	subi	XH, 127		; exp of 1.0
	brsh	1f
  ; fabs(x) < 1.0:	*iptr = copysign(0, x),  return x
	clr	rB0
	clr	rB1
	clr	rB2
	andi	rB3, 0x80
	rjmp	.L_write
  ; Is there a fraction part?
1:	subi	XH, 23
	brsh	4f		; no fraction part
  ; check fraction:  B >>= 23 - (exp-127)
	mov	XL, XH		; Now XH is -23..-1
	clr	r0		; to control, is the fraction zero?
2:	lsr	rB2
	ror	rB1
	ror	rB0
	adc	r0, r1
	inc	XL
	brmi	2b
	tst	r0
	breq	.L_nfrc		; fraction == 0
  ; restore and clear fraction:  B <<= 23 - (exp-127)
3:	lsl	rB0
	rol	rB1
	rol	rB2
	inc	XH
	brmi	3b
  ; write B
	rcall	.L_write
  ; return nonzero fraction:  A - B
	rjmp	_U(__subsf3)
  ; exponent too big:  compare with smallest NaN (0x7f800001)
4:	cpi	rA0, 1
	cpc	rA1, r1
	ldi	XL, 0x80
	cpc	rA2, XL
	sbci	XH, 128 - 23
	brsh	.L_write	; NaN: write and return as is
  ; fraction == 0
.L_nfrc:
	X_movw	rB0, rA0
	X_movw	rB2, rA2
  ; A = 0.0 with sign
	clr	rA0
	clr	rA1
	clr	rA2
	andi	rA3, 0x80
.L_write:
	adiw	ZL, 0		; skip writing with NULL pointer
	breq	1f
	st	Z,   rB0
	std	Z+1, rB1
	std	Z+2, rB2
	std	Z+3, rB3
1:	ret
ENDFUNC