3 // Support for 16-bit signed division/modulus.
5 // Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
6 // Contributed by picoChip Designs Ltd.
7 // Maintained by Daniel Towner (daniel.towner@picochip.com)
9 // This file is free software; you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 2, or (at your option) any
14 // In addition to the permissions in the GNU General Public License, the
15 // Free Software Foundation gives you unlimited permission to link the
16 // compiled version of this file into combinations with other programs,
17 // and to distribute those combinations without any restriction coming
18 // from the use of this file. (The General Public License restrictions
19 // do apply in other respects; for example, they cover modification of
20 // the file, and distribution when not linked into a combine
23 // This file is distributed in the hope that it will be useful, but
24 // WITHOUT ANY WARRANTY; without even the implied warranty of
25 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 // General Public License for more details.
28 // You should have received a copy of the GNU General Public License
29 // along with this program; see the file COPYING. If not, write to
30 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
31 // Boston, MA 02110-1301, USA.
38 _picoMark_FUNCTION_BEGIN=
40 // picoChip Function Prologue : &__divmodhi4 = 4 bytes
42 // 16-bit signed division. Most of the special cases are dealt
43 // with by the 15-bit signed division library (e.g., division by
44 // zero, division by 1, and so on). This wrapper simply inverts
45 // any negative inputs, calls the 15-bit library, and flips any
46 // results as necessary. The
47 // only special cases to be handled here are where either the
48 // divisor or the dividend are the maximum negative values.
50 // Encode r5 with a bit pattern which indicates whether the
51 // outputs of the division must be negated. The MSB will be set
52 // to the sign of the dividend (which controls the remainder's
53 // sign), while the LSB will store the XOR of the two signs,
54 // which indicates the quotient's sign. R5 is not modified by the
55 // 15-bit divmod routine.
56 sub.0 r1,16#8000#,r15 \ asr.1 r0,15,r4
57 beq divisorIsLargestNegative \ lsr.0 r1,15,r3
58 =-> sub.0 r0,16#8000#,r15 \ xor.1 r3,r4,r5
60 // Handle least negative dividend with a special case. Note that the
61 // absolute value of the divisor is also computed here.
62 add.0 [asr r1,15],r1,r3 \ beq dividendIsLargestNegative
63 =-> xor.0 [asr r1,15],r3,r1 \ stw lr,(fp)-1
65 // Compute the absolute value of the dividend, and call the main
67 add.0 r4,r0,r2 \ jl (&__divmod15) // fn_call &__divmod15
71 // Speculatively store the negation of the results.
72 sub.0 0,r0,r2 \ sub.1 0,r1,r3
74 // Does the quotient need negating? The LSB indicates this.
75 and.0 r5,1,r15 \ ldw (fp)-1,lr
78 asr.0 r5,15,r15 \ jr (lr)
81 dividendIsLargestNegative:
83 // Divide the constant -32768. Use the Hacker's Delight
84 // algorithm (i.e., ((dividend / 2) / divisor) * 2) gives
85 // approximate answer). This code is a special case, so no
86 // great effort is made to make it fast, only to make it
89 lsr.0 r0,1,r0 \ jl (&__divmod15) // fn_call &__divmod15
92 // Load the original divisor, and compute the new quotient and
94 lsl.0 r0,1,r0 \ ldw (fp)-2,r3
95 lsl.0 r1,1,r1 // Fill stall slot
97 // The error in the quotient is 0 or 1. The error can be determined
98 // by comparing the remainder to the original divisor. If the
99 // remainder is bigger, then an error of 1 has been introduced,
100 // which must be fixed.
102 blo noCompensationForError
104 add.0 r0,1,r0 \ sub.1 r1,r3,r1
105 noCompensationForError:
106 bra handleNegatedResults
109 divisorIsLargestNegative:
110 // The flags indicate whether the dividend is also the maximum negative
111 copy.0 r0,r1 \ copy.1 0,r0
112 copyeq r0,r1 \ jr (lr)
115 _picoMark_FUNCTION_END=
116 // picoChip Function Epilogue : __divmodhi4
119 //============================================================================
120 // All DWARF information between this marker, and the END OF DWARF
121 // marker should be included in the source file. Search for
122 // FUNCTION_STACK_SIZE_GOES_HERE and FUNCTION NAME GOES HERE, and
123 // provide the relevent information. Add markers called
124 // _picoMark_FUNCTION_BEGIN and _picoMark_FUNCTION_END around the
125 // function in question.
126 //============================================================================
128 //============================================================================
129 // Frame information.
130 //============================================================================
132 .section .debug_frame
133 _picoMark_DebugFrame=
135 // Common CIE header.
136 .unalignedInitLong _picoMark_CieEnd-_picoMark_CieBegin
138 .unalignedInitLong 0xffffffff
139 .initByte 0x1 // CIE Version
140 .ascii 16#0# // CIE Augmentation
141 .uleb128 0x1 // CIE Code Alignment Factor
142 .sleb128 2 // CIE Data Alignment Factor
143 .initByte 0xc // CIE RA Column
144 .initByte 0xc // DW_CFA_def_cfa
151 _picoMark_LSFDE0I900821033007563=
152 .unalignedInitLong _picoMark_FdeEnd-_picoMark_FdeBegin
154 .unalignedInitLong _picoMark_DebugFrame // FDE CIE offset
155 .unalignedInitWord _picoMark_FUNCTION_BEGIN // FDE initial location
156 .unalignedInitWord _picoMark_FUNCTION_END-_picoMark_FUNCTION_BEGIN
157 .initByte 0xe // DW_CFA_def_cfa_offset
158 .uleb128 0x4 // <-- FUNCTION_STACK_SIZE_GOES_HERE
159 .initByte 0x4 // DW_CFA_advance_loc4
160 .unalignedInitLong _picoMark_FUNCTION_END-_picoMark_FUNCTION_BEGIN
161 .initByte 0xe // DW_CFA_def_cfa_offset
166 //============================================================================
167 // Abbrevation information.
168 //============================================================================
170 .section .debug_abbrev
171 _picoMark_ABBREVIATIONS=
173 .section .debug_abbrev
174 .uleb128 0x1 // (abbrev code)
175 .uleb128 0x11 // (TAG: DW_TAG_compile_unit)
176 .initByte 0x1 // DW_children_yes
177 .uleb128 0x10 // (DW_AT_stmt_list)
178 .uleb128 0x6 // (DW_FORM_data4)
179 .uleb128 0x12 // (DW_AT_high_pc)
180 .uleb128 0x1 // (DW_FORM_addr)
181 .uleb128 0x11 // (DW_AT_low_pc)
182 .uleb128 0x1 // (DW_FORM_addr)
183 .uleb128 0x25 // (DW_AT_producer)
184 .uleb128 0x8 // (DW_FORM_string)
185 .uleb128 0x13 // (DW_AT_language)
186 .uleb128 0x5 // (DW_FORM_data2)
187 .uleb128 0x3 // (DW_AT_name)
188 .uleb128 0x8 // (DW_FORM_string)
192 .uleb128 0x2 ;# (abbrev code)
193 .uleb128 0x2e ;# (TAG: DW_TAG_subprogram)
194 .initByte 0x0 ;# DW_children_no
195 .uleb128 0x3 ;# (DW_AT_name)
196 .uleb128 0x8 ;# (DW_FORM_string)
197 .uleb128 0x11 ;# (DW_AT_low_pc)
198 .uleb128 0x1 ;# (DW_FORM_addr)
199 .uleb128 0x12 ;# (DW_AT_high_pc)
200 .uleb128 0x1 ;# (DW_FORM_addr)
206 //============================================================================
207 // Line information. DwarfLib requires this to be present, but it can
209 //============================================================================
214 //============================================================================
216 //============================================================================
220 .unalignedInitLong _picoMark_DEBUG_INFO_END-_picoMark_DEBUG_INFO_BEGIN
221 _picoMark_DEBUG_INFO_BEGIN=
222 .unalignedInitWord 0x2
223 .unalignedInitLong _picoMark_ABBREVIATIONS
226 // Compile unit information.
227 .uleb128 0x1 // (DIE 0xb) DW_TAG_compile_unit)
228 .unalignedInitLong _picoMark_LINES
229 .unalignedInitWord _picoMark_FUNCTION_END
230 .unalignedInitWord _picoMark_FUNCTION_BEGIN
231 // Producer is `picoChip'
232 .ascii 16#70# 16#69# 16#63# 16#6f# 16#43# 16#68# 16#69# 16#70# 16#00#
233 .unalignedInitWord 0xcafe // ASM language
234 .ascii 16#0# // Name. DwarfLib expects this to be present.
236 .uleb128 0x2 ;# (DIE DW_TAG_subprogram)
238 // FUNCTION NAME GOES HERE. Use `echo name | od -t x1' to get the hex. Each hex
239 // digit is specified using the format 16#XX#
240 .ascii 16#5f# 16#64# 16#69# 16#76# 16#6d# 16#6f# 16#64# 16#68# 16#69# 16#34# 16#0# // Function name `_divmodhi4'
241 .unalignedInitWord _picoMark_FUNCTION_BEGIN // DW_AT_low_pc
242 .unalignedInitWord _picoMark_FUNCTION_END // DW_AT_high_pc
244 .initByte 0x0 // end of compile unit children.
246 _picoMark_DEBUG_INFO_END=
248 //============================================================================
250 //============================================================================