[BACK]Return to t-aors_1.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpn

Annotation of OpenXM_contrib/gmp/tests/mpn/t-aors_1.c, Revision 1.1

1.1     ! ohara       1: /* Test mpn_add_1 and mpn_sub_1.
        !             2:
        !             3: Copyright 2001, 2002 Free Software Foundation, Inc.
        !             4:
        !             5: This file is part of the GNU MP Library.
        !             6:
        !             7: The GNU MP Library is free software; you can redistribute it and/or modify
        !             8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
        !            10: option) any later version.
        !            11:
        !            12: The GNU MP Library is distributed in the hope that it will be useful, but
        !            13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
        !            15: License for more details.
        !            16:
        !            17: You should have received a copy of the GNU Lesser General Public License
        !            18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            20: MA 02111-1307, USA.
        !            21: */
        !            22:
        !            23: #include <stdio.h>
        !            24: #include <stdlib.h>
        !            25:
        !            26: #include "gmp.h"
        !            27: #include "gmp-impl.h"
        !            28: #include "tests.h"
        !            29:
        !            30:
        !            31: #define M      GMP_NUMB_MAX
        !            32: #define ASIZE  10
        !            33: #define MAGIC  0x1234
        !            34:
        !            35: #define SETUP()                         \
        !            36:   do {                                  \
        !            37:     refmpn_random (got, data[i].size);  \
        !            38:     got[data[i].size] = MAGIC;          \
        !            39:   } while (0)
        !            40:
        !            41: #define SETUP_INPLACE()                                 \
        !            42:   do {                                                  \
        !            43:     refmpn_copyi (got, data[i].src, data[i].size);      \
        !            44:     got[data[i].size] = MAGIC;                          \
        !            45:   } while (0)
        !            46:
        !            47: #define VERIFY(name)                            \
        !            48:   do {                                          \
        !            49:     verify (name, i, data[i].src, data[i].n,    \
        !            50:             got_c, data[i].want_c,              \
        !            51:             got, data[i].want, data[i].size);   \
        !            52:   } while (0)
        !            53:
        !            54: typedef mp_limb_t (*mpn_aors_1_t)
        !            55:      _PROTO ((mp_ptr, mp_srcptr, mp_size_t, mp_limb_t));
        !            56: mpn_aors_1_t fudge _PROTO ((mpn_aors_1_t));
        !            57:
        !            58:
        !            59: void
        !            60: verify (const char *name, int i,
        !            61:         mp_srcptr src, mp_limb_t n,
        !            62:         mp_limb_t got_c, mp_limb_t want_c,
        !            63:         mp_srcptr got, mp_srcptr want, mp_size_t size)
        !            64: {
        !            65:   if (got[size] != MAGIC)
        !            66:     {
        !            67:       printf ("Overwrite at %s i=%d\n", name, i);
        !            68:       abort ();
        !            69:     }
        !            70:
        !            71:   if (got_c != want_c || ! refmpn_equal_anynail (got, want, size))
        !            72:     {
        !            73:       printf ("Wrong at %s i=%d size=%ld\n", name, i, size);
        !            74:       mpn_trace ("   src", src,  size);
        !            75:       mpn_trace ("     n", &n,   (mp_size_t) 1);
        !            76:       mpn_trace ("   got", got,  size);
        !            77:       mpn_trace ("  want", want, size);
        !            78:       mpn_trace (" got c", &got_c,  (mp_size_t) 1);
        !            79:       mpn_trace ("want c", &want_c, (mp_size_t) 1);
        !            80:       abort ();
        !            81:     }
        !            82: }
        !            83:
        !            84:
        !            85: void
        !            86: check_add_1 (void)
        !            87: {
        !            88:   static const struct {
        !            89:     mp_size_t        size;
        !            90:     mp_limb_t        n;
        !            91:     const mp_limb_t  src[ASIZE];
        !            92:     mp_limb_t        want_c;
        !            93:     const mp_limb_t  want[ASIZE];
        !            94:   } data[] = {
        !            95:     { 1, 0, { 0 },  0, { 0 } },
        !            96:     { 1, 0, { 1 },  0, { 1 } },
        !            97:     { 1, 1, { 0 },  0, { 1 } },
        !            98:     { 1, 0, { M },  0, { M } },
        !            99:     { 1, M, { 0 },  0, { M } },
        !           100:     { 1, 1, { 123 }, 0, { 124 } },
        !           101:
        !           102:     { 1, 1, { M },  1, { 0 } },
        !           103:     { 1, M, { 1 },  1, { 0 } },
        !           104:     { 1, M, { M },  1, { M-1 } },
        !           105:
        !           106:     { 2, 0, { 0, 0 },  0, { 0, 0 } },
        !           107:     { 2, 0, { 1, 0 },  0, { 1, 0 } },
        !           108:     { 2, 1, { 0, 0 },  0, { 1, 0 } },
        !           109:     { 2, 0, { M, 0 },  0, { M, 0 } },
        !           110:     { 2, M, { 0, 0 },  0, { M, 0 } },
        !           111:     { 2, 1, { M, 0 },  0, { 0, 1 } },
        !           112:     { 2, M, { 1, 0 },  0, { 0, 1 } },
        !           113:     { 2, M, { M, 0 },  0, { M-1, 1 } },
        !           114:     { 2, M, { M, 0 },  0, { M-1, 1 } },
        !           115:
        !           116:     { 2, 1, { M, M },  1, { 0, 0 } },
        !           117:     { 2, M, { 1, M },  1, { 0, 0 } },
        !           118:     { 2, M, { M, M },  1, { M-1, 0 } },
        !           119:     { 2, M, { M, M },  1, { M-1, 0 } },
        !           120:
        !           121:     { 3, 1, { M, M, M },  1, { 0, 0, 0 } },
        !           122:     { 3, M, { 1, M, M },  1, { 0, 0, 0 } },
        !           123:     { 3, M, { M, M, M },  1, { M-1, 0, 0 } },
        !           124:     { 3, M, { M, M, M },  1, { M-1, 0, 0 } },
        !           125:
        !           126:     { 4, 1, { M, M, M, M },  1, { 0, 0, 0, 0 } },
        !           127:     { 4, M, { 1, M, M, M },  1, { 0, 0, 0, 0 } },
        !           128:     { 4, M, { M, M, M, M },  1, { M-1, 0, 0, 0 } },
        !           129:     { 4, M, { M, M, M, M },  1, { M-1, 0, 0, 0 } },
        !           130:
        !           131:     { 4, M, { M, 0,   M, M },  0, { M-1, 1, M, M } },
        !           132:     { 4, M, { M, M-1, M, M },  0, { M-1, M, M, M } },
        !           133:
        !           134:     { 4, M, { M, M, 0,   M },  0, { M-1, 0, 1, M } },
        !           135:     { 4, M, { M, M, M-1, M },  0, { M-1, 0, M, M } },
        !           136:   };
        !           137:
        !           138:   mp_limb_t  got[ASIZE];
        !           139:   mp_limb_t  got_c;
        !           140:   int        i;
        !           141:
        !           142:   for (i = 0; i < numberof (data); i++)
        !           143:     {
        !           144:       SETUP ();
        !           145:       got_c = mpn_add_1 (got, data[i].src, data[i].size, data[i].n);
        !           146:       VERIFY ("check_add_1 (separate)");
        !           147:
        !           148:       SETUP_INPLACE ();
        !           149:       got_c = mpn_add_1 (got, got, data[i].size, data[i].n);
        !           150:       VERIFY ("check_add_1 (in-place)");
        !           151:
        !           152:       if (data[i].n == 1)
        !           153:         {
        !           154:           SETUP ();
        !           155:           got_c = mpn_add_1 (got, data[i].src, data[i].size, CNST_LIMB(1));
        !           156:           VERIFY ("check_add_1 (separate, const 1)");
        !           157:
        !           158:           SETUP_INPLACE ();
        !           159:           got_c = mpn_add_1 (got, got, data[i].size, CNST_LIMB(1));
        !           160:           VERIFY ("check_add_1 (in-place, const 1)");
        !           161:         }
        !           162:
        !           163:       /* Same again on functions, not inlines. */
        !           164:       SETUP ();
        !           165:       got_c = (*fudge(mpn_add_1)) (got, data[i].src, data[i].size, data[i].n);
        !           166:       VERIFY ("check_add_1 (function, separate)");
        !           167:
        !           168:       SETUP_INPLACE ();
        !           169:       got_c = (*fudge(mpn_add_1)) (got, got, data[i].size, data[i].n);
        !           170:       VERIFY ("check_add_1 (function, in-place)");
        !           171:     }
        !           172: }
        !           173:
        !           174: void
        !           175: check_sub_1 (void)
        !           176: {
        !           177:   static const struct {
        !           178:     mp_size_t        size;
        !           179:     mp_limb_t        n;
        !           180:     const mp_limb_t  src[ASIZE];
        !           181:     mp_limb_t        want_c;
        !           182:     const mp_limb_t  want[ASIZE];
        !           183:   } data[] = {
        !           184:     { 1, 0, { 0 },  0, { 0 } },
        !           185:     { 1, 0, { 1 },  0, { 1 } },
        !           186:     { 1, 1, { 1 },  0, { 0 } },
        !           187:     { 1, 0, { M },  0, { M } },
        !           188:     { 1, 1, { M },  0, { M-1 } },
        !           189:     { 1, 1, { 123 }, 0, { 122 } },
        !           190:
        !           191:     { 1, 1, { 0 },  1, { M } },
        !           192:     { 1, M, { 0 },  1, { 1 } },
        !           193:
        !           194:     { 2, 0, { 0, 0 },  0, { 0, 0 } },
        !           195:     { 2, 0, { 1, 0 },  0, { 1, 0 } },
        !           196:     { 2, 1, { 1, 0 },  0, { 0, 0 } },
        !           197:     { 2, 0, { M, 0 },  0, { M, 0 } },
        !           198:     { 2, 1, { M, 0 },  0, { M-1, 0 } },
        !           199:     { 2, 1, { 123, 0 }, 0, { 122, 0 } },
        !           200:
        !           201:     { 2, 1, { 0, 0 },  1, { M, M } },
        !           202:     { 2, M, { 0, 0 },  1, { 1, M } },
        !           203:
        !           204:     { 3, 0, { 0,   0, 0 },  0, { 0,   0, 0 } },
        !           205:     { 3, 0, { 123, 0, 0 },  0, { 123, 0, 0 } },
        !           206:
        !           207:     { 3, 1, { 0, 0, 0 },  1, { M, M, M } },
        !           208:     { 3, M, { 0, 0, 0 },  1, { 1, M, M } },
        !           209:
        !           210:     { 4, 1, { 0, 0, 0, 0 },  1, { M, M, M, M } },
        !           211:     { 4, M, { 0, 0, 0, 0 },  1, { 1, M, M, M } },
        !           212:
        !           213:     { 4, 1, { 0, 0, 1,   42 },  0, { M, M, 0,   42 } },
        !           214:     { 4, M, { 0, 0, 123, 24 },  0, { 1, M, 122, 24 } },
        !           215:   };
        !           216:
        !           217:   mp_limb_t  got[ASIZE];
        !           218:   mp_limb_t  got_c;
        !           219:   int        i;
        !           220:
        !           221:   for (i = 0; i < numberof (data); i++)
        !           222:     {
        !           223:       SETUP ();
        !           224:       got_c = mpn_sub_1 (got, data[i].src, data[i].size, data[i].n);
        !           225:       VERIFY ("check_sub_1 (separate)");
        !           226:
        !           227:       SETUP_INPLACE ();
        !           228:       got_c = mpn_sub_1 (got, got, data[i].size, data[i].n);
        !           229:       VERIFY ("check_sub_1 (in-place)");
        !           230:
        !           231:       if (data[i].n == 1)
        !           232:         {
        !           233:           SETUP ();
        !           234:           got_c = mpn_sub_1 (got, data[i].src, data[i].size, CNST_LIMB(1));
        !           235:           VERIFY ("check_sub_1 (separate, const 1)");
        !           236:
        !           237:           SETUP_INPLACE ();
        !           238:           got_c = mpn_sub_1 (got, got, data[i].size, CNST_LIMB(1));
        !           239:           VERIFY ("check_sub_1 (in-place, const 1)");
        !           240:         }
        !           241:
        !           242:       /* Same again on functions, not inlines. */
        !           243:       SETUP ();
        !           244:       got_c = (*fudge(mpn_sub_1)) (got, data[i].src, data[i].size, data[i].n);
        !           245:       VERIFY ("check_sub_1 (function, separate)");
        !           246:
        !           247:       SETUP_INPLACE ();
        !           248:       got_c = (*fudge(mpn_sub_1)) (got, got, data[i].size, data[i].n);
        !           249:       VERIFY ("check_sub_1 (function, in-place)");
        !           250:     }
        !           251: }
        !           252:
        !           253: /* Try to prevent the optimizer inlining. */
        !           254: mpn_aors_1_t
        !           255: fudge (mpn_aors_1_t f)
        !           256: {
        !           257:   return f;
        !           258: }
        !           259:
        !           260: int
        !           261: main (void)
        !           262: {
        !           263:   tests_start ();
        !           264:   mp_trace_base = -16;
        !           265:
        !           266:   check_add_1 ();
        !           267:   check_sub_1 ();
        !           268:
        !           269:   tests_end ();
        !           270:   exit (0);
        !           271: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>