ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
sgn.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char DATATYPE_AS_STRING[][16];
42
50/* <usermanual>
51@keyword SGN
52
53@english
54
55The ''SGN'' function is a very simple but useful mathematical function. Its main
56function is to determine the sign of a number. In other words, ''SGN'' tells you
57whether a number is positive, negative, or zero. So, it returns 1 if number
58is positive, 0 if ''number'' is zero and -1 if ''number'' is negative.
59
60You can use ''SGN'' inside an ''IF'' condition to perform different actions
61depending on the sign of a number, and in some cases, ''SGN'' can make your
62code more concise and readable.
63
64
65@italian
66
67La funzione ''SGN'' è una funzione matematica molto semplice ma utile.
68La sua funzione principale è determinare il segno di un numero. In altre
69parole, ''SGN'' ti dice se un numero è positivo, negativo o zero. Quindi,
70restituisce 1 se il ''number'' è positivo, 0 se ''number'' è zero e -1 se
71''number'' è negativo.
72
73Puoi usare ''SGN'' all'interno di una condizione ''IF'' per eseguire azioni
74diverse a seconda del segno di un numero e, in alcuni casi, ''SGN'' può
75rendere il tuo codice più conciso e leggibile.
76
77@syntax = SGN(number)
78
79@example x = SNG(-42)
80
81@usedInExample maths_signs_01.bas
82
83</usermanual> */
84
85Variable * sign( Environment * _environment, char * _value ) {
86 Variable * value = variable_retrieve( _environment, _value );
87
88 Variable * result = variable_temporary( _environment, VT_SBYTE, "(result of SGN)");
89
91
92 char positiveLabel[MAX_TEMPORARY_STORAGE]; sprintf(positiveLabel, "%spos", label );
93 char zeroLabel[MAX_TEMPORARY_STORAGE]; sprintf(zeroLabel, "%szero", label );
94 char negativeLabel[MAX_TEMPORARY_STORAGE]; sprintf(negativeLabel, "%snev", label );
95 char endLabel[MAX_TEMPORARY_STORAGE]; sprintf(endLabel, "%send", label );
96
97 switch( VT_BITWIDTH( value->type ) ) {
98 case 32:
99 cpu_compare_and_branch_32bit_const( _environment, value->realName, 0, zeroLabel, 1 );
100 break;
101 case 16:
102 cpu_compare_and_branch_16bit_const( _environment, value->realName, 0, zeroLabel, 1 );
103 break;
104 case 8:
105 cpu_compare_and_branch_8bit_const( _environment, value->realName, 0, zeroLabel, 1 );
106 break;
107 case 0:
109 break;
110 }
111
112 switch( VT_BITWIDTH( value->type ) ) {
113 case 32:
114 case 16:
115 case 8:
116 if ( VT_SIGNED( value->type ) ) {
117
118 cpu_bit_check( _environment, value->realName, VT_BITWIDTH( value->type ) - 1, result->realName, VT_BITWIDTH( value->type ) );
119 cpu_bveq( _environment, result->realName, positiveLabel );
120
121 cpu_label( _environment, negativeLabel );
122 variable_store( _environment, result->name, VT_SIGN_8BIT(-1) );
123 cpu_jump( _environment, endLabel );
124
125 cpu_label( _environment, positiveLabel );
126 variable_store( _environment, result->name, 1 );
127 cpu_jump( _environment, endLabel );
128
129 } else {
130 variable_store( _environment, result->name, 1 );
131 }
132 break;
133 case 0:
135 break;
136 }
137
138 cpu_label( _environment, zeroLabel );
139 variable_store( _environment, result->name, 0 );
140 cpu_label( _environment, endLabel );
141
142 return result;
143}
144
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_compare_and_branch_16bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 8 bit values and jump if they are equal/different
Definition 6309.c:1578
void cpu_compare_and_branch_32bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 32 bit values and jump if they are equal/different
Definition 6309.c:2914
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_bit_check(Environment *_environment, char *_value, int _position, char *_result, int _bitwidth)
Definition 6309.c:5577
void cpu_compare_and_branch_8bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 8 bit values and jump if they are equal/different
Definition 6309.c:876
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * sign(Environment *_environment, char *_value)
Return the sign of a variable.
Definition sgn.c:85
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define VT_SIGNED(t)
Definition ugbc.h:618
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_SBYTE
Definition ugbc.h:452
#define VT_SIGN_8BIT(v)
Definition ugbc.h:647
#define VT_BITWIDTH(t)
Definition ugbc.h:595
#define CRITICAL_SGN_UNSUPPORTED(v, t)
Definition ugbc.h:3476
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]