1
/*===========================================================================
2
* FileName : operations-srfi60.c
3
* About : SRFI-60 integers as bits
5
* Copyright (C) 2005 by YamaKen
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of authors nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
23
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
26
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
* =========================================================================*/
34
/*=======================================
36
=======================================*/
38
/*=======================================
40
=======================================*/
41
#include "sigscheme.h"
42
#include "sigschemeinternal.h"
44
/*=======================================
45
File Local Struct Declarations
46
=======================================*/
48
/*=======================================
49
File Local Macro Declarations
50
=======================================*/
51
#define BITWISE_OPERATION_BODY(op, opstr) \
60
case SCM_REDUCE_PARTWAY: \
61
case SCM_REDUCE_LAST: \
62
/* left is already ensured as int by previous loop */ \
64
result = (SCM_INT_VALUE(left) op SCM_INT_VALUE(right)); \
67
SigScm_Error(opstr " : (internal error) unrecognized state specifier: %d", *state); \
69
return Scm_NewInt(result); \
70
} while (/* CONSTCOND */ 0)
72
/*=======================================
74
=======================================*/
76
/*=======================================
77
File Local Function Declarations
78
=======================================*/
80
/*=======================================
81
Function Implementations
82
=======================================*/
83
void SigScm_Initialize_SRFI60(void)
85
/*=======================================================================
87
=======================================================================*/
88
Scm_RegisterReductionOperator("logand" , ScmOp_SRFI60_logand);
89
Scm_RegisterReductionOperator("logior" , ScmOp_SRFI60_logior);
90
Scm_RegisterReductionOperator("logxor" , ScmOp_SRFI60_logxor);
91
Scm_RegisterProcedureFixed1("lognot" , ScmOp_SRFI60_lognot);
92
Scm_RegisterProcedureFixed3("bitwise-if" , ScmOp_SRFI60_bitwise_if);
93
Scm_RegisterProcedureFixed2("logtest" , ScmOp_SRFI60_logtest);
94
Scm_DefineAlias("bitwise-and" , "logand");
95
Scm_DefineAlias("bitwise-ior" , "logior");
96
Scm_DefineAlias("bitwise-xor" , "logxor");
97
Scm_DefineAlias("bitwise-not" , "lognot");
98
Scm_DefineAlias("bitwise-merge" , "bitwise-if");
99
Scm_DefineAlias("any-bits-set?" , "logtest");
102
/*=============================================================================
103
SRFI-60 : Integers as Bits
104
=============================================================================*/
106
/* Bitwise Operations */
107
ScmObj ScmOp_SRFI60_logand(ScmObj left, ScmObj right,
108
enum ScmReductionState *state)
110
DECLARE_FUNCTION("logand", ReductionOperator);
111
BITWISE_OPERATION_BODY(&, "logand");
114
ScmObj ScmOp_SRFI60_logior(ScmObj left, ScmObj right,
115
enum ScmReductionState *state)
117
DECLARE_FUNCTION("logior", ReductionOperator);
118
BITWISE_OPERATION_BODY(|, "logior");
121
ScmObj ScmOp_SRFI60_logxor(ScmObj left, ScmObj right,
122
enum ScmReductionState *state)
124
DECLARE_FUNCTION("logxor", ReductionOperator);
125
BITWISE_OPERATION_BODY(^, "logxor");
128
ScmObj ScmOp_SRFI60_lognot(ScmObj n)
130
DECLARE_FUNCTION("lognot", ProcedureFixed1);
134
return Scm_NewInt(~SCM_INT_VALUE(n));
137
ScmObj ScmOp_SRFI60_bitwise_if(ScmObj mask, ScmObj n0, ScmObj n1)
140
DECLARE_FUNCTION("bitwise-if", ProcedureFixed3);
146
c_mask = SCM_INT_VALUE(mask);
147
result = (c_mask & SCM_INT_VALUE(n0)) | (~c_mask & SCM_INT_VALUE(n1));
149
return Scm_NewInt(result);
152
ScmObj ScmOp_SRFI60_logtest(ScmObj j, ScmObj k)
154
DECLARE_FUNCTION("logtest", ProcedureFixed2);
159
return (SCM_INT_VALUE(j) & SCM_INT_VALUE(k)) ? SCM_TRUE : SCM_FALSE;