OpenCores
URL https://opencores.org/ocsvn/forwardcom/forwardcom/trunk

Subversion Repositories forwardcom

[/] [forwardcom/] [libraries/] [atoi_light.as] - Blame information for rev 166

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 120 Agner
/*********************************  atoi_light.as *****************************
2
* Author:        Agner Fog
3
* date created:  2018-03-23
4
* Last modified: 2021-05-16
5
* Version:       1.11
6
* Project:       ForwardCom library libc_light.li
7
* Description:   atoi: convert string to integer
8
* This version is for CPUs with limited capabilities
9
* C declaration: int64_t atoi(const char * str)
10
*
11
* Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses
12
*****************************************************************************/
13
 
14
code section execute align = 4                   // code section
15
 
16
_atoi function public reguse = 3, 0
17
_atol function public reguse = 3, 0
18
 
19
if (int64 r0 == 0) {jump EMPTYEND}               // NULL pointer
20
 
21
int64 sp -= 3*8                                  // save r2 - r4
22
int64 [sp]      = r2
23
int64 [sp+8]    = r3
24
int64 [sp+0x10] = r4
25
 
26
int    r1 = 0                                    // state:  0: after whitespace
27
                                                 //         1: after +/-
28
                                                 //         2: after digit
29
int64  r2 = 0                                    // value
30
int    r3 = 0                                    // sign
31
int8   r4 = [r0]                                 // read first character from string
32
// loop through string until terminating zero
33
while (int8+ r4 != 0) {
34
   if (uint8+ r4 <= ' ') {                       // whitespace
35
      if (int r1 != 0) {jump ERROREND}           // space not allowed if state != 0. end of number
36
      jump NEXT                                  // else
37
   }
38
   if (uint8+ r4 <= '-') {                       // '+' or '-'
39
      if (int r1 != 0) {jump ERROREND}           // sign not allowed if state != 0. end of number
40
      int r1 = 1                                 // state = 1
41
      if (uint8+ r4 == '-') {
42
         int8+ r3 = 1                            // sign
43
         jump NEXT                               // else
44
      }
45
      if (uint8+ r4 != '+') {
46
         jump ERROREND                           // anything else than '+'. end of number
47
      }
48
   }
49
   else {
50
      int8+ r4 -= '0'                            // subtract ASCII '0'
51
      if (uint8+ r4 > 9) {jump ERROREND}         // anything else than 0-9. end of number
52
      //int64 r2 *= 10
53
      // multiply value by 10 without using mul instruction
54
      int64 r1 = r2 << 3                         // value * 8
55
      int64 r2 <<= 1                             // value * 2
56
      int64 r2 += r1                             // value * 10
57
      int64 r2 += r4                             // + digit
58
      int   r1 = 2                               // state = 2
59
   }
60
   NEXT:
61
   int64 r0++                                    // point to next character
62
   int8 r4 = [r0]                                // read next character from string
63
}
64
ERROREND:                                        // jump here when a character that cannot be part of the string is met
65
int64 r0 = r3 ? -r2 : r2                         // change sign if r3
66
 
67
int64 r2 = [sp]                                  // restore r2 - r4
68
int64 r3 = [sp+8]
69
int64 r4 = [sp+0x10]
70
int64 sp += 3*8
71
 
72
EMPTYEND:
73
return
74
 
75
_atoi end
76
 
77
code end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.