1 //  arith-print.cpp                             Copyright (C) 2020 Codemist
2 
3 #ifdef ARITHLIB
4 
5 /**************************************************************************
6  * Copyright (C) 2020, Codemist.                         A C Norman       *
7  *                                                                        *
8  * Redistribution and use in source and binary forms, with or without     *
9  * modification, are permitted provided that the following conditions are *
10  * met:                                                                   *
11  *                                                                        *
12  *     * Redistributions of source code must retain the relevant          *
13  *       copyright notice, this list of conditions and the following      *
14  *       disclaimer.                                                      *
15  *     * Redistributions in binary form must reproduce the above          *
16  *       copyright notice, this list of conditions and the following      *
17  *       disclaimer in the documentation and/or other materials provided  *
18  *       with the distribution.                                           *
19  *                                                                        *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    *
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      *
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS      *
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE         *
24  * COPYRIGHT OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,   *
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,   *
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS  *
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR  *
29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF     *
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
31  * DAMAGE.                                                                *
32  *************************************************************************/
33 
34 
35 // $Id: arith-print.cpp 5387 2020-08-20 19:40:24Z arthurcnorman $
36 
37 #include "headers.h"
38 #include "dispatch.h"
39 
40 
41 static thread_local std::vector<char> print_buffer;
42 
print_newbignum(LispObject u,bool blankp,int nobreak)43 void print_newbignum(LispObject u, bool blankp, int nobreak)
44 {   size_t approx_length =
45         arithlib_lowlevel::bignum_to_string_length(
46             (uint64_t *)(u - TAG_NUMBERS + 8),
47             (length_of_header(numhdr(u)) - 8)/8);
48     if (print_buffer.size() < approx_length)
49         print_buffer.resize(approx_length);
50     char *b = print_buffer.data();
51 // I now have a buffer long enough to put my digits in.
52     size_t len =
53         arithlib_lowlevel::bignum_to_string(
54             b, approx_length,
55             (uint64_t *)(u - TAG_NUMBERS + 8),
56             (length_of_header(numhdr(u)) - 8)/8);
57 // Now len is the actual length of the output and the buffer b contains
58 // that many characters. I will need to cope with line breaks and the
59 // like...
60     unsigned int line_length =
61         other_write_action(WRITE_GET_INFO+WRITE_GET_LINE_LENGTH,
62                            active_stream);
63     unsigned int column =
64         other_write_action(WRITE_GET_INFO+WRITE_GET_COLUMN, active_stream);
65     if (blankp)
66     {   if (nobreak==0 && column+len >= line_length)
67         {   if (column != 0) putc_stream('\n', active_stream);
68         }
69         else putc_stream(' ', active_stream);
70     }
71     else if (nobreak==0 && column != 0 && column+len > line_length)
72         putc_stream('\n', active_stream);
73     for (size_t i=0; i<len; i++) putc_stream(b[i], active_stream);
74 // Printing was potentially a fairly expensive step. So I will check to
75 // see if an interrupt was posted during it.
76     if ((uintptr_t)stack >=
77         ((uintptr_t)stackLimit | event_flag.load()))
78         respond_to_stack_event();
79 }
80 
81 #pragma message ("print_newbighexoctbin")
print_newbighexoctbin(LispObject u,int radix,int width,bool blankp,int nobreak)82 void print_newbighexoctbin(LispObject u, int radix, int width,
83                            bool blankp, int nobreak)
84 //
85 // This prints a bignum in base 16, 8 or 2.
86 //
87 {   unsigned int line_length =
88         other_write_action(WRITE_GET_INFO+WRITE_GET_LINE_LENGTH,
89                            active_stream);
90     unsigned int column =
91         other_write_action(WRITE_GET_INFO+WRITE_GET_COLUMN, active_stream);
92 
93     /*
94             if (blankp)
95             {   if (nobreak==0 && column+len >= line_length)
96                 {   if (column != 0) putc_stream('\n', active_stream);
97                 }
98                 else putc_stream(' ', active_stream);
99             }
100             else if (nobreak==0 && column != 0 && column+len > line_length)
101                 putc_stream('\n', active_stream);
102             while (--i >= 0) putc_stream(my_buff[i], active_stream);
103 
104             if ((uintptr_t)stack >=
105                 ((uintptr_t)stackLimit | event_flag.load()))
106                 respond_to_stack_event();
107         }
108 
109     */
110 }
111 
112 #endif // ARITHLIB
113 
114 // end of arith-print.cpp
115