~ubuntu-branches/ubuntu/hardy/sigscheme/hardy-proposed

« back to all changes in this revision

Viewing changes to src/module-srfi60.c

  • Committer: Bazaar Package Importer
  • Author(s): NIIBE Yutaka
  • Date: 2006-05-23 21:46:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060523214641-6ix4gz34wpiehub8
Tags: 0.5.0-2
* debian/control (Build-Depends): Added ruby.
  Thanks to Frederik Schueler.  Closes: #368571
* debian/rules (clean): invoke 'distclean' instead of 'clean'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
 *  FileName : module-srfi60.c
 
3
 *  About    : SRFI-60 Integers as Bits
 
4
 *
 
5
 *  Copyright (C) 2005-2006 YamaKen
 
6
 *
 
7
 *  All rights reserved.
 
8
 *
 
9
 *  Redistribution and use in source and binary forms, with or without
 
10
 *  modification, are permitted provided that the following conditions
 
11
 *  are met:
 
12
 *
 
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.
 
21
 *
 
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
 
 
35
#include "config.h"
 
36
 
 
37
/*=======================================
 
38
  System Include
 
39
=======================================*/
 
40
 
 
41
/*=======================================
 
42
  Local Include
 
43
=======================================*/
 
44
#include "sigscheme.h"
 
45
#include "sigschemeinternal.h"
 
46
 
 
47
/*=======================================
 
48
  File Local Struct Declarations
 
49
=======================================*/
 
50
 
 
51
/*=======================================
 
52
  File Local Macro Declarations
 
53
=======================================*/
 
54
#define BITWISE_OPERATION_BODY(op, left, right)                              \
 
55
    do {                                                                     \
 
56
        scm_int_t result;                                                    \
 
57
                                                                             \
 
58
        result = 0;                                                          \
 
59
        switch (*state) {                                                    \
 
60
        case SCM_REDUCE_0:                                                   \
 
61
            break;                                                           \
 
62
        case SCM_REDUCE_1:                                                   \
 
63
            ENSURE_INT(right);                                               \
 
64
            return right;                                                    \
 
65
        case SCM_REDUCE_PARTWAY:                                             \
 
66
        case SCM_REDUCE_LAST:                                                \
 
67
            ENSURE_INT(left);                                                \
 
68
            ENSURE_INT(right);                                               \
 
69
            result = (SCM_INT_VALUE(left) op SCM_INT_VALUE(right));          \
 
70
            break;                                                           \
 
71
        default:                                                             \
 
72
            SCM_ASSERT(scm_false);                                           \
 
73
        }                                                                    \
 
74
        return MAKE_INT(result);                                             \
 
75
    } while (/* CONSTCOND */ 0)
 
76
 
 
77
/*=======================================
 
78
  Variable Declarations
 
79
=======================================*/
 
80
#include "functable-srfi60.c"
 
81
 
 
82
/*=======================================
 
83
  File Local Function Declarations
 
84
=======================================*/
 
85
 
 
86
/*=======================================
 
87
  Function Implementations
 
88
=======================================*/
 
89
void
 
90
scm_initialize_srfi60(void)
 
91
{
 
92
    scm_register_funcs(scm_srfi60_func_info_table);
 
93
 
 
94
    scm_define_alias("bitwise-and",   "logand");
 
95
    scm_define_alias("bitwise-ior",   "logior");
 
96
    scm_define_alias("bitwise-xor",   "logxor");
 
97
    scm_define_alias("bitwise-not",   "lognot");
 
98
    scm_define_alias("bitwise-merge", "bitwise-if");
 
99
    scm_define_alias("any-bits-set?", "logtest");
 
100
}
 
101
 
 
102
/* Bitwise Operations */
 
103
ScmObj
 
104
scm_p_srfi60_logand(ScmObj left, ScmObj right, enum ScmReductionState *state)
 
105
{
 
106
    DECLARE_FUNCTION("logand", reduction_operator);
 
107
 
 
108
    BITWISE_OPERATION_BODY(&, left, right);
 
109
}
 
110
 
 
111
ScmObj
 
112
scm_p_srfi60_logior(ScmObj left, ScmObj right, enum ScmReductionState *state)
 
113
{
 
114
    DECLARE_FUNCTION("logior", reduction_operator);
 
115
 
 
116
    BITWISE_OPERATION_BODY(|, left, right);
 
117
}
 
118
 
 
119
ScmObj
 
120
scm_p_srfi60_logxor(ScmObj left, ScmObj right, enum ScmReductionState *state)
 
121
{
 
122
    DECLARE_FUNCTION("logxor", reduction_operator);
 
123
 
 
124
    BITWISE_OPERATION_BODY(^, left, right);
 
125
}
 
126
 
 
127
ScmObj
 
128
scm_p_srfi60_lognot(ScmObj n)
 
129
{
 
130
    DECLARE_FUNCTION("lognot", procedure_fixed_1);
 
131
 
 
132
    ENSURE_INT(n);
 
133
 
 
134
    return MAKE_INT(~SCM_INT_VALUE(n));
 
135
}
 
136
 
 
137
ScmObj
 
138
scm_p_srfi60_bitwise_if(ScmObj mask, ScmObj n0, ScmObj n1)
 
139
{
 
140
    scm_int_t result, c_mask;
 
141
    DECLARE_FUNCTION("bitwise-if", procedure_fixed_3);
 
142
 
 
143
    ENSURE_INT(mask);
 
144
    ENSURE_INT(n0);
 
145
    ENSURE_INT(n1);
 
146
 
 
147
    c_mask = SCM_INT_VALUE(mask);
 
148
    result = (c_mask & SCM_INT_VALUE(n0)) | (~c_mask & SCM_INT_VALUE(n1));
 
149
 
 
150
    return MAKE_INT(result);
 
151
}
 
152
 
 
153
ScmObj
 
154
scm_p_srfi60_logtest(ScmObj j, ScmObj k)
 
155
{
 
156
    DECLARE_FUNCTION("logtest", procedure_fixed_2);
 
157
 
 
158
    ENSURE_INT(j);
 
159
    ENSURE_INT(k);
 
160
 
 
161
    return MAKE_BOOL(SCM_INT_VALUE(j) & SCM_INT_VALUE(k));
 
162
}