Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp3844788pxy; Mon, 26 Apr 2021 11:05:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwU0Rs6v4sDv4CYXxUOHkQ/+ScCVBu8LGdzzWkkdCWDZsEG4jjQunQtaW0VA9AsIVS5rnCY X-Received: by 2002:a05:6402:199:: with SMTP id r25mr22683460edv.128.1619460310892; Mon, 26 Apr 2021 11:05:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619460310; cv=none; d=google.com; s=arc-20160816; b=ddkyh46nT2k36kzoB4/JAp1nVvxDccg2WRwsjrUt3M5jPrB8OKJAC7RVUYFk/2ZOh/ CIYAl5FzeswMzSITT0oWnYi6SyeHQQxZZ8Y17Znk0qNwk4hBvJDXIYdGzd/e3LKDXvGS +khq3PreFPKVO09EGe2OEk1IWtkHY/XElgzmzozfqiDM8/F6iMd97XMFEEOtoz90F/yz Y+++AW7F1p3y3acrGlh/XnRE2J2Ej3L3Hd4uELgDpgq0ueg0s/iFB3ueNL9RZwIadi6Y +odr3CvQRiaM+ERLRbr0wRm6eVM6545z+bhnFQ80i5K1vbHMHze8ElS6bZB+QIJ3qMOW 8v2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Jy4OFsO0PDb4s3VtfvvtXkiYMuYdbRS3kT7/Hvwb0lc=; b=NFMyy0vXXR69apSkiZyAbxBKcU9o9eiHn49lW1//zTjvCwWQbVqiSsU4fxddsT1rtr KA8/6zZryzSFmRxK7SCD9RmnJ9FePAFhPCNBnSgldibljLGOGEK9yQEaxFsVcn9mCpJo YfZwWoZCZVStHrElCeisj1+AQsXwn1zyloeleIJNng+SafSGmREA1uhRuGYrQSymkSn7 KjzBtSd93XO+D2ed/C7Ho9KIap+CAbjeoS8DSATrAsMNEOtmNxTsnRj8cilPFwJyHYUV q6+CzvVhZsewU5/62zvArOL5+8HCABHTXuQ+kci3sZvSb90kvoR/S0fE7Nqi48TAxWY4 WocA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Cc0m3YnB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n17si14317554ejb.145.2021.04.26.11.04.46; Mon, 26 Apr 2021 11:05:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Cc0m3YnB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237685AbhDZSCX (ORCPT + 99 others); Mon, 26 Apr 2021 14:02:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235357AbhDZSCS (ORCPT ); Mon, 26 Apr 2021 14:02:18 -0400 Received: from mail-yb1-xb30.google.com (mail-yb1-xb30.google.com [IPv6:2607:f8b0:4864:20::b30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4D31C061574; Mon, 26 Apr 2021 11:01:33 -0700 (PDT) Received: by mail-yb1-xb30.google.com with SMTP id p126so12345668yba.1; Mon, 26 Apr 2021 11:01:33 -0700 (PDT) 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:to :cc; bh=Jy4OFsO0PDb4s3VtfvvtXkiYMuYdbRS3kT7/Hvwb0lc=; b=Cc0m3YnBbqfGZSKW9pO2qzUWrj0YihP9qtdd9ehIRk/+kz6tR4873rBCMwZSaqUM6G jkoxmHjSGp0ha53OhAfkLfcm38V8ixOLFtgIEUIZHkE2J0uv3zEztiOSXOe9MhoInQ9z iR3CXcCk3Sloff6y3nwbUvQ6y+vkCCjtspr1Ppx3EP2iqjFuxKTXLceQXGS/YiTbzgFy uNjiG6iXo1g4GULV1udH2ZyrpEZX2Nl2hDuM0g6PC1U1IZbc4smsXqhK2RWzTabFKUc4 xbuz4VwCtjcX7zF/VnPOhZv93q7x7EMwqFkXIKXQ8f6pn9pEnnBMHCdF9XdQIS736nIn 4MdQ== 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:to:cc; bh=Jy4OFsO0PDb4s3VtfvvtXkiYMuYdbRS3kT7/Hvwb0lc=; b=cLTyk+g1ggbJLvN8Bt5dFVUNW+r323ScrMW9vOjBaoP5F0xtMFoSFLIVTcBhMazrdZ mq1SxFe4+hQE3/N5G4/rQ/Dm9V15BzSB8QIHkVQSAzbMDYCGmUEU/wdiPdtFW99aEdJS o80A4bRCuMFbyXi6WUs9wpn29KD/uJa52hJF39Zgqic+cWVtmzvVcBRgAgOENu6IqnII 6KjY1H+fCF47YEWCH/TQiF3+MuaQEVZzPT/8jIDsEZonxutf0Z8MpLdy6u8QIvFqtqzB ecWAl4geSZpBMW6DIwk076cMcn4JzxfoPtzKzZytyEZKpB6V6KTRayFhnql5jS7VVro/ ch2Q== X-Gm-Message-State: AOAM5325J3Td89PtXV8TXg2T9UtBqApYdvmELtWhAf37bq3SsX/7iYl1 DTbFDrk6oQRh8BkIt7ldmHmrG7h5oTkZ+iznoo0= X-Received: by 2002:a25:bfc9:: with SMTP id q9mr25486485ybm.33.1619460093262; Mon, 26 Apr 2021 11:01:33 -0700 (PDT) MIME-Version: 1.0 References: <20210414184604.23473-1-ojeda@kernel.org> In-Reply-To: From: Miguel Ojeda Date: Mon, 26 Apr 2021 20:01:21 +0200 Message-ID: Subject: Re: [PATCH 00/13] [RFC] Rust support To: Linus Walleij Cc: Wedson Almeida Filho , Peter Zijlstra , Miguel Ojeda , Linus Torvalds , Greg Kroah-Hartman , rust-for-linux , linux-kbuild , Linux Doc Mailing List , linux-kernel Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 26, 2021 at 2:18 AM Linus Walleij wrote: > > static void yas53x_extract_calibration(u8 *data, struct yas5xx_calibration *c) > { > u64 val = get_unaligned_be64(data); > > c->a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32; > c->a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8; > c->a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32; > c->a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38; > c->a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32; > c->a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64; > c->a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32; > c->a9 = FIELD_GET(GENMASK_ULL(22, 15), val); > c->k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10; > c->dck = FIELD_GET(GENMASK_ULL(9, 7), val); > } By the way, to give a more concrete example, this function could look like this: fn yas53x_extract_calibration(data: [u8; 8], c: &mut yas5xx_calibration) { let val = u64::from_be_bytes(data); c.a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32; c.a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8; c.a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32; c.a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38; c.a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32; c.a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64; c.a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32; c.a9 = FIELD_GET(GENMASK_ULL(22, 15), val); c.k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10; c.dck = FIELD_GET(GENMASK_ULL(9, 7), val) as u8; } assuming `FIELD_GET()` returns `i32`. In particular, `GENMASK_ULL` and `FIELD_GET` can be written as normal functions, no need for macros (and can be `const fn` too -- i.e. can be evaluated at compile-time if needed). As you see, it looks remarkably similar, and there is no `unsafe` because we pass the array of bytes instead of a raw pointer. The caller needs to get the array from somewhere, of course -- if you only have a raw pointer to start with, then the caller will need an `unsafe` line to dereference it, as usual. Cheers, Miguel