From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Tim Meehan Newsgroups: gmane.lisp.guile.user Subject: Re: C extensions Date: Sun, 21 Feb 2021 15:58:48 -0600 Message-ID: References: <87zgzx5s3d.fsf@clara> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38478"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Feb 21 22:59:15 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lDwl1-0009tb-Ez for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 22:59:15 +0100 Original-Received: from localhost ([::1]:58418 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lDwl0-0000MA-9j for guile-user@m.gmane-mx.org; Sun, 21 Feb 2021 16:59:14 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56294) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lDwko-0000Lw-Br for guile-user@gnu.org; Sun, 21 Feb 2021 16:59:02 -0500 Original-Received: from mail-qk1-x72f.google.com ([2607:f8b0:4864:20::72f]:40536) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lDwkm-0000Q5-8d for guile-user@gnu.org; Sun, 21 Feb 2021 16:59:02 -0500 Original-Received: by mail-qk1-x72f.google.com with SMTP id t62so10898534qke.7 for ; Sun, 21 Feb 2021 13:58:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:cc; bh=zhYk7CqIE+GfG1KEYqIiPAkaF7Njw9STRskupoJInsQ=; b=dnAumb7hmpW9pPHi2ZrFYE7ejxeNCJuGECOhxxOJy53eOUwF12M1DZPf3zXL/5xksU vCGkp/u67XDd3pHuUK4LNvtiA0SrTR/FJS5ii+Nsem7huFSOTc88avmgow37ZcAtlpR9 oGUm2y2YLs8/Ik0biftE6UMkmiiQepcGFPgTQ4VXNaiE2Hl+2CxsnXjO84nX3utG6mG5 68aPuI7UXuAPaWBAKJUDd2EdW5JeGKbAwlPedJrFiQooL3PkpPB8mhJnUSg/asR3+Eml 9gn99kqFZuajCkzrSgGawUp7qh0eZGOSIK2mSO3SCrahX9eS0SJHRPzCDs4cA5/cfO66 DavA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:cc; bh=zhYk7CqIE+GfG1KEYqIiPAkaF7Njw9STRskupoJInsQ=; b=A+A8aDEszjZOORIK+xFGsQgg2QDYpW22V5X1kM3NFGFCXdfDk9JF0zp2+pewRA6wcW skLnjaA6ihg7lHN2Ane4BeBNtiyie3G8ExSWHkwN74nyvytchoz1zpGg13s4Uu+a+eCv Fi31zWbfb/ZLFE+PJDdfE9/vDfHdG4MOllYzFEGf6XZ2KbDKVPT5KRSV2bmj2yiiSW5s ZH/NzIAiCZ+kxDiSXv1fpXdv/otTLX/+aQBg2BlRNvjh5nm3brYJGrNw1Y2V3qKna31P FejYLrqWx/IHpXWLw5ofFExq+rb+x50NPhk2cbJaWoF0qjgIKEvccPNy35jaYKKJdnzg qKBQ== X-Gm-Message-State: AOAM532n/dZRknCO+er2pqryoUEPep4T4LPT2lpq/GhsHT9IFprRRJSf ylwBEfK+N+9jBlBtlXXLWc9rsyHnDX3YAk0Ax+l4KNX1BIo= X-Google-Smtp-Source: ABdhPJyEpG7rcuc2ILT+SEvzsDptASuvlMe6E63ejTBq4a/ZlLZz+SvOCvTV4HFgCEalaO614CTfIvuPxG7jvnv2e00= X-Received: by 2002:a05:620a:208c:: with SMTP id e12mr17943452qka.270.1613944739050; Sun, 21 Feb 2021 13:58:59 -0800 (PST) In-Reply-To: <87zgzx5s3d.fsf@clara> Received-SPF: pass client-ip=2607:f8b0:4864:20::72f; envelope-from=btmeehan@gmail.com; helo=mail-qk1-x72f.google.com X-Spam_score_int: 4 X-Spam_score: 0.4 X-Spam_bar: / X-Spam_report: (0.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, MALFORMED_FREEMAIL=1.49, MISSING_HEADERS=1.021, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17298 Archived-At: Hello Olivier, thanks! Those functions helped, and now I know where in the source of Guile to look. Is "inexact" the same thing as "floating point number" in Guile-speak? Here is one of the functions in my extension - it returns a lognormal variate. There are a bunch of things like this that could be added to "random.c" - but I'm imagining that the only user of stuff like that might be ... me ... anyhow: Right now, it doesn't give really helpful errors when you give it a negative standard deviation: scheme@(guile-user)> (random:lognormal 0 -1) $1 = #f SCM_DEFINE (scm_random_lognormal, "random:lognormal", 2, 1, 0, (SCM _mu, SCM _sigma, SCM state), "Return an inexact real in a Log Normal distribution.\n" "\n" "@var{mu} - mean of the underlying normal distribution.\n" "@var{sigma} - standard deviation of the same.\n" "Optionally, a random @var{state} can be provided.\n" "See @code{seed->random-state}.") #define FUNC_NAME s_scm_random_lognormal { if (SCM_UNBNDP (state)) state = SCM_VARIABLE_REF (scm_var_random_state); SCM_VALIDATE_RSTATE (1, state); if (scm_is_false (scm_positive_p (_sigma))) return SCM_BOOL_F; // I'd like to return some meaningful error here ... double mu = scm_to_double (_mu); double sigma = scm_to_double (_sigma); return scm_from_double (exp (mu + sigma*scm_c_normal01 (SCM_RSTATE(state)))); } #undef FUNC_NAME On Sun, Feb 21, 2021 at 9:59 AM Olivier Dion wrote: > On Sat, 20 Feb 2021, Tim Meehan wrote: > > I'm trying my hand a writing C extensions. > > I've done this for stuff like Matlab before, and was wondering how you do > > the usual checking of the arguments that are passed in from Guile. > > > > In the manual, 6.13.13.1 "C Support" has a few functions. > > libguile/numbers.h has a bunch more ... > > > > What I have is an extension function, sort of like the bessel function in > > the tutorial: > > > https://www.gnu.org/software/guile/manual/html_node/A-Sample-Guile-Extension.html > > Usually I do this in 2 or 3 steps. > > 1. Define the primitive in C with raw arguments. > > 2. Make a wrapper of this primitive for Guile to use. Optionnaly do > the arguments checking here or go to step 3. > > 3. Make a second wrapper in Scheme and check the arguments there. > This wrapper will call wrapper in made in 2. > > Here's a full example: > ---------------------------------------------------------------------- > float c_sin(double x) > { > return sin(x); > } > > SCM_DEFINE(c_sin_wrapper, "sin", 1, 0, 0, > (SCM x), > "My sine") > { > if (scm_is_number(x)) { > return c_sin(scm_to_double(x)); > } > > return SCM_BOOL_F; > } > ---------------------------------------------------------------------- > > > What I would like to do is verify that the first argument is an inexact > > number, larger than 0. How would I go about that? Perhaps some of it > > could > > For your example, use the following predicates: > ---------------------------------------------------------------------- > scm_is_inexact(x) && scm_is_true(scm_positive_p(x)) > ---------------------------------------------------------------------- > > > be: > > SCM_REALP > > scm_misc_error > > > > Any tips? > -- > Olivier Dion > PolyMtl >